restructured build.ts and added a helpcommand

This commit is contained in:
2022-06-10 22:50:59 -05:00
parent c51ca65751
commit d00ddeb1e1
9 changed files with 225 additions and 177 deletions

View File

@@ -1,16 +1,14 @@
import { buildSync, BuildFailure, BuildOptions } from 'esbuild'
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, Options } from 'prettier'
import { format, resolveConfig } from 'prettier'
import { CLIArgs } from './main'
export default function (
name: string,
watchCallback: (meta: UserScriptMetaFull, error: string | null) => void,
PrettierConfig: Options,
CLIArgs: CLIArgs
watchCallback: (meta: UserScriptMetaFull, error: string | null) => void
): [UserScriptMetaFull, string | null] {
//read meta file
let [metaJson, metaString] = readMeta(name)
@@ -39,45 +37,16 @@ export default function (
},
},
result => {
let error = runPostEsbuild(
name,
result,
metaString,
CLIArgs,
PrettierConfig
)
let error = postBuild(name, result, metaString)
watchCallback(metaJson, error)
},
CLIArgs
}
)
let error = runPostEsbuild(
name,
result,
metaString,
CLIArgs,
PrettierConfig
)
let error = postBuild(name, result, metaString)
return [metaJson, error]
}
function doErrorFile(
pathError: string,
pathOutFile: string,
error: string | null
) {
if (error !== null) {
writeFileSync(pathError, `${new Date().toISOString()}\n\n${error}`)
if (existsSync(pathOutFile)) {
unlinkSync(pathOutFile)
}
} else if (existsSync(pathError)) {
unlinkSync(pathError)
}
}
interface RunEsbuildResult {
content: string | null
error: string | null
@@ -86,49 +55,37 @@ interface RunEsbuildResult {
function runEsbuild(
opts: BuildOptions,
watchCallback: (result: RunEsbuildResult) => void,
CLIArgs: CLIArgs
watchCallback: (result: RunEsbuildResult) => void
): RunEsbuildResult {
opts.write = false
if (CLIArgs.watch) {
opts.watch = {
onRebuild(err, res) {
if (err) {
watchCallback({
content: null,
error: (err as BuildFailure).message,
errorRaw: err,
})
} else if (res) {
let content = ''
if (res.outputFiles && res.outputFiles.length > 0) {
content = res.outputFiles[0].text
}
if (content === '') {
watchCallback({
content: null,
error: 'No output',
})
}
watchCallback({
content,
error: null,
})
} else {
watchCallback({
content: null,
error: 'No result',
})
}
watchCallback(getResult(err, res))
},
}
}
try {
let res = buildSync(opts)
return getResult(null, res)
} catch (err) {
return getResult(err as BuildFailure, null)
}
}
function getResult(error: BuildFailure | null, result: BuildResult | null) {
if (error) {
return {
content: null,
error: (error as BuildFailure).message,
errorRaw: error,
}
} else if (result) {
let content = ''
if (res.outputFiles && res.outputFiles.length > 0) {
content = clearFilenameComments(res.outputFiles[0].text)
if (result.outputFiles && result.outputFiles.length > 0) {
content = result.outputFiles[0].text
if (!CLIArgs.srccomment) content = clearFilenameComments(content)
}
if (content === '') {
return {
@@ -140,45 +97,59 @@ function runEsbuild(
content,
error: null,
}
} catch (err) {
} else {
return {
content: null,
error: (err as BuildFailure).message,
error: 'No result',
}
}
}
function runPostEsbuild(
name: string,
result: RunEsbuildResult,
metaString: string,
CLIArgs: CLIArgs,
PrettierConfig: Options
) {
function clearFilenameComments(content: string): string {
let regexp = new RegExp(`//\\s*${ScriptBase}/.*(?:\\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 PrettierConfig = resolveConfig.sync(path.dir) || {}
if (result.error) {
console.error(name, result.errorRaw || result.error)
error = result.error
} else if (result.content) {
let content = metaString + result.content
if (CLIArgs.prettier) {
content = format(content, PrettierConfig)
content = format(content, {
...PrettierConfig,
parser: 'babel',
})
}
writeFileSync(pathDist, content)
} else {
console.error(name, 'No output')
}
doErrorFile(path.error, pathDist, error)
doErrorFile(name, error)
return error
}
//remove all filename comments
function clearFilenameComments(content: string): string {
let regexp = new RegExp(`//\\s*${ScriptBase}/.*(?:\\n|$)`, 'g')
return content.replace(regexp, '')
function doErrorFile(name: string, error: string | null) {
let path = ScriptPath(name)
let outfile = DistPath(name)
let content = `${new Date().toISOString()}\n\n${error}`
if (error !== null) {
writeFileSync(path.error, content)
if (existsSync(outfile)) {
unlinkSync(outfile)
}
} else if (existsSync(path.error)) {
unlinkSync(path.error)
}
}