/
BuilderDiscoverer.ts
57 lines (48 loc) · 1.81 KB
/
BuilderDiscoverer.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
55
56
57
import * as fs from 'fs';
import { glob } from 'glob';
import * as _ from 'lodash';
import * as path from 'path';
import { Builders } from './Builder';
import { ConfigPlugin } from './ConfigPlugin';
import ConfigReader from './ConfigReader';
import Zen from './Zen';
export default class BuilderDiscoverer {
private configReader: ConfigReader;
private cwd: string;
private argv: any;
constructor(zen: Zen, plugins: ConfigPlugin[], argv: any) {
this.configReader = new ConfigReader(zen, plugins);
this.cwd = zen.cwd;
this.argv = argv;
}
public discover(builderOverrides: any): Builders {
const packageRootPaths = this._detectRootPaths();
return packageRootPaths.reduce((res: any, pathName: string) => {
return { ...res, ...this._discoverRecursively(pathName, builderOverrides) };
}, {});
}
private _discoverRecursively(dir: string, builderOverrides: any): Builders {
if (path.basename(dir) === '.expo') {
return undefined;
}
let builders = this.configReader.readConfig({
...(this.argv.x ? { inferedConfig: {} } : {}),
filePath: this.argv.c ? path.join(dir, this.argv.c) : dir,
builderOverrides
});
const files = fs.readdirSync(dir);
for (const name of files) {
const dirPath = path.join(dir, name);
if (name !== 'node_modules' && fs.statSync(dirPath).isDirectory()) {
builders = { ...builders, ...this._discoverRecursively(dirPath, builderOverrides) };
}
}
return builders;
}
private _detectRootPaths(): string[] {
const rootConfig = JSON.parse(fs.readFileSync(`${this.cwd}/package.json`, 'utf8'));
return rootConfig.workspaces && rootConfig.workspaces.length
? _.flatten(rootConfig.workspaces.map((ws: string) => glob.sync(ws))).map((ws: string) => path.join(this.cwd, ws))
: [this.cwd];
}
}