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
108 lines (87 loc) · 3.04 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
#!/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 flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2));
const options = getOptions(flags);
execute(options);
function execute(options) {
let commands = [
() => runTns(options.command, options.platform),
];
if (options.bundle) {
commands.unshift(() => webpack(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}...`);
spawnChildProcess("tns", "clean-app", platform)
.then(() => spawnChildProcess("webpack", `--config=webpack.${platform}.js`, "--progress"))
.then(resolve)
.catch(throwError);
});
}
function runTns(command, platform) {
console.log(`Running tns ${command}...`);
return new Promise((resolve, reject) => {
spawnChildProcess("tns", command, platform, "--bundle", "--disable-npm-install")
.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(command, ...args) {
return new Promise((resolve, reject) => {
const childProcess = spawn(command, args, { stdio: "inherit", pwd: PROJECT_DIR });
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);
}