Skip to content

Commit b89ed4b

Browse files
committed
refactor: add cordova util and replace q.promise with native promise in command-executor
1 parent 966868d commit b89ed4b

2 files changed

Lines changed: 35 additions & 23 deletions

File tree

src/command-executor.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var opener = require("opener");
99
import os from "os";
1010
import path from "path";
1111
var prompt = require("prompt");
12-
import Q from "q";
1312
import * as rimraf from "rimraf";
1413
var which = require("which");
1514
import wordwrap = require("wordwrap");
@@ -38,11 +37,11 @@ import {
3837
import { isBinaryOrZip } from "./lib/file-utils";
3938
import { out } from "./util/interaction";
4039
import { isValidRange } from "./lib/validation-utils";
40+
import { getCordovaProjectAppVersion } from "./lib/cordova-utils";
4141

4242
var configFilePath: string = path.join(process.env.LOCALAPPDATA || process.env.HOME, ".code-push.config");
4343
var emailValidator = require("email-validator");
4444
var packageJson = require("../package.json");
45-
var parseXml = Q.denodeify(require("xml2js").parseString);
4645

4746
const CLI_HEADERS: Headers = {
4847
"X-CodePush-CLI-Version": packageJson.version,
@@ -1063,12 +1062,12 @@ export var release = (command: cli.IReleaseCommand): Promise<void> => {
10631062
type: command.type,
10641063
};
10651064

1066-
var releaseHooksPromise = hooks.reduce((accumulatedPromise: Q.Promise<cli.IReleaseCommand>, hook: cli.ReleaseHook) => {
1065+
var releaseHooksPromise = hooks.reduce((accumulatedPromise: Promise<cli.IReleaseCommand>, hook: cli.ReleaseHook) => {
10671066
return accumulatedPromise.then((modifiedCommand: cli.IReleaseCommand) => {
10681067
currentCommand = modifiedCommand || currentCommand;
10691068
return hook(currentCommand, command, sdk);
10701069
});
1071-
}, Q(currentCommand));
1070+
}, Promise.resolve(currentCommand));
10721071

10731072
return releaseHooksPromise.then(() => {}).catch((err: CodePushError) => releaseErrorHandler(err, command));
10741073
});
@@ -1083,7 +1082,6 @@ export var releaseCordova = (command: cli.IReleaseCordovaCommand): Promise<void>
10831082
var platform: string = command.platform.toLowerCase();
10841083
var projectRoot: string = process.cwd();
10851084
var platformFolder: string = path.join(projectRoot, "platforms", platform);
1086-
var platformCordova: string = path.join(platformFolder, "cordova");
10871085
var outputFolder: string;
10881086

10891087
if (platform === "ios") {
@@ -1125,31 +1123,17 @@ export var releaseCordova = (command: cli.IReleaseCordovaCommand): Promise<void>
11251123
);
11261124
}
11271125

1128-
try {
1129-
var configString: string = fs.readFileSync(path.join(projectRoot, "config.xml"), { encoding: "utf8" });
1130-
} catch (error) {
1131-
throw new Error(
1132-
`Unable to find or read "config.xml" in the CWD. The "release-cordova" command must be executed in a Cordova project folder.`
1133-
);
1134-
}
1135-
1136-
var configPromise: Promise<any> = parseXml(configString) as any;
1137-
11381126
releaseCommand.package = outputFolder;
11391127
releaseCommand.type = cli.CommandType.release;
11401128

1141-
return configPromise.catch((err: any) => {
1142-
throw new Error(`Unable to parse "config.xml" in the CWD. Ensure that the contents of "config.xml" is valid XML.`);
1143-
});
1129+
return getCordovaProjectAppVersion(projectRoot);
11441130
})
1145-
.then((parsedConfig: any) => {
1146-
var config: any = parsedConfig.widget;
1147-
1131+
.then((appVersion: string) => {
11481132
var releaseTargetVersion: string;
11491133
if (command.appStoreVersion) {
11501134
releaseTargetVersion = command.appStoreVersion;
11511135
} else {
1152-
releaseTargetVersion = config["$"].version;
1136+
releaseTargetVersion = appVersion;
11531137
}
11541138

11551139
throwForInvalidSemverRange(releaseTargetVersion);
@@ -1231,7 +1215,7 @@ export var releaseReact = (command: cli.IReleaseReactCommand): Promise<void> =>
12311215
}
12321216

12331217
var appVersionPromise: Promise<string> = command.appStoreVersion
1234-
? (Q(command.appStoreVersion) as any)
1218+
? Promise.resolve(command.appStoreVersion)
12351219
: getReactNativeProjectAppVersion(command, projectName);
12361220

12371221
if (command.sourcemapOutputDir && command.sourcemapOutput) {

src/lib/cordova-utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as xml2js from "xml2js";
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
5+
export function getCordovaProjectAppVersion(projectRoot?: string): Promise<string> {
6+
return new Promise<string>((resolve, reject) => {
7+
let configString: string;
8+
try {
9+
projectRoot = projectRoot || process.cwd();
10+
configString = fs.readFileSync(path.join(projectRoot, "config.xml"), { encoding: "utf8" });
11+
} catch (error) {
12+
return reject(
13+
new Error(
14+
`Unable to find or read "config.xml" in the CWD. The "release-cordova" command must be executed in a Cordova project folder.`
15+
)
16+
);
17+
}
18+
19+
xml2js.parseString(configString, (err: Error, parsedConfig: any) => {
20+
if (err) {
21+
reject(new Error(`Unable to parse "config.xml" in the CWD. Ensure that the contents of "config.xml" is valid XML.`));
22+
}
23+
24+
const config: any = parsedConfig.widget;
25+
resolve(config["$"].version);
26+
});
27+
});
28+
}

0 commit comments

Comments
 (0)