fixed watch

This commit is contained in:
2022-06-09 10:28:05 -05:00
parent 5c29ba1ef2
commit 6d4f4c57bc
6 changed files with 249 additions and 136 deletions

View File

@@ -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,
}
}
}

View File

@@ -1,12 +1,6 @@
import {
existsSync,
lstatSync,
readdirSync,
unlinkSync,
writeFileSync,
} from 'fs'
import { existsSync, lstatSync, readdirSync, unlinkSync } from 'fs'
import commandLineArgs from 'command-line-args'
import { DistBase, DistPath, ScriptBase, ScriptPath } from './paths'
import { DistBase, ScriptBase, ScriptPath } from './paths'
import { readmeData, updateReadmeFile } from './readmefile'
import runBuild from './build'
import { UserScriptMetaFull } from './types'
@@ -47,12 +41,8 @@ readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
meta: UserScriptMetaFull,
error: string | null
) {
console.log('watch callback')
scriptMeta[id] = { meta, error }
doErrorFile(path.error, DistPath(name), error)
console.log('WATCH', name, meta.version)
updateReadmeFile(scriptMeta)
}
@@ -63,29 +53,14 @@ readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
scriptMeta[id] = { meta, error }
console.log(name, meta.version)
doErrorFile(path.error, DistPath(name), error)
}
}
updateReadmeFile(scriptMeta)
console.log('\nFinished Compiling\n')
if (CLIArgs.watch) console.log('Listening for Changes\n')
console.log(
`\nFinished Compiling\n${
CLIArgs.watch ? 'Listening for Changes\n' : ''
}`
)
})()
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)
}
}