Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit e4505d1

Browse files
committed
fix(ns-bundle): clean android build for NativeScript CLI 3.0.1<=
related to NativeScript/android#759
1 parent 30e9c97 commit e4505d1

File tree

1 file changed

+70
-29
lines changed

1 file changed

+70
-29
lines changed

Diff for: bin/ns-bundle

+70-29
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ function execute(options) {
4343
const platform = options.platform;
4444

4545
if (options.bundle) {
46-
commands.push(() => cleanApp(platform));
47-
commands.push(() => webpack(platform));
48-
}
49-
50-
if (platform === "android") {
51-
commands.push(() => gradlewClean());
46+
commands = [
47+
() => cleanApp(platform),
48+
() => webpack(platform),
49+
() => cleanBuildArtifacts(platform)
50+
];
5251
}
5352

5453
// If "build-app" or "start-app" is specified,
@@ -63,12 +62,21 @@ function execute(options) {
6362
return commands.reduce((current, next) => current.then(next), Promise.resolve());
6463
}
6564

66-
// Clear platform/**/app folder contents
67-
function cleanApp(platform) {
65+
function cleanBuildArtifacts(platform) {
6866
return new Promise((resolve, reject) => {
69-
spawnChildProcess(true, "tns", "clean-app", platform)
70-
.then(resolve)
71-
.catch(throwError)
67+
if (platform !== "android") {
68+
resolve();
69+
}
70+
71+
getTnsVersion().then(versionString => {
72+
const version = versionToNumber(versionString);
73+
74+
// for nativescript-cli v3.0.1 and below
75+
// the android build artifacts should be cleaned manually
76+
if (version <= 301) {
77+
return gradlewClean();
78+
}
79+
}).catch(throwError);
7280
});
7381
}
7482

@@ -80,18 +88,49 @@ function gradlewClean() {
8088
resolve();
8189
}
8290

83-
spawnChildProcess(true, gradlew, "-p", platformsPath, "clean")
91+
spawnChildProcess(gradlew, "-p", platformsPath, "clean")
8492
.then(resolve)
8593
.catch(throwError);
8694
});
8795
}
8896

97+
function getTnsVersion() {
98+
return new Promise((resolve, reject) => {
99+
const childProcess = spawn("tns", ["--version"], { shell: true });
100+
101+
childProcess.stdout.on("data", resolve);
102+
103+
childProcess.on("close", code => {
104+
if (code) {
105+
reject({
106+
code,
107+
message: `child process exited with code ${code}`,
108+
});
109+
}
110+
});
111+
});
112+
}
113+
114+
function versionToNumber(version) {
115+
const VERSION_MATCHER = /(\d+)\.(\d+)\.(\d+)/;
116+
117+
return Number(VERSION_MATCHER.exec(version).splice(1).join(""));
118+
}
119+
120+
// Clear platform/**/app folder contents
121+
function cleanApp(platform) {
122+
return new Promise((resolve, reject) => {
123+
spawnChildProcess("tns", "clean-app", platform)
124+
.then(resolve)
125+
.catch(throwError)
126+
});
127+
}
128+
89129
function webpack(platform) {
90130
return new Promise(function (resolve, reject) {
91131
console.log(`Running webpack for ${platform}...`);
92132

93133
const args = [
94-
true, // show output on console
95134
`webpack`,
96135
`--config=webpack.config.js`,
97136
`--progress`,
@@ -109,7 +148,7 @@ function runTns(command, platform) {
109148
return new Promise((resolve, reject) => {
110149
console.log(`Running tns ${command}...`);
111150

112-
spawnChildProcess(true, "tns", command, platform, "--bundle", "--disable-npm-install", ...tnsArgs)
151+
spawnChildProcess("tns", command, platform, "--bundle", "--disable-npm-install", ...tnsArgs)
113152
.then(resolve)
114153
.catch(throwError);
115154
});
@@ -150,22 +189,24 @@ function getCommand(flags) {
150189
}
151190
}
152191

153-
function spawnChildProcess(shouldPrintOutput, command, ...args) {
154-
const stdio = shouldPrintOutput ? "inherit" : "ignore";
155-
192+
function spawnChildProcess(command, ...args) {
156193
return new Promise((resolve, reject) => {
157-
const childProcess = spawn(command, args, { stdio, pwd: PROJECT_DIR, shell: true });
158-
159-
childProcess.on("close", (code) => {
160-
if (code === 0) {
161-
resolve();
162-
} else {
163-
reject({
164-
code,
165-
message: `child process exited with code ${code}`,
166-
});
167-
}
168-
});
194+
const childProcess = spawn(command, args, {
195+
stdio: "inherit",
196+
pwd: PROJECT_DIR,
197+
shell: true
198+
});
199+
200+
childProcess.on("close", code => {
201+
if (code === 0) {
202+
resolve();
203+
} else {
204+
reject({
205+
code,
206+
message: `child process exited with code ${code}`,
207+
});
208+
}
209+
});
169210
});
170211
}
171212

0 commit comments

Comments
 (0)