Skip to content

Commit

Permalink
generateLicenses.jsを書き直す (#2000)
Browse files Browse the repository at this point in the history
* Change: generateLicenses.jsを書き直し

* Fix: execFileSync周りの型を修正

* Code: argvを離す

* Fix: 細かいところを修正
  • Loading branch information
sevenc-nanashi authored Apr 18, 2024
1 parent 0be6de9 commit c0fa953
Show file tree
Hide file tree
Showing 3 changed files with 439 additions and 521 deletions.
127 changes: 65 additions & 62 deletions build/generateLicenses.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// @ts-check
/* eslint-disable @typescript-eslint/no-var-requires */

const process = require("process");
const { execFileSync } = require("child_process");
const fs = require("fs");
const fs = require("fs/promises");
const path = require("path");
const yargs = require("yargs/yargs");
const yargs = require("yargs");
const { hideBin } = require("yargs/helpers");

const argv = yargs(hideBin(process.argv))
.option("output_path", {
alias: "o",
Expand All @@ -15,54 +17,43 @@ const argv = yargs(hideBin(process.argv))
.help()
.parse();

const tmp = require("tmp");

const isWindows = process.platform === "win32";

const customFormat = {
name: "",
version: "",
description: "",
licenses: "",
copyright: "",
licenseFile: "none",
licenseText: "none",
licenseModified: "no",
};

const customFormatFile = tmp.fileSync();
fs.writeFileSync(customFormatFile.name, JSON.stringify(customFormat));

const disallowedLicenses = ["GPL", "GPL-2.0", "GPL-3.0", "AGPL", "NGPL"];

// On Windows, npm's global packages can be called with the extension `.cmd` or `.ps1`.
// On Linux (bash), they can be called without extensions.
const extension = isWindows ? ".cmd" : "";
const licenseChecker = require("license-checker-rseidelsohn");

// https://github.com/davglass/license-checker
// npm install -g license-checker
const licenseJson = execFileSync(
`license-checker${extension}`,
[
"--production",
"--excludePrivatePackages",
"--json",
`--customPath=${customFormatFile.name}`,
`--failOn=${disallowedLicenses.join(";")}`,
],
{
encoding: "utf-8",
maxBuffer: 1024 * 1024 * 10, // FIXME: stdoutではなくファイル出力にする
},
);
(async () => {
const disallowedLicenses = ["GPL", "GPL-2.0", "GPL-3.0", "AGPL", "NGPL"];

const checkerLicenses = JSON.parse(licenseJson);
/** @type {licenseChecker.ModuleInfos} */
const licenseJson = await new Promise((resolve, reject) => {
licenseChecker.init(
{
start: process.cwd(),
production: true,
failOn: disallowedLicenses.join(";"),
excludePrivatePackages: true,
customFormat: {
name: "",
version: "",
description: "",
licenses: "",
copyright: "",
licenseFile: "none",
licenseText: "none",
licenseModified: "no",
},
},
(err, json) => {
if (err) {
reject(err);
} else {
resolve(json);
}
},
);
});

const externalLicenses = [];
const externalLicenses = [];

externalLicenses.push({
name: "7-Zip",
version: execFileSync(
const sevenZipVersionMatch = execFileSync(
path.join(
__dirname,
"vendored",
Expand All @@ -77,21 +68,33 @@ externalLicenses.push({
{
encoding: "utf-8",
},
).match(/7-Zip\s+(?:\(.\))?\s*([0-9.]+)/)[1],
license: "LGPL-2.1",
text: fs.readFileSync(path.join(__dirname, "vendored", "7z", "License.txt"), {
encoding: "utf-8",
}),
});
).match(/7-Zip\s+(?:\(.\))?\s*([0-9.]+)/);

if (!sevenZipVersionMatch) {
throw new Error("Failed to find 7-Zip version");
}

externalLicenses.push({
name: "7-Zip",
version: sevenZipVersionMatch[1],
license: "LGPL-2.1",
text: await fs.readFile(
path.join(__dirname, "vendored", "7z", "License.txt"),
{
encoding: "utf-8",
},
),
});

const licenses = Object.entries(checkerLicenses)
.map(([, license]) => ({
name: license.name,
version: license.version,
license: license.licenses,
text: license.licenseText,
}))
.concat(externalLicenses);
const licenses = Object.entries(licenseJson)
.map(([, license]) => ({
name: license.name,
version: license.version,
license: license.licenses,
text: license.licenseText,
}))
.concat(externalLicenses);

const outputPath = argv.output_path;
fs.writeFileSync(outputPath, JSON.stringify(licenses));
const outputPath = argv.output_path;
await fs.writeFile(outputPath, JSON.stringify(licenses));
})();
Loading

0 comments on commit c0fa953

Please sign in to comment.