Skip to content

Instantly share code, notes, and snippets.

@dfee
Created June 3, 2020 01:48
Show Gist options
  • Save dfee/13b7b5fa1197519699333c396629b824 to your computer and use it in GitHub Desktop.
Save dfee/13b7b5fa1197519699333c396629b824 to your computer and use it in GitHub Desktop.
import {
CommandLineChoiceParameter,
CommandLineFlagParameter,
CommandLineStringParameter,
} from "@rushstack/ts-command-line";
import { DeployManager } from "../../logic/deploy/DeployManager";
import { RushCommandLineParser } from "../RushCommandLineParser";
import { BaseRushAction } from "./BaseRushAction";
export class DeployAction extends BaseRushAction {
private _scenario!: CommandLineStringParameter;
private _projectName!: CommandLineStringParameter;
private _includeDevDependencies!: CommandLineFlagParameter;
private _includeNpmIgnoreFiles!: CommandLineFlagParameter;
private _linkCreation!: CommandLineChoiceParameter;
private _overwrite!: CommandLineFlagParameter;
private _targetFolder!: CommandLineStringParameter;
public constructor(parser: RushCommandLineParser) {
super({
actionName: "deploy",
summary:
"(EXPERIMENTAL) Copy a subset of Rush projects and their dependencies to a deployment folder",
documentation:
'(EXPERIMENTAL) After building the repo, "rush deploy" can be used to copy a subset of' +
" Rush projects and their dependencies to a deployment target folder, which can then be copied to" +
' a production machine. The "rush deploy" behavior is specified by a scenario config file located under' +
' the "common/config/deploy" folder. You can define multiple scenarios. Use the "rush init-deploy" command' +
" to create a new config file.",
parser,
});
}
protected onDefineParameters(): void {
this._scenario = this.defineStringParameter({
parameterLongName: "--scenario",
parameterShortName: "-s",
argumentName: "SCENARIO",
required: false,
description:
"Specifies the name of a config file describing the deployment. " +
'For example if SCENARIO is "web", the input file is: common/config/deploy/web.json',
});
this._projectName = this.defineStringParameter({
parameterLongName: "--projectName",
parameterShortName: "-p",
argumentName: "PACKAGE_NAME",
required: false,
description: "Specifies the name of the package to deploy.",
});
this._includeDevDependencies = this.defineFlagParameter({
parameterLongName: "--includeDevDependencies",
description:
"By default, deployment will not include dev dependenices. " +
"Specifying this flag will include dev dependencies in the deployment.",
});
this._includeNpmIgnoreFiles = this.defineFlagParameter({
parameterLongName: "--includeNpmIgnoreFiles",
description:
"By default, deployment will not include npm ignored files. " +
"Specifying this flag will include npm ignored files in the deployment.",
});
this._linkCreation = this.defineChoiceParameter({
parameterLongName: "--linkCreation",
argumentName: "LINK_CREATION_OPTION",
required: false,
description:
"Specifies the strategy for creating links during deployment. " +
"The default behavior is to generate the symlinks",
alternatives: ["default", "none", "script"],
defaultValue: "default",
});
this._overwrite = this.defineFlagParameter({
parameterLongName: "--overwrite",
description:
"By default, deployment will fail if the target folder is not empty. SPECIFYING THIS FLAG " +
"WILL RECURSIVELY DELETE EXISTING CONTENTS OF THE TARGET FOLDER.",
});
this._targetFolder = this.defineStringParameter({
parameterLongName: "--target-folder",
parameterShortName: "-t",
argumentName: "PATH",
environmentVariable: "RUSH_DEPLOY_TARGET_FOLDER",
description:
"By default, files are deployed to the common/deploy folder inside the Rush repo." +
" Use this parameter to specify a different location. " +
' WARNING: USE CAUTION WHEN COMBINING WITH "--overwrite"',
});
}
protected async run(): Promise<void> {
const deployManager: DeployManager = new DeployManager(
this.rushConfiguration,
);
let options: Partial<DeployScenarioOptions> = {
overwrite: !!this._overwrite.value,
targetFolder: this._targetFolder.value,
};
if (this._scenario.value !== undefined) {
options = { scenario: this._scenario.value };
} else {
if (this._projectName.value === undefined) {
throw new Error("--project-name or --scenario must be provided");
}
options = {
includeDevDependencies: this._includeDevDependencies.value ?? false,
includeNpmIgnoreFiles: this._includeNpmIgnoreFiles.value ?? false,
linkCreation: (this._linkCreation.value ??
"default") as LinkCreationChoices,
projectName: this._projectName.value,
};
}
deployManager.deployScenario(options);
}
}
type LinkCreationChoices = "default" | "none" | "script";
type DeployScenarioOptions = {
overwrite: boolean;
targetFolder: string;
} & (
| {
scenario: string;
}
| {
includeDevDependencies: boolean;
includeNpmIgnoreFiles: boolean;
linkCreation: LinkCreationChoices;
projectName: string;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment