import { build, BuildFailure } from 'esbuild' import { lstatSync, readdirSync, readFileSync, unlinkSync, writeFileSync, } from 'fs' import { DistBase, DistPath, ScriptBase, ScriptPath } from './paths' import { readmeData, updateReadmeFile } from './readmefile' import readMeta from './readmeta' 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) { error = (e as BuildFailure).message } //add UserScript header let content = readFileSync(outPath).toString() writeFileSync(outPath, metaString + content) return [metaJson, error] } if (!lstatSync('package.json').isFile()) { console.error('package.json not found, unwilling to run') process.exit(1) } //delete compiled scripts readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`)) //compile scripts ;(async () => { let scripts = readdirSync(ScriptBase) // let scriptMeta: { // [name: string]: [UserScriptMetaFull, string | null] // } = {} let scriptMeta: readmeData[] = [] for (let name of scripts) { let path = ScriptPath(name) if ( !name.endsWith('_') && lstatSync(path.dir).isDirectory() && lstatSync(path.main).isFile() ) { let [meta, error] = await compileProject(name) scriptMeta.push({ meta, error, }) } } updateReadmeFile(scriptMeta) })()