-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathpostinstall.es6
169 lines (152 loc) · 5.02 KB
/
postinstall.es6
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import fs from 'fs-plus'
import path from 'path'
import childProcess from 'child_process'
const TARGET_ALL = 'all'
const TARGET_CLOUD = 'cloud'
const TARGET_CLIENT = 'client'
async function spawn(cmd, args, opts = {}) {
return new Promise((resolve, reject) => {
const options = Object.assign({stdio: 'inherit'}, opts);
const proc = childProcess.spawn(cmd, args, options)
proc.on("error", reject)
proc.on("exit", resolve)
})
}
function unlinkIfExistsSync(p) {
try {
if (fs.lstatSync(p)) {
fs.removeSync(p);
}
} catch (err) {
return
}
}
function installClientSyncPackage() {
console.log("\n---> Linking client-sync")
// link client-sync
const clientSyncDir = path.resolve(path.join('packages', 'client-sync'));
const destination = path.resolve(path.join('packages', 'client-app', 'internal_packages', 'client-sync'));
unlinkIfExistsSync(destination);
fs.symlinkSync(clientSyncDir, destination, 'dir');
}
async function lernaBootstrap(installTarget) {
console.log("\n---> Installing packages");
const lernaCmd = process.platform === 'win32' ? 'lerna.cmd' : 'lerna';
const args = ["bootstrap"]
switch (installTarget) {
case TARGET_CLIENT:
args.push(`--ignore='cloud-*'`)
break
case TARGET_CLOUD:
args.push(`--ignore='client-*'`)
break
default:
break
}
await spawn(path.join('node_modules', '.bin', lernaCmd), args)
}
const npmEnvs = {
system: process.env,
apm: Object.assign({}, process.env, {
NPM_CONFIG_TARGET: '0.10.40',
}),
electron: Object.assign({}, process.env, {
NPM_CONFIG_TARGET: '1.4.15',
NPM_CONFIG_ARCH: process.arch,
NPM_CONFIG_TARGET_ARCH: process.arch,
NPM_CONFIG_DISTURL: 'https://atom.io/download/electron',
NPM_CONFIG_RUNTIME: 'electron',
NPM_CONFIG_BUILD_FROM_SOURCE: true,
}),
};
async function npm(cmd, options) {
const {cwd, env} = Object.assign({cwd: '.', env: 'system'}, options);
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
await spawn(npmCmd, [cmd], {
cwd: path.resolve(__dirname, '..', cwd),
env: npmEnvs[env],
})
}
async function electronRebuild() {
if (!fs.existsSync(path.join("packages", "client-app", "apm"))) {
console.log("\n---> No client app to rebuild. Moving on")
return;
}
await npm('install', {
cwd: path.join('packages', 'client-app', 'apm'),
env: 'apm',
})
await npm('rebuild', {
cwd: path.join('packages', 'client-app'),
env: 'electron',
})
}
const getJasmineDir = (packageName) => path.resolve(
path.join('packages', packageName, 'spec', 'jasmine')
)
const getJasmineConfigPath = (packageName) => path.resolve(
path.join(getJasmineDir(packageName), 'config.json')
)
function linkJasmineConfigs() {
console.log("\n---> Linking Jasmine configs");
const linkToPackages = ['cloud-api', 'cloud-core', 'cloud-workers']
const from = getJasmineConfigPath('isomorphic-core')
for (const packageName of linkToPackages) {
const packageDir = path.join('packages', packageName)
if (!fs.existsSync(packageDir)) {
console.log("\n---> No cloud packages to link. Moving on")
return
}
const jasmineDir = getJasmineDir(packageName)
if (!fs.existsSync(jasmineDir)) {
fs.mkdirSync(jasmineDir)
}
const to = getJasmineConfigPath(packageName)
unlinkIfExistsSync(to)
fs.symlinkSync(from, to, 'file')
}
}
function linkIsomorphicCoreSpecs() {
console.log("\n---> Linking isomorphic-core specs to client-app specs")
const from = path.resolve(path.join('packages', 'isomorphic-core', 'spec'))
const to = path.resolve(path.join('packages', 'client-app', 'spec', 'isomorphic-core'))
unlinkIfExistsSync(to)
fs.symlinkSync(from, to, 'dir')
}
function getInstallTarget() {
const {INSTALL_TARGET} = process.env
if (!INSTALL_TARGET) {
return TARGET_ALL
}
if (![TARGET_ALL, TARGET_CLIENT, TARGET_CLOUD].includes(INSTALL_TARGET)) {
throw new Error(`postinstall: INSTALL_TARGET must be one of client, cloud, or all. It was set to ${INSTALL_TARGET}`)
}
return INSTALL_TARGET
}
async function main() {
try {
const installTarget = getInstallTarget()
console.log(`\n---> Installing for target ${installTarget}`);
if ([TARGET_ALL, TARGET_CLIENT].includes(installTarget)) {
installClientSyncPackage()
}
await lernaBootstrap(installTarget);
if ([TARGET_ALL, TARGET_CLIENT].includes(installTarget)) {
if (process.platform === "darwin") {
// Given that `lerna bootstrap` does not install optional dependencies, we
// need to manually run `npm install` inside `client-app` so
// `node-mac-notifier` get's correctly installed and included in the build
// See https://github.com/lerna/lerna/issues/121
console.log("\n---> Reinstalling client-app dependencies to include optional dependencies");
await npm('install', {cwd: 'packages/client-app'})
}
await electronRebuild();
linkJasmineConfigs();
linkIsomorphicCoreSpecs();
}
} catch (err) {
console.error(err);
process.exit(1);
}
}
main()