1 | const { execFileSync } = require('child_process');
|
2 | const os = require('os');
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const findUp = require('find-up');
|
6 |
|
7 | exports.executeInScope = env => {
|
8 | let rootPath = findUp.sync('.yo-rc.json', {
|
9 | cwd: env.cwd
|
10 | });
|
11 |
|
12 | rootPath = rootPath ? path.dirname(rootPath) : env.cwd;
|
13 |
|
14 | if (!rootPath) {
|
15 | return false;
|
16 | }
|
17 |
|
18 | const configPath = path.join(rootPath, 'config');
|
19 |
|
20 | if (!fs.existsSync(configPath)) {
|
21 | return false;
|
22 | }
|
23 |
|
24 | if (!process.env.EG_CONFIG_DIR) {
|
25 | process.env.EG_CONFIG_DIR = configPath;
|
26 | }
|
27 |
|
28 | const binDirectory = path.join(rootPath, 'node_modules', '.bin');
|
29 | const egFile = os.platform() === 'win32' ? 'eg.cmd' : 'eg';
|
30 | const localBin = path.join(binDirectory, egFile);
|
31 |
|
32 |
|
33 | if (!process.env.EG_LOCAL_EXEC && fs.existsSync(localBin)) {
|
34 | const childEnv = process.env;
|
35 | childEnv.EG_LOCAL_EXEC = true;
|
36 |
|
37 | execFileSync(localBin, process.argv.slice(2), {
|
38 | cwd: env.cwd,
|
39 | env: childEnv,
|
40 | stdio: 'inherit'
|
41 | });
|
42 | return true;
|
43 | }
|
44 |
|
45 | return false;
|
46 | };
|