Skip to content

Commit

Permalink
Merge pull request #150 from Microsoft/productize-sdk
Browse files Browse the repository at this point in the history
Productize the management SDK
  • Loading branch information
Richard Hua committed Mar 8, 2016
2 parents 8eac552 + a89a979 commit b5600b7
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 183 deletions.
42 changes: 22 additions & 20 deletions cli/script/command-executor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="../../definitions/generated/code-push.d.ts" />

import AccountManager = require("code-push");
import * as base64 from "base-64";
import * as chalk from "chalk";
var childProcess = require("child_process");
Expand All @@ -22,17 +23,18 @@ import * as yazl from "yazl";
import wordwrap = require("wordwrap");

import * as cli from "../definitions/cli";
import { AcquisitionStatus } from "code-push/script/acquisition-sdk";
import { AccessKey, Account, AccountManager, App, CollaboratorMap, CollaboratorProperties, Deployment, DeploymentMetrics, Package, Permissions, UpdateMetrics } from "code-push";
import { AccessKey, Account, App, CollaboratorMap, CollaboratorProperties, Deployment, DeploymentMetrics, Headers, Package, UpdateMetrics } from "code-push/script/types";

var configFilePath: string = path.join(process.env.LOCALAPPDATA || process.env.HOME, ".code-push.config");
var emailValidator = require("email-validator");
var packageJson = require("../package.json");
var progress = require("progress");
import Promise = Q.Promise;
var userAgent: string = packageJson.name + "/" + packageJson.version;

const ACTIVE_METRICS_KEY: string = "Active";
const CLI_HEADERS: Headers = {
"X-CodePush-CLI-Version": packageJson.version
};
const DOWNLOADED_METRICS_KEY: string = "Downloaded";

interface NameToCountMap {
Expand Down Expand Up @@ -182,7 +184,7 @@ function appRemove(command: cli.IAppRemoveCommand): Promise<void> {
}

function appRename(command: cli.IAppRenameCommand): Promise<void> {
return sdk.updateApp(command.currentAppName, { name: command.newAppName })
return sdk.renameApp(command.currentAppName, command.newAppName)
.then((): void => {
log("Successfully renamed the \"" + command.currentAppName + "\" app to \"" + command.newAppName + "\".");
});
Expand Down Expand Up @@ -223,7 +225,7 @@ function addCollaborator(command: cli.ICollaboratorAddCommand): Promise<void> {
function listCollaborators(command: cli.ICollaboratorListCommand): Promise<void> {
throwForInvalidOutputFormat(command.format);

return sdk.getCollaboratorsList(command.appName)
return sdk.getCollaborators(command.appName)
.then((retrievedCollaborators: CollaboratorMap): void => {
printCollaboratorsList(command.format, retrievedCollaborators);
});
Expand Down Expand Up @@ -324,7 +326,7 @@ function deploymentRemove(command: cli.IDeploymentRemoveCommand): Promise<void>
}

function deploymentRename(command: cli.IDeploymentRenameCommand): Promise<void> {
return sdk.updateDeployment(command.appName, command.currentDeploymentName, { name: command.newDeploymentName })
return sdk.renameDeployment(command.appName, command.currentDeploymentName, command.newDeploymentName)
.then((): void => {
log("Successfully renamed the \"" + command.currentDeploymentName + "\" deployment to \"" + command.newDeploymentName + "\" for the \"" + command.appName + "\" app.");
});
Expand All @@ -335,12 +337,12 @@ function deploymentHistory(command: cli.IDeploymentHistoryCommand): Promise<void

return Q.all<any>([
sdk.getAccountInfo(),
sdk.getPackageHistory(command.appName, command.deploymentName),
sdk.getDeploymentHistory(command.appName, command.deploymentName),
sdk.getDeploymentMetrics(command.appName, command.deploymentName)
])
.spread<void>((account: Account, packageHistory: Package[], metrics: DeploymentMetrics): void => {
.spread<void>((account: Account, deploymentHistory: Package[], metrics: DeploymentMetrics): void => {
var totalActive: number = getTotalActiveFromDeploymentMetrics(metrics);
packageHistory.forEach((packageObject: Package) => {
deploymentHistory.forEach((packageObject: Package) => {
if (metrics[packageObject.label]) {
(<PackageWithMetrics>packageObject).metrics = {
active: metrics[packageObject.label].active,
Expand All @@ -351,7 +353,7 @@ function deploymentHistory(command: cli.IDeploymentHistoryCommand): Promise<void
};
}
});
printDeploymentHistory(command, <PackageWithMetrics[]>packageHistory, account.email);
printDeploymentHistory(command, <PackageWithMetrics[]>deploymentHistory, account.email);
});
}

Expand Down Expand Up @@ -393,7 +395,7 @@ export function execute(command: cli.ICommand): Promise<void> {
throw new Error("You are not currently logged in. Run the 'code-push login' command to authenticate with the CodePush server.");
}

sdk = new AccountManager(connectionInfo.accessKey, userAgent, connectionInfo.customServerUrl);
sdk = new AccountManager(connectionInfo.accessKey, CLI_HEADERS, connectionInfo.customServerUrl);
break;
}

Expand Down Expand Up @@ -515,7 +517,7 @@ function initiateExternalAuthenticationAsync(action: string, serverUrl?: string)
function login(command: cli.ILoginCommand): Promise<void> {
// Check if one of the flags were provided.
if (command.accessKey) {
sdk = new AccountManager(command.accessKey, userAgent, command.serverUrl);
sdk = new AccountManager(command.accessKey, CLI_HEADERS, command.serverUrl);
return sdk.isAuthenticated()
.then((isAuthenticated: boolean): void => {
if (isAuthenticated) {
Expand All @@ -539,7 +541,7 @@ function loginWithExternalAuthentication(action: string, serverUrl?: string): Pr
return;
}

sdk = new AccountManager(accessKey, userAgent, serverUrl);
sdk = new AccountManager(accessKey, CLI_HEADERS, serverUrl);

return sdk.isAuthenticated()
.then((isAuthenticated: boolean): void => {
Expand Down Expand Up @@ -601,7 +603,7 @@ function printAppList(format: string, apps: App[], deploymentLists: string[][]):
}

function getCollaboratorDisplayName(email: string, collaboratorProperties: CollaboratorProperties): string {
return (collaboratorProperties.permission === Permissions.Owner) ? email + chalk.magenta(" (" + Permissions.Owner + ")") : email;
return (collaboratorProperties.permission === AccountManager.AppPermission.OWNER) ? email + chalk.magenta(" (Owner)") : email;
}

function printCollaboratorsList(format: string, collaborators: CollaboratorMap): void {
Expand Down Expand Up @@ -651,9 +653,9 @@ function printDeploymentList(command: cli.IDeploymentListCommand, deployments: D
}
}

function printDeploymentHistory(command: cli.IDeploymentHistoryCommand, packageHistory: PackageWithMetrics[], currentUserEmail: string): void {
function printDeploymentHistory(command: cli.IDeploymentHistoryCommand, deploymentHistory: PackageWithMetrics[], currentUserEmail: string): void {
if (command.format === "json") {
printJson(packageHistory);
printJson(deploymentHistory);
} else if (command.format === "table") {
var headers = ["Label", "Release Time", "App Version", "Mandatory"];
if (command.displayAuthor) {
Expand All @@ -663,7 +665,7 @@ function printDeploymentHistory(command: cli.IDeploymentHistoryCommand, packageH
headers.push("Description", "Install Metrics");

printTable(headers, (dataSource: any[]) => {
packageHistory.forEach((packageObject: Package) => {
deploymentHistory.forEach((packageObject: Package) => {
var releaseTime: string = formatDate(packageObject.uploadTime);
var releaseSource: string;
if (packageObject.releaseMethod === "Promote") {
Expand Down Expand Up @@ -833,7 +835,7 @@ function register(command: cli.IRegisterCommand): Promise<void> {
}

function promote(command: cli.IPromoteCommand): Promise<void> {
return sdk.promotePackage(command.appName, command.sourceDeploymentName, command.destDeploymentName)
return sdk.promote(command.appName, command.sourceDeploymentName, command.destDeploymentName)
.then((): void => {
log("Successfully promoted the \"" + command.sourceDeploymentName + "\" deployment of the \"" + command.appName + "\" app to the \"" + command.destDeploymentName + "\" deployment.");
});
Expand Down Expand Up @@ -907,7 +909,7 @@ export var release = (command: cli.IReleaseCommand): Promise<void> => {

return getPackageFilePromise
.then((file: IPackageFile): Promise<void> => {
return sdk.releasePackage(command.appName, command.deploymentName, file.path, command.description, command.appStoreVersion, command.mandatory, uploadProgress)
return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, command.description, command.mandatory, uploadProgress)
.then((): void => {
log("Successfully released an update containing the \"" + command.package + "\" " + (isSingleFilePackage ? "file" : "directory") + " to the \"" + command.deploymentName + "\" deployment of the \"" + command.appName + "\" app.");

Expand Down Expand Up @@ -1003,7 +1005,7 @@ function rollback(command: cli.IRollbackCommand): Promise<void> {
return;
}

return sdk.rollbackPackage(command.appName, command.deploymentName, command.targetRelease || undefined)
return sdk.rollback(command.appName, command.deploymentName, command.targetRelease || undefined)
.then((): void => {
log("Successfully performed a rollback on the \"" + command.deploymentName + "\" deployment of the \"" + command.appName + "\" app.");
});
Expand Down
3 changes: 1 addition & 2 deletions cli/script/command-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AccountManager } from "code-push";
import * as yargs from "yargs";
import * as yargs from "yargs";
import * as cli from "../definitions/cli";
import * as chalk from "chalk";
import * as updateNotifier from "update-notifier";
Expand Down
32 changes: 16 additions & 16 deletions cli/test/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as sinon from "sinon";
import Q = require("q");
import * as path from "path";
import Promise = Q.Promise;
import * as codePush from "code-push";
import * as codePush from "code-push/script/types";
import * as cli from "../definitions/cli";
import * as cmdexec from "../script/command-executor";
import * as os from "os";
Expand Down Expand Up @@ -91,7 +91,7 @@ export class SdkStub {
}]);
}

public getPackageHistory(appId: string, deploymentId: string): Promise<codePush.Package[]> {
public getDeploymentHistory(appId: string, deploymentId: string): Promise<codePush.Package[]> {
return Q([
<codePush.Package>{
description: null,
Expand Down Expand Up @@ -136,7 +136,7 @@ export class SdkStub {
});
}

public getCollaboratorsList(app: codePush.App): Promise<any> {
public getCollaborators(app: codePush.App): Promise<any> {
return Q({
"[email protected]": {
permission: "Owner",
Expand Down Expand Up @@ -169,15 +169,15 @@ export class SdkStub {
return Q(<void>null);
}

public updateApp(app: codePush.App): Promise<void> {
public renameApp(app: codePush.App): Promise<void> {
return Q(<void>null);
}

public transferApp(app: codePush.App): Promise<void> {
return Q(<void>null);
}

public updateDeployment(appId: string, deployment: codePush.Deployment): Promise<void> {
public renameDeployment(appId: string, deployment: codePush.Deployment): Promise<void> {
return Q(<void>null);
}
}
Expand Down Expand Up @@ -402,11 +402,11 @@ describe("CLI", () => {
newAppName: "c"
};

var updateApp: Sinon.SinonSpy = sandbox.spy(cmdexec.sdk, "updateApp");
var renameApp: Sinon.SinonSpy = sandbox.spy(cmdexec.sdk, "renameApp");

cmdexec.execute(command)
.done((): void => {
sinon.assert.calledOnce(updateApp);
sinon.assert.calledOnce(renameApp);
sinon.assert.calledOnce(log);
sinon.assert.calledWithExactly(log, "Successfully renamed the \"a\" app to \"c\".");

Expand Down Expand Up @@ -612,11 +612,11 @@ describe("CLI", () => {
newDeploymentName: "c"
};

var updateDeployment: Sinon.SinonSpy = sandbox.spy(cmdexec.sdk, "updateDeployment");
var renameDeployment: Sinon.SinonSpy = sandbox.spy(cmdexec.sdk, "renameDeployment");

cmdexec.execute(command)
.done((): void => {
sinon.assert.calledOnce(updateDeployment);
sinon.assert.calledOnce(renameDeployment);
sinon.assert.calledOnce(log);
sinon.assert.calledWithExactly(log, "Successfully renamed the \"Staging\" deployment to \"c\" for the \"a\" app.");

Expand All @@ -633,11 +633,11 @@ describe("CLI", () => {
displayAuthor: false
};

var getPackageHistory: Sinon.SinonSpy = sandbox.spy(cmdexec.sdk, "getPackageHistory");
var getDeploymentHistory: Sinon.SinonSpy = sandbox.spy(cmdexec.sdk, "getDeploymentHistory");

cmdexec.execute(command)
.done((): void => {
sinon.assert.calledOnce(getPackageHistory);
sinon.assert.calledOnce(getDeploymentHistory);
sinon.assert.calledOnce(log);
assert.equal(log.args[0].length, 1);

Expand Down Expand Up @@ -933,7 +933,7 @@ describe("CLI", () => {
})
.done();
});

it("release-react defaults bundle name to \"index.android.bundle\" if not provided and platform is \"android\"", (done: MochaDone): void => {
var command: cli.IReleaseReactCommand = {
type: cli.CommandType.releaseReact,
Expand Down Expand Up @@ -968,7 +968,7 @@ describe("CLI", () => {
})
.done();
});

it("release-react generates dev bundle", (done: MochaDone): void => {
var bundleName = "bundle.js";
var command: cli.IReleaseReactCommand = {
Expand Down Expand Up @@ -1007,7 +1007,7 @@ describe("CLI", () => {
})
.done();
});

it("release-react generates sourcemaps", (done: MochaDone): void => {
var bundleName = "bundle.js";
var command: cli.IReleaseReactCommand = {
Expand Down Expand Up @@ -1068,7 +1068,7 @@ describe("CLI", () => {
.then(() => {
var releaseCommand: cli.IReleaseCommand = <any>command;
releaseCommand.package = path.join(os.tmpdir(), "CodePush");

sinon.assert.calledOnce(spawn);
var spawnCommand: string = spawn.args[0][0];
var spawnCommandArgs: string = spawn.args[0][1].join(" ");
Expand All @@ -1082,7 +1082,7 @@ describe("CLI", () => {
})
.done();
});

function releaseHelperFunction(command: cli.IReleaseCommand, done: MochaDone, expectedError: string): void {
var release: Sinon.SinonSpy = sandbox.spy(cmdexec.sdk, "release");
cmdexec.execute(command)
Expand Down
3 changes: 2 additions & 1 deletion sdk/script/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./account-manager";
import AccountManager = require("./management-sdk");
export = AccountManager;
Loading

0 comments on commit b5600b7

Please sign in to comment.