appcenter-cli
Version:
Command line tool for Visual Studio App Center
171 lines (138 loc) • 7.19 kB
text/typescript
import { CommandResult, ErrorCodes, failure, hasArg, help, longName, shortName, defaultValue } from "../../util/commandline";
import CodePushReleaseCommandSkeleton from "./lib/release-command-skeleton";
import { AppCenterClient, clientRequest, models } from "../../util/apis";
import { out } from "../../util/interaction";
import { inspect } from "util";
import * as pfs from "../../util/misc/promisfied-fs";
import * as path from "path";
import * as mkdirp from "mkdirp";
import * as chalk from "chalk";
import { fileDoesNotExistOrIsDirectory, createEmptyTmpReleaseFolder } from "./lib/file-utils";
import { isValidRange, isValidDeployment } from "./lib/validation-utils";
import { getElectronProjectAppVersion, runWebPackBundleCommand, isValidOS, isValidPlatform, isElectronProject } from "./lib/electron-utils";
const debug = require("debug")("appcenter-cli:commands:codepush:release-electron");
export default class CodePushReleaseElectronCommand extends CodePushReleaseCommandSkeleton {
public bundleName: string;
public development: boolean;
public config: string;
public entryFile: string;
public sourcemapOutput: string;
public sourcemapOutputDir: string;
public outputDir: string;
public specifiedTargetBinaryVersion: string;
public extraBundlerOptions: string | string[];
private os: string;
private platform: string;
private mode: string;
public async run(client: AppCenterClient): Promise<CommandResult> {
if (!isElectronProject()) {
return failure(ErrorCodes.InvalidParameter, `The project in the CWD is not a Electron project.`);
}
if (!(await isValidDeployment(client, this.app, this.specifiedDeploymentName))) {
return failure(ErrorCodes.InvalidParameter, `Deployment "${this.specifiedDeploymentName}" does not exist.`);
} else {
this.deploymentName = this.specifiedDeploymentName;
}
const appInfo = (await out.progress("Getting app info...", clientRequest<models.AppResponse>(
(cb) => client.apps.get(this.app.ownerName, this.app.appName, cb)))).result;
this.os = appInfo.os.toLowerCase();
this.platform = appInfo.platform.toLowerCase();
this.updateContentsPath = this.outputDir || await pfs.mkTempDir("code-push");
// we have to add "CodePush" root folder to make update contents file structure
// to be compatible with Electron-CodePush client SDK
this.updateContentsPath = path.join(this.updateContentsPath, "CodePush");
mkdirp.sync(this.updateContentsPath);
if (!isValidOS(this.os)) {
return failure(ErrorCodes.InvalidParameter, `OS must be "linux", "macos" or "windows".`);
}
if (!this.bundleName) {
this.bundleName = `index.electron.bundle`;
}
this.mode = this.development ? "development" : "production";
if (!this.config) {
if (!fileDoesNotExistOrIsDirectory("webpack.config.js")) {
this.config = "webpack.config.js";
}
} else {
if (fileDoesNotExistOrIsDirectory(this.config)) {
return failure(ErrorCodes.NotFound, `WebPack Config file "${this.config}" does not exist.`);
}
}
if (!this.entryFile) {
this.entryFile = `index.${this.os}.js`;
if (fileDoesNotExistOrIsDirectory(this.entryFile)) {
this.entryFile = `index.js`;
}
if (fileDoesNotExistOrIsDirectory(this.entryFile)) {
return failure(ErrorCodes.NotFound, `Entry file "index.${this.os}.js" or "index.js" does not exist.`);
}
} else {
if (fileDoesNotExistOrIsDirectory(this.entryFile)) {
return failure(ErrorCodes.NotFound, `Entry file "${this.entryFile}" does not exist.`);
}
}
if (this.sourcemapOutputDir && this.sourcemapOutput) {
out.text(("\n\"sourcemap-output-dir\" argument will be ignored as \"sourcemap-output\" argument is provided.\n"));
}
if ((this.outputDir || this.sourcemapOutputDir) && !this.sourcemapOutput) {
const sourcemapDir = this.sourcemapOutputDir || this.updateContentsPath;
this.sourcemapOutput = path.join(sourcemapDir, this.bundleName + ".map");
}
this.targetBinaryVersion = this.specifiedTargetBinaryVersion;
if (this.targetBinaryVersion && !isValidRange(this.targetBinaryVersion)) {
return failure(ErrorCodes.InvalidParameter, "Invalid binary version(s) for a release.");
} else if (!this.targetBinaryVersion) {
this.targetBinaryVersion = await getElectronProjectAppVersion();
}
if (typeof this.extraBundlerOptions === "string") {
this.extraBundlerOptions = [this.extraBundlerOptions];
}
try {
createEmptyTmpReleaseFolder(this.updateContentsPath);
await runWebPackBundleCommand(this.bundleName, this.mode, this.config, this.entryFile, this.updateContentsPath, this.sourcemapOutput, this.extraBundlerOptions);
out.text(chalk.cyan("\nReleasing update contents to CodePush:\n"));
return await this.release(client);
} catch (error) {
debug(`Failed to release a CodePush update - ${inspect(error)}`);
return failure(ErrorCodes.Exception, "Failed to release a CodePush update.");
} finally {
if (!this.outputDir) {
await pfs.rmDir(this.updateContentsPath);
}
}
}
}