fix: build async bug

fix: eslint esm bug
This commit is contained in:
windingwind 2023-05-07 01:43:04 +08:00
parent 7f733416ee
commit c48e085b8d
5 changed files with 88 additions and 79 deletions

View File

@ -12,14 +12,14 @@
}, },
"main": "src/index.ts", "main": "src/index.ts",
"scripts": { "scripts": {
"build-dev": "cross-env NODE_ENV=development 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.js", "build-prod": "cross-env NODE_ENV=production node scripts/build.mjs",
"build": "concurrently -c auto npm:build-prod npm:tsc", "build": "concurrently -c auto npm:build-prod npm:tsc",
"tsc": "tsc --noEmit", "tsc": "tsc --noEmit",
"start-z6": "node scripts/start.js --z 6", "start-z6": "node scripts/start.mjs --z 6",
"start-z7": "node scripts/start.js --z 7", "start-z7": "node scripts/start.mjs --z 7",
"start": "node scripts/start.js", "start": "node scripts/start.mjs",
"stop": "node scripts/stop.js", "stop": "node scripts/stop.mjs",
"restart-dev": "npm run build-dev && npm run stop && npm run start", "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-prod": "npm run build-prod && npm run stop && npm run start",
"restart": "npm run restart-dev", "restart": "npm run restart-dev",

View File

@ -1,46 +1,50 @@
const esbuild = require("esbuild"); import { build } from "esbuild";
const compressing = require("compressing"); import { zip } from "compressing";
const path = require("path"); import { join, basename } from "path";
const fs = require("fs"); import {
const process = require("process"); existsSync,
const replace = require("replace-in-file"); lstatSync,
const { writeFileSync,
name, readFileSync,
author, mkdirSync,
description, readdirSync,
homepage, rmSync,
version, } from "fs";
config, import { env, exit } from "process";
} = require("../package.json"); 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) { function copyFileSync(source, target) {
var targetFile = target; var targetFile = target;
// If target is a directory, a new file with the same name will be created // If target is a directory, a new file with the same name will be created
if (fs.existsSync(target)) { if (existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) { if (lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source)); targetFile = join(target, basename(source));
} }
} }
fs.writeFileSync(targetFile, fs.readFileSync(source)); writeFileSync(targetFile, readFileSync(source));
} }
function copyFolderRecursiveSync(source, target) { function copyFolderRecursiveSync(source, target) {
var files = []; var files = [];
// Check if folder needs to be created or integrated // Check if folder needs to be created or integrated
var targetFolder = path.join(target, path.basename(source)); var targetFolder = join(target, basename(source));
if (!fs.existsSync(targetFolder)) { if (!existsSync(targetFolder)) {
fs.mkdirSync(targetFolder); mkdirSync(targetFolder);
} }
// Copy // Copy
if (fs.lstatSync(source).isDirectory()) { if (lstatSync(source).isDirectory()) {
files = fs.readdirSync(source); files = readdirSync(source);
files.forEach(function (file) { files.forEach(function (file) {
var curSource = path.join(source, file); var curSource = join(source, file);
if (fs.lstatSync(curSource).isDirectory()) { if (lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder); copyFolderRecursiveSync(curSource, targetFolder);
} else { } else {
copyFileSync(curSource, targetFolder); copyFileSync(curSource, targetFolder);
@ -50,11 +54,11 @@ function copyFolderRecursiveSync(source, target) {
} }
function clearFolder(target) { function clearFolder(target) {
if (fs.existsSync(target)) { if (existsSync(target)) {
fs.rmSync(target, { recursive: true, force: true }); rmSync(target, { recursive: true, force: true });
} }
fs.mkdirSync(target, { recursive: true }); mkdirSync(target, { recursive: true });
} }
function dateFormat(fmt, date) { function dateFormat(fmt, date) {
@ -86,7 +90,7 @@ async function main() {
console.log( console.log(
`[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[ `[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.json", "update.json");
copyFileSync("update-template.rdf", "update.rdf"); copyFileSync("update-template.rdf", "update.rdf");
await esbuild await build({
.build({
entryPoints: ["src/index.ts"], entryPoints: ["src/index.ts"],
define: { define: {
__env__: `"${process.env.NODE_ENV}"`, __env__: `"${env.NODE_ENV}"`,
}, },
bundle: true, bundle: true,
outfile: path.join(buildDir, "addon/chrome/content/scripts/index.js"), outfile: join(buildDir, "addon/chrome/content/scripts/index.js"),
// Don't turn minify on // Don't turn minify on
// minify: true, // minify: true,
}) }).catch(() => exit(1));
.catch(() => process.exit(1));
console.log("[Build] Run esbuild OK"); console.log("[Build] Run esbuild OK");
@ -129,15 +131,15 @@ async function main() {
const optionsAddon = { const optionsAddon = {
files: [ files: [
path.join(buildDir, "**/*.rdf"), join(buildDir, "**/*.rdf"),
path.join(buildDir, "**/*.dtd"), join(buildDir, "**/*.dtd"),
path.join(buildDir, "**/*.xul"), join(buildDir, "**/*.xul"),
path.join(buildDir, "**/*.xhtml"), join(buildDir, "**/*.xhtml"),
path.join(buildDir, "**/*.json"), join(buildDir, "**/*.json"),
path.join(buildDir, "addon/prefs.js"), join(buildDir, "addon/prefs.js"),
path.join(buildDir, "addon/chrome.manifest"), join(buildDir, "addon/chrome.manifest"),
path.join(buildDir, "addon/manifest.json"), join(buildDir, "addon/manifest.json"),
path.join(buildDir, "addon/bootstrap.js"), join(buildDir, "addon/bootstrap.js"),
"update.json", "update.json",
"update.rdf", "update.rdf",
], ],
@ -146,21 +148,21 @@ async function main() {
countMatches: true, countMatches: true,
}; };
_ = replace.sync(optionsAddon); const replaceResult = sync(optionsAddon);
console.log( console.log(
"[Build] Run replace in ", "[Build] Run replace in ",
_.filter((f) => f.hasChanged).map( replaceResult
(f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}` .filter((f) => f.hasChanged)
) .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`)
); );
console.log("[Build] Replace OK"); console.log("[Build] Replace OK");
console.log("[Build] Addon prepare OK"); console.log("[Build] Addon prepare OK");
compressing.zip.compressDir( await zip.compressDir(
path.join(buildDir, "addon"), join(buildDir, "addon"),
path.join(buildDir, `${name}.xpi`), join(buildDir, `${name}.xpi`),
{ {
ignoreBase: true, ignoreBase: true,
} }
@ -174,5 +176,5 @@ async function main() {
main().catch((err) => { main().catch((err) => {
console.log(err); console.log(err);
process.exit(1); exit(1);
}); });

View File

@ -1,9 +1,12 @@
const { execSync } = require("child_process"); import process from "process";
const { exit } = require("process"); import { execSync } from "child_process";
const { exec } = require("./zotero-cmd.json"); 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 // 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) { if (args.help || args.h) {
console.log("Start Zotero Args:"); console.log("Start Zotero Args:");

View File

@ -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) {}

14
scripts/stop.mjs Normal file
View File

@ -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);
}