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

fix(ns-bundle): clean android build for NativeScript CLI 3.0.1<= #163

Merged
merged 1 commit into from
May 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 70 additions & 29 deletions bin/ns-bundle
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ function execute(options) {
const platform = options.platform;

if (options.bundle) {
commands.push(() => cleanApp(platform));
commands.push(() => webpack(platform));
}

if (platform === "android") {
commands.push(() => gradlewClean());
commands = [
() => cleanApp(platform),
() => cleanBuildArtifacts(platform),
() => webpack(platform),
];
}

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

// Clear platform/**/app folder contents
function cleanApp(platform) {
function cleanBuildArtifacts(platform) {
return new Promise((resolve, reject) => {
spawnChildProcess(true, "tns", "clean-app", platform)
.then(resolve)
.catch(throwError)
if (platform !== "android") {
resolve();
}

getTnsVersion().then(versionString => {
const version = versionToNumber(versionString);

// for nativescript-cli v3.0.1 and below
// the android build artifacts should be cleaned manually
if (version <= 301) {
gradlewClean().then(resolve).catch(throwError);
}
}).catch(throwError);
});
}

Expand All @@ -80,18 +88,49 @@ function gradlewClean() {
resolve();
}

spawnChildProcess(true, gradlew, "-p", platformsPath, "clean")
spawnChildProcess(gradlew, "-p", platformsPath, "clean")
.then(resolve)
.catch(throwError);
});
}

function getTnsVersion() {
return new Promise((resolve, reject) => {
const childProcess = spawn("tns", ["--version"], { shell: true });

childProcess.stdout.on("data", resolve);

childProcess.on("close", code => {
if (code) {
reject({
code,
message: `child process exited with code ${code}`,
});
}
});
});
}

function versionToNumber(version) {
const VERSION_MATCHER = /(\d+)\.(\d+)\.(\d+)/;

return Number(VERSION_MATCHER.exec(version).splice(1).join(""));
}

// Clear platform/**/app folder contents
function cleanApp(platform) {
return new Promise((resolve, reject) => {
spawnChildProcess("tns", "clean-app", platform)
.then(resolve)
.catch(throwError)
});
}

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`,
Expand All @@ -109,7 +148,7 @@ function runTns(command, platform) {
return new Promise((resolve, reject) => {
console.log(`Running tns ${command}...`);

spawnChildProcess(true, "tns", command, platform, "--bundle", "--disable-npm-install", ...tnsArgs)
spawnChildProcess("tns", command, platform, "--bundle", "--disable-npm-install", ...tnsArgs)
.then(resolve)
.catch(throwError);
});
Expand Down Expand Up @@ -150,22 +189,24 @@ function getCommand(flags) {
}
}

function spawnChildProcess(shouldPrintOutput, command, ...args) {
const stdio = shouldPrintOutput ? "inherit" : "ignore";

function spawnChildProcess(command, ...args) {
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}`,
});
}
});
const childProcess = spawn(command, args, {
stdio: "inherit",
pwd: PROJECT_DIR,
shell: true,
});

childProcess.on("close", code => {
if (code === 0) {
resolve();
} else {
reject({
code,
message: `child process exited with code ${code}`,
});
}
});
});
}

Expand Down