-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·166 lines (151 loc) · 4.25 KB
/
index.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
#!/usr/bin/env node
'use strict';
(() => { // BEGIN IIFE
// Need the IIFE so we can do the early return. It's not actually necessary,
// but standard barfs without it.
if (process.argv.length > 2 && !process.argv[2].startsWith('-')) {
const { spawn } = require('child_process');
spawn('npm', process.argv.slice(2), { stdio: 'inherit' })
.on('close', (code, signal) => {
process.exitCode = code || 0;
if (signal) {
console.error('npm exited due to signal', signal);
}
})
.on('error', e => {
throw e;
});
return;
}
const config = require('./lib/config.js');
const download = require('qdownload');
const { isdir, mkdirp, cpr } = require('qfastfs');
const fs = require('fs');
const util = require('util');
const path = require('path');
const isProd = config.production;
const useCache = !config.noCache;
const useDest = !config.cacheOnly;
if (fs.existsSync(`${process.cwd()}/node_modules`)) {
console.error('Please delete your node_modules directory before installing.');
process.exit(1);
}
let tree;
try {
tree = require(`${process.cwd()}/package-lock.json`);
} catch (e) {
if ('QDD_LOCKJS' in process.env) {
const content = fs.readFileSync(process.env.QDD_LOCKJS, 'utf8');
const afterJson = content.split(/^\/\*\*package-lock(?:\s|$)/m)[1];
if (afterJson) {
const [json, rest] = afterJson.split(/\*\*\/$/m);
if (rest) {
try {
tree = JSON.parse(json.replace(/^\s*\*/mg, ''));
} catch (err) {
throw new Error('badly formed in-line package-lock');
}
}
}
} else {
throw e;
}
}
const todos = [];
const binTodos = [];
function install (mod, dir, topLevel = false) {
const deps = mod.dependencies;
if (!deps) return;
const depEntries = Object.entries(deps);
for (const [name, entry] of depEntries) {
if (entry.bundled) {
continue;
}
if (isProd && entry.dev) {
continue;
}
const { integrity, resolved: url } = entry;
if (!integrity || !url) {
console.error('invalid entry found in package-lock.json');
console.error(util.inspect({ [name]: entry }));
console.error('fix this, delete node_modules, and try again');
process.exit(1);
}
const destDir = useDest ? `${dir}/node_modules/${name}` : null;
const cacheDir = useCache ? config.cacheDir + '/' + integrity : null;
todos.push(installOne.bind(null, name, integrity, url, destDir, cacheDir));
if (entry.dependencies) {
install(entry, destDir);
}
if (topLevel && destDir) {
binTodos.push(installBin.bind(null, name, destDir, `${dir}/node_modules/.bin`));
}
}
}
function installOne (name, integrity, url, destDir, cacheDir, cb) {
if (cacheDir) {
isdir(cacheDir, (err, isDir) => {
if (err || !isDir) {
return download(url, integrity, cacheDir, destDir, cb);
}
if (useDest) {
cpr(cacheDir, destDir, cb);
}
});
} else {
return download(url, integrity, cacheDir, destDir, cb);
}
}
function installBin (name, destDir, binDir, cb) {
mkdirp(binDir, err => {
if (err) {
cb(err);
return;
}
let { bin } = require(destDir + '/package.json');
if (typeof bin === 'string') {
bin = { [name]: bin };
}
for (const binName in bin) {
try {
fs.symlinkSync(path.join(destDir, bin[binName]), path.join(binDir, binName));
} catch (e) {
cb(e);
return;
}
}
cb();
});
}
function doTheTodos () {
let todo = todos.length;
for (const fn of todos) {
fn(err => {
todo--;
if (err) {
throw err;
}
if (todo === 0) {
for (const binFn of binTodos) {
binFn(err => {
if (err) {
throw err;
}
});
}
}
});
}
}
install(tree, process.cwd(), true);
if (useCache) {
mkdirp(config.cacheDir, (err) => {
if (err) {
throw err;
}
doTheTodos();
});
} else {
doTheTodos();
}
})(); // END IIFE