Merge branch 'main' of git.zomo.dev:zomo/browser-scripts-builder into main
This commit is contained in:
133
lib/build.js
133
lib/build.js
@@ -7,21 +7,19 @@ const esbuild_1 = require("esbuild");
|
|||||||
const fs_1 = require("fs");
|
const fs_1 = require("fs");
|
||||||
const paths_1 = require("./paths");
|
const paths_1 = require("./paths");
|
||||||
const readmeta_1 = __importDefault(require("./readmeta"));
|
const readmeta_1 = __importDefault(require("./readmeta"));
|
||||||
async function default_1(name, watchCallback = false) {
|
const prettier_1 = require("prettier");
|
||||||
|
function default_1(name, watchCallback, PrettierConfig, CLIArgs) {
|
||||||
//read meta file
|
//read meta file
|
||||||
let [metaJson, metaString] = (0, readmeta_1.default)(name);
|
let [metaJson, metaString] = (0, readmeta_1.default)(name);
|
||||||
let outPath = (0, paths_1.DistPath)(name);
|
let pathDist = (0, paths_1.DistPath)(name);
|
||||||
let error = null;
|
let result = runEsbuild({
|
||||||
console.log('build watch?', !watchCallback ? false : 'obj');
|
|
||||||
try {
|
|
||||||
await (0, esbuild_1.build)({
|
|
||||||
entryPoints: [(0, paths_1.ScriptPath)(name).main],
|
entryPoints: [(0, paths_1.ScriptPath)(name).main],
|
||||||
outfile: outPath,
|
outfile: pathDist,
|
||||||
target: 'esnext',
|
target: 'esnext',
|
||||||
platform: 'node',
|
platform: 'node',
|
||||||
format: 'esm',
|
format: 'esm',
|
||||||
bundle: true,
|
bundle: true,
|
||||||
minify: false,
|
minify: CLIArgs.minify,
|
||||||
define: {
|
define: {
|
||||||
UserScriptName: `'${metaJson.name}'`,
|
UserScriptName: `'${metaJson.name}'`,
|
||||||
UserScriptNamespace: `'${metaJson.namespace}'`,
|
UserScriptNamespace: `'${metaJson.namespace}'`,
|
||||||
@@ -30,23 +28,110 @@ async function default_1(name, watchCallback = false) {
|
|||||||
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
||||||
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
||||||
},
|
},
|
||||||
});
|
}, result => {
|
||||||
}
|
let error = runPostEsbuild(name, result, metaString, CLIArgs, PrettierConfig);
|
||||||
catch (err) {
|
watchCallback(metaJson, error);
|
||||||
console.error(name, err);
|
}, CLIArgs);
|
||||||
error = err.message;
|
let error = runPostEsbuild(name, result, metaString, CLIArgs, PrettierConfig);
|
||||||
}
|
|
||||||
//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);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
(0, fs_1.unlinkSync)(outPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return [metaJson, error];
|
return [metaJson, error];
|
||||||
}
|
}
|
||||||
exports.default = default_1;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function runEsbuild(opts, watchCallback, CLIArgs) {
|
||||||
|
opts.write = false;
|
||||||
|
if (CLIArgs.watch) {
|
||||||
|
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 = (0, esbuild_1.buildSync)(opts);
|
||||||
|
let content = '';
|
||||||
|
if (res.outputFiles && res.outputFiles.length > 0) {
|
||||||
|
content = clearFilenameComments(res.outputFiles[0].text);
|
||||||
|
}
|
||||||
|
if (content === '') {
|
||||||
|
return {
|
||||||
|
content: null,
|
||||||
|
error: 'No output',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
content,
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
return {
|
||||||
|
content: null,
|
||||||
|
error: err.message,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function runPostEsbuild(name, result, metaString, CLIArgs, PrettierConfig) {
|
||||||
|
let error = null;
|
||||||
|
let path = (0, paths_1.ScriptPath)(name);
|
||||||
|
let pathDist = (0, paths_1.DistPath)(name);
|
||||||
|
if (result.error) {
|
||||||
|
console.error(name, result.errorRaw || result.error);
|
||||||
|
error = result.error;
|
||||||
|
}
|
||||||
|
else if (result.content) {
|
||||||
|
let content = metaString + result.content;
|
||||||
|
if (CLIArgs.prettier) {
|
||||||
|
content = (0, prettier_1.format)(content, PrettierConfig);
|
||||||
|
}
|
||||||
|
(0, fs_1.writeFileSync)(pathDist, content);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.error(name, 'No output');
|
||||||
|
}
|
||||||
|
doErrorFile(path.error, pathDist, error);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
//remove all filename comments
|
||||||
|
function clearFilenameComments(content) {
|
||||||
|
let regexp = new RegExp(`//\\s*${paths_1.ScriptBase}/.*(?:\\n|$)`, 'g');
|
||||||
|
return content.replace(regexp, '');
|
||||||
|
}
|
||||||
//# sourceMappingURL=build.js.map
|
//# sourceMappingURL=build.js.map
|
||||||
@@ -1 +1 @@
|
|||||||
{"version":3,"file":"build.js","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":";;;;;AAAA,qCAA6C;AAC7C,2BAAwE;AACxE,mCAA8C;AAE9C,0DAAiC;AAElB,KAAK,oBAChB,IAAY,EACZ,gBAEc,KAAK;IAEnB,gBAAgB;IAChB,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,GAAG,IAAA,kBAAQ,EAAC,IAAI,CAAC,CAAA;IAC3C,IAAI,OAAO,GAAG,IAAA,gBAAQ,EAAC,IAAI,CAAC,CAAA;IAE5B,IAAI,KAAK,GAAkB,IAAI,CAAA;IAE/B,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;IAE3D,IAAI;QACA,MAAM,IAAA,eAAK,EAAC;YACR,WAAW,EAAE,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,CAAC,IAAI,CAAC;YACpC,OAAO,EAAE,OAAO;YAEhB,MAAM,EAAE,QAAQ;YAChB,QAAQ,EAAE,MAAM;YAChB,MAAM,EAAE,KAAK;YAEb,MAAM,EAAE,IAAI;YACZ,MAAM,EAAE,KAAK;YAEb,MAAM,EAAE;gBACJ,cAAc,EAAE,IAAI,QAAQ,CAAC,IAAI,GAAG;gBACpC,mBAAmB,EAAE,IAAI,QAAQ,CAAC,SAAS,GAAG;gBAC9C,iBAAiB,EAAE,IAAI,QAAQ,CAAC,OAAO,GAAG;gBAE1C,qBAAqB,EAAE,IAAI,QAAQ,CAAC,WAAW,GAAG;gBAClD,oBA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AAA;IAC/D,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;AACtC,CAAC"}
|
||||||
44
lib/main.js
44
lib/main.js
@@ -5,11 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const fs_1 = require("fs");
|
const fs_1 = require("fs");
|
||||||
const command_line_args_1 = __importDefault(require("command-line-args"));
|
const command_line_args_1 = __importDefault(require("command-line-args"));
|
||||||
|
const prettier_1 = require("prettier");
|
||||||
const paths_1 = require("./paths");
|
const paths_1 = require("./paths");
|
||||||
const readmefile_1 = require("./readmefile");
|
const readmefile_1 = require("./readmefile");
|
||||||
const build_1 = __importDefault(require("./build"));
|
const build_1 = __importDefault(require("./build"));
|
||||||
const CLIArgs = (0, command_line_args_1.default)([
|
const CLIArgs = (0, command_line_args_1.default)([
|
||||||
{ name: 'watch', alias: 'w', type: Boolean },
|
{ name: 'watch', alias: 'w', type: Boolean, defaultValue: false },
|
||||||
|
{ name: 'minify', alias: 'm', type: Boolean, defaultValue: false },
|
||||||
|
{ name: 'prettier', alias: 'p', type: Boolean, defaultValue: false },
|
||||||
]);
|
]);
|
||||||
//if package.json doesn't exist then there is no point in continuing
|
//if package.json doesn't exist then there is no point in continuing
|
||||||
if (!(0, fs_1.existsSync)('package.json') || !(0, fs_1.lstatSync)('package.json').isFile()) {
|
if (!(0, fs_1.existsSync)('package.json') || !(0, fs_1.lstatSync)('package.json').isFile()) {
|
||||||
@@ -18,10 +21,18 @@ if (!(0, fs_1.existsSync)('package.json') || !(0, fs_1.lstatSync)('package.json'
|
|||||||
}
|
}
|
||||||
//delete compiled scripts
|
//delete compiled scripts
|
||||||
(0, fs_1.readdirSync)(paths_1.DistBase).forEach(file => (0, fs_1.unlinkSync)(`${paths_1.DistBase}/${file}`));
|
(0, fs_1.readdirSync)(paths_1.DistBase).forEach(file => (0, fs_1.unlinkSync)(`${paths_1.DistBase}/${file}`));
|
||||||
(async () => {
|
//read prettierrc file and make sure `babel` is the configured parser
|
||||||
let scripts = (0, fs_1.readdirSync)(paths_1.ScriptBase);
|
const PrettierConfig = (() => {
|
||||||
let scriptMeta = [];
|
let config = prettier_1.resolveConfig.sync(process.cwd()) || {};
|
||||||
for (let name of scripts) {
|
return {
|
||||||
|
...config,
|
||||||
|
parser: 'babel',
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
//compile scripts
|
||||||
|
let scripts = (0, fs_1.readdirSync)(paths_1.ScriptBase);
|
||||||
|
let scriptMeta = [];
|
||||||
|
for (let name of scripts) {
|
||||||
let path = (0, paths_1.ScriptPath)(name);
|
let path = (0, paths_1.ScriptPath)(name);
|
||||||
if (!name.endsWith('_') &&
|
if (!name.endsWith('_') &&
|
||||||
(0, fs_1.existsSync)(path.dir) &&
|
(0, fs_1.existsSync)(path.dir) &&
|
||||||
@@ -30,32 +41,15 @@ if (!(0, fs_1.existsSync)('package.json') || !(0, fs_1.lstatSync)('package.json'
|
|||||||
(0, fs_1.lstatSync)(path.main).isFile()) {
|
(0, fs_1.lstatSync)(path.main).isFile()) {
|
||||||
let id = scriptMeta.length;
|
let id = scriptMeta.length;
|
||||||
function postWatchUpdate(meta, error) {
|
function postWatchUpdate(meta, error) {
|
||||||
console.log('watch callback');
|
|
||||||
scriptMeta[id] = { meta, error };
|
scriptMeta[id] = { meta, error };
|
||||||
doErrorFile(path.error, (0, paths_1.DistPath)(name), error);
|
|
||||||
console.log('WATCH', name, meta.version);
|
console.log('WATCH', name, meta.version);
|
||||||
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
||||||
}
|
}
|
||||||
let [meta, error] = await (0, build_1.default)(name, CLIArgs.watch ? postWatchUpdate : false);
|
let [meta, error] = (0, build_1.default)(name, postWatchUpdate, PrettierConfig, CLIArgs);
|
||||||
scriptMeta[id] = { meta, error };
|
scriptMeta[id] = { meta, error };
|
||||||
console.log(name, meta.version);
|
console.log(name, meta.version);
|
||||||
doErrorFile(path.error, (0, paths_1.DistPath)(name), error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
|
||||||
console.log('\nFinished Compiling\n');
|
|
||||||
if (CLIArgs.watch)
|
|
||||||
console.log('Listening for Changes\n');
|
|
||||||
})();
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
||||||
|
console.log(`\nFinished Compiling\n${CLIArgs.watch ? 'Listening for Changes\n' : ''}`);
|
||||||
//# sourceMappingURL=main.js.map
|
//# sourceMappingURL=main.js.map
|
||||||
@@ -1 +1 @@
|
|||||||
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;;AAAA,2BAMW;AACX,0EAA+C;AAC/C,mCAAoE;AACpE,6CAA2D;AAC3D,oDAA8B;AAG9B,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC;IAC5B,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE;CAC/C,CAEA,CAAA;AAED,oEAAoE;AACpE,IAAI,CAAC,IAAA,eAAU,EAAC,cAAc,CAAC,IAAI,CAAC,IAAA,cAAS,EAAC,cAAc,CAAC,CAAC,MAAM,EAAE,EAAE;IACpE,OAAO,CAAC,KAAK,CAAC,0CAA0C,CAAC,CA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C,QAAQ,CAAC,GAAG,CAAC;QACnB,IAAA,eAAU,EAAC,IAAI,CAAC,GAAG,CAAC;QACpB,IAAA,cAAS,EAAC,IAAI,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE;QACjC,IAAA,eAAU,EAAC,IAAI,CAAC,IAAI,CAAC;QACrB,IAAA,cAAS,EAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAC/B;QACE,IAAI,EAAE,GAAG,UAAU,CAAC,MAAM,CAAA;QAE1B,SAAS,eAAe,CACpB,IAAwB,EACxB,KAAoB;YAEpB,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;YAChC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;YACxC,IAAA,6BAAgB,EAAC,UAAU,CAAC,CAAA;QAChC,CAAC;QAED,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,IAAA,eAAQ,EACxB,IAAI,EACJ,eAAe,EACf,cAAc,EACd,OAAO,CACV,CAAA;QACD,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;QAEhC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,CAAA;KAClC;CACJ;AAED,IAAA,6BAAgB,EAAC,UAAU,CAAC,CAAA;AAE5B,OAAO,CAAC,GAAG,CACP,yBAAyB,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAAC,EAAE,EAAE,CAC5E,CAAA"}
|
||||||
@@ -19,14 +19,15 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/command-line-args": "^5.2.0",
|
|
||||||
"command-line-args": "^5.2.1",
|
"command-line-args": "^5.2.1",
|
||||||
"esbuild": "^0.14.42",
|
"esbuild": "^0.14.42",
|
||||||
"typescript": "^4.7.3"
|
"typescript": "^4.7.3",
|
||||||
|
"prettier": "^2.6.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/command-line-args": "^5.2.0",
|
||||||
"@types/node": "^17.0.40",
|
"@types/node": "^17.0.40",
|
||||||
"eslint": "^8.17.0",
|
"@types/prettier": "^2.6.3",
|
||||||
"prettier": "^2.6.2"
|
"eslint": "^8.17.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
pnpm-lock.yaml
generated
14
pnpm-lock.yaml
generated
@@ -3,6 +3,7 @@ lockfileVersion: 5.4
|
|||||||
specifiers:
|
specifiers:
|
||||||
'@types/command-line-args': ^5.2.0
|
'@types/command-line-args': ^5.2.0
|
||||||
'@types/node': ^17.0.40
|
'@types/node': ^17.0.40
|
||||||
|
'@types/prettier': ^2.6.3
|
||||||
command-line-args: ^5.2.1
|
command-line-args: ^5.2.1
|
||||||
esbuild: ^0.14.42
|
esbuild: ^0.14.42
|
||||||
eslint: ^8.17.0
|
eslint: ^8.17.0
|
||||||
@@ -10,15 +11,16 @@ specifiers:
|
|||||||
typescript: ^4.7.3
|
typescript: ^4.7.3
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/command-line-args': 5.2.0
|
|
||||||
command-line-args: 5.2.1
|
command-line-args: 5.2.1
|
||||||
esbuild: 0.14.42
|
esbuild: 0.14.42
|
||||||
|
prettier: 2.6.2
|
||||||
typescript: 4.7.3
|
typescript: 4.7.3
|
||||||
|
|
||||||
devDependencies:
|
devDependencies:
|
||||||
|
'@types/command-line-args': 5.2.0
|
||||||
'@types/node': 17.0.40
|
'@types/node': 17.0.40
|
||||||
|
'@types/prettier': 2.6.3
|
||||||
eslint: 8.17.0
|
eslint: 8.17.0
|
||||||
prettier: 2.6.2
|
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
@@ -56,12 +58,16 @@ packages:
|
|||||||
|
|
||||||
/@types/command-line-args/5.2.0:
|
/@types/command-line-args/5.2.0:
|
||||||
resolution: {integrity: sha512-UuKzKpJJ/Ief6ufIaIzr3A/0XnluX7RvFgwkV89Yzvm77wCh1kFaFmqN8XEnGcN62EuHdedQjEMb8mYxFLGPyA==}
|
resolution: {integrity: sha512-UuKzKpJJ/Ief6ufIaIzr3A/0XnluX7RvFgwkV89Yzvm77wCh1kFaFmqN8XEnGcN62EuHdedQjEMb8mYxFLGPyA==}
|
||||||
dev: false
|
dev: true
|
||||||
|
|
||||||
/@types/node/17.0.40:
|
/@types/node/17.0.40:
|
||||||
resolution: {integrity: sha512-UXdBxNGqTMtm7hCwh9HtncFVLrXoqA3oJW30j6XWp5BH/wu3mVeaxo7cq5benFdBw34HB3XDT2TRPI7rXZ+mDg==}
|
resolution: {integrity: sha512-UXdBxNGqTMtm7hCwh9HtncFVLrXoqA3oJW30j6XWp5BH/wu3mVeaxo7cq5benFdBw34HB3XDT2TRPI7rXZ+mDg==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/@types/prettier/2.6.3:
|
||||||
|
resolution: {integrity: sha512-ymZk3LEC/fsut+/Q5qejp6R9O1rMxz3XaRHDV6kX8MrGAhOSPqVARbDi+EZvInBpw+BnCX3TD240byVkOfQsHg==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
/acorn-jsx/5.3.2_acorn@8.7.1:
|
/acorn-jsx/5.3.2_acorn@8.7.1:
|
||||||
resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==}
|
resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
@@ -715,7 +721,7 @@ packages:
|
|||||||
resolution: {integrity: sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==}
|
resolution: {integrity: sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==}
|
||||||
engines: {node: '>=10.13.0'}
|
engines: {node: '>=10.13.0'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
dev: true
|
dev: false
|
||||||
|
|
||||||
/punycode/2.1.1:
|
/punycode/2.1.1:
|
||||||
resolution: {integrity: sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==}
|
resolution: {integrity: sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==}
|
||||||
|
|||||||
24
readme.md
24
readme.md
@@ -23,3 +23,27 @@ Root
|
|||||||
└───dist
|
└───dist
|
||||||
└───[each script folder].user.js
|
└───[each script folder].user.js
|
||||||
```
|
```
|
||||||
|
|
||||||
|
if there is an error compiling a file, an `error.log` will be placed inside the source folder:
|
||||||
|
|
||||||
|
```text
|
||||||
|
Root
|
||||||
|
├───<package.json/etc>
|
||||||
|
└───scripts
|
||||||
|
└───[each script folder]
|
||||||
|
└───error.log
|
||||||
|
```
|
||||||
|
|
||||||
|
## Command Line Options
|
||||||
|
|
||||||
|
```text
|
||||||
|
--watch
|
||||||
|
alias: -w
|
||||||
|
automatically recompile on save
|
||||||
|
--minify
|
||||||
|
alias: -m
|
||||||
|
minify output files
|
||||||
|
--prettier
|
||||||
|
alias: -p
|
||||||
|
prettify output files
|
||||||
|
```
|
||||||
|
|||||||
187
src/build.ts
187
src/build.ts
@@ -1,34 +1,32 @@
|
|||||||
import { build, BuildFailure } from 'esbuild'
|
import { buildSync, BuildFailure, BuildOptions } from 'esbuild'
|
||||||
import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'fs'
|
import { existsSync, writeFileSync, unlinkSync } from 'fs'
|
||||||
import { DistPath, ScriptPath } from './paths'
|
import { DistPath, ScriptBase, ScriptPath } from './paths'
|
||||||
import { UserScriptMetaFull } from './types'
|
import { UserScriptMetaFull } from './types'
|
||||||
import readMeta from './readmeta'
|
import readMeta from './readmeta'
|
||||||
|
import { format, Options } from 'prettier'
|
||||||
|
import { CLIArgs } from './main'
|
||||||
|
|
||||||
export default async function (
|
export default function (
|
||||||
name: string,
|
name: string,
|
||||||
watchCallback:
|
watchCallback: (meta: UserScriptMetaFull, error: string | null) => void,
|
||||||
| ((meta: UserScriptMetaFull, error: string | null) => void)
|
PrettierConfig: Options,
|
||||||
| false = false
|
CLIArgs: CLIArgs
|
||||||
): Promise<[UserScriptMetaFull, string | null]> {
|
): [UserScriptMetaFull, string | null] {
|
||||||
//read meta file
|
//read meta file
|
||||||
let [metaJson, metaString] = readMeta(name)
|
let [metaJson, metaString] = readMeta(name)
|
||||||
let outPath = DistPath(name)
|
let pathDist = DistPath(name)
|
||||||
|
|
||||||
let error: string | null = null
|
let result = runEsbuild(
|
||||||
|
{
|
||||||
console.log('build watch?', !watchCallback ? false : 'obj')
|
|
||||||
|
|
||||||
try {
|
|
||||||
await build({
|
|
||||||
entryPoints: [ScriptPath(name).main],
|
entryPoints: [ScriptPath(name).main],
|
||||||
outfile: outPath,
|
outfile: pathDist,
|
||||||
|
|
||||||
target: 'esnext',
|
target: 'esnext',
|
||||||
platform: 'node',
|
platform: 'node',
|
||||||
format: 'esm',
|
format: 'esm',
|
||||||
|
|
||||||
bundle: true,
|
bundle: true,
|
||||||
minify: false,
|
minify: CLIArgs.minify,
|
||||||
|
|
||||||
define: {
|
define: {
|
||||||
UserScriptName: `'${metaJson.name}'`,
|
UserScriptName: `'${metaJson.name}'`,
|
||||||
@@ -39,21 +37,148 @@ export default async function (
|
|||||||
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
||||||
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
} catch (err) {
|
result => {
|
||||||
console.error(name, err)
|
let error = runPostEsbuild(
|
||||||
error = (err as BuildFailure).message
|
name,
|
||||||
}
|
result,
|
||||||
|
metaString,
|
||||||
|
CLIArgs,
|
||||||
|
PrettierConfig
|
||||||
|
)
|
||||||
|
watchCallback(metaJson, error)
|
||||||
|
},
|
||||||
|
CLIArgs
|
||||||
|
)
|
||||||
|
|
||||||
//add UserScript header
|
let error = runPostEsbuild(
|
||||||
if (existsSync(outPath)) {
|
name,
|
||||||
if (!error) {
|
result,
|
||||||
let content = readFileSync(outPath).toString()
|
metaString,
|
||||||
writeFileSync(outPath, metaString + content)
|
CLIArgs,
|
||||||
} else {
|
PrettierConfig
|
||||||
unlinkSync(outPath)
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return [metaJson, 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
|
||||||
|
}
|
||||||
|
|
||||||
|
function runEsbuild(
|
||||||
|
opts: BuildOptions,
|
||||||
|
watchCallback: (result: RunEsbuildResult) => void,
|
||||||
|
CLIArgs: CLIArgs
|
||||||
|
): RunEsbuildResult {
|
||||||
|
opts.write = false
|
||||||
|
if (CLIArgs.watch) {
|
||||||
|
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 = buildSync(opts)
|
||||||
|
let content = ''
|
||||||
|
if (res.outputFiles && res.outputFiles.length > 0) {
|
||||||
|
content = clearFilenameComments(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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function runPostEsbuild(
|
||||||
|
name: string,
|
||||||
|
result: RunEsbuildResult,
|
||||||
|
metaString: string,
|
||||||
|
CLIArgs: CLIArgs,
|
||||||
|
PrettierConfig: Options
|
||||||
|
) {
|
||||||
|
let error: string | null = null
|
||||||
|
let path = ScriptPath(name)
|
||||||
|
let pathDist = DistPath(name)
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
console.error(name, result.errorRaw || result.error)
|
||||||
|
error = result.error
|
||||||
|
} else if (result.content) {
|
||||||
|
let content = metaString + result.content
|
||||||
|
if (CLIArgs.prettier) {
|
||||||
|
content = format(content, PrettierConfig)
|
||||||
|
}
|
||||||
|
writeFileSync(pathDist, content)
|
||||||
|
} else {
|
||||||
|
console.error(name, 'No output')
|
||||||
|
}
|
||||||
|
|
||||||
|
doErrorFile(path.error, pathDist, error)
|
||||||
|
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
|
||||||
|
//remove all filename comments
|
||||||
|
function clearFilenameComments(content: string): string {
|
||||||
|
let regexp = new RegExp(`//\\s*${ScriptBase}/.*(?:\\n|$)`, 'g')
|
||||||
|
return content.replace(regexp, '')
|
||||||
|
}
|
||||||
|
|||||||
81
src/main.ts
81
src/main.ts
@@ -1,22 +1,23 @@
|
|||||||
import {
|
import { existsSync, lstatSync, readdirSync, unlinkSync } from 'fs'
|
||||||
existsSync,
|
|
||||||
lstatSync,
|
|
||||||
readdirSync,
|
|
||||||
unlinkSync,
|
|
||||||
writeFileSync,
|
|
||||||
} from 'fs'
|
|
||||||
import commandLineArgs from 'command-line-args'
|
import commandLineArgs from 'command-line-args'
|
||||||
import { DistBase, DistPath, ScriptBase, ScriptPath } from './paths'
|
import { resolveConfig } from 'prettier'
|
||||||
|
import { DistBase, ScriptBase, ScriptPath } from './paths'
|
||||||
import { readmeData, updateReadmeFile } from './readmefile'
|
import { readmeData, updateReadmeFile } from './readmefile'
|
||||||
import runBuild from './build'
|
import runBuild from './build'
|
||||||
import { UserScriptMetaFull } from './types'
|
import { UserScriptMetaFull } from './types'
|
||||||
|
|
||||||
const CLIArgs = commandLineArgs([
|
export interface CLIArgs {
|
||||||
{ name: 'watch', alias: 'w', type: Boolean },
|
|
||||||
]) as {
|
|
||||||
watch: boolean
|
watch: boolean
|
||||||
|
minify: boolean
|
||||||
|
prettier: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CLIArgs = commandLineArgs([
|
||||||
|
{ name: 'watch', alias: 'w', type: Boolean, defaultValue: false },
|
||||||
|
{ name: 'minify', alias: 'm', type: Boolean, defaultValue: false },
|
||||||
|
{ name: 'prettier', alias: 'p', type: Boolean, defaultValue: false },
|
||||||
|
]) as CLIArgs
|
||||||
|
|
||||||
//if package.json doesn't exist then there is no point in continuing
|
//if package.json doesn't exist then there is no point in continuing
|
||||||
if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
||||||
console.error('package.json not found, unwilling to run')
|
console.error('package.json not found, unwilling to run')
|
||||||
@@ -26,12 +27,20 @@ if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
|||||||
//delete compiled scripts
|
//delete compiled scripts
|
||||||
readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
|
readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
|
||||||
|
|
||||||
//compile scripts
|
//read prettierrc file and make sure `babel` is the configured parser
|
||||||
;(async () => {
|
const PrettierConfig = (() => {
|
||||||
let scripts = readdirSync(ScriptBase)
|
let config = resolveConfig.sync(process.cwd()) || {}
|
||||||
let scriptMeta: readmeData[] = []
|
return {
|
||||||
|
...config,
|
||||||
|
parser: 'babel',
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
for (let name of scripts) {
|
//compile scripts
|
||||||
|
let scripts = readdirSync(ScriptBase)
|
||||||
|
let scriptMeta: readmeData[] = []
|
||||||
|
|
||||||
|
for (let name of scripts) {
|
||||||
let path = ScriptPath(name)
|
let path = ScriptPath(name)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -47,45 +56,25 @@ readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
|
|||||||
meta: UserScriptMetaFull,
|
meta: UserScriptMetaFull,
|
||||||
error: string | null
|
error: string | null
|
||||||
) {
|
) {
|
||||||
console.log('watch callback')
|
|
||||||
scriptMeta[id] = { meta, error }
|
scriptMeta[id] = { meta, error }
|
||||||
doErrorFile(path.error, DistPath(name), error)
|
|
||||||
|
|
||||||
console.log('WATCH', name, meta.version)
|
console.log('WATCH', name, meta.version)
|
||||||
|
|
||||||
updateReadmeFile(scriptMeta)
|
updateReadmeFile(scriptMeta)
|
||||||
}
|
}
|
||||||
|
|
||||||
let [meta, error] = await runBuild(
|
let [meta, error] = runBuild(
|
||||||
name,
|
name,
|
||||||
CLIArgs.watch ? postWatchUpdate : false
|
postWatchUpdate,
|
||||||
|
PrettierConfig,
|
||||||
|
CLIArgs
|
||||||
)
|
)
|
||||||
scriptMeta[id] = { meta, error }
|
scriptMeta[id] = { meta, error }
|
||||||
|
|
||||||
console.log(name, meta.version)
|
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')
|
|
||||||
})()
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateReadmeFile(scriptMeta)
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`\nFinished Compiling\n${CLIArgs.watch ? 'Listening for Changes\n' : ''}`
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user