-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgetlicense.js
More file actions
76 lines (67 loc) · 2.83 KB
/
getlicense.js
File metadata and controls
76 lines (67 loc) · 2.83 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
/*
* This file is part of PROS Application Browser, a flash enabled
* browser restricted to preconfigured servers.
* Copyright (C) 2018 PROS, Inc.
*
* PROS Application Browser is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PROS Application Browser 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PROS Application Browser. If not, see http://www.gnu.org/licenses/
* You can contact PROS, Inc. with any questions at http://www.pros.com
*/
/**
* The script parses npm-shrinkwrap.json to identify non dev dependencies
* and copies all corresponding license files from the npm registry
* to the project config/copyright folder.
*/
/* eslint no-console: 0 */
/* eslint arrow-body-style: 0 */
const fs = require('fs');
const fetch = require('node-fetch');
const path = require('path');
const artifactoryUrl = process.env.LICENSE_URL;
const licenseFolderPath = path.join(__dirname, 'copyright');
const npmShrinkwrapPath = path.join(__dirname, 'npm-shrinkwrap.json');
if (!fs.existsSync(licenseFolderPath)) {
fs.mkdirSync(licenseFolderPath);
}
console.log(`license folder is ${licenseFolderPath}`);
const shrinkwrapData = JSON.parse(fs.readFileSync(npmShrinkwrapPath, 'utf8'));
const dependencyPathsArray = Object.getOwnPropertyNames(shrinkwrapData.dependencies);
dependencyPathsArray.forEach(dependencyPath => {
const dependencyData = shrinkwrapData.dependencies[dependencyPath];
if (dependencyData.dev === undefined || dependencyData.dev === false) {
const dependencyVersion = dependencyData.version;
const dependencyPathParts = dependencyPath.split('/');
const dependencyName = dependencyPathParts[dependencyPathParts.length - 1];
const licenseFileName = `${dependencyName}-${dependencyVersion}-license.txt`;
const dependencyLicenseUrl = `${artifactoryUrl}/${dependencyPath}/-/${licenseFileName}`;
console.log(dependencyLicenseUrl);
fetch(dependencyLicenseUrl)
.then(res => {
if (res.statusCode > 400 && res.statusCode < 499) {
console.log(`License Missing -- ${licenseFileName}`);
throw err;
} else {
return res.text();
}
})
.then(body => {
fs.writeFile(path.join(licenseFolderPath, licenseFileName), body, 'utf8', err => {
if (err) throw err;
console.log(`File saved - ${licenseFileName}`);
});
})
.catch(err => {
console.error(err);
});
}
});