Merge branch 'main' of git.zomo.dev:zomo/browser-scripts-builder into main
This commit is contained in:
159
lib/build.js
159
lib/build.js
@@ -7,46 +7,131 @@ const esbuild_1 = require("esbuild");
|
||||
const fs_1 = require("fs");
|
||||
const paths_1 = require("./paths");
|
||||
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
|
||||
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,
|
||||
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);
|
||||
}
|
||||
else {
|
||||
(0, fs_1.unlinkSync)(outPath);
|
||||
}
|
||||
}
|
||||
let pathDist = (0, paths_1.DistPath)(name);
|
||||
let result = runEsbuild({
|
||||
entryPoints: [(0, paths_1.ScriptPath)(name).main],
|
||||
outfile: pathDist,
|
||||
target: 'esnext',
|
||||
platform: 'node',
|
||||
format: 'esm',
|
||||
bundle: true,
|
||||
minify: CLIArgs.minify,
|
||||
define: {
|
||||
UserScriptName: `'${metaJson.name}'`,
|
||||
UserScriptNamespace: `'${metaJson.namespace}'`,
|
||||
UserScriptVersion: `'${metaJson.version}'`,
|
||||
UserScriptDownloadURL: `'${metaJson.downloadURL}'`,
|
||||
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
||||
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
||||
},
|
||||
}, result => {
|
||||
let error = runPostEsbuild(name, result, metaString, CLIArgs, PrettierConfig);
|
||||
watchCallback(metaJson, error);
|
||||
}, CLIArgs);
|
||||
let error = runPostEsbuild(name, result, metaString, CLIArgs, PrettierConfig);
|
||||
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);
|
||||
}
|
||||
}
|
||||
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
|
||||
@@ -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,Q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,gBAAQ,EAAC,IAAI,CAAC,CAAA;IAE7B,IAAI,MAAM,CAAC,KAAK,EAAE;QACd,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,KAAK,CAAC,CAAA;QACpD,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;KACvB;SAAM,IAAI,MAAM,CAAC,OAAO,EAAE;QACvB,IAAI,OAAO,GAAG,UAAU,GAAG,MAAM,CAAC,OAAO,CAAA;QACzC,IAAI,OAAO,CAAC,QAAQ,EAAE;YAClB,OAAO,GAAG,IAAA,iBAAM,EAAC,OAAO,EAAE,cAAc,CAAC,CAAA;SAC5C;QACD,IAAA,kBAAa,EAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;KACnC;SAAM;QACH,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,WAAW,CAAC,CAAA;KACnC;IAED,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAA;IAExC,OAAO,KAAK,CAAA;AAChB,CAAC;AAED,8BAA8B;AAC9B,SAAS,qBAAqB,CAAC,OAAe;IAC1C,IAAI,MAAM,GAAG,IAAI,MAAM,CAAC,SAAS,kBAAU,cAAc,EAAE,GAAG,CAAC,CAAA;IAC/D,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;AACtC,CAAC"}
|
||||
68
lib/main.js
68
lib/main.js
@@ -5,11 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs_1 = require("fs");
|
||||
const command_line_args_1 = __importDefault(require("command-line-args"));
|
||||
const prettier_1 = require("prettier");
|
||||
const paths_1 = require("./paths");
|
||||
const readmefile_1 = require("./readmefile");
|
||||
const build_1 = __importDefault(require("./build"));
|
||||
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 (!(0, fs_1.existsSync)('package.json') || !(0, fs_1.lstatSync)('package.json').isFile()) {
|
||||
@@ -18,44 +21,35 @@ if (!(0, fs_1.existsSync)('package.json') || !(0, fs_1.lstatSync)('package.json'
|
||||
}
|
||||
//delete compiled scripts
|
||||
(0, fs_1.readdirSync)(paths_1.DistBase).forEach(file => (0, fs_1.unlinkSync)(`${paths_1.DistBase}/${file}`));
|
||||
(async () => {
|
||||
let scripts = (0, fs_1.readdirSync)(paths_1.ScriptBase);
|
||||
let scriptMeta = [];
|
||||
for (let name of scripts) {
|
||||
let path = (0, paths_1.ScriptPath)(name);
|
||||
if (!name.endsWith('_') &&
|
||||
(0, fs_1.existsSync)(path.dir) &&
|
||||
(0, fs_1.lstatSync)(path.dir).isDirectory() &&
|
||||
(0, fs_1.existsSync)(path.main) &&
|
||||
(0, fs_1.lstatSync)(path.main).isFile()) {
|
||||
let id = scriptMeta.length;
|
||||
function postWatchUpdate(meta, error) {
|
||||
console.log('watch callback');
|
||||
scriptMeta[id] = { meta, error };
|
||||
doErrorFile(path.error, (0, paths_1.DistPath)(name), error);
|
||||
console.log('WATCH', name, meta.version);
|
||||
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
||||
}
|
||||
let [meta, error] = await (0, build_1.default)(name, CLIArgs.watch ? postWatchUpdate : false);
|
||||
scriptMeta[id] = { meta, error };
|
||||
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');
|
||||
//read prettierrc file and make sure `babel` is the configured parser
|
||||
const PrettierConfig = (() => {
|
||||
let config = prettier_1.resolveConfig.sync(process.cwd()) || {};
|
||||
return {
|
||||
...config,
|
||||
parser: 'babel',
|
||||
};
|
||||
})();
|
||||
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);
|
||||
//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);
|
||||
if (!name.endsWith('_') &&
|
||||
(0, fs_1.existsSync)(path.dir) &&
|
||||
(0, fs_1.lstatSync)(path.dir).isDirectory() &&
|
||||
(0, fs_1.existsSync)(path.main) &&
|
||||
(0, fs_1.lstatSync)(path.main).isFile()) {
|
||||
let id = scriptMeta.length;
|
||||
function postWatchUpdate(meta, error) {
|
||||
scriptMeta[id] = { meta, error };
|
||||
console.log('WATCH', name, meta.version);
|
||||
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
||||
}
|
||||
}
|
||||
else if ((0, fs_1.existsSync)(pathError)) {
|
||||
(0, fs_1.unlinkSync)(pathError);
|
||||
let [meta, error] = (0, build_1.default)(name, postWatchUpdate, PrettierConfig, CLIArgs);
|
||||
scriptMeta[id] = { meta, error };
|
||||
console.log(name, meta.version);
|
||||
}
|
||||
}
|
||||
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
||||
console.log(`\nFinished Compiling\n${CLIArgs.watch ? 'Listening for Changes\n' : ''}`);
|
||||
//# 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,oD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,CAAC,GAAG,CACP,yBAAyB,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAAC,EAAE,EAAE,CAC5E,CAAA"}
|
||||
@@ -19,14 +19,15 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@types/command-line-args": "^5.2.0",
|
||||
"command-line-args": "^5.2.1",
|
||||
"esbuild": "^0.14.42",
|
||||
"typescript": "^4.7.3"
|
||||
"typescript": "^4.7.3",
|
||||
"prettier": "^2.6.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/command-line-args": "^5.2.0",
|
||||
"@types/node": "^17.0.40",
|
||||
"eslint": "^8.17.0",
|
||||
"prettier": "^2.6.2"
|
||||
"@types/prettier": "^2.6.3",
|
||||
"eslint": "^8.17.0"
|
||||
}
|
||||
}
|
||||
|
||||
14
pnpm-lock.yaml
generated
14
pnpm-lock.yaml
generated
@@ -3,6 +3,7 @@ lockfileVersion: 5.4
|
||||
specifiers:
|
||||
'@types/command-line-args': ^5.2.0
|
||||
'@types/node': ^17.0.40
|
||||
'@types/prettier': ^2.6.3
|
||||
command-line-args: ^5.2.1
|
||||
esbuild: ^0.14.42
|
||||
eslint: ^8.17.0
|
||||
@@ -10,15 +11,16 @@ specifiers:
|
||||
typescript: ^4.7.3
|
||||
|
||||
dependencies:
|
||||
'@types/command-line-args': 5.2.0
|
||||
command-line-args: 5.2.1
|
||||
esbuild: 0.14.42
|
||||
prettier: 2.6.2
|
||||
typescript: 4.7.3
|
||||
|
||||
devDependencies:
|
||||
'@types/command-line-args': 5.2.0
|
||||
'@types/node': 17.0.40
|
||||
'@types/prettier': 2.6.3
|
||||
eslint: 8.17.0
|
||||
prettier: 2.6.2
|
||||
|
||||
packages:
|
||||
|
||||
@@ -56,12 +58,16 @@ packages:
|
||||
|
||||
/@types/command-line-args/5.2.0:
|
||||
resolution: {integrity: sha512-UuKzKpJJ/Ief6ufIaIzr3A/0XnluX7RvFgwkV89Yzvm77wCh1kFaFmqN8XEnGcN62EuHdedQjEMb8mYxFLGPyA==}
|
||||
dev: false
|
||||
dev: true
|
||||
|
||||
/@types/node/17.0.40:
|
||||
resolution: {integrity: sha512-UXdBxNGqTMtm7hCwh9HtncFVLrXoqA3oJW30j6XWp5BH/wu3mVeaxo7cq5benFdBw34HB3XDT2TRPI7rXZ+mDg==}
|
||||
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:
|
||||
resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==}
|
||||
peerDependencies:
|
||||
@@ -715,7 +721,7 @@ packages:
|
||||
resolution: {integrity: sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==}
|
||||
engines: {node: '>=10.13.0'}
|
||||
hasBin: true
|
||||
dev: true
|
||||
dev: false
|
||||
|
||||
/punycode/2.1.1:
|
||||
resolution: {integrity: sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==}
|
||||
|
||||
24
readme.md
24
readme.md
@@ -23,3 +23,27 @@ Root
|
||||
└───dist
|
||||
└───[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 { existsSync, readFileSync, writeFileSync, unlinkSync } from 'fs'
|
||||
import { DistPath, ScriptPath } from './paths'
|
||||
import { buildSync, BuildFailure, BuildOptions } from 'esbuild'
|
||||
import { existsSync, writeFileSync, unlinkSync } from 'fs'
|
||||
import { DistPath, ScriptBase, ScriptPath } from './paths'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
import readMeta from './readmeta'
|
||||
import { format, Options } from 'prettier'
|
||||
import { CLIArgs } from './main'
|
||||
|
||||
export default async function (
|
||||
export default function (
|
||||
name: string,
|
||||
watchCallback:
|
||||
| ((meta: UserScriptMetaFull, error: string | null) => void)
|
||||
| false = false
|
||||
): Promise<[UserScriptMetaFull, string | null]> {
|
||||
watchCallback: (meta: UserScriptMetaFull, error: string | null) => void,
|
||||
PrettierConfig: Options,
|
||||
CLIArgs: CLIArgs
|
||||
): [UserScriptMetaFull, string | null] {
|
||||
//read meta file
|
||||
let [metaJson, metaString] = readMeta(name)
|
||||
let outPath = DistPath(name)
|
||||
let pathDist = DistPath(name)
|
||||
|
||||
let error: string | null = null
|
||||
|
||||
console.log('build watch?', !watchCallback ? false : 'obj')
|
||||
|
||||
try {
|
||||
await build({
|
||||
let result = runEsbuild(
|
||||
{
|
||||
entryPoints: [ScriptPath(name).main],
|
||||
outfile: outPath,
|
||||
outfile: pathDist,
|
||||
|
||||
target: 'esnext',
|
||||
platform: 'node',
|
||||
format: 'esm',
|
||||
|
||||
bundle: true,
|
||||
minify: false,
|
||||
minify: CLIArgs.minify,
|
||||
|
||||
define: {
|
||||
UserScriptName: `'${metaJson.name}'`,
|
||||
@@ -39,21 +37,148 @@ export default async function (
|
||||
UserScriptSupportURL: `'${metaJson.supportURL}'`,
|
||||
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(name, err)
|
||||
error = (err as BuildFailure).message
|
||||
}
|
||||
},
|
||||
result => {
|
||||
let error = runPostEsbuild(
|
||||
name,
|
||||
result,
|
||||
metaString,
|
||||
CLIArgs,
|
||||
PrettierConfig
|
||||
)
|
||||
watchCallback(metaJson, error)
|
||||
},
|
||||
CLIArgs
|
||||
)
|
||||
|
||||
//add UserScript header
|
||||
if (existsSync(outPath)) {
|
||||
if (!error) {
|
||||
let content = readFileSync(outPath).toString()
|
||||
writeFileSync(outPath, metaString + content)
|
||||
} else {
|
||||
unlinkSync(outPath)
|
||||
}
|
||||
}
|
||||
let error = runPostEsbuild(
|
||||
name,
|
||||
result,
|
||||
metaString,
|
||||
CLIArgs,
|
||||
PrettierConfig
|
||||
)
|
||||
|
||||
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, '')
|
||||
}
|
||||
|
||||
123
src/main.ts
123
src/main.ts
@@ -1,22 +1,23 @@
|
||||
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 { resolveConfig } from 'prettier'
|
||||
import { DistBase, ScriptBase, ScriptPath } from './paths'
|
||||
import { readmeData, updateReadmeFile } from './readmefile'
|
||||
import runBuild from './build'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
|
||||
const CLIArgs = commandLineArgs([
|
||||
{ name: 'watch', alias: 'w', type: Boolean },
|
||||
]) as {
|
||||
export interface CLIArgs {
|
||||
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 (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
||||
console.error('package.json not found, unwilling to run')
|
||||
@@ -26,66 +27,54 @@ if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
||||
//delete compiled scripts
|
||||
readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
|
||||
|
||||
//compile scripts
|
||||
;(async () => {
|
||||
let scripts = readdirSync(ScriptBase)
|
||||
let scriptMeta: readmeData[] = []
|
||||
|
||||
for (let name of scripts) {
|
||||
let path = ScriptPath(name)
|
||||
|
||||
if (
|
||||
!name.endsWith('_') &&
|
||||
existsSync(path.dir) &&
|
||||
lstatSync(path.dir).isDirectory() &&
|
||||
existsSync(path.main) &&
|
||||
lstatSync(path.main).isFile()
|
||||
) {
|
||||
let id = scriptMeta.length
|
||||
|
||||
function postWatchUpdate(
|
||||
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)
|
||||
}
|
||||
|
||||
let [meta, error] = await runBuild(
|
||||
name,
|
||||
CLIArgs.watch ? postWatchUpdate : false
|
||||
)
|
||||
scriptMeta[id] = { meta, error }
|
||||
|
||||
console.log(name, meta.version)
|
||||
|
||||
doErrorFile(path.error, DistPath(name), error)
|
||||
}
|
||||
//read prettierrc file and make sure `babel` is the configured parser
|
||||
const PrettierConfig = (() => {
|
||||
let config = resolveConfig.sync(process.cwd()) || {}
|
||||
return {
|
||||
...config,
|
||||
parser: 'babel',
|
||||
}
|
||||
|
||||
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}`)
|
||||
//compile scripts
|
||||
let scripts = readdirSync(ScriptBase)
|
||||
let scriptMeta: readmeData[] = []
|
||||
|
||||
if (existsSync(pathOutFile)) {
|
||||
unlinkSync(pathOutFile)
|
||||
for (let name of scripts) {
|
||||
let path = ScriptPath(name)
|
||||
|
||||
if (
|
||||
!name.endsWith('_') &&
|
||||
existsSync(path.dir) &&
|
||||
lstatSync(path.dir).isDirectory() &&
|
||||
existsSync(path.main) &&
|
||||
lstatSync(path.main).isFile()
|
||||
) {
|
||||
let id = scriptMeta.length
|
||||
|
||||
function postWatchUpdate(
|
||||
meta: UserScriptMetaFull,
|
||||
error: string | null
|
||||
) {
|
||||
scriptMeta[id] = { meta, error }
|
||||
console.log('WATCH', name, meta.version)
|
||||
updateReadmeFile(scriptMeta)
|
||||
}
|
||||
} else if (existsSync(pathError)) {
|
||||
unlinkSync(pathError)
|
||||
|
||||
let [meta, error] = runBuild(
|
||||
name,
|
||||
postWatchUpdate,
|
||||
PrettierConfig,
|
||||
CLIArgs
|
||||
)
|
||||
scriptMeta[id] = { meta, error }
|
||||
|
||||
console.log(name, meta.version)
|
||||
}
|
||||
}
|
||||
|
||||
updateReadmeFile(scriptMeta)
|
||||
|
||||
console.log(
|
||||
`\nFinished Compiling\n${CLIArgs.watch ? 'Listening for Changes\n' : ''}`
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user