updated cli args and help screen

This commit is contained in:
2023-02-09 17:24:40 -06:00
parent 806a82baae
commit 7e67128abe
12 changed files with 435 additions and 206 deletions

View File

@@ -1,4 +1,4 @@
import { existsSync, lstatSync, readdirSync, unlinkSync } from 'fs'
import { existsSync, lstatSync, mkdirSync, readdirSync, unlinkSync } from 'fs'
import commandLineArgs from 'command-line-args'
import { updateReadmeFile } from './readmefile'
import runBuild, { runBuildResult } from './build'
@@ -10,11 +10,13 @@ export interface CLIArgsT {
minify: boolean
prettier: boolean
srccomment: boolean
readme: boolean
baseurl?: string
url?: string
supporturl?: string
remotebranch?: string
scriptpath?: string
distpath?: string
in?: string
out?: string
help?: boolean
}
@@ -24,20 +26,23 @@ export const CLIArgs = commandLineArgs([
{ name: 'minify', alias: 'm', type: Boolean, defaultValue: false },
{ name: 'prettier', alias: 'p', type: Boolean, defaultValue: false },
{ name: 'srccomment', alias: 'c', type: Boolean, defaultValue: false },
{ name: 'readme', alias: 'r', type: Boolean, defaultValue: false },
{ name: 'baseurl', alias: 'u', type: String, defaultValue: '' },
{ name: 'remotebranch', alias: 'b', type: String, defaultValue: 'main' },
{ name: 'scriptsdir', alias: 's', type: String, defaultValue: 'scripts' },
{ name: 'distdir', alias: 'd', type: String, defaultValue: 'dist' },
{ name: 'url', alias: 'u', type: String, defaultValue: '' },
{ name: 'supporturl', alias: 's', type: String, defaultValue: '' },
{ name: 'remotebranch', alias: 'b', type: String, defaultValue: '' },
{ name: 'in', alias: 'i', type: String, defaultValue: 'scripts' },
{ name: 'out', alias: 'o', type: String, defaultValue: 'dist' },
{ name: 'help', alias: 'h', type: Boolean },
]) as CLIArgsT
export const AllPaths = getAllPaths({
baseURL: CLIArgs.baseurl || '',
remoteBranch: CLIArgs.remotebranch || 'main',
scriptBase: CLIArgs.scriptpath || 'scripts',
distBase: CLIArgs.distpath || 'dist',
baseUrl: CLIArgs.url || '',
supportUrl: CLIArgs.supporturl || '',
remoteBranch: CLIArgs.remotebranch || '',
inBase: CLIArgs.in || 'scripts',
outBase: CLIArgs.out || 'dist',
})
if (CLIArgs.help) {
@@ -56,16 +61,46 @@ Usage: ${command} [options]
options:
--watch
alias: -w
default: false
automatically recompile on save
--minify
alias: -m
default: false
minify output files
--prettier
alias: -p
default: false
prettify output files
--srccomment
alias: -s
alias: -c
default: false
include src file path comments in the output files, i.e. // scripts/example/main.ts
--readme
alias: -r
default: false
update the readme.md file in your directory to include links to each userscript
--url <url>
alias: -u <url>
default: ""
the base for urls used in the meta comments
--supporturl <url>
alias: -s <url>
default: ""
the support url used in the meta comments
--remotebranch <name>
alias: -b <name>
default: ""
if included, the included base url will be treated as a git repo, and the support url is not required
--in
alias: -i
default: "scripts"
include src file path comments in the output files, i.e. // scripts/example/main.ts
--out
alias: -o
default: "dist"
include src file path comments in the output files, i.e. // scripts/example/main.ts
--help
alias: -h
show this help message
@@ -79,14 +114,18 @@ if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
process.exit(1)
}
//delete compiled scripts
readdirSync(AllPaths.base.dist).forEach(file =>
unlinkSync(`${AllPaths.base.dist}/${file}`)
)
//delete compiled scripts or create output folder if it doesnt exist
if (!existsSync(AllPaths.base.out)) {
mkdirSync(AllPaths.base.out)
} else {
readdirSync(AllPaths.base.out).forEach(file =>
unlinkSync(`${AllPaths.base.out}/${file}`)
)
}
//compile scripts
async function doCompile() {
let scripts = readdirSync(AllPaths.base.script)
let scripts = readdirSync(AllPaths.base.in)
let scriptMeta: runBuildResult[] = []
for (let name of scripts) {
@@ -104,7 +143,7 @@ async function doCompile() {
function update(result: runBuildResult) {
console.log('WATCH', name, result.meta.version)
scriptMeta[id] = result
updateReadmeFile(scriptMeta)
if (CLIArgs.readme) updateReadmeFile(scriptMeta)
}
let result = await runBuild(name, update)
@@ -116,7 +155,7 @@ async function doCompile() {
return scriptMeta
}
doCompile().then(scriptMeta => {
updateReadmeFile(scriptMeta)
if (CLIArgs.readme) updateReadmeFile(scriptMeta)
console.log(
`\nFinished Compiling\n${