restructured build.ts and added a helpcommand
This commit is contained in:
61
src/main.ts
61
src/main.ts
@@ -1,22 +1,59 @@
|
||||
import { existsSync, lstatSync, readdirSync, unlinkSync } from 'fs'
|
||||
import commandLineArgs from 'command-line-args'
|
||||
import { resolveConfig } from 'prettier'
|
||||
import { DistBase, ScriptBase, ScriptPath } from './paths'
|
||||
import { readmeData, updateReadmeFile } from './readmefile'
|
||||
import runBuild from './build'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
import * as Path from 'path'
|
||||
|
||||
export interface CLIArgs {
|
||||
export interface CLIArgsT {
|
||||
watch: boolean
|
||||
minify: boolean
|
||||
prettier: boolean
|
||||
srccomment: boolean
|
||||
help: boolean
|
||||
}
|
||||
|
||||
const CLIArgs = commandLineArgs([
|
||||
export const CLIArgs = commandLineArgs([
|
||||
{ name: 'watch', alias: 'w', type: Boolean, defaultValue: false },
|
||||
{ name: 'minify', alias: 'm', type: Boolean, defaultValue: false },
|
||||
{ name: 'prettier', alias: 'p', type: Boolean, defaultValue: false },
|
||||
]) as CLIArgs
|
||||
{ name: 'srccomment', alias: 's', type: Boolean, defaultValue: false },
|
||||
{ name: 'help', alias: 'h', type: Boolean, defaultValue: false },
|
||||
]) as CLIArgsT
|
||||
|
||||
if (CLIArgs.help) {
|
||||
let command = '<command>'
|
||||
if (process.argv.length > 0) {
|
||||
command = Path.parse(process.argv[0]).name
|
||||
}
|
||||
if (command.toLowerCase() === 'node' && process.argv.length > 1) {
|
||||
let path = Path.relative(process.cwd(), process.argv[1]) || '.'
|
||||
command = `${command} ${path}`
|
||||
}
|
||||
|
||||
console.log(`
|
||||
Usage: ${command} [options]
|
||||
|
||||
options:
|
||||
--watch
|
||||
alias: -w
|
||||
automatically recompile on save
|
||||
--minify
|
||||
alias: -m
|
||||
minify output files
|
||||
--prettier
|
||||
alias: -p
|
||||
prettify output files
|
||||
--srccomment
|
||||
alias: -s
|
||||
include src file path comments in the output files, i.e. // scripts/example/main.ts
|
||||
--help
|
||||
alias: -h
|
||||
show this help message
|
||||
`)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
//if package.json doesn't exist then there is no point in continuing
|
||||
if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
||||
@@ -27,15 +64,6 @@ if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
||||
//delete compiled scripts
|
||||
readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
|
||||
|
||||
//read prettierrc file and make sure `babel` is the configured parser
|
||||
const PrettierConfig = (() => {
|
||||
let config = resolveConfig.sync(process.cwd()) || {}
|
||||
return {
|
||||
...config,
|
||||
parser: 'babel',
|
||||
}
|
||||
})()
|
||||
|
||||
//compile scripts
|
||||
let scripts = readdirSync(ScriptBase)
|
||||
let scriptMeta: readmeData[] = []
|
||||
@@ -61,12 +89,7 @@ for (let name of scripts) {
|
||||
updateReadmeFile(scriptMeta)
|
||||
}
|
||||
|
||||
let [meta, error] = runBuild(
|
||||
name,
|
||||
postWatchUpdate,
|
||||
PrettierConfig,
|
||||
CLIArgs
|
||||
)
|
||||
let [meta, error] = runBuild(name, postWatchUpdate)
|
||||
scriptMeta[id] = { meta, error }
|
||||
|
||||
console.log(name, meta.version)
|
||||
|
||||
Reference in New Issue
Block a user