-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
79 lines (63 loc) · 1.89 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
// @flow
'use strict';
const bolt = require('bolt');
const path = require('path');
const fs = require('fs');
const globby = require('globby');
const promisify = require('typeable-promisify');
const parseJson = require('parse-json');
/*::
type Globs = {
[key: string]: string,
};
type Options = {
cwd?: string,
projectFiles?: Globs,
workspaceFiles?: Globs,
};
*/
function readFile(fileName /*: string */) {
return promisify(cb => fs.readFile(fileName, cb));
}
async function getFiles(cwd /*: string */, globs /*: Globs */) {
let result = {};
for (let name of Object.keys(globs)) {
let fileNames = await globby(globs[name], { cwd });
let promises = [];
for (let fileName of fileNames) {
let filePath = path.join(cwd, fileName);
let promise = readFile(filePath).then(buffer => {
let fileContents = buffer.toString();
return { filePath, fileContents };
});
promises.push(promise);
}
result[name] = await Promise.all(promises);
}
return result;
}
async function getPackage(dir /*: string */, globs /*: Globs */) {
let pkgPath = path.join(dir, 'package.json');
let pkgContents = await readFile(pkgPath);
let pkg = parseJson(pkgContents);
let files = await getFiles(dir, globs);
return { dir, pkgPath, pkg, files };
}
async function query(options /*: Options */ = {}) {
let opts = Object.assign({
cwd: process.cwd(),
projectFiles: {},
workspaceFiles: {},
}, options);
let dir = (await bolt.getProject({ cwd: opts.cwd })).dir;
let pkg = await getPackage(dir, opts.projectFiles);
let pkgs = await bolt.getWorkspaces({ cwd: dir });
let pkgDirs = pkgs.map(pkg => pkg.dir);
let promises = [];
for (let pkgDir of pkgDirs) {
promises.push(await getPackage(pkgDir, opts.workspaceFiles));
}
let workspaces = await Promise.all(promises);
return Object.assign({}, pkg, { workspaces });
}
module.exports = query;