From c48e085b8d4ce6d3de9422df3799036251df47b4 Mon Sep 17 00:00:00 2001 From: windingwind Date: Sun, 7 May 2023 01:43:04 +0800 Subject: [PATCH] fix: build async bug fix: eslint esm bug --- package.json | 14 ++-- scripts/{build.js => build.mjs} | 118 ++++++++++++++++---------------- scripts/{start.js => start.mjs} | 11 +-- scripts/stop.js | 10 --- scripts/stop.mjs | 14 ++++ 5 files changed, 88 insertions(+), 79 deletions(-) rename scripts/{build.js => build.mjs} (54%) rename scripts/{start.js => start.mjs} (65%) delete mode 100644 scripts/stop.js create mode 100644 scripts/stop.mjs diff --git a/package.json b/package.json index e3e3715..e1a375e 100644 --- a/package.json +++ b/package.json @@ -12,14 +12,14 @@ }, "main": "src/index.ts", "scripts": { - "build-dev": "cross-env NODE_ENV=development node scripts/build.js", - "build-prod": "cross-env NODE_ENV=production node scripts/build.js", + "build-dev": "cross-env NODE_ENV=development node scripts/build.mjs", + "build-prod": "cross-env NODE_ENV=production node scripts/build.mjs", "build": "concurrently -c auto npm:build-prod npm:tsc", "tsc": "tsc --noEmit", - "start-z6": "node scripts/start.js --z 6", - "start-z7": "node scripts/start.js --z 7", - "start": "node scripts/start.js", - "stop": "node scripts/stop.js", + "start-z6": "node scripts/start.mjs --z 6", + "start-z7": "node scripts/start.mjs --z 7", + "start": "node scripts/start.mjs", + "stop": "node scripts/stop.mjs", "restart-dev": "npm run build-dev && npm run stop && npm run start", "restart-prod": "npm run build-prod && npm run stop && npm run start", "restart": "npm run restart-dev", @@ -61,4 +61,4 @@ "prettier": { "tabWidth": 2 } -} +} \ No newline at end of file diff --git a/scripts/build.js b/scripts/build.mjs similarity index 54% rename from scripts/build.js rename to scripts/build.mjs index c13b6f4..c1a766e 100644 --- a/scripts/build.js +++ b/scripts/build.mjs @@ -1,46 +1,50 @@ -const esbuild = require("esbuild"); -const compressing = require("compressing"); -const path = require("path"); -const fs = require("fs"); -const process = require("process"); -const replace = require("replace-in-file"); -const { - name, - author, - description, - homepage, - version, - config, -} = require("../package.json"); +import { build } from "esbuild"; +import { zip } from "compressing"; +import { join, basename } from "path"; +import { + existsSync, + lstatSync, + writeFileSync, + readFileSync, + mkdirSync, + readdirSync, + rmSync, +} from "fs"; +import { env, exit } from "process"; +import replaceInFile from "replace-in-file"; +const { sync } = replaceInFile; +import details from "../package.json" assert { type: "json" }; + +const { name, author, description, homepage, version, config } = details; function copyFileSync(source, target) { var targetFile = target; // If target is a directory, a new file with the same name will be created - if (fs.existsSync(target)) { - if (fs.lstatSync(target).isDirectory()) { - targetFile = path.join(target, path.basename(source)); + if (existsSync(target)) { + if (lstatSync(target).isDirectory()) { + targetFile = join(target, basename(source)); } } - fs.writeFileSync(targetFile, fs.readFileSync(source)); + writeFileSync(targetFile, readFileSync(source)); } function copyFolderRecursiveSync(source, target) { var files = []; // Check if folder needs to be created or integrated - var targetFolder = path.join(target, path.basename(source)); - if (!fs.existsSync(targetFolder)) { - fs.mkdirSync(targetFolder); + var targetFolder = join(target, basename(source)); + if (!existsSync(targetFolder)) { + mkdirSync(targetFolder); } // Copy - if (fs.lstatSync(source).isDirectory()) { - files = fs.readdirSync(source); + if (lstatSync(source).isDirectory()) { + files = readdirSync(source); files.forEach(function (file) { - var curSource = path.join(source, file); - if (fs.lstatSync(curSource).isDirectory()) { + var curSource = join(source, file); + if (lstatSync(curSource).isDirectory()) { copyFolderRecursiveSync(curSource, targetFolder); } else { copyFileSync(curSource, targetFolder); @@ -50,11 +54,11 @@ function copyFolderRecursiveSync(source, target) { } function clearFolder(target) { - if (fs.existsSync(target)) { - fs.rmSync(target, { recursive: true, force: true }); + if (existsSync(target)) { + rmSync(target, { recursive: true, force: true }); } - fs.mkdirSync(target, { recursive: true }); + mkdirSync(target, { recursive: true }); } function dateFormat(fmt, date) { @@ -86,7 +90,7 @@ async function main() { console.log( `[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[ - process.env.NODE_ENV, + env.NODE_ENV, ]}` ); @@ -97,18 +101,16 @@ async function main() { copyFileSync("update-template.json", "update.json"); copyFileSync("update-template.rdf", "update.rdf"); - await esbuild - .build({ - entryPoints: ["src/index.ts"], - define: { - __env__: `"${process.env.NODE_ENV}"`, - }, - bundle: true, - outfile: path.join(buildDir, "addon/chrome/content/scripts/index.js"), - // Don't turn minify on - // minify: true, - }) - .catch(() => process.exit(1)); + await build({ + entryPoints: ["src/index.ts"], + define: { + __env__: `"${env.NODE_ENV}"`, + }, + bundle: true, + outfile: join(buildDir, "addon/chrome/content/scripts/index.js"), + // Don't turn minify on + // minify: true, + }).catch(() => exit(1)); console.log("[Build] Run esbuild OK"); @@ -129,15 +131,15 @@ async function main() { const optionsAddon = { files: [ - path.join(buildDir, "**/*.rdf"), - path.join(buildDir, "**/*.dtd"), - path.join(buildDir, "**/*.xul"), - path.join(buildDir, "**/*.xhtml"), - path.join(buildDir, "**/*.json"), - path.join(buildDir, "addon/prefs.js"), - path.join(buildDir, "addon/chrome.manifest"), - path.join(buildDir, "addon/manifest.json"), - path.join(buildDir, "addon/bootstrap.js"), + join(buildDir, "**/*.rdf"), + join(buildDir, "**/*.dtd"), + join(buildDir, "**/*.xul"), + join(buildDir, "**/*.xhtml"), + join(buildDir, "**/*.json"), + join(buildDir, "addon/prefs.js"), + join(buildDir, "addon/chrome.manifest"), + join(buildDir, "addon/manifest.json"), + join(buildDir, "addon/bootstrap.js"), "update.json", "update.rdf", ], @@ -146,21 +148,21 @@ async function main() { countMatches: true, }; - _ = replace.sync(optionsAddon); + const replaceResult = sync(optionsAddon); console.log( "[Build] Run replace in ", - _.filter((f) => f.hasChanged).map( - (f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}` - ) + replaceResult + .filter((f) => f.hasChanged) + .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`) ); console.log("[Build] Replace OK"); console.log("[Build] Addon prepare OK"); - compressing.zip.compressDir( - path.join(buildDir, "addon"), - path.join(buildDir, `${name}.xpi`), + await zip.compressDir( + join(buildDir, "addon"), + join(buildDir, `${name}.xpi`), { ignoreBase: true, } @@ -174,5 +176,5 @@ async function main() { main().catch((err) => { console.log(err); - process.exit(1); + exit(1); }); diff --git a/scripts/start.js b/scripts/start.mjs similarity index 65% rename from scripts/start.js rename to scripts/start.mjs index 2485412..5cec962 100644 --- a/scripts/start.js +++ b/scripts/start.mjs @@ -1,9 +1,12 @@ -const { execSync } = require("child_process"); -const { exit } = require("process"); -const { exec } = require("./zotero-cmd.json"); +import process from "process"; +import { execSync } from "child_process"; +import { exit } from "process"; +import minimist from "minimist"; +import cmd from "./zotero-cmd.json" assert { type: "json" }; +const { exec } = cmd; // Run node start.js -h for help -const args = require("minimist")(process.argv.slice(2)); +const args = minimist(process.argv.slice(2)); if (args.help || args.h) { console.log("Start Zotero Args:"); diff --git a/scripts/stop.js b/scripts/stop.js deleted file mode 100644 index fa0a2b4..0000000 --- a/scripts/stop.js +++ /dev/null @@ -1,10 +0,0 @@ -const { execSync } = require("child_process"); -const { killZoteroWindows, killZoteroUnix } = require("./zotero-cmd.json"); - -try { - if (process.platform === "win32") { - execSync(killZoteroWindows); - } else { - execSync(killZoteroUnix); - } -} catch (e) {} diff --git a/scripts/stop.mjs b/scripts/stop.mjs new file mode 100644 index 0000000..197ef04 --- /dev/null +++ b/scripts/stop.mjs @@ -0,0 +1,14 @@ +import process from "process"; +import { execSync } from "child_process"; +import cmd from "./zotero-cmd.json" assert { type: "json" }; +const { killZoteroWindows, killZoteroUnix } = cmd; + +try { + if (process.platform === "win32") { + execSync(killZoteroWindows); + } else { + execSync(killZoteroUnix); + } +} catch (e) { + console.error(e); +}