init
This commit is contained in:
64
src/main.ts
Normal file
64
src/main.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { build } from 'esbuild'
|
||||
import {
|
||||
lstatSync,
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
unlinkSync,
|
||||
writeFileSync,
|
||||
} from 'fs'
|
||||
import { DistBase, DistPath, ScriptBase, ScriptPath } from './paths'
|
||||
import readMeta from './readmeta'
|
||||
|
||||
async function compileProject(name: string) {
|
||||
//read meta file
|
||||
let [metaJson, metaString] = readMeta(name)
|
||||
let outPath = DistPath(name)
|
||||
|
||||
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}'`,
|
||||
},
|
||||
})
|
||||
|
||||
//add UserScript header
|
||||
let content = readFileSync(outPath).toString()
|
||||
|
||||
writeFileSync(outPath, metaString + content)
|
||||
}
|
||||
|
||||
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
|
||||
readdirSync(ScriptBase).forEach(name => {
|
||||
let path = ScriptPath(name)
|
||||
|
||||
if (
|
||||
!name.endsWith('_') &&
|
||||
lstatSync(path.dir).isDirectory() &&
|
||||
lstatSync(path.main).isFile()
|
||||
) {
|
||||
compileProject(name)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user