refactord paths.ts

This commit is contained in:
2022-06-11 00:05:32 -05:00
parent 29b40b4e43
commit 83570d9535
14 changed files with 148 additions and 97 deletions

View File

@@ -1,10 +1,9 @@
import { buildSync, BuildFailure, BuildOptions, BuildResult } from 'esbuild'
import { existsSync, writeFileSync, unlinkSync } from 'fs'
import { DistPath, ScriptBase, ScriptPath } from './paths'
import { UserScriptMetaFull } from './types'
import readMeta from './readmeta'
import { format, resolveConfig } from 'prettier'
import { CLIArgs } from './main'
import { AllPaths, CLIArgs } from './main'
export default interface runBuild {
meta: UserScriptMetaFull
@@ -17,12 +16,12 @@ export default function runBuild(
) {
//read meta file
let { meta, metaString } = readMeta(name)
let pathDist = DistPath(name)
let paths = AllPaths.script(name)
let result = runEsbuild(
{
entryPoints: [ScriptPath(name).main],
outfile: pathDist,
entryPoints: [paths.main],
outfile: paths.dist,
target: 'esnext',
platform: 'node',
@@ -117,16 +116,15 @@ function getResult(error: BuildFailure | null, result: BuildResult | null) {
}
function clearFilenameComments(content: string): string {
let regexp = new RegExp(`//\\s*${ScriptBase}/.*(?:\\n|$)`, 'g')
let regexp = new RegExp(`//\\s*${AllPaths.base.script}/.*(?:\\n|$)`, 'g')
return content.replace(regexp, '')
}
function postBuild(name: string, result: RunEsbuildResult, metaString: string) {
let error: string | null = null
let path = ScriptPath(name)
let pathDist = DistPath(name)
let paths = AllPaths.script(name)
let PrettierConfig = resolveConfig.sync(path.dir) || {}
let PrettierConfig = resolveConfig.sync(paths.dir) || {}
if (result.error) {
console.error(name, result.errorRaw || result.error)
@@ -139,7 +137,7 @@ function postBuild(name: string, result: RunEsbuildResult, metaString: string) {
parser: 'babel',
})
}
writeFileSync(pathDist, content)
writeFileSync(paths.dist, content)
} else {
console.error(name, 'No output')
}
@@ -150,17 +148,16 @@ function postBuild(name: string, result: RunEsbuildResult, metaString: string) {
}
function doErrorFile(name: string, error: string | null) {
let path = ScriptPath(name)
let outfile = DistPath(name)
let paths = AllPaths.script(name)
let content = `${new Date().toISOString()}\n\n${error}`
if (error !== null) {
writeFileSync(path.error, content)
if (existsSync(outfile)) {
unlinkSync(outfile)
writeFileSync(paths.error, content)
if (existsSync(paths.dist)) {
unlinkSync(paths.dist)
}
} else if (existsSync(path.error)) {
unlinkSync(path.error)
} else if (existsSync(paths.error)) {
unlinkSync(paths.error)
}
}