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

Commit 35ce787

Browse files
sis0k0vchimev
authored and
vchimev
committed
fix(ns-bundle): clean android build for NativeScript CLI 3.0.1<= (#163)
related to NativeScript/android#759
1 parent 2ccf55b commit 35ce787

File tree

1 file changed

+72
-30
lines changed

1 file changed

+72
-30
lines changed

Diff for: bin/ns-bundle

+72-30
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+
() => cleanBuildArtifacts(platform),
49+
() => webpack(platform),
50+
];
5251
}
5352

5453
// If "build-app" or "start-app" is specified,
@@ -59,16 +58,26 @@ function execute(options) {
5958
} else {
6059
commands.shift(() => runTns("prepare", platform))
6160
}
62-
6361
return commands.reduce((current, next) => current.then(next), Promise.resolve());
6462
}
6563

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

@@ -80,18 +89,49 @@ function gradlewClean() {
8089
resolve();
8190
}
8291

83-
spawnChildProcess(true, gradlew, "-p", platformsPath, "clean")
92+
spawnChildProcess(gradlew, "-p", platformsPath, "clean")
8493
.then(resolve)
8594
.catch(throwError);
8695
});
8796
}
8897

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

93134
const args = [
94-
true, // show output on console
95135
`webpack`,
96136
`--config=webpack.config.js`,
97137
`--progress`,
@@ -109,7 +149,7 @@ function runTns(command, platform) {
109149
return new Promise((resolve, reject) => {
110150
console.log(`Running tns ${command}...`);
111151

112-
spawnChildProcess(true, "tns", command, platform, "--bundle", "--disable-npm-install", ...tnsArgs)
152+
spawnChildProcess("tns", command, platform, "--bundle", "--disable-npm-install", ...tnsArgs)
113153
.then(resolve)
114154
.catch(throwError);
115155
});
@@ -150,22 +190,24 @@ function getCommand(flags) {
150190
}
151191
}
152192

153-
function spawnChildProcess(shouldPrintOutput, command, ...args) {
154-
const stdio = shouldPrintOutput ? "inherit" : "ignore";
155-
193+
function spawnChildProcess(command, ...args) {
156194
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-
});
195+
const childProcess = spawn(command, args, {
196+
stdio: "inherit",
197+
pwd: PROJECT_DIR,
198+
shell: true,
199+
});
200+
201+
childProcess.on("close", code => {
202+
if (code === 0) {
203+
resolve();
204+
} else {
205+
reject({
206+
code,
207+
message: `child process exited with code ${code}`,
208+
});
209+
}
210+
});
169211
});
170212
}
171213

0 commit comments

Comments
 (0)