/
bootstrap-local-dependencies.ts
54 lines (49 loc) · 1.75 KB
/
bootstrap-local-dependencies.ts
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
import { ListByPackage, LocalInstaller, progress } from 'install-local';
import fs from 'fs';
import glob = require('glob');
import path = require('path');
console.log('starting installation of local dependencies');
function globAsPromised(pattern: string, options: glob.IOptions) {
return new Promise<string[]>((res, rej) => {
glob(pattern, options, (err, matches) => {
if (err) {
rej(err);
} else {
res(matches);
}
});
});
}
function readFile(fileName: string) {
return new Promise<string>((res, rej) => fs.readFile(fileName, 'utf8', (err, content) => {
if (err) {
rej(err);
} else {
res(content);
}
}));
}
interface Package {
localDependencies?: { [name: string]: string };
}
/**
* Installs local dependencies in one go,
* reads package.json and test/* /package.json files and installs the local dependencies marked there
* @param directory the directory where the tests live
*/
export async function bootstrapLocalDependencies(directory: string) {
console.log('bootstrap ' + path.resolve(directory));
const files = await globAsPromised('{package.json,test/*/package.json}', { cwd: path.resolve(directory) });
const packages = await Promise.all(files.map(fileName => readFile(fileName)
.then(content => ({ dir: path.dirname(fileName), content: JSON.parse(content) as Package }))));
const sourcesByTarget: ListByPackage = {};
for (const pkg of packages) {
const localDeps = pkg.content.localDependencies;
if (localDeps) {
sourcesByTarget[path.resolve(pkg.dir)] = Object.keys(localDeps).map(key => path.resolve(pkg.dir, localDeps[key]));
}
}
const localInstaller = new LocalInstaller(sourcesByTarget);
progress(localInstaller);
await localInstaller.install();
}