better cli arg supper
This commit is contained in:
100
src/build.ts
100
src/build.ts
@@ -4,17 +4,16 @@ import { DistPath, ScriptBase, ScriptPath } from './paths'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
import readMeta from './readmeta'
|
||||
import { format, Options } from 'prettier'
|
||||
import { CLIArgs } from './main'
|
||||
|
||||
export default function (
|
||||
name: string,
|
||||
watchCallback:
|
||||
| ((meta: UserScriptMetaFull, error: string | null) => void)
|
||||
| false = false,
|
||||
PrettierConfig: Options | null
|
||||
watchCallback: (meta: UserScriptMetaFull, error: string | null) => void,
|
||||
PrettierConfig: Options | null,
|
||||
CLIArgs: CLIArgs
|
||||
): [UserScriptMetaFull, string | null] {
|
||||
//read meta file
|
||||
let [metaJson, metaString] = readMeta(name)
|
||||
let path = ScriptPath(name)
|
||||
let pathDist = DistPath(name)
|
||||
|
||||
let result = runEsbuild(
|
||||
@@ -27,7 +26,7 @@ export default function (
|
||||
format: 'esm',
|
||||
|
||||
bundle: true,
|
||||
minify: false,
|
||||
minify: CLIArgs.minify,
|
||||
|
||||
define: {
|
||||
UserScriptName: `'${metaJson.name}'`,
|
||||
@@ -40,46 +39,25 @@ export default function (
|
||||
},
|
||||
},
|
||||
result => {
|
||||
let error: string | null = null
|
||||
|
||||
if (result.error) {
|
||||
console.error(name, result.errorRaw || result.error)
|
||||
error = result.error
|
||||
} else if (result.content) {
|
||||
let content = format(
|
||||
metaString + result.content,
|
||||
PrettierConfig === null ? undefined : PrettierConfig
|
||||
)
|
||||
writeFileSync(pathDist, content)
|
||||
} else {
|
||||
console.error(name, 'No output')
|
||||
}
|
||||
|
||||
doErrorFile(path.error, pathDist, error)
|
||||
|
||||
if (watchCallback !== false) {
|
||||
watchCallback(metaJson, error)
|
||||
}
|
||||
let error = runPostEsbuild(
|
||||
name,
|
||||
result,
|
||||
metaString,
|
||||
CLIArgs,
|
||||
PrettierConfig
|
||||
)
|
||||
watchCallback(metaJson, error)
|
||||
},
|
||||
watchCallback !== false
|
||||
CLIArgs
|
||||
)
|
||||
|
||||
let error: string | null = null
|
||||
|
||||
if (result.error) {
|
||||
console.error(name, result.errorRaw || result.error)
|
||||
error = result.error
|
||||
} else if (result.content) {
|
||||
let content = format(
|
||||
metaString + result.content,
|
||||
PrettierConfig === null ? undefined : PrettierConfig
|
||||
)
|
||||
writeFileSync(pathDist, content)
|
||||
} else {
|
||||
console.error(name, 'No output')
|
||||
}
|
||||
|
||||
doErrorFile(path.error, pathDist, error)
|
||||
let error = runPostEsbuild(
|
||||
name,
|
||||
result,
|
||||
metaString,
|
||||
CLIArgs,
|
||||
PrettierConfig
|
||||
)
|
||||
|
||||
return [metaJson, error]
|
||||
}
|
||||
@@ -109,10 +87,10 @@ interface RunEsbuildResult {
|
||||
function runEsbuild(
|
||||
opts: BuildOptions,
|
||||
watchCallback: (result: RunEsbuildResult) => void,
|
||||
toWatch: boolean = false
|
||||
CLIArgs: CLIArgs
|
||||
): RunEsbuildResult {
|
||||
opts.write = false
|
||||
if (toWatch) {
|
||||
if (CLIArgs.watch) {
|
||||
opts.watch = {
|
||||
onRebuild(err, res) {
|
||||
if (err) {
|
||||
@@ -170,6 +148,38 @@ function runEsbuild(
|
||||
}
|
||||
}
|
||||
|
||||
function runPostEsbuild(
|
||||
name: string,
|
||||
result: RunEsbuildResult,
|
||||
metaString: string,
|
||||
CLIArgs: CLIArgs,
|
||||
PrettierConfig: Options | null
|
||||
) {
|
||||
let error: string | null = null
|
||||
let path = ScriptPath(name)
|
||||
let pathDist = DistPath(name)
|
||||
|
||||
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 === null ? undefined : PrettierConfig
|
||||
)
|
||||
}
|
||||
writeFileSync(pathDist, content)
|
||||
} else {
|
||||
console.error(name, 'No output')
|
||||
}
|
||||
|
||||
doErrorFile(path.error, pathDist, error)
|
||||
|
||||
return error
|
||||
}
|
||||
|
||||
//remove all filename comments
|
||||
function clearFilenameComments(content: string): string {
|
||||
let regexp = new RegExp(`//\\s*${ScriptBase}/.*(?:\\n|$)`, 'g')
|
||||
|
||||
Reference in New Issue
Block a user