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
178 lines (144 loc) · 4.87 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
#!/usr/bin/env node
const { spawn } = require("child_process");
const { resolve: pathResolve } = require("path");
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);
let platformVersion;
try {
platformVersion = packageJson.nativescript[`tns-${options.platform}`].version;
} catch(e) {}
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 = [];
if (options.bundle) {
commands.push(() => clearPlatform(options.platform));
commands.push(() => webpack(options.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, options.platform));
} else {
commands.shift(() => runTns("prepare", options.platform))
}
return commands.reduce((current, next) => current.then(next), Promise.resolve());
}
function webpack(platform) {
return new Promise(function (resolve, reject) {
console.log(`Running webpack for ${platform}...`);
const args = [
true, // show output on console
`webpack`,
`--config=webpack.config.js`,
`--progress`,
`--env.${platform}`,
process.env.npm_config_uglify && `--env.uglify`,
];
spawnChildProcess(...args)
.then(resolve)
.catch(throwError);
});
}
function clearPlatform(platform) {
return removePlatform(platform)
.then(() => addPlatform(platform));
}
function removePlatform(platform) {
return new Promise(function (resolve, reject) {
console.log(`Removing platform ${platform}...`);
spawnChildProcess(false, "tns", "platform", "remove", platform)
.then(resolve)
.catch(resolve);
});
}
function addPlatform(platform) {
return new Promise(function (resolve, reject) {
const platformToAdd = platformVersion ? `${platform}@${platformVersion}` : platform;
console.log(`Adding platform ${platformToAdd}...`);
spawnChildProcess(false, "tns", "platform", "add", platformToAdd)
.then(resolve)
.catch(resolve);
});
}
function runTns(command, platform) {
return new Promise((resolve, reject) => {
console.log(`Running tns ${command}...`);
spawnChildProcess(true, "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(shouldPrintOutput, command, ...args) {
const stdio = shouldPrintOutput ? "inherit" : "ignore";
return new Promise((resolve, reject) => {
const childProcess = spawn(command, args, { stdio, 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);
}