reorganized main file
This commit is contained in:
71
src/build.ts
Normal file
71
src/build.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { build, BuildFailure } from 'esbuild'
|
||||
import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'fs'
|
||||
import { DistPath, ScriptPath } from './paths'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
import readMeta from './readmeta'
|
||||
|
||||
export default async function (
|
||||
name: string,
|
||||
watchCallback:
|
||||
| ((meta: UserScriptMetaFull, error: string | null) => void)
|
||||
| false = false
|
||||
): Promise<[UserScriptMetaFull, string | null]> {
|
||||
//read meta file
|
||||
let [metaJson, metaString] = readMeta(name)
|
||||
let outPath = DistPath(name)
|
||||
|
||||
let error: string | null = null
|
||||
|
||||
try {
|
||||
await build({
|
||||
entryPoints: [ScriptPath(name).main],
|
||||
outfile: outPath,
|
||||
|
||||
target: 'esnext',
|
||||
platform: 'node',
|
||||
format: 'esm',
|
||||
|
||||
bundle: true,
|
||||
minify: false,
|
||||
|
||||
watch: !watchCallback
|
||||
? false
|
||||
: {
|
||||
onRebuild(err, result) {
|
||||
if (err) {
|
||||
console.error(name, err)
|
||||
let error = (err as BuildFailure).message
|
||||
watchCallback(metaJson, error)
|
||||
} else {
|
||||
console.log('watch build succeeded:', result)
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
define: {
|
||||
UserScriptName: `'${metaJson.name}'`,
|
||||
UserScriptNamespace: `'${metaJson.namespace}'`,
|
||||
UserScriptVersion: `'${metaJson.version}'`,
|
||||
|
||||
UserScriptDownloadURL: `'${metaJson.downloadURL}'`,
|
||||
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
||||
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(name, err)
|
||||
error = (err as BuildFailure).message
|
||||
}
|
||||
|
||||
//add UserScript header
|
||||
if (existsSync(outPath)) {
|
||||
if (!error) {
|
||||
let content = readFileSync(outPath).toString()
|
||||
writeFileSync(outPath, metaString + content)
|
||||
} else {
|
||||
unlinkSync(outPath)
|
||||
}
|
||||
}
|
||||
|
||||
return [metaJson, error]
|
||||
}
|
||||
103
src/main.ts
103
src/main.ts
@@ -1,66 +1,23 @@
|
||||
import { build, BuildFailure } from 'esbuild'
|
||||
import {
|
||||
existsSync,
|
||||
lstatSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
unlinkSync,
|
||||
writeFileSync,
|
||||
} from 'fs'
|
||||
import { DistBase, DistPath, ScriptBase, ScriptPath } from './paths'
|
||||
import commandLineArgs from 'command-line-args'
|
||||
import { DistBase, ScriptBase, ScriptPath } from './paths'
|
||||
import { readmeData, updateReadmeFile } from './readmefile'
|
||||
import readMeta from './readmeta'
|
||||
import runBuild from './build'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
|
||||
async function compileProject(
|
||||
name: string
|
||||
): Promise<[UserScriptMetaFull, null | string]> {
|
||||
//read meta file
|
||||
let [metaJson, metaString] = readMeta(name)
|
||||
let outPath = DistPath(name)
|
||||
|
||||
let error: null | string = null
|
||||
|
||||
try {
|
||||
await build({
|
||||
entryPoints: [ScriptPath(name).main],
|
||||
outfile: outPath,
|
||||
|
||||
target: 'esnext',
|
||||
platform: 'node',
|
||||
format: 'esm',
|
||||
|
||||
bundle: true,
|
||||
minify: false,
|
||||
|
||||
define: {
|
||||
UserScriptName: `'${metaJson.name}'`,
|
||||
UserScriptNamespace: `'${metaJson.namespace}'`,
|
||||
UserScriptVersion: `'${metaJson.version}'`,
|
||||
|
||||
UserScriptDownloadURL: `'${metaJson.downloadURL}'`,
|
||||
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
||||
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
||||
},
|
||||
})
|
||||
} catch (e) {
|
||||
console.error(name, e) //TODO better error log
|
||||
error = (e as BuildFailure).message
|
||||
}
|
||||
|
||||
//add UserScript header
|
||||
if (existsSync(outPath)) {
|
||||
if (!error) {
|
||||
let content = readFileSync(outPath).toString()
|
||||
writeFileSync(outPath, metaString + content)
|
||||
} else {
|
||||
unlinkSync(outPath)
|
||||
}
|
||||
}
|
||||
|
||||
return [metaJson, error]
|
||||
const CLIArgs = commandLineArgs([
|
||||
{ name: 'watch', alias: 'w', type: Boolean },
|
||||
]) as {
|
||||
watch: boolean
|
||||
}
|
||||
|
||||
//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)
|
||||
@@ -72,17 +29,11 @@ readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
|
||||
//compile scripts
|
||||
;(async () => {
|
||||
let scripts = readdirSync(ScriptBase)
|
||||
|
||||
let scriptMeta: readmeData[] = []
|
||||
|
||||
for (let name of scripts) {
|
||||
let path = ScriptPath(name)
|
||||
|
||||
//delete error file if it exists
|
||||
if (existsSync(path.error)) {
|
||||
unlinkSync(path.error)
|
||||
}
|
||||
|
||||
if (
|
||||
!name.endsWith('_') &&
|
||||
existsSync(path.dir) &&
|
||||
@@ -90,23 +41,39 @@ readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
|
||||
existsSync(path.main) &&
|
||||
lstatSync(path.main).isFile()
|
||||
) {
|
||||
let [meta, error] = await compileProject(name)
|
||||
scriptMeta.push({
|
||||
meta,
|
||||
error,
|
||||
})
|
||||
let id = scriptMeta.length
|
||||
|
||||
//write error file
|
||||
if (error !== null) {
|
||||
writeFileSync(
|
||||
path.error,
|
||||
`${new Date().toISOString()}\n\n${error}`
|
||||
)
|
||||
function postWatchUpdate(
|
||||
meta: UserScriptMetaFull,
|
||||
error: string | null
|
||||
) {
|
||||
scriptMeta[id] = { meta, error }
|
||||
doErrorFile(path.error, error)
|
||||
|
||||
console.log('WATCH', name, meta.version)
|
||||
|
||||
updateReadmeFile(scriptMeta)
|
||||
}
|
||||
|
||||
let [meta, error] = await runBuild(name, postWatchUpdate)
|
||||
scriptMeta[id] = { meta, error }
|
||||
|
||||
console.log(name, meta.version)
|
||||
|
||||
doErrorFile(path.error, error)
|
||||
}
|
||||
}
|
||||
|
||||
updateReadmeFile(scriptMeta)
|
||||
|
||||
console.log('\nFinished Compiling\n')
|
||||
if (CLIArgs.watch) console.log('Listening for Changes\n')
|
||||
})()
|
||||
|
||||
function doErrorFile(pathError: string, error: string | null) {
|
||||
if (error !== null) {
|
||||
writeFileSync(pathError, `${new Date().toISOString()}\n\n${error}`)
|
||||
} else if (existsSync(pathError)) {
|
||||
unlinkSync(pathError)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user