-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathgenerate-license-files.js
More file actions
100 lines (83 loc) · 3.55 KB
/
generate-license-files.js
File metadata and controls
100 lines (83 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
const path = require('node:path');
const { readFile, writeFile, stat, readdir } = require('node:fs/promises');
const prettier = require('prettier');
const { BUNDLED_DEPENDENCIES } = require('../shared/bundled-dependencies.js');
// Generate our LICENSE files for each package, including any bundled dependencies
// This is modeled after how Rollup does it:
// https://github.com/rollup/rollup/blob/0b665c3/build-plugins/generate-license-file.ts
// Async fs.existsSync
async function exists(filename) {
try {
await stat(filename);
return true;
} catch (_err) {
return false;
}
}
async function findLicenseText(depName) {
// Iterate through possible names for the license file
const names = ['LICENSE', 'LICENSE.md', 'LICENSE.txt'];
// We would use require.resolve, but 1) that doesn't work if the module lacks a "main" in its `package.json`,
// and 2) it gives us a deep `./path/to/index.js` which makes it harder to find a top-level LICENSE file. So
// just assume that our deps are hoisted to the top-level `node_modules`.
const resolvedDepPath = path.join(process.cwd(), 'node_modules', depName);
for (const name of names) {
const fullFilePath = path.join(resolvedDepPath, name);
if (await exists(fullFilePath)) {
return (await readFile(fullFilePath, 'utf-8')).trim();
}
}
// Get the license from the package.json if we can't find it elsewhere
const pkgJson = JSON.parse(await readFile(path.join(resolvedDepPath, 'package.json'), 'utf-8'));
const { license, version } = pkgJson;
return `${license} license defined in package.json in v${version}.`;
}
async function main() {
const coreLicense = await readFile('LICENSE-CORE.md', 'utf-8');
const bundledLicenses = await Promise.all(
BUNDLED_DEPENDENCIES.map(async (depName) => {
return `## ${depName}\n\n` + (await findLicenseText(depName));
})
);
const newLicense =
`# LWC core license\n\n${coreLicense}\n# Licenses of bundled dependencies\n\n${bundledLicenses.join(
'\n'
)}`.trim() + '\n';
const formattedLicense = await prettier.format(newLicense, {
parser: 'markdown',
});
// Check against current top-level license for changes
const shouldWarnChanges =
process.argv.includes('--test') &&
formattedLicense !== (await readFile('LICENSE.md', 'utf-8'));
// Top level license
await writeFile('LICENSE.md', formattedLicense, 'utf-8');
// License file for each package as well, so that we publish it to npm
const atLwcPackages = (await readdir('packages/@lwc'))
// skip dotfiles like .DS_Store
.filter((_) => !_.startsWith('.'))
.map((_) => `@lwc/${_}`);
const packages = ['lwc', ...atLwcPackages];
await Promise.all(
packages.map(async (pkg) => {
await writeFile(path.join('packages/', pkg, 'LICENSE.md'), formattedLicense, 'utf-8');
})
);
if (shouldWarnChanges) {
const relativeFilename = path.relative(process.cwd(), __filename);
throw new Error(
'Either the LWC core license or the license of a bundled dependency has been updated.\n' +
`Please run \`node ${relativeFilename}\` and commit the result.`
);
}
}
main().catch((err) => {
console.error(err);
process.exitCode ||= 1;
});