Files
browser-scripts-builder/src/main.ts
2024-07-17 21:22:33 -05:00

191 lines
5.3 KiB
TypeScript

import { existsSync, lstatSync, mkdirSync, readdirSync, unlinkSync } from 'fs'
import commandLineArgs from 'command-line-args'
import { updateReadmeFile } from './readmefile'
import runBuild, { runBuildResult } from './build'
import * as Path from 'path'
import getAllPaths from './paths'
import * as chokidar from 'chokidar'
export interface CLIArgsT {
watch: boolean
minify: boolean
prettier: boolean
srccomment: boolean
readme: boolean
url?: string
supporturl?: string
homepageurl?: string
remotebranch?: string
in?: string
out?: string
help?: boolean
src?: string
}
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 },
{ name: 'srccomment', alias: 'c', type: Boolean, defaultValue: false },
{ name: 'readme', alias: 'r', type: Boolean, defaultValue: false },
{ name: 'url', alias: 'u', type: String, defaultValue: '' },
{ name: 'supporturl', alias: 's', type: String, defaultValue: '' },
{ name: 'homepageurl', alias: 'U', 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.url || '',
supportUrl: CLIArgs.supporturl || '',
homepageUrl: CLIArgs.homepageurl || '',
remoteBranch: CLIArgs.remotebranch || '',
inBase: CLIArgs.in || 'scripts',
outBase: CLIArgs.out || 'dist',
})
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
default: false
automatically recompile on save
--minify
alias: -m
default: false
minify output files
--prettier
alias: -p
default: false
prettify output files
--srccomment
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 for @downloadURL
--supporturl <url>
alias: -s <url>
default: ""
the support url used in the meta comments for @supportURL
--homepageurl <url>
alias: -U <url>
default: ""
the support url used in the meta comments for @homepageURL
--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
`)
process.exit(0)
}
//if package.json doesn't exist then there is no point in continuing
if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
console.error('package.json not found, unwilling to run')
process.exit(1)
}
//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.in)
let scriptMeta: runBuildResult[] = []
for (let name of scripts) {
let path = AllPaths.script(name)
if (
!name.endsWith('_') &&
existsSync(path.dir) &&
lstatSync(path.dir).isDirectory() &&
existsSync(path.main) &&
lstatSync(path.main).isFile()
) {
let id = scriptMeta.length
scriptMeta[id] = await runBuild(name)
console.log(name, scriptMeta[id].meta.version)
var running = false
async function update(eventName: string) {
if (running) {
return
}
running = true
scriptMeta[id] = await runBuild(name)
console.log(
`WATCH ${eventName}`,
name,
scriptMeta[id].meta.version
)
if (CLIArgs.readme) updateReadmeFile(scriptMeta)
running = false
}
if (CLIArgs.watch) {
chokidar.watch(path.dir).on('all', update)
}
}
}
return scriptMeta
}
doCompile().then(scriptMeta => {
if (CLIArgs.readme) updateReadmeFile(scriptMeta)
console.log(
`\nFinished Compiling\n${
CLIArgs.watch ? 'Listening for Changes\n' : ''
}`
)
})