reorganized main file

This commit is contained in:
2022-06-08 16:46:44 -05:00
parent 3fdb3ad110
commit 74904d1c0f
8 changed files with 240 additions and 123 deletions

65
lib/build.js Normal file
View File

@@ -0,0 +1,65 @@
"use strict";
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 paths_1 = require("./paths");
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;
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,
watch: !watchCallback
? false
: {
onRebuild(err, result) {
if (err) {
console.error(name, err);
let error = err.message;
watchCallback(metaJson, error);
}
else {
console.log('watch build succeeded:', result);
}
},
},
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);
}
}
return [metaJson, error];
}
exports.default = default_1;
//# sourceMappingURL=build.js.map

1
lib/build.js.map Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"build.js","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":";;;;;AAAA,qCAA6C;AAC7C,2BAAwE;AACxE,mCAA8C;AAE9C,0DAAiC;AAElB,KAAK,oB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;aACrD;SACJ,CAAC,CAAA;KACL;IAAC,OAAO,GAAG,EAAE;QACV,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;QACxB,KAAK,GAAI,GAAoB,CAAC,OAAO,CAAA;KACxC;IAED,uBAAuB;IACvB,IAAI,IAAA,eAAU,EAAC,OAAO,CAAC,EAAE;QACrB,IAAI,CAAC,KAAK,EAAE;YACR,IAAI,OAAO,GAAG,IAAA,iBAAY,EAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAA;YAC9C,IAAA,kBAAa,EAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,CAAA;SAC/C;aAAM;YACH,IAAA,eAAU,EAAC,OAAO,CAAC,CAAA;SACtB;KACJ;IAED,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;AAC5B,CAAC;AAhED,4BAgEC"}

View File

@@ -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)) {
(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YAE/B,WAAW,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;SACjC;KACJ;IAED,IAAA,6BAAgB,EAAC,UAAU,CAAC,CAAA;IAE5B,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAA;IACrC,IAAI,OAAO,CAAC,KAAK;QAAE,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAA;AAC7D,CAAC,CAAC,EAAE,CAAA;AAEJ,SAAS,WAAW,CAAC,SAAiB,EAAE,KAAoB;IACxD,IAAI,KAAK,KAAK,IAAI,EAAE;QAChB,IAAA,kBAAa,EAAC,SAAS,EAAE,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,OAAO,KAAK,EAAE,CAAC,CAAA;KACtE;SAAM,IAAI,IAAA,eAAU,EAAC,SAAS,CAAC,EAAE;QAC9B,IAAA,eAAU,EAAC,SAAS,CAAC,CAAA;KACxB;AACL,CAAC"}

View File

@@ -19,6 +19,8 @@
"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"
},

39
pnpm-lock.yaml generated
View File

@@ -1,13 +1,17 @@
lockfileVersion: 5.3
specifiers:
'@types/command-line-args': ^5.2.0
'@types/node': ^17.0.40
command-line-args: ^5.2.1
esbuild: ^0.14.42
eslint: ^8.17.0
prettier: ^2.6.2
typescript: ^4.7.3
dependencies:
'@types/command-line-args': 5.2.0
command-line-args: 5.2.1
esbuild: 0.14.42
typescript: 4.7.3
@@ -50,6 +54,10 @@ packages:
resolution: {integrity: sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==}
dev: true
/@types/command-line-args/5.2.0:
resolution: {integrity: sha512-UuKzKpJJ/Ief6ufIaIzr3A/0XnluX7RvFgwkV89Yzvm77wCh1kFaFmqN8XEnGcN62EuHdedQjEMb8mYxFLGPyA==}
dev: false
/@types/node/17.0.40:
resolution: {integrity: sha512-UXdBxNGqTMtm7hCwh9HtncFVLrXoqA3oJW30j6XWp5BH/wu3mVeaxo7cq5benFdBw34HB3XDT2TRPI7rXZ+mDg==}
dev: true
@@ -93,6 +101,11 @@ packages:
resolution: {integrity: sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==}
dev: true
/array-back/3.1.0:
resolution: {integrity: sha512-TkuxA4UCOvxuDK6NZYXCalszEzj+TLszyASooky+i742l9TqsOdYCMJJupxRic61hwquNtppB3hgcuq9SVSH1Q==}
engines: {node: '>=6'}
dev: false
/balanced-match/1.0.2:
resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}
dev: true
@@ -128,6 +141,16 @@ packages:
resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==}
dev: true
/command-line-args/5.2.1:
resolution: {integrity: sha512-H4UfQhZyakIjC74I9d34fGYDwk3XpSr17QhEd0Q3I9Xq1CETHo4Hcuo87WyWHpAF1aSLjLRf5lD9ZGX2qStUvg==}
engines: {node: '>=4.0.0'}
dependencies:
array-back: 3.1.0
find-replace: 3.0.0
lodash.camelcase: 4.3.0
typical: 4.0.0
dev: false
/concat-map/0.0.1:
resolution: {integrity: sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=}
dev: true
@@ -501,6 +524,13 @@ packages:
flat-cache: 3.0.4
dev: true
/find-replace/3.0.0:
resolution: {integrity: sha512-6Tb2myMioCAgv5kfvP5/PkZZ/ntTpVK39fHY7WkWBgvbeE+VHd/tZuZ4mrC+bxh4cfOZeYKVPaJIZtZXV7GNCQ==}
engines: {node: '>=4.0.0'}
dependencies:
array-back: 3.1.0
dev: false
/flat-cache/3.0.4:
resolution: {integrity: sha512-dm9s5Pw7Jc0GvMYbshN6zchCA9RgQlzzEZX3vylR9IqFfS8XciblUXOKfW6SiuJ0e13eDYZoZV5wdrev7P3Nwg==}
engines: {node: ^10.12.0 || >=12.0.0}
@@ -619,6 +649,10 @@ packages:
type-check: 0.4.0
dev: true
/lodash.camelcase/4.3.0:
resolution: {integrity: sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==}
dev: false
/lodash.merge/4.6.2:
resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==}
dev: true
@@ -758,6 +792,11 @@ packages:
hasBin: true
dev: false
/typical/4.0.0:
resolution: {integrity: sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw==}
engines: {node: '>=8'}
dev: false
/uri-js/4.4.1:
resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==}
dependencies:

71
src/build.ts Normal file
View File

@@ -0,0 +1,71 @@
import { build, BuildFailure } from 'esbuild'
import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'fs'
import { DistPath, ScriptPath } from './paths'
import { UserScriptMetaFull } from './types'
import readMeta from './readmeta'
export default async function (
name: string,
watchCallback:
| ((meta: UserScriptMetaFull, error: string | null) => void)
| false = false
): Promise<[UserScriptMetaFull, string | null]> {
//read meta file
let [metaJson, metaString] = readMeta(name)
let outPath = DistPath(name)
let error: string | null = null
try {
await build({
entryPoints: [ScriptPath(name).main],
outfile: outPath,
target: 'esnext',
platform: 'node',
format: 'esm',
bundle: true,
minify: false,
watch: !watchCallback
? false
: {
onRebuild(err, result) {
if (err) {
console.error(name, err)
let error = (err as BuildFailure).message
watchCallback(metaJson, error)
} else {
console.log('watch build succeeded:', result)
}
},
},
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 as BuildFailure).message
}
//add UserScript header
if (existsSync(outPath)) {
if (!error) {
let content = readFileSync(outPath).toString()
writeFileSync(outPath, metaString + content)
} else {
unlinkSync(outPath)
}
}
return [metaJson, error]
}

View File

@@ -1,66 +1,23 @@
import { build, BuildFailure } from 'esbuild'
import {
existsSync,
lstatSync,
readdirSync,
readFileSync,
unlinkSync,
writeFileSync,
} from 'fs'
import { DistBase, DistPath, ScriptBase, ScriptPath } from './paths'
import commandLineArgs from 'command-line-args'
import { DistBase, ScriptBase, ScriptPath } from './paths'
import { readmeData, updateReadmeFile } from './readmefile'
import readMeta from './readmeta'
import runBuild from './build'
import { UserScriptMetaFull } from './types'
async function compileProject(
name: string
): Promise<[UserScriptMetaFull, null | string]> {
//read meta file
let [metaJson, metaString] = readMeta(name)
let outPath = DistPath(name)
let error: null | string = null
try {
await build({
entryPoints: [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 as BuildFailure).message
}
//add UserScript header
if (existsSync(outPath)) {
if (!error) {
let content = readFileSync(outPath).toString()
writeFileSync(outPath, metaString + content)
} else {
unlinkSync(outPath)
}
}
return [metaJson, error]
const CLIArgs = commandLineArgs([
{ name: 'watch', alias: 'w', type: Boolean },
]) as {
watch: boolean
}
//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')
process.exit(1)
@@ -72,17 +29,11 @@ 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)
//delete error file if it exists
if (existsSync(path.error)) {
unlinkSync(path.error)
}
if (
!name.endsWith('_') &&
existsSync(path.dir) &&
@@ -90,23 +41,39 @@ readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
existsSync(path.main) &&
lstatSync(path.main).isFile()
) {
let [meta, error] = await compileProject(name)
scriptMeta.push({
meta,
error,
})
let id = scriptMeta.length
//write error file
if (error !== null) {
writeFileSync(
path.error,
`${new Date().toISOString()}\n\n${error}`
)
function postWatchUpdate(
meta: UserScriptMetaFull,
error: string | null
) {
scriptMeta[id] = { meta, error }
doErrorFile(path.error, error)
console.log('WATCH', name, meta.version)
updateReadmeFile(scriptMeta)
}
let [meta, error] = await runBuild(name, postWatchUpdate)
scriptMeta[id] = { meta, error }
console.log(name, meta.version)
doErrorFile(path.error, error)
}
}
updateReadmeFile(scriptMeta)
console.log('\nFinished Compiling\n')
if (CLIArgs.watch) console.log('Listening for Changes\n')
})()
function doErrorFile(pathError: string, error: string | null) {
if (error !== null) {
writeFileSync(pathError, `${new Date().toISOString()}\n\n${error}`)
} else if (existsSync(pathError)) {
unlinkSync(pathError)
}
}