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
159 lines (128 loc) · 4.18 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
#!/usr/bin/env node
const spawn = require("child_process").spawn;
const path = require("path");
const PROJECT_DIR = path.resolve(__dirname, "../../../");
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) {
let 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 = [
() => runTns(options.command, options.platform),
];
if (options.bundle) {
commands = [
() => prepare(options.platform),
() => webpack(options.platform),
...commands
];
}
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}...`);
spawnChildProcess(true, "webpack", `--config=webpack.${platform}.js`, "--progress")
.then(resolve)
.catch(throwError);
});
}
function prepare(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) {
console.log(`Adding platform ${platform}...`);
spawnChildProcess(false, "tns", "platform", "add", platform)
.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";
} else {
throwError({message: "You must provide either --start-app, or --build-app flag!"});
}
}
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);
}