-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstall.mjs
104 lines (83 loc) · 3.42 KB
/
install.mjs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import fs from 'fs';
import path from 'path';
import { spawn } from 'child_process';
function findFiles(directories, filenamePattern) {
const results = [];
for (const directory of directories) {
const files = fs.readdirSync(directory);
for (const file of files) {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isFile() && file.match(filenamePattern)) {
results.push(filePath);
} else if (stat.isDirectory()) {
const subResults = findFiles([filePath], filenamePattern);
results.push(...subResults);
}
}
}
return results;
}
function mergePackageJson(inputPackageJson, toMergePackageJson) {
const outputPackageJson = { ...inputPackageJson };
const dependencies = ['dependencies', 'devDependencies', 'peerDependencies'];
dependencies.forEach(depType => {
if (outputPackageJson[depType] && toMergePackageJson[depType]) {
Object.keys(toMergePackageJson[depType]).forEach(dep => {
const version1 = outputPackageJson[depType][dep];
const version2 = toMergePackageJson[depType][dep];
const highestVersion = version1 && version2 ? (version1 > version2 ? version1 : version2) : (version1 || version2);
if (outputPackageJson[depType][dep]) {
if (highestVersion > outputPackageJson[depType][dep]) {
outputPackageJson[depType][dep] = highestVersion;
}
} else {
outputPackageJson[depType][dep] = highestVersion;
}
});
} else if (toMergePackageJson[depType]) {
outputPackageJson[depType] = toMergePackageJson[depType];
} else if (outputPackageJson[depType]) {
outputPackageJson[depType] = outputPackageJson[depType];
}
});
return outputPackageJson;
}
function processFiles(res) {
if (!res || res.length == 0) { console.log('Nothing to do'); }
else {
let mainPackagePath = "./package-base.json";
let tempPackagePath = "./package-combined.json";
if (fs.existsSync(tempPackagePath)) {
fs.unlinkSync(tempPackagePath);
}
for (var index = 0; index < res.length; index++) {
const mainPackageFile = fs.readFileSync(mainPackagePath);
const packageJsonMain = JSON.parse(mainPackageFile);
const mergePackageFile = fs.readFileSync(res[index]);
const packageJsonMerge = JSON.parse(mergePackageFile);
let combinedJson = mergePackageJson(packageJsonMain, packageJsonMerge);
let outputString = JSON.stringify(combinedJson, null, 4);
if (process.platform === 'win32') {
outputString = outputString.replace(`"workerDirectory": "build_artifacts/public"`,`"workerDirectory": "build_artifacts\\\\public"`);
}
fs.writeFileSync(tempPackagePath, outputString);
mainPackagePath = tempPackagePath;
}
fs.rename(tempPackagePath, "./package.json", function (rename_err) {
if (rename_err) {
console.log("Rename error: " + rename_err);
}
});
}
}
const directories = ['./src/components/'];
const files = findFiles(directories, 'package.json');
processFiles(files);
console.log("");
console.log("React Component Toolkit has been initialized");
console.log("--------------------------------------------------------------------------------------------------");
console.log("");
console.log("run npm install to complete the installation and get started!");