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')
|
||||
|
||||
17
src/main.ts
17
src/main.ts
@@ -6,11 +6,17 @@ import { readmeData, updateReadmeFile } from './readmefile'
|
||||
import runBuild from './build'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
|
||||
export interface CLIArgs {
|
||||
watch: boolean
|
||||
minify: boolean
|
||||
prettier: boolean
|
||||
}
|
||||
|
||||
const CLIArgs = commandLineArgs([
|
||||
{ name: 'watch', alias: 'w', type: Boolean },
|
||||
]) as {
|
||||
watch: boolean
|
||||
}
|
||||
{ name: 'minify', alias: 'm', type: Boolean },
|
||||
{ name: 'prettier', alias: 'p', type: Boolean },
|
||||
]) as CLIArgs
|
||||
|
||||
//if package.json doesn't exist then there is no point in continuing
|
||||
if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
||||
@@ -50,8 +56,9 @@ for (let name of scripts) {
|
||||
|
||||
let [meta, error] = runBuild(
|
||||
name,
|
||||
CLIArgs.watch ? postWatchUpdate : false,
|
||||
PrettierConfig
|
||||
postWatchUpdate,
|
||||
PrettierConfig,
|
||||
CLIArgs
|
||||
)
|
||||
scriptMeta[id] = { meta, error }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user