-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
build.js
198 lines (166 loc) · 5.48 KB
/
build.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict';
module.exports = build;
const { promises: fs } = require('graceful-fs');
const figgy = require('figgy-pudding');
const toml = require('@iarna/toml');
const home = require('user-home');
const semver = require('semver');
const path = require('path');
const fetchPackageVersion = require('../fetch-package-version');
const parsePackageSpec = require('../canonicalize-spec');
const loadPackageToml = require('../load-package-toml');
const fetchPackage = require('../fetch-package');
const fetchObject = require('../fetch-object');
const buildOpts = figgy({
registry: { default: 'https://registry.entropic.dev' },
argv: true,
expires: true,
cache: { default: path.join(home, '.ds', 'cache') },
log: { default: require('npmlog') }
});
// generate or read a lockfile.
async function build(opts) {
opts = buildOpts(opts);
await loadTree(opts, process.cwd());
// ds add [email protected]/foo
// ds add [email protected]/bar --dev
// ds add [email protected]/bar -D
// ds rm [email protected]/bar -D
// ds build / ds make
// ds clean / ds troy
}
async function loadTree(opts, where) {
const lock = path.join(where, 'Package.lock');
const loadingFiles = [];
const tier = await loadLock(where)
.catch(() => null)
.then(xs => xs || buildFromMeta(opts, where, loadingFiles));
await Promise.all(loadingFiles);
const dirc = {};
await unfurlTree(opts, ['ds'], { tier, files: {} }, dirc);
await printTree(tier);
}
const MODULES = 'node_modules';
async function unfurlTree(opts, dir, tree, dirc) {
dir.push(MODULES);
dir.push(undefined);
for (const dep in tree.tier.installed) {
dir[dir.length - 1] = dep;
await unfurlTree(opts, dir, tree.tier.installed[dep], dirc);
}
dir.pop();
dir.pop();
const fetching = [];
for (const file in tree.files) {
const [, , ...rest] = file.split('/');
const filename = rest.pop();
const fullpath = [...dir, ...rest];
await mkdirs(fullpath, dirc);
fullpath.push(filename);
fetchObject(opts, tree.files[file], true)
.then(({ data }) => {
return fs.writeFile(path.join(...fullpath), data);
})
.catch(err => {
return fs.writeFile(path.join(...fullpath), '');
});
}
}
async function mkdirs(dir, dirc) {
for (var i = 0; i < dir.length; ++i) {
const check = path.join(...dir.slice(0, i + 1));
if (dirc[check]) {
continue;
}
dirc[check] = true;
await fs.mkdir(check, { recursive: true });
}
}
function printTree(tree, level = 0) {
let saw = 0;
for (const dep in tree.installed) {
++saw;
console.log(
`${' '.repeat(level)} - ${dep} @ ${tree.installed[dep].range} -> ${
tree.installed[dep].version
}`
);
printTree(tree.installed[dep].tier, level + 1);
}
}
async function loadLock() {
throw new Error('Not implemented.');
}
async function buildFromMeta(opts, meta, loadingFiles, now = Date.now()) {
const { location, content } = await loadPackageToml(meta);
const defaultHost = opts.registry.replace(/^https?:\/\//, '');
const toplevel = { installed: {}, parent: null, name: 'root' };
// todo list of "canonical dep name", "range", tree tier
content.dependencies = content.dependencies || {};
const todo = Object.entries(content.dependencies).map(xs => [
parsePackageSpec(xs[0], defaultHost),
xs[1],
toplevel
]);
// assume you have a set of installed deps at versions that starts empty, pointing at a parent list of installed deps at versions or null.
// for each set of initial deps at the current level
// if any versions present satisfy the current dep, halt
// otherwise:
// walk from current level up to the highest level that does not have dep.
// add that dep to that level.
// add that target to the install list
next: while (todo.length) {
const [dep, range, tier] = todo.pop();
// q: what to do about legacy from different hostnames?
const named = dep.namespace === 'legacy' ? dep.name : dep.canonical;
let current = tier;
let lastWithout = current;
while (current) {
if (!current.installed[named]) {
lastWithout = current;
current = current.parent;
continue;
}
// needs to be added to lastWithout
if (!semver.satisfies(current.installed[named].version, range)) {
break;
}
// dep is satisfied. go to the next todo list item.
continue next;
}
// fetch the dep, resolve the maxSatisfying version, add it to lastWithout.dependencies[dep]
// add the dep's deps to the todo list, with a new tier
const pkg = await fetchPackage(opts, dep.canonical, now);
const version = semver.maxSatisfying(Object.keys(pkg.versions), range);
if (version === null) {
throw new Error(`Could not satisfy ${dep.canonical} at ${range}`);
}
const integrity = pkg.versions[version];
const data = await fetchPackageVersion(
opts,
dep.canonical,
version,
integrity
);
for (const file in data.files) {
const fetcher = fetchObject(opts, data.files[file]).catch(() => {});
loadingFiles.push(fetcher);
}
const newTier = {
installed: {},
parent: lastWithout,
name: `tree of ${dep}`
};
lastWithout.installed[named] = {
version,
range,
integrity,
files: data.files,
tier: newTier
};
for (const [child, range] of Object.entries(data.dependencies).reverse()) {
todo.push([parsePackageSpec(child, defaultHost), range, newTier]);
}
}
return toplevel;
}