forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-local-deps.js
174 lines (152 loc) · 5 KB
/
install-local-deps.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Installs repo-local dependencies manually, as lerna ignores those...
*/
const { exec, execSync } = require('child_process');
const { lstatSync, mkdirpSync, readJsonSync, removeSync, symlinkSync, writeJsonSync } = require('fs-extra');
const { dirname, join, relative, resolve } = require('path');
const LOCAL_PACKAGE_REGEX = /^file:(.+)$/;
exec('lerna ls --json --all', { shell: true }, (error, stdout) => {
if (error) {
console.error('Error: ', error);
process.exit(-1);
}
const modules = JSON.parse(stdout.toString('utf8'));
for (const module of modules.reverse()) {
const packageInfo = require(join(module.location, 'package.json'));
if (process.env.VERBOSE) {
console.log(`Installing local dependencies of ${module.name}`);
}
installDeps(module.location,
{ depList: packageInfo.dependencies },
{ depList: packageInfo.devDependencies, dev: true });
}
console.log('Done.');
});
function installDeps(location, ...depLists) {
const nodeModules = join(location, 'node_modules');
const lockFile = join(location, 'package-lock.json');
const locks = pathExistsSync(lockFile) && require(lockFile);
const linked = new Set();
const paths = [];
for (const { depList, dev } of depLists) {
for (const [name, version] of Object.entries(depList || {})) {
if (linked.has(name)) { continue; }
const matched = version.match(LOCAL_PACKAGE_REGEX);
if (!matched) { continue; }
const path = matched[1];
const modulePath = resolve(location, path);
const { requires, dependencies } = installDependency(nodeModules, modulePath);
linked.add(name);
paths.push(path);
if (locks) {
fixupDependencies(dependencies, dev);
locks.dependencies[name] = {
version,
dev,
requires: removeLocalPackages(requires),
dependencies,
};
}
}
}
if (process.env.VERBOSE) {
console.log(`Fixing up package-lock.json in ${location}`);
}
if (locks) {
sortKeys(locks.dependencies);
writeJsonSync(lockFile, locks, { spaces: '\t' });
} else {
// This is dog slow, hence we're playing funky games elsewhere... but we're not in the business of bootstrapping
// a lock-file from scratch, so if there was none, let npm do it's thing instead of trying to replicate.
execSync(`npm install --package-lock-only ${paths.join(' ')}`, { cwd: location, shell: true });
}
}
/**
* Installs a symlink to a local package, and symlinks any "bin" items into the node_modules/.bin directory
* @param {string} nodeModules the root of the "installing" node_modules directory
* @param {string} localPath the path of the "installed" module
*/
function installDependency(nodeModules, localPath) {
const packageInfo = readJsonSync(join(localPath, 'package.json'));
const linkLocation = join(nodeModules, packageInfo.name);
mkdirpSync(dirname(linkLocation));
if (pathExistsSync(linkLocation)) {
if (process.env.VERBOSE) {
console.log(`Re-linking ${linkLocation} to ${localPath}`);
}
removeSync(linkLocation);
}
symlinkSync(relative(dirname(linkLocation), localPath), linkLocation);
if (packageInfo.bin) {
const bin = join(nodeModules, '.bin');
mkdirpSync(bin);
for (const [name, cmd] of Object.entries(packageInfo.bin)) {
const binLink = join(bin, name);
const linkTarget = join('..', packageInfo.name, cmd);
if (pathExistsSync(binLink)) {
if (process.env.VERBOSE) {
console.log(`Re-linking ${binLink} to ${linkTarget}`);
}
removeSync(binLink);
}
symlinkSync(linkTarget, binLink);
}
}
const lockFilePath = join(localPath, 'package-lock.json');
const lockFile = pathExistsSync(lockFilePath)
? readJsonSync(lockFilePath)
: undefined;
return {
requires: packageInfo.dependencies,
dependencies: lockFile && lockFile.dependencies,
};
}
function sortKeys(object) {
if (!object) {
return object;
}
const sorted = {};
for (const key of Object.keys(object).sort()) {
sorted[key] = object[key];
}
return sorted;
}
function removeLocalPackages(dependencies) {
if (!dependencies) {
return dependencies;
}
for (const [name, version] of Object.entries(dependencies)) {
const matched = version.match(LOCAL_PACKAGE_REGEX);
if (!matched) { continue; }
delete dependencies[name];
}
return dependencies;
}
function pathExistsSync(path) {
try {
lstatSync(path);
// lstat would throw if the file does not exist, and return if it does.
return true;
} catch (e) {
return false;
}
}
function fixupDependencies(deps, dev) {
if (!deps) {
return deps;
}
for (const [name, dep] of Object.entries(deps)) {
if (!dev && dep.dev) {
delete deps[name];
continue;
}
if (dev) { dep.dev = true; }
const matched = dep.version.match(LOCAL_PACKAGE_REGEX);
if (matched) {
delete deps[name];
continue;
}
dep.requires = removeLocalPackages(dep.requires);
fixupDependencies(dep.dependencies, dev);
}
}