Skip to content

Commit

Permalink
Introduce adm-zip
Browse files Browse the repository at this point in the history
  • Loading branch information
wtetsu committed Nov 22, 2024
1 parent af893d9 commit 8bccd87
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 802 deletions.
783 changes: 26 additions & 757 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@vitest/coverage-v8": "^2.1.5",
"archiver": "^7.0.1",
"adm-zip": "^0.5.16",
"esbuild": "^0.24.0",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
Expand Down
35 changes: 9 additions & 26 deletions tools/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,27 @@

// Package a browser extension.

const fs = require("fs");
const archiver = require("archiver");
const path = require("path");
const fs = require("node:fs");
const path = require("node:path");
const AdmZip = require("adm-zip");

const main = (sourcePath, outZipPath) => {
if (!fs.existsSync(sourcePath)) {
console.error(`Not found: ${sourcePath}`);
process.exit(1);
}

const stream = fs.createWriteStream(outZipPath, { flags: "w" });
const zip = new AdmZip();
zip.addLocalFolder(sourcePath);
zip.writeZip(outZipPath);

const archive = startArchiver(sourcePath, stream);

stream.on("close", () => {
const size = archive.pointer() / 1_024.0 + " KB";
console.log(`${outZipPath}: ${size}`);
});

archive.finalize();
};

const startArchiver = (targetPath, stream) => {
const archive = archiver("zip", { zlib: { level: 9 } });
archive.on("warning", (err) => {
throw err;
});
archive.on("error", (err) => {
throw err;
});
archive.pipe(stream);
archive.directory(targetPath, false);
return archive;
const size = fs.statSync(outZipPath).size / 1_024.0 + " KB";
console.log(`${outZipPath}: ${size}`);
};

if (require.main === module) {
if (process.argv.length <= 2) {
console.error(`Usage: node archive.js postfix`);
console.error("Usage: node archive.js postfix");
process.exit(1);
}

Expand Down
6 changes: 3 additions & 3 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const main = async (browser, mode, watchMode) => {

const copyStaticFiles = (browser, mode) => {
const sourceDirs = ["base", "gen", `gen-${browser}`, "pdf"];
if (mode != "production") {
if (mode !== "production") {
sourceDirs.push("overwrite");
}
for (const sourceDir of sourceDirs) {
Expand Down Expand Up @@ -71,7 +71,7 @@ const watch = async (browser, mode) => {
name: "on-end",
setup(build) {
build.onEnd((result) => {
if (result.errors.length == 0) {
if (result.errors.length === 0) {
console.info(`[${getTime()}]✅ Generated: ${io.outfile}`);
}
});
Expand Down Expand Up @@ -125,7 +125,7 @@ const getTime = () => {

if (require.main === module) {
if (process.argv.length <= 3) {
console.error(`Usage: node build.js browser mode`);
console.error("Usage: node build.js browser mode");
process.exit(1);
}

Expand Down
6 changes: 3 additions & 3 deletions tools/bundle_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

// Bundle multiple JSON files into a single file.

const fs = require("fs");
const path = require("path");
const fs = require("node:fs");
const path = require("node:path");
const json5 = require("json5");
const jaRule = require("deinja/src/data");

Expand Down Expand Up @@ -50,5 +50,5 @@ main(
{ name: "verb", file: "data/rule/verb.json5" },
{ name: "ja", data: jaRule },
],
"static/gen/data/rule.json"
"static/gen/data/rule.json",
);
8 changes: 4 additions & 4 deletions tools/make_dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

// Make dictionary data and metadata.

const fs = require("fs");
const path = require("path");
const fs = require("node:fs");
const path = require("node:path");
const json5 = require("json5");
const glob = require("fast-glob");

Expand Down Expand Up @@ -35,7 +35,7 @@ const splitDataAndWrite = (data, split, to, outputDirPath) => {
for (let i = 1; i <= keys.length; i++) {
const key = keys[i];
outData[key] = data[key];
if (i >= nextThreshold || i == keys.length) {
if (i >= nextThreshold || i === keys.length) {
const outJson = JSON.stringify(outData);
const outFileName = `/${to}${outFiles.length}.json`;
const outPath = path.join(outputDirPath, outFileName);
Expand Down Expand Up @@ -68,5 +68,5 @@ main(
to: "data/dict",
split: 10,
},
"static/gen/"
"static/gen/",
);
8 changes: 4 additions & 4 deletions tools/make_license_html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// Convert license.json to a simple html file.

const fs = require("fs");
const fs = require("node:fs");

const main = (licenseJsonPath) => {
const licenseRecords = readJson(licenseJsonPath);
Expand Down Expand Up @@ -87,15 +87,15 @@ const makeHtmlContent = (licenseContent) => {
.version {
font-size: 0.8em;
color: #4a4a4a;
}
}
.license {
font-size: 0.8em;
color: #4a4a4a;
}
.publisher {
font-size: 0.5em;
color: #4a4a4a;
}
}
</style>
</head>
<body>
Expand All @@ -111,7 +111,7 @@ const readJson = (fileName) => {

if (require.main === module) {
if (process.argv.length <= 2) {
console.error(`Usage: node make_license_html.js license_file_path`);
console.error("Usage: node make_license_html.js license_file_path");
process.exit(1);
}

Expand Down
8 changes: 4 additions & 4 deletions tools/make_manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

// Make manifest.json.

const fs = require("fs");
const path = require("path");
const fs = require("node:fs");
const path = require("node:path");
const version = require("../package.json").version;

const main = (options, outputDirPath) => {
Expand Down Expand Up @@ -56,7 +56,7 @@ const getDebugConfigFileName = (fileName) => {

if (require.main === module) {
if (process.argv.length <= 3) {
console.error(`Usage: node make_manifest.js browser mode`);
console.error("Usage: node make_manifest.js browser mode");
process.exit(1);
}

Expand All @@ -76,6 +76,6 @@ if (require.main === module) {
activate_extension_command,
debug: mode !== "production",
},
`static/gen-${browser}/`
`static/gen-${browser}/`,
);
}

0 comments on commit 8bccd87

Please sign in to comment.