Files
browser-scripts-builder/lib/main.js
2024-07-17 21:22:33 -05:00

175 lines
6.6 KiB
JavaScript

#!/usr/bin/env node
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AllPaths = exports.CLIArgs = void 0;
const fs_1 = require("fs");
const command_line_args_1 = __importDefault(require("command-line-args"));
const readmefile_1 = require("./readmefile");
const build_1 = __importDefault(require("./build"));
const Path = __importStar(require("path"));
const paths_1 = __importDefault(require("./paths"));
const chokidar = __importStar(require("chokidar"));
exports.CLIArgs = (0, command_line_args_1.default)([
{ name: 'watch', alias: 'w', type: Boolean, defaultValue: false },
{ name: 'minify', alias: 'm', type: Boolean, defaultValue: false },
{ name: 'prettier', alias: 'p', type: Boolean, defaultValue: false },
{ name: 'srccomment', alias: 'c', type: Boolean, defaultValue: false },
{ name: 'readme', alias: 'r', type: Boolean, defaultValue: false },
{ name: 'url', alias: 'u', type: String, defaultValue: '' },
{ name: 'supporturl', alias: 's', type: String, defaultValue: '' },
{ name: 'homepageurl', alias: 'U', type: String, defaultValue: '' },
{ name: 'remotebranch', alias: 'b', type: String, defaultValue: '' },
{ name: 'in', alias: 'i', type: String, defaultValue: 'scripts' },
{ name: 'out', alias: 'o', type: String, defaultValue: 'dist' },
{ name: 'help', alias: 'h', type: Boolean },
]);
exports.AllPaths = (0, paths_1.default)({
baseUrl: exports.CLIArgs.url || '',
supportUrl: exports.CLIArgs.supporturl || '',
homepageUrl: exports.CLIArgs.homepageurl || '',
remoteBranch: exports.CLIArgs.remotebranch || '',
inBase: exports.CLIArgs.in || 'scripts',
outBase: exports.CLIArgs.out || 'dist',
});
if (exports.CLIArgs.help) {
let command = '<command>';
if (process.argv.length > 0) {
command = Path.parse(process.argv[0]).name;
}
if (command.toLowerCase() === 'node' && process.argv.length > 1) {
let path = Path.relative(process.cwd(), process.argv[1]) || '.';
command = `${command} ${path}`;
}
console.log(`
Usage: ${command} [options]
options:
--watch
alias: -w
default: false
automatically recompile on save
--minify
alias: -m
default: false
minify output files
--prettier
alias: -p
default: false
prettify output files
--srccomment
alias: -c
default: false
include src file path comments in the output files, i.e. // scripts/example/main.ts
--readme
alias: -r
default: false
update the readme.md file in your directory to include links to each userscript
--url <url>
alias: -u <url>
default: ""
the base for urls used in the meta comments for @downloadURL
--supporturl <url>
alias: -s <url>
default: ""
the support url used in the meta comments for @supportURL
--homepageurl <url>
alias: -U <url>
default: ""
the support url used in the meta comments for @homepageURL
--remotebranch <name>
alias: -b <name>
default: ""
if included, the included base url will be treated as a git repo, and the support url is not required
--in
alias: -i
default: "scripts"
include src file path comments in the output files, i.e. // scripts/example/main.ts
--out
alias: -o
default: "dist"
include src file path comments in the output files, i.e. // scripts/example/main.ts
--help
alias: -h
show this help message
`);
process.exit(0);
}
//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);
}
//delete compiled scripts or create output folder if it doesnt exist
if (!(0, fs_1.existsSync)(exports.AllPaths.base.out)) {
(0, fs_1.mkdirSync)(exports.AllPaths.base.out);
}
else {
(0, fs_1.readdirSync)(exports.AllPaths.base.out).forEach(file => (0, fs_1.unlinkSync)(`${exports.AllPaths.base.out}/${file}`));
}
//compile scripts
async function doCompile() {
let scripts = (0, fs_1.readdirSync)(exports.AllPaths.base.in);
let scriptMeta = [];
for (let name of scripts) {
let path = exports.AllPaths.script(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;
scriptMeta[id] = await (0, build_1.default)(name);
console.log(name, scriptMeta[id].meta.version);
var running = false;
async function update(eventName) {
if (running) {
return;
}
running = true;
scriptMeta[id] = await (0, build_1.default)(name);
console.log(`WATCH ${eventName}`, name, scriptMeta[id].meta.version);
if (exports.CLIArgs.readme)
(0, readmefile_1.updateReadmeFile)(scriptMeta);
running = false;
}
if (exports.CLIArgs.watch) {
chokidar.watch(path.dir).on('all', update);
}
}
}
return scriptMeta;
}
doCompile().then(scriptMeta => {
if (exports.CLIArgs.readme)
(0, readmefile_1.updateReadmeFile)(scriptMeta);
console.log(`\nFinished Compiling\n${exports.CLIArgs.watch ? 'Listening for Changes\n' : ''}`);
});
//# sourceMappingURL=main.js.map