-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathlicense-management.mjs
More file actions
127 lines (113 loc) · 4.12 KB
/
license-management.mjs
File metadata and controls
127 lines (113 loc) · 4.12 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2025 SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
import licenseChecker from 'license-checker';
import fs from 'node:fs/promises';
import path from 'node:path/posix';
// Configuration
const MAIN_LICENSE_FILE = path.resolve('LICENSE.txt');
const OUTPUT_DIR = path.resolve('./licenses/');
const THIRD_PARTY_LICENSES_DIR = path.resolve('./licenses/THIRD_PARTY_LICENSES');
await fs.rm(OUTPUT_DIR, { recursive: true, force: true });
await fs.mkdir(OUTPUT_DIR, { recursive: true });
await fs.mkdir(THIRD_PARTY_LICENSES_DIR, { recursive: true });
// Convert callback-based licenseChecker to Promise
const initLicenseChecker = options => {
return new Promise((resolve, reject) => {
licenseChecker.init(options, (err, packages) => {
if (err) {
reject(err);
} else {
resolve(packages);
}
});
});
};
// Copy main license file
async function copyMainLicense() {
console.log('Copying main license...');
try {
await fs.copyFile(MAIN_LICENSE_FILE, path.join(OUTPUT_DIR, 'LICENSE.txt'));
console.log(`✅ Successfully copied main license to ${OUTPUT_DIR}/LICENSE.txt`);
} catch (error) {
console.error(`❌ Failed to copy main license: ${error.message}`);
throw error;
}
}
// Download and process dependency licenses
async function downloadLicenses() {
console.log('Downloading dependency licenses...');
try {
const packages = await initLicenseChecker({
start: process.cwd(),
production: true,
excludePrivatePackages: true,
onlyAllow: null, // Allow all licenses
});
// Download and save license files
let successCount = 0;
let skippedCount = 0;
let failedCount = 0;
for (const [packageName, info] of Object.entries(packages)) {
// Download and save license file
try {
if (info.licenseFile) {
await fs.access(info.licenseFile);
const packageNameWithoutVersion = packageName.substring(0, packageName.lastIndexOf('@'));
const safePackageName = packageNameWithoutVersion.replace(/\//g, '-');
const licenseFileName = `${safePackageName}-LICENSE.txt`;
const licensePath = path.join(THIRD_PARTY_LICENSES_DIR, licenseFileName);
// Read the license file
const licenseContent = await fs.readFile(info.licenseFile, 'utf8');
// Write to destination
await fs.writeFile(licensePath, licenseContent);
console.log(`✅ Saved license for ${packageName}`);
successCount++;
} else {
console.warn(`⚠️ No license file found for ${packageName}`);
skippedCount++;
}
} catch (error) {
console.error(`❌ Failed to save license for ${packageName}: ${error.message}`);
failedCount++;
}
}
console.log(`\nLicense processing complete:`);
console.log(`- Successfully processed: ${successCount}`);
console.log(`- Skipped: ${skippedCount}`);
console.log(`- Failed: ${failedCount}`);
if (failedCount > 0) {
throw new Error('Some license files failed to process');
}
} catch (error) {
console.error('License check failed:', error);
throw error;
}
}
// Main function
async function main() {
console.log('=== Starting license management process ===');
try {
await downloadLicenses();
await copyMainLicense();
console.log('=== License management completed successfully ===');
} catch (error) {
console.error('License management failed:', error);
process.exit(1);
}
}
// Run the main function
await main();