This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathns-bundle
218 lines (181 loc) · 5.8 KB
/
ns-bundle
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
#!/usr/bin/env node
const { spawn } = require("child_process");
const { resolve: pathResolve } = require("path");
const { existsSync } = require("fs");
const { getPackageJson } = require("../projectHelpers");
const PROJECT_DIR = pathResolve(__dirname, "../../../");
const packageJson = getPackageJson(PROJECT_DIR);
if (!process.env.npm_config_argv) {
throwError({message: "No flags provided."});
}
const npmArgs = JSON.parse(process.env.npm_config_argv).original;
const tnsArgs = getTnsArgs(npmArgs).map(escape);
const flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2));
const options = getOptions(flags);
function getTnsArgs(args) {
const other = [
"run",
"ns-bundle",
"--android",
"--ios",
"--build-app",
"--start-app",
"--uglify",
"--nobundle",
];
return args.filter(a => !other.includes(a));
}
function escape(arg) {
return `"${arg}"`;
}
execute(options);
function execute(options) {
let commands = [];
const platform = options.platform;
if (options.bundle) {
commands = [
() => cleanApp(platform),
() => cleanBuildArtifacts(platform),
() => webpack(platform),
];
}
// If "build-app" or "start-app" is specified,
// the respective command should be run last.
// Otherwise, the app should be just prepared.
if (options.command) {
commands.push(() => runTns(options.command, platform));
} else {
commands.shift(() => runTns("prepare", platform))
}
return commands.reduce((current, next) => current.then(next), Promise.resolve());
}
function cleanBuildArtifacts(platform) {
return new Promise((resolve, reject) => {
if (platform !== "android") {
return resolve();
}
getTnsVersion().then(versionString => {
const version = versionToNumber(versionString);
// for nativescript-cli v3.0.1 and below
// the android build artifacts should be cleaned manually
if (version <= 301) {
gradlewClean().then(resolve).catch(throwError);
} else {
return resolve();
}
}).catch(throwError);
});
}
function gradlewClean() {
return new Promise((resolve, reject) => {
const platformsPath = pathResolve(PROJECT_DIR, "platforms", "android")
const gradlew = pathResolve(platformsPath, "gradlew");
if (!existsSync(gradlew)) {
return resolve();
}
spawnChildProcess(gradlew, "-p", platformsPath, "clean")
.then(resolve)
.catch(throwError);
});
}
function getTnsVersion() {
return new Promise((resolve, reject) => {
const childProcess = spawn("tns", ["--version"], { shell: true });
childProcess.stdout.on("data", resolve);
childProcess.on("close", code => {
if (code) {
reject({
code,
message: `child process exited with code ${code}`,
});
}
});
});
}
function versionToNumber(version) {
const VERSION_MATCHER = /(\d+)\.(\d+)\.(\d+)/;
return Number(VERSION_MATCHER.exec(version).splice(1).join(""));
}
// Clear platform/**/app folder contents
function cleanApp(platform) {
return new Promise((resolve, reject) => {
spawnChildProcess("tns", "clean-app", platform)
.then(resolve)
.catch(throwError)
});
}
function webpack(platform) {
return new Promise(function (resolve, reject) {
console.log(`Running webpack for ${platform}...`);
const args = [
`webpack`,
`--config=webpack.config.js`,
`--progress`,
`--env.${platform}`,
process.env.npm_config_uglify && `--env.uglify`,
];
spawnChildProcess(...args)
.then(resolve)
.catch(throwError);
});
}
function runTns(command, platform) {
return new Promise((resolve, reject) => {
console.log(`Running tns ${command}...`);
spawnChildProcess("tns", command, platform, "--bundle", "--disable-npm-install", ...tnsArgs)
.then(resolve)
.catch(throwError);
});
}
function getOptions(flags) {
let options = {};
options.platform = getPlatform(flags);
options.command = getCommand(flags);
options.bundle = !flags.includes("nobundle");
return options;
}
function getPlatform(flags) {
if (flags.includes("android") && flags.includes("ios")) {
throwError({message: "You cannot use both --android and --ios flags!"});
}
if (flags.includes("android")) {
return "android";
} else if (flags.includes("ios")) {
return "ios";
} else {
throwError({message: "You must provide a target platform! Use either --android, or --ios flag."});
}
}
function getCommand(flags) {
if (flags.includes("start-app") && flags.includes("build-app")) {
throwError({message: "You cannot use both --start-app and --build-app flags!"});
}
if (flags.includes("start-app")) {
return "run";
} else if (flags.includes("build-app")) {
return "build";
}
}
function spawnChildProcess(command, ...args) {
return new Promise((resolve, reject) => {
const childProcess = spawn(command, args, {
stdio: "inherit",
pwd: PROJECT_DIR,
shell: true,
});
childProcess.on("close", code => {
if (code === 0) {
resolve();
} else {
reject({
code,
message: `child process exited with code ${code}`,
});
}
});
});
}
function throwError(error) {
console.error(error.message);
process.exit(error.code || 1);
}