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

Commit c3e7376

Browse files
committed
fix(ns-bundle): escape command and args when spawning child process
fixes #209
1 parent b2aec11 commit c3e7376

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

Diff for: bin/ns-bundle

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
const { spawn } = require("child_process");
4-
const { resolve: pathResolve } = require("path");
4+
const { resolve: pathResolve, join } = require("path");
55
const { existsSync } = require("fs");
66
const { getPackageJson } = require("../projectHelpers");
77
const { isVersionGte } = require("../utils");
@@ -13,14 +13,16 @@ if (!process.env.npm_config_argv) {
1313
throwError({message: "No flags provided."});
1414
}
1515

16-
const escape = arg => `"${arg}"`;
16+
const escapeWithQuotes = arg => `"${arg}"`;
1717
const isTnsCommand = flag => flag.endsWith("-app");
1818
const isEnvCommand = flag => flag.indexOf("env.") > -1;
1919
const shouldUglify = () => process.env.npm_config_uglify;
20-
const shouldSnapshot = (platform) => platform == "android" && require("os").type() != "Windows_NT" && process.env.npm_config_snapshot;
20+
const shouldSnapshot = platform => platform == "android" &&
21+
require("os").type() != "Windows_NT" &&
22+
process.env.npm_config_snapshot;
2123

2224
const npmArgs = JSON.parse(process.env.npm_config_argv).original;
23-
const tnsArgs = getTnsArgs(npmArgs).map(escape);
25+
const tnsArgs = getTnsArgs(npmArgs).map(escapeWithQuotes);
2426
const flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2));
2527
const options = getOptions(flags);
2628

@@ -51,7 +53,7 @@ function execute(options) {
5153
...commands,
5254
() => cleanApp(platform),
5355
() => cleanSnapshotArtefacts(),
54-
() => cleanBuildArtifacts(platform),
56+
() => cleanBuildArtefacts(platform),
5557
() => webpack(platform, options.env),
5658
];
5759
}
@@ -68,14 +70,14 @@ function execute(options) {
6870
return commands.reduce((current, next) => current.then(next), Promise.resolve());
6971
}
7072

71-
function cleanBuildArtifacts(platform) {
73+
function cleanBuildArtefacts(platform) {
7274
return new Promise((resolve, reject) => {
7375
if (platform !== "android") {
7476
return resolve();
7577
}
7678

7779
getTnsVersion().then(version => {
78-
// the android build artifacts should be cleaned manually
80+
// the android build artefacts should be cleaned manually
7981
// for nativescript-cli v3.0.1 and below or if using uglify
8082
if (isVersionGte(version, "3.0.1") || shouldUglify()) {
8183
gradlewClean().then(resolve).catch(throwError);
@@ -96,7 +98,7 @@ function installSnapshotArtefacts() {
9698

9799
function gradlewClean() {
98100
return new Promise((resolve, reject) => {
99-
const platformsPath = pathResolve(PROJECT_DIR, "platforms", "android")
101+
const platformsPath = join(PROJECT_DIR, "platforms", "android")
100102
const gradlew = pathResolve(platformsPath, "gradlew");
101103
if (!existsSync(gradlew)) {
102104
return resolve();
@@ -134,6 +136,7 @@ function versionToNumber(version) {
134136
// Clear platform/**/app folder contents
135137
function cleanApp(platform) {
136138
return new Promise((resolve, reject) => {
139+
137140
spawnChildProcess("tns", "clean-app", platform)
138141
.then(resolve)
139142
.catch(throwError)
@@ -171,18 +174,14 @@ function runTns(command, platform) {
171174
}
172175

173176
function getOptions(flags) {
174-
let options = {};
175-
options.platform = getPlatform(flags);
176-
options.command = getCommand(flags);
177-
options.env = getEnv(flags);
178-
options.bundle = !flags.includes("nobundle");
179-
180-
return options;
177+
return {
178+
platform: getPlatform(flags),
179+
command: getCommand(flags),
180+
env: flags.filter(isEnvCommand),
181+
bundle: !flags.includes("nobundle"),
182+
};
181183
}
182184

183-
function getEnv(flags) {
184-
return flags.filter(item => isEnvCommand(item));
185-
}
186185

187186
function getPlatform(flags) {
188187
if (flags.includes("android") && flags.includes("ios")) {
@@ -194,7 +193,9 @@ function getPlatform(flags) {
194193
} else if (flags.includes("ios")) {
195194
return "ios";
196195
} else {
197-
throwError({message: "You must provide a target platform! Use either --android, or --ios flag."});
196+
throwError({message:
197+
"You must provide a target platform! " +
198+
"Use either --android, or --ios flag."});
198199
}
199200
}
200201

@@ -209,7 +210,10 @@ function getCommand(flags) {
209210

210211
function spawnChildProcess(command, ...args) {
211212
return new Promise((resolve, reject) => {
212-
const childProcess = spawn(command, args, {
213+
const escapedArgs = args.map(escapeWithQuotes)
214+
const escapedCommand = escapeWithQuotes(command)
215+
216+
const childProcess = spawn(escapedCommand, escapedArgs, {
213217
stdio: "inherit",
214218
pwd: PROJECT_DIR,
215219
shell: true,

0 commit comments

Comments
 (0)