fixed watch
This commit is contained in:
161
lib/build.js
161
lib/build.js
@@ -10,57 +10,126 @@ const readmeta_1 = __importDefault(require("./readmeta"));
|
||||
async function default_1(name, watchCallback = false) {
|
||||
//read meta file
|
||||
let [metaJson, metaString] = (0, readmeta_1.default)(name);
|
||||
let outPath = (0, paths_1.DistPath)(name);
|
||||
let error = null;
|
||||
console.log('build watch?', !watchCallback ? false : 'obj');
|
||||
try {
|
||||
await (0, esbuild_1.build)({
|
||||
entryPoints: [(0, paths_1.ScriptPath)(name).main],
|
||||
outfile: outPath,
|
||||
target: 'esnext',
|
||||
platform: 'node',
|
||||
format: 'esm',
|
||||
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}'`,
|
||||
UserScriptVersion: `'${metaJson.version}'`,
|
||||
UserScriptDownloadURL: `'${metaJson.downloadURL}'`,
|
||||
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
||||
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
||||
},
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
console.error(name, err);
|
||||
error = err.message;
|
||||
}
|
||||
//add UserScript header
|
||||
if ((0, fs_1.existsSync)(outPath)) {
|
||||
if (!error) {
|
||||
let content = (0, fs_1.readFileSync)(outPath).toString();
|
||||
(0, fs_1.writeFileSync)(outPath, metaString + content);
|
||||
let path = (0, paths_1.ScriptPath)(name);
|
||||
let pathDist = (0, paths_1.DistPath)(name);
|
||||
let result = await runEsbuild({
|
||||
entryPoints: [(0, paths_1.ScriptPath)(name).main],
|
||||
outfile: pathDist,
|
||||
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}'`,
|
||||
},
|
||||
}, result => {
|
||||
let error = null;
|
||||
if (result.error) {
|
||||
console.error(name, result.errorRaw || result.error);
|
||||
error = result.error;
|
||||
}
|
||||
else if (result.content) {
|
||||
(0, fs_1.writeFileSync)(pathDist, metaString + result.content);
|
||||
}
|
||||
else {
|
||||
(0, fs_1.unlinkSync)(outPath);
|
||||
console.error(name, 'No output');
|
||||
}
|
||||
doErrorFile(path.error, pathDist, error);
|
||||
if (watchCallback !== false) {
|
||||
watchCallback(metaJson, error);
|
||||
}
|
||||
}, watchCallback !== false);
|
||||
let error = null;
|
||||
if (result.error) {
|
||||
console.error(name, result.errorRaw || result.error);
|
||||
error = result.error;
|
||||
}
|
||||
else if (result.content) {
|
||||
(0, fs_1.writeFileSync)(pathDist, metaString + result.content);
|
||||
}
|
||||
else {
|
||||
console.error(name, 'No output');
|
||||
}
|
||||
doErrorFile(path.error, pathDist, error);
|
||||
return [metaJson, error];
|
||||
}
|
||||
exports.default = default_1;
|
||||
function doErrorFile(pathError, pathOutFile, error) {
|
||||
if (error !== null) {
|
||||
(0, fs_1.writeFileSync)(pathError, `${new Date().toISOString()}\n\n${error}`);
|
||||
if ((0, fs_1.existsSync)(pathOutFile)) {
|
||||
(0, fs_1.unlinkSync)(pathOutFile);
|
||||
}
|
||||
}
|
||||
else if ((0, fs_1.existsSync)(pathError)) {
|
||||
(0, fs_1.unlinkSync)(pathError);
|
||||
}
|
||||
}
|
||||
async function runEsbuild(opts, watchCallback, toWatch = false) {
|
||||
opts.write = false;
|
||||
if (toWatch) {
|
||||
opts.watch = {
|
||||
onRebuild(err, res) {
|
||||
if (err) {
|
||||
watchCallback({
|
||||
content: null,
|
||||
error: err.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 (0, esbuild_1.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.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=build.js.map
|
||||
Reference in New Issue
Block a user