-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.js
More file actions
201 lines (181 loc) · 5.52 KB
/
build.js
File metadata and controls
201 lines (181 loc) · 5.52 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* @fileoverview
* This file is supposed to be in the project root, as it uses `__dirname` to
* determine all paths.
*/
const fse = require('fs-extra');
const path = require('path');
const { execSync, spawn } = require('child_process');
const PACKAGES = [
{ name: 'communication', copyDeps: false },
{ name: 'server-wrapper', copyDeps: false },
{ name: 'database', copyDeps: true },
{ name: 'wallet', copyDeps: true },
{ name: 'protocols', copyDeps: true }
];
/**
* @param dir - Directory to empty and create again.
* @description Helper function to empty the specified directory.
*/
function resetDirectory(dir) {
try {
console.log(`[info] Deleting ${dir}...`);
fse.rmSync(path.join(__dirname, dir), { recursive: true, force: true });
fse.mkdirSync(path.join(__dirname, dir));
} catch (err) {
console.error(`[error] Trouble resetting directory "${dir}".`, err);
process.exit(1);
}
}
/**
* @param source - First arg of `cp -r` (source file/directory to copy)
* @param destination - Second arg of `cp -r` (target location of copied files)
* @description Helper function to implement "cp -r" based on current OS.
*/
function copyRecursive(source, destination) {
fse.copySync(source, destination, { overwrite: true, recursive: true });
}
/**
* @param key {string} Name of env variable to set
* @param value {string} Environment variable value (without quotes)
* @description Return a platform-specific command to set an environment variable.
*/
function getEnvCommand(key, value = '') {
const envCommand =
process.platform === 'win32'
? `set ${key}="${value}"`
: `export ${key}="${value}"`;
return envCommand;
}
/**
* @param packageName - Directory name of package to remove (optional).
* @description Remove specified package build, or all builds if nothing specified.
*/
function removePreviousBuild(packageName) {
if (PACKAGES.find(pkg => pkg.name === packageName)) {
resetDirectory(path.join(`@cypherock`, packageName));
return;
}
PACKAGES.forEach(pkg => {
resetDirectory(path.join('@cypherock', pkg.name));
});
}
/**
* @param arg {string} - Either a package name, or 'all'.
* @description Build specified package, or 'all' packages.
*/
function build(arg) {
if (arg === 'all') {
removePreviousBuild();
PACKAGES.forEach(pkg => {
buildPackage(pkg.name, pkg.copyDeps);
});
return;
}
if (PACKAGES.find(pkg => pkg.name === arg)) {
removePreviousBuild(arg);
buildPackage(arg);
} else {
console.error('Invalid args to build command, aborting.');
process.exit(1);
}
}
/** Build the specified package */
function buildPackage(pkgName, copyDeps = false) {
const packageDir = path.join(__dirname, 'packages', pkgName);
console.log(`Building package "${pkgName}"...`);
if (copyDeps) {
fse.rmSync(path.join(packageDir, 'node_modules', '@cypherock'), {
recursive: true,
force: true
});
copyRecursive(
'@cypherock',
path.join(packageDir, 'node_modules', '@cypherock')
);
}
const commands = [];
commands.push(getEnvCommand('NPM_AUTH', ''));
commands.push('yarn build');
const unifiedCommand = commands.join(' && ').replace(/,/g, ' ');
execSync(unifiedCommand, {
cwd: packageDir,
encoding: 'utf-8',
stdio: 'inherit'
});
copyRecursive(
path.join(packageDir, 'dist'),
path.join('@cypherock', pkgName, 'dist')
);
fse.copyFileSync(
path.join(packageDir, 'package.json'),
path.join('@cypherock', pkgName, 'package.json')
);
console.log(`Done building package "${pkgName}"!`);
}
/**
* @description Runs "yarn" inside cysync directory.
*/
function installDependencies() {
const commands = [];
commands.push(getEnvCommand('NPM_AUTH', ''));
commands.push('cd cysync'.split(' '));
commands.push('yarn --network-timeout 100000');
commands.push('cd ..'.split(' '));
const unifiedCommand = commands.join(' && ').replace(/,/g, ' ');
const cmdSequence = unifiedCommand.split(' ');
spawn(cmdSequence.shift(), cmdSequence, {
shell: true,
stdio: 'inherit'
});
}
/**
* @description Run "yarn" inside all packages.
*/
function installPackageDependencies() {
const commands = [];
commands.push(getEnvCommand('NPM_AUTH', ''));
PACKAGES.forEach(pkg => {
commands.push(`cd packages/${pkg.name}`.split(' '));
commands.push('yarn --network-timeout 100000');
commands.push('cd ../..'.split(' '));
});
const unifiedCommand = commands.join(' && ').replace(/,/g, ' ');
const cmdSequence = unifiedCommand.split(' ');
spawn(cmdSequence.shift(), cmdSequence, {
shell: true,
stdio: 'inherit'
});
}
/** Rewrite all package builds to `cysync/app` */
function copyToDesktop() {
const appDir = path.join(__dirname, 'cysync');
console.log('Copying to desktop...');
fse.rmSync(path.join(appDir, 'node_modules', '@cypherock'), {
recursive: true,
force: true
});
copyRecursive('@cypherock', path.join(appDir, 'node_modules', '@cypherock'));
console.log('Done copying to desktop!');
}
/**
* Calling process.exit() kills all ongoing async tasks.
* To avoid using process.exit() to exit script, everything is wrapped inside
* main() so that a `return` can exit the program.
*/
function main() {
const args = process.argv.slice(2);
if (!fse.existsSync('@cypherock')) fse.mkdirSync('@cypherock');
args.forEach(arg => {
build(arg);
});
if (args.length > 0) copyToDesktop();
else {
console.error('[FAIL] No arguments specified, aborting.');
}
}
if (require.main === module) main();
module.exports = {
PACKAGES,
getEnvCommand
};