reorganized main file
This commit is contained in:
80
lib/main.js
80
lib/main.js
@@ -3,51 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const esbuild_1 = require("esbuild");
|
||||
const fs_1 = require("fs");
|
||||
const command_line_args_1 = __importDefault(require("command-line-args"));
|
||||
const paths_1 = require("./paths");
|
||||
const readmefile_1 = require("./readmefile");
|
||||
const readmeta_1 = __importDefault(require("./readmeta"));
|
||||
async function compileProject(name) {
|
||||
//read meta file
|
||||
let [metaJson, metaString] = (0, readmeta_1.default)(name);
|
||||
let outPath = (0, paths_1.DistPath)(name);
|
||||
let error = null;
|
||||
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 (e) {
|
||||
console.error(name, e); //TODO better error log
|
||||
error = e.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);
|
||||
}
|
||||
}
|
||||
return [metaJson, error];
|
||||
}
|
||||
const build_1 = __importDefault(require("./build"));
|
||||
const CLIArgs = (0, command_line_args_1.default)([
|
||||
{ name: 'watch', alias: 'w', type: Boolean },
|
||||
]);
|
||||
//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()) {
|
||||
console.error('package.json not found, unwilling to run');
|
||||
process.exit(1);
|
||||
@@ -59,27 +23,35 @@ if (!(0, fs_1.existsSync)('package.json') || !(0, fs_1.lstatSync)('package.json'
|
||||
let scriptMeta = [];
|
||||
for (let name of scripts) {
|
||||
let path = (0, paths_1.ScriptPath)(name);
|
||||
//delete error file if it exists
|
||||
if ((0, fs_1.existsSync)(path.error)) {
|
||||
(0, fs_1.unlinkSync)(path.error);
|
||||
}
|
||||
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 [meta, error] = await compileProject(name);
|
||||
scriptMeta.push({
|
||||
meta,
|
||||
error,
|
||||
});
|
||||
//write error file
|
||||
if (error !== null) {
|
||||
(0, fs_1.writeFileSync)(path.error, `${new Date().toISOString()}\n\n${error}`);
|
||||
let id = scriptMeta.length;
|
||||
function postWatchUpdate(meta, error) {
|
||||
scriptMeta[id] = { meta, error };
|
||||
doErrorFile(path.error, error);
|
||||
console.log('WATCH', name, meta.version);
|
||||
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
||||
}
|
||||
let [meta, error] = await (0, build_1.default)(name, postWatchUpdate);
|
||||
scriptMeta[id] = { meta, error };
|
||||
console.log(name, meta.version);
|
||||
doErrorFile(path.error, error);
|
||||
}
|
||||
}
|
||||
(0, readmefile_1.updateReadmeFile)(scriptMeta);
|
||||
console.log('\nFinished Compiling\n');
|
||||
if (CLIArgs.watch)
|
||||
console.log('Listening for Changes\n');
|
||||
})();
|
||||
function doErrorFile(pathError, error) {
|
||||
if (error !== null) {
|
||||
(0, fs_1.writeFileSync)(pathError, `${new Date().toISOString()}\n\n${error}`);
|
||||
}
|
||||
else if ((0, fs_1.existsSync)(pathError)) {
|
||||
(0, fs_1.unlinkSync)(pathError);
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=main.js.map
|
||||
Reference in New Issue
Block a user