fixed watch
This commit is contained in:
163
src/build.ts
163
src/build.ts
@@ -1,5 +1,5 @@
|
||||
import { build, BuildFailure } from 'esbuild'
|
||||
import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'fs'
|
||||
import { build, BuildFailure, BuildOptions } from 'esbuild'
|
||||
import { existsSync, writeFileSync, unlinkSync } from 'fs'
|
||||
import { DistPath, ScriptPath } from './paths'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
import readMeta from './readmeta'
|
||||
@@ -12,16 +12,13 @@ export default async function (
|
||||
): Promise<[UserScriptMetaFull, string | null]> {
|
||||
//read meta file
|
||||
let [metaJson, metaString] = readMeta(name)
|
||||
let outPath = DistPath(name)
|
||||
let path = ScriptPath(name)
|
||||
let pathDist = DistPath(name)
|
||||
|
||||
let error: string | null = null
|
||||
|
||||
console.log('build watch?', !watchCallback ? false : 'obj')
|
||||
|
||||
try {
|
||||
await build({
|
||||
let result = await runEsbuild(
|
||||
{
|
||||
entryPoints: [ScriptPath(name).main],
|
||||
outfile: outPath,
|
||||
outfile: pathDist,
|
||||
|
||||
target: 'esnext',
|
||||
platform: 'node',
|
||||
@@ -30,22 +27,6 @@ export default async function (
|
||||
bundle: true,
|
||||
minify: false,
|
||||
|
||||
// write: false, //TODO this will cause result.outputFiles to have the file contents so i can write the file instead
|
||||
|
||||
// watch: !watchCallback
|
||||
// ? false
|
||||
// : {
|
||||
// onRebuild(err, _result) {
|
||||
// console.log('onrebuild')
|
||||
// let error = null
|
||||
// if (err) {
|
||||
// console.error(name, err)
|
||||
// error = (err as BuildFailure).message
|
||||
// }
|
||||
// watchCallback(metaJson, error)
|
||||
// },
|
||||
// },
|
||||
|
||||
define: {
|
||||
UserScriptName: `'${metaJson.name}'`,
|
||||
UserScriptNamespace: `'${metaJson.namespace}'`,
|
||||
@@ -55,21 +36,125 @@ export default async function (
|
||||
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
||||
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(name, err)
|
||||
error = (err as BuildFailure).message
|
||||
},
|
||||
result => {
|
||||
let error: string | null = null
|
||||
|
||||
if (result.error) {
|
||||
console.error(name, result.errorRaw || result.error)
|
||||
error = result.error
|
||||
} else if (result.content) {
|
||||
writeFileSync(pathDist, metaString + result.content)
|
||||
} else {
|
||||
console.error(name, 'No output')
|
||||
}
|
||||
|
||||
doErrorFile(path.error, pathDist, error)
|
||||
|
||||
if (watchCallback !== false) {
|
||||
watchCallback(metaJson, error)
|
||||
}
|
||||
},
|
||||
watchCallback !== false
|
||||
)
|
||||
|
||||
let error: string | null = null
|
||||
|
||||
if (result.error) {
|
||||
console.error(name, result.errorRaw || result.error)
|
||||
error = result.error
|
||||
} else if (result.content) {
|
||||
writeFileSync(pathDist, metaString + result.content)
|
||||
} else {
|
||||
console.error(name, 'No output')
|
||||
}
|
||||
|
||||
//add UserScript header
|
||||
if (existsSync(outPath)) {
|
||||
if (!error) {
|
||||
let content = readFileSync(outPath).toString()
|
||||
writeFileSync(outPath, metaString + content)
|
||||
} else {
|
||||
unlinkSync(outPath)
|
||||
}
|
||||
}
|
||||
doErrorFile(path.error, pathDist, error)
|
||||
|
||||
return [metaJson, error]
|
||||
}
|
||||
|
||||
function doErrorFile(
|
||||
pathError: string,
|
||||
pathOutFile: string,
|
||||
error: string | null
|
||||
) {
|
||||
if (error !== null) {
|
||||
writeFileSync(pathError, `${new Date().toISOString()}\n\n${error}`)
|
||||
|
||||
if (existsSync(pathOutFile)) {
|
||||
unlinkSync(pathOutFile)
|
||||
}
|
||||
} else if (existsSync(pathError)) {
|
||||
unlinkSync(pathError)
|
||||
}
|
||||
}
|
||||
|
||||
interface RunEsbuildResult {
|
||||
content: string | null
|
||||
error: string | null
|
||||
errorRaw?: BuildFailure
|
||||
}
|
||||
|
||||
async function runEsbuild(
|
||||
opts: BuildOptions,
|
||||
watchCallback: (result: RunEsbuildResult) => void,
|
||||
toWatch: boolean = false
|
||||
): Promise<RunEsbuildResult> {
|
||||
opts.write = false
|
||||
if (toWatch) {
|
||||
opts.watch = {
|
||||
onRebuild(err, res) {
|
||||
if (err) {
|
||||
watchCallback({
|
||||
content: null,
|
||||
error: (err as BuildFailure).message,
|
||||
errorRaw: err,
|
||||
})
|
||||
} else if (res) {
|
||||
let content = ''
|
||||
if (res.outputFiles && res.outputFiles.length > 0) {
|
||||
content = res.outputFiles[0].text
|
||||
}
|
||||
if (content === '') {
|
||||
watchCallback({
|
||||
content: null,
|
||||
error: 'No output',
|
||||
})
|
||||
}
|
||||
watchCallback({
|
||||
content,
|
||||
error: null,
|
||||
})
|
||||
} else {
|
||||
watchCallback({
|
||||
content: null,
|
||||
error: 'No result',
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
try {
|
||||
let res = await build(opts)
|
||||
let content = ''
|
||||
if (res.outputFiles && res.outputFiles.length > 0) {
|
||||
content = res.outputFiles[0].text
|
||||
}
|
||||
if (content === '') {
|
||||
return {
|
||||
content: null,
|
||||
error: 'No output',
|
||||
}
|
||||
}
|
||||
return {
|
||||
content,
|
||||
error: null,
|
||||
}
|
||||
} catch (err) {
|
||||
return {
|
||||
content: null,
|
||||
error: (err as BuildFailure).message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user