-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
281 lines (230 loc) · 9.53 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*global require, module,process*/
import { packageDownload } from "./download.js";
import { PackageMap, PackageSpec } from "./package-map.js";
import { BuildProcess } from "./build.js";
import { basename, dirname, isAbsolute, normalize as normPath, join as j } from "path";
import fs from "fs";
import { inspect } from "util";
import semver from "./deps/semver.min.js"
// FIXME for resources
import node_fetch from "./deps/node-fetch.js";
if (!global.fetch) {
Object.assign(
global,
{ fetch: node_fetch.default },
["Response", "Headers", "Request"].reduce((all, name) =>
Object.assign(all, node_fetch[name]), {}));
}
const debug = false;
export {
installDependenciesOfPackage,
addDependencyToPackage,
installPackage,
buildPackage,
buildPackageMap,
ensurePackageMap,
setPackageDirsOfEnv,
packageDirsFromEnv
}
function ensurePathFormat(dirOrArray) {
// for flatn pure we expect directories to be specified in normal file system
// form like /home/foo/bar/, not as lively.resources or as URL file://...
// This ensures that...
if (Array.isArray(dirOrArray)) return dirOrArray.map(ensurePathFormat);
if (dirOrArray.isResource) return dirOrArray.path()
if (dirOrArray.startsWith("file://")) dirOrArray = dirOrArray.replace("file://", "");
return dirOrArray;
}
function buildPackageMap(packageCollectionDirs, individualPackageDirs, devPackageDirs) {
if (packageCollectionDirs) packageCollectionDirs = ensurePathFormat(packageCollectionDirs);
if (individualPackageDirs) individualPackageDirs = ensurePathFormat(individualPackageDirs);
if (devPackageDirs) devPackageDirs = ensurePathFormat(devPackageDirs);
return PackageMap.build(packageCollectionDirs, individualPackageDirs, devPackageDirs);
}
function ensurePackageMap(packageCollectionDirs, individualPackageDirs, devPackageDirs) {
if (packageCollectionDirs) packageCollectionDirs = ensurePathFormat(packageCollectionDirs);
if (individualPackageDirs) individualPackageDirs = ensurePathFormat(individualPackageDirs);
if (devPackageDirs) devPackageDirs = ensurePathFormat(devPackageDirs);
return PackageMap.ensure(packageCollectionDirs, individualPackageDirs, devPackageDirs);
}
function packageDirsFromEnv() {
let env = process.env;
return {
packageCollectionDirs: (env.FLATN_PACKAGE_COLLECTION_DIRS || "").split(":").filter(Boolean),
individualPackageDirs: (env.FLATN_PACKAGE_DIRS || "").split(":").filter(Boolean),
devPackageDirs: (env.FLATN_DEV_PACKAGE_DIRS || "").split(":").filter(Boolean)
}
}
function setPackageDirsOfEnv(packageCollectionDirs, individualPackageDirs, devPackageDirs) {
packageCollectionDirs = ensurePathFormat(packageCollectionDirs);
individualPackageDirs = ensurePathFormat(individualPackageDirs);
devPackageDirs = ensurePathFormat(devPackageDirs);
process.env.FLATN_PACKAGE_COLLECTION_DIRS = packageCollectionDirs.join(":");
process.env.FLATN_PACKAGE_DIRS = individualPackageDirs.join(":");
process.env.FLATN_DEV_PACKAGE_DIRS = devPackageDirs.join(":");
}
async function buildPackage(
packageSpecOrDir,
packageMapOrDirs,
dependencyFields = ["dependencies"],
verbose = false,
forceBuild = false
) {
if (typeof packageSpecOrDir === "string" || packageSpecOrDir.isResource)
packageSpecOrDir = ensurePathFormat(packageSpecOrDir);
if (Array.isArray(packageMapOrDirs))
packageMapOrDirs = ensurePathFormat(packageMapOrDirs);
let packageSpec = typeof packageSpecOrDir === "string"
? PackageSpec.fromDir(packageSpecOrDir)
: packageSpecOrDir,
packageMap = Array.isArray(packageMapOrDirs)
? buildPackageMap(packageMapOrDirs)
: packageMapOrDirs,
{ name, version } = packageSpec;
return await BuildProcess.for(packageSpec, packageMap, dependencyFields, forceBuild).run();
}
async function installPackage(
pNameAndVersion,
destinationDir,
packageMap,
dependencyFields,
isDev = false,
verbose = false
) {
// will lookup or install a package matching pNameAndVersion. Will
// recursively install dependencies
if (!packageMap) console.warn(`[flatn] install of ${pNameAndVersion}: No package map specified, using empty package map.`);
if (!packageMap) packageMap = PackageMap.empty();
if (!dependencyFields) dependencyFields = ["dependencies"];
destinationDir = ensurePathFormat(destinationDir);
if (!fs.existsSync(destinationDir))
fs.mkdirSync(destinationDir);
let atIndex = pNameAndVersion.lastIndexOf("@");
if (atIndex === -1) atIndex = pNameAndVersion.length;
let name = pNameAndVersion.slice(0, atIndex),
version = pNameAndVersion.slice(atIndex + 1),
queue = [[name, version]],
seen = {},
newPackages = [],
installedNew = 0;
while (queue.length) {
let [name, version] = queue.shift(),
installed = packageMap.lookup(name, version);
if (!installed) {
(verbose || debug) && console.log(`[flatn] installing package ${name}@${version}`);
installed = await packageDownload(name, version, destinationDir, verbose);
if (!installed)
throw new Error(`Could not download package ${name + "@" + version}`);
packageMap.addPackage(installed, isDev);
newPackages.push(installed);
} else {
(verbose || debug) && console.log(`[flatn] ${name}@${version} already installed in ${installed.location}`);
}
if (!installed) throw new Error(`cannot install package ${name}@${version}!`);
let deps = Object.assign({},
dependencyFields.reduce((map, key) =>
Object.assign(map, installed[key]), {}));
for (let name in deps) {
let nameAndVersion = `${name}@${deps[name]}`;
if (nameAndVersion in seen) continue;
queue.push([name, deps[name]]);
seen[nameAndVersion] = true;
}
}
if (newPackages.length > 0)
console.log(`[flatn] installed ${newPackages.length} new packages into ${destinationDir}`);
return { packageMap, newPackages };
}
function addDependencyToPackage(
packageSpecOrDir,
depNameAndRange,
packageDepDir,
packageMap,
dependencyField,
save = true,
verbose = false
) {
if (typeof packageSpecOrDir === "string" || packageSpecOrDir.isResource)
packageSpecOrDir = ensurePathFormat(packageSpecOrDir);
packageDepDir = ensurePathFormat(packageDepDir);
if (!dependencyField) dependencyField = "dependencies"; /*vs devDependencies etc.*/
let packageSpec = typeof packageSpecOrDir === "string"
? PackageSpec.fromDir(packageSpecOrDir)
: packageSpecOrDir;
let { location } = packageSpec;
if (!packageSpec[dependencyField]) packageSpec[dependencyField] = {};
let atIndex = depNameAndRange.lastIndexOf("@");
if (atIndex === -1) atIndex = depNameAndRange.length;
let depName = depNameAndRange.slice(0, atIndex),
depVersionRange = depNameAndRange.slice(atIndex + 1),
depVersion = packageSpec[dependencyField][depName];
return installPackage(
depNameAndRange,
packageDepDir,
packageMap,
[dependencyField]/*dependencyFields*/,
false/*isDev*/,
verbose
).then(result => {
if (!save) return result;
let dep = result.packageMap.lookup(depName, depVersionRange);
if (!depVersionRange) depVersionRange = dep.version;
let isRange = semver.validRange(depVersionRange, true);
let isRealRange = !semver.parse(depVersionRange, true);
if (!isRange) depVersionRange = "*";
else if (!isRealRange) depVersionRange = "^" + depVersionRange;
if (dep) {
if (!depVersion || !semver.parse(depVersion, true) || !semver.satisfies(depVersion, depVersionRange, true)) {
packageSpec[dependencyField][depName] = depVersionRange;
let config = fs.existsSync(j(location, "package.json")) ?
JSON.parse(String(fs.readFileSync(j(location, "package.json")))) :
{ name: depName, version: dep.version };
if (!config[dependencyField]) config[dependencyField] = {}
config[dependencyField][depName] = depVersionRange;
fs.writeFileSync(j(location, "package.json"), JSON.stringify(config, null, 2));
}
}
return result;
});
}
async function installDependenciesOfPackage(
packageSpecOrDir,
dirToInstallDependenciesInto,
packageMap,
dependencyFields,
verbose
) {
// Given a package spec of an installed package (retrieved via
// `PackageSpec.fromDir`), make sure all dependencies (specified in properties
// `dependencyFields` of package.json) are installed
if (typeof packageSpecOrDir === "string" || packageSpecOrDir.isResource)
packageSpecOrDir = ensurePathFormat(packageSpecOrDir);
if (dirToInstallDependenciesInto)
dirToInstallDependenciesInto = ensurePathFormat(dirToInstallDependenciesInto);
let packageSpec = typeof packageSpecOrDir === "string"
? PackageSpec.fromDir(packageSpecOrDir)
: packageSpecOrDir;
if (!packageSpec)
throw new Error(`Cannot resolve package: ${inspect(packageSpec, { depth: 0 })}`);
if (!dirToInstallDependenciesInto) dirToInstallDependenciesInto = dirname(packageSpec.location);
if (!dependencyFields) dependencyFields = ["dependencies"];
let deps = Object.assign({},
dependencyFields.reduce((map, key) => Object.assign(map, packageSpec[key]), {})),
depNameAndVersions = [],
newPackages = [];
for (let name in deps) {
let newPackagesSoFar = newPackages;
({ packageMap, newPackages } = await installPackage(
`${name}@${deps[name]}`,
dirToInstallDependenciesInto,
packageMap,
["dependencies"],
false,
verbose
));
newPackages = newPackages.concat(newPackagesSoFar);
}
if ((verbose || debug) && !newPackages.length)
console.log(`[flatn] no new packages need to be installed for ${packageSpec.name}`);
return { packageMap, newPackages };
}