-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcli.js
63 lines (59 loc) · 2.18 KB
/
cli.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
#!/usr/bin/env node
const childProcess = require('child_process');
const { ncp } = require('ncp');
const fs = require('fs');
const path = require('path');
const appExpobook = path.resolve(__dirname, '../../__expobook__');
const depExpobook = path.resolve(__dirname, './__expobook__');
// Copy the .expobook folder to the app folder
if (!fs.existsSync(appExpobook)) {
if (!fs.existsSync(depExpobook)) {
throw new Error('Expobook looks to be corrupt, please reinstall');
}
ncp(depExpobook, appExpobook, (err) => {
if (err) throw err;
// eslint-disable-next-line no-use-before-define
copyAppConfig();
// eslint-disable-next-line no-use-before-define
runExpo();
});
} else {
// eslint-disable-next-line no-use-before-define
copyAppConfig();
// eslint-disable-next-line no-use-before-define
runExpo();
}
// Copy the sdk version from the parent project if it exists
function copyAppConfig() {
const jsonPath = path.resolve(__dirname, '../../app.json');
const expobookJsonPath = path.resolve(appExpobook, './expobook-app.json');
const existingExpoConfig = fs.existsSync(jsonPath);
if (!existingExpoConfig) return;
// eslint-disable-next-line
const json = require(jsonPath);
if (json && json.expo) {
// eslint-disable-next-line
const expobookJson = require(expobookJsonPath);
expobookJson.sdkVersion = json.expo.sdkVersion;
fs.writeFileSync(expobookJsonPath, JSON.stringify(expobookJson, null, 2));
}
}
function runExpo() {
const childExp = path.resolve(__dirname, './node_modules/.bin/exp');
const depExp = path.resolve(__dirname, '../.bin/exp');
const isExpChild = fs.existsSync(childExp);
const isExpDep = fs.existsSync(depExp);
if (!isExpChild && !isExpDep) {
throw new Error("Couldn't find exp in dependencies - run `npm i` and try again");
}
const expPath = isExpDep ? depExp : childExp;
const cwd = path.resolve(appExpobook, '../');
process.chdir(path.resolve(__dirname, '../../'));
const pathToConfig = path.resolve(appExpobook, './expobook-app.json');
childProcess.execSync(
`cd ${cwd} && ${expPath} start --lan --ios --config ${pathToConfig}`,
{
stdio: [process.stdin, process.stdout, process.stderr],
},
);
}