-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui-build.js
256 lines (218 loc) · 8.72 KB
/
ui-build.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* NOTE: This is a Studio Managed File. DO NOT EDIT THIS FILE. Your changes may be reverted by Studio.*/
/**
* Check the node version to be make sure user installed project supported node.
* React Native project (expo format) is generated by this file.
*
* CONSOLE ARGUMENTS:-
* runtimeUIVersion:String: Runtime version (Eg: 10.6.6-next.10243) and wavemaker-app-runtime-wm-build
* appSrc:String: Source folder to generate the react native app (current directory Eg: '.')
* appTarget: Target folder to save the generated app (Eg: 'target/ui-build/generated-app')
*/
const { execSync } = require("child_process");
const fs = require('fs');
const os = require('os');
const UI_BUILD_ERROR_LOG = 'UI BUILD ERROR';
const MSG_CODEGEN_LOG = 'CODEGEN REACT NATIVE APP: ';
const MSG_RN_CODEGEN_SUCCESS = 'REACT_NATIVE_CODEGEN_SUCCESS';
const NPM_PACKAGE_SCOPE = '@wavemaker';
/**
* This function is executed successfully if the system node version is in a specified range. If not, the process will be killed
*
*/
const checkNodeVersion = () => {
if (!isSystemHasValidNodeVersion()) {
console.log("\x1b[31m", "-------******* Project configuration doesn't meet, Please install and use Node Version 12.X.X *******-------");
console.log("\x1b[31m", "-------******* Your current Node Version is: " + process.versions.node + " *******-------");
process.exit(1);
} else {
console.log("\x1b[47m\x1b[32m%s\x1b[0m", "-------******* Good to Go with your Node Version: " + process.versions.node + " *******-------");
}
}
/**
* Return 1 if systemInstalledVersion > requiredVersion
* Return -1 if systemInstalledVersion < requiredVersion
* Return 0 if systemInstalledVersion == requiredVersion
*/
const compareNodeVersion = (requiredVersion) => {
let systemInstalledVersion = process.versions.node;
if (systemInstalledVersion === requiredVersion) {
return 0;
}
let systemInstalledVersion_components = systemInstalledVersion.split(".");
let requiredVersion_components = requiredVersion.split(".");
let len = Math.min(systemInstalledVersion_components.length, requiredVersion_components.length);
// loop while the components are equal
for (let i = 0; i < len; i++) {
// systemInstalledVersion bigger than requiredVersion
if (parseInt(systemInstalledVersion_components[i]) > parseInt(requiredVersion_components[i])) {
return 1;
}
// requiredVersion bigger than systemInstalledVersion
if (parseInt(systemInstalledVersion_components[i]) < parseInt(requiredVersion_components[i])) {
return -1;
}
}
// If one's a prefix of the other, the longer one is greater.
if (systemInstalledVersion_components.length > requiredVersion_components.length) {
return 1;
}
if (systemInstalledVersion_components.length < requiredVersion_components.length) {
return -1;
}
// Otherwise they are the same.
return 0;
}
/**
* To restrict the node version in the given range
* @returns boolean true/false
*/
const isSystemHasValidNodeVersion = () => {
const nodeMinVersion = '12.0.0';
const nodeMaxVersion = '14.15.9999';
if (compareNodeVersion(nodeMinVersion) >= 0 && compareNodeVersion(nodeMaxVersion) < 0) {
return true;
} else {
return false;
}
}
/**
* To check the system node version is valid or not for the project
*/
checkNodeVersion();
/**
* Read the console arguments and prepare the object.
* @returns console arguments as key value pairs
*/
const getArgs = () => {
const args = {};
process.argv
.slice(2, process.argv.length)
.forEach(arg => {
if (arg.slice(0, 2) === '--') {
const longArg = arg.split('=');
const longArgFlag = longArg[0].slice(2, longArg[0].length);
const longArgValue = longArg.length > 2 ? longArg.slice(1, longArg.length).join('=') : longArg[1];
args[longArgFlag] = longArgValue;
}
});
return args;
}
const args = getArgs();
// TO capture the ctrl+C signal
process.on('SIGINT', function (e) {
console.log("Caught interrupt signal", e);
process.exit(1);
});
/**
* To check the npm package installation successs or not
* @param {*} path File path where installation success message was written
* @param {*} msg Success messsage to confirm that package was installed
* @returns boolean true/false
*/
const isNPMPackageExist = (path, msg) => {
if (fs.existsSync(path)) {
const successMsg = fs.readFileSync(path, { encoding: 'utf8', flag: 'r' });
if (successMsg == msg) {
return true;
}
} else {
return false;
}
}
/**
* To run the system command via node child process.
* @param {*} cmd Command in string format to execute in node environment
* @param {*} errorCallback callback if anything needs to be handled on command failure
*/
const executeSyncCmd = (cmd, errorCallback, msg) => {
try {
console.log(msg + 'Current running cmd: ' + cmd)
execSync(cmd, { stdio: 'inherit' });
} catch (err) {
if (errorCallback) {
errorCallback(err);
}
console.log(msg + 'FAILED command: ' + cmd, err);
process.exit(err.code || err.pid);
}
}
/**
* Check node modules package were installed or not
* Create dir for packages with the version name
* Run npm install
* Write success file to be make sure it was installed successfully.
*/
const downloadNPMPackage = (packageInfo) => {
const HOME_DIR = os.homedir();
const PATH_NPM_PACKAGE = (packageInfo.baseDir || HOME_DIR + '/.wm/node_modules/' ) + packageInfo.name + '/' + packageInfo.version;
const PATH_NPM_PACKAGE_SUCCESS = PATH_NPM_PACKAGE + '/.SUCCESS';
// To check global app runtime node modules.
if (!isNPMPackageExist(PATH_NPM_PACKAGE_SUCCESS, packageInfo.successMsg)) {
fs.mkdirSync(PATH_NPM_PACKAGE, { recursive: true });
let npmInstallCMD = 'npm install ';
if (packageInfo.packageJsonFile && fs.existsSync(packageInfo.packageJsonFile)) {
fs.copyFileSync(packageInfo.packageJsonFile, PATH_NPM_PACKAGE + '/package.json');
} else {
npmInstallCMD = 'npm init -y && ' + npmInstallCMD + packageInfo.scope + '/' + packageInfo.name + '@' + packageInfo.version;
}
executeSyncCmd('cd ' + PATH_NPM_PACKAGE + ' && ' + npmInstallCMD, () => {
console.log(packageInfo.infoMsg + ' Something wrong with npm installation');
}, packageInfo.infoMsg);
fs.writeFileSync(PATH_NPM_PACKAGE_SUCCESS, packageInfo.successMsg);
} else {
console.log(packageInfo.infoMsg + ' Node packages already installed!');
}
return PATH_NPM_PACKAGE;
}
/**
* Download react native codegen package and install if it is doesn't exist
* @returns Return the codegen package path
*/
const downloadCodegenAndGetTheInstallationPath = (basedir) => {
let codegenPackageInfo = {
scope: NPM_PACKAGE_SCOPE,
version: args.runtimeUIVersion,
name: 'rn-codegen',
packageJsonFile: '',
successMsg: MSG_RN_CODEGEN_SUCCESS,
infoMsg: MSG_CODEGEN_LOG
};
codegenPackageInfo.baseDir = basedir;
const PATH_RN_CODEGEN = downloadNPMPackage(codegenPackageInfo);
return PATH_RN_CODEGEN + '/node_modules/' + codegenPackageInfo.scope + '/' + codegenPackageInfo.name + '/';
}
/**
* To check the platform is windows or not
* @returns boolean
*/
const isWindows = () => {
return process.platform === "win32";
}
const init = () => {
const sourceDir = (args.appSrc || '.');
let appTarget = (args.appTarget || `${sourceDir}/generated-rn-app`);
/**
* By default optimizeUIBuild will be true.
* If environment is windows then optimizeUIBuild flag will be false which install all node modules.
*/
let optimizeUIBuild;
if(args.optimizeUIBuild) {
optimizeUIBuild = args.optimizeUIBuild === 'true';
} else {
optimizeUIBuild = !isWindows();
}
/**
* If optimization enabled download it in .wm folder at homedir
* If optimization not enabled download it in appTarget folder
*/
let baseDir = optimizeUIBuild ? undefined : appTarget.split('/').slice(0,2).join('/') + '/';
const codegenPath = downloadCodegenAndGetTheInstallationPath(baseDir);
executeSyncCmd(['node ' + codegenPath + 'index.js',
'transpile',
(args.nodeVMArgs || ''),
'--profile="' + (args.targetPlatform || 'default') + '"',
'--page="'+ (args.page || '') +'"',
sourceDir + '/src/main/webapp',
appTarget].join(' '));
}
init();