Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generateLicenses.jsを書き直す #2000

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Comment on lines -5 to +6
Copy link
Member

@Hiroshiba Hiroshiba Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

別に良いけど実は不要な変更かも。
nodejsはpromiseの方が推奨されてるとかあったりするなら変更したほうが良さそう。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メインがasyncになったのでせっかくなので、って感じです。どちらでも良さそう。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほどです、まあ良さそう。

仮に戻すってなった時のこと考えると、どちらでも良いならたぶん変えない方が良さそう。
けどまあpromiseは取り回しやすいし(await忘れが怖いけど)、関数内だからどちらにせよ差分は発生するし、まあ良いかな。

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",
sevenc-nanashi marked this conversation as resolved.
Show resolved Hide resolved
"--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
Loading