Skip to content

Commit 4821cb7

Browse files
committed
Support packing packages into tarball in rush publish action
1 parent 75e8115 commit 4821cb7

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

‎apps/rush-lib/src/cli/actions/PublishAction.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import * as colors from 'colors';
55
import { EOL } from 'os';
6+
import * as fsx from 'fs-extra';
7+
import * as path from 'path';
68
import {
79
CommandLineFlagParameter,
810
CommandLineStringParameter
@@ -41,6 +43,9 @@ export default class PublishAction extends BaseRushAction {
4143
private _prereleaseToken: PrereleaseToken;
4244
private _versionPolicy: CommandLineStringParameter;
4345

46+
private _releaseFolder: CommandLineStringParameter;
47+
private _pack: CommandLineFlagParameter;
48+
4449
private _hotfixTagOverride: string;
4550

4651
constructor(parser: RushCommandLineParser) {
@@ -84,6 +89,8 @@ export default class PublishAction extends BaseRushAction {
8489
parameterShortName: undefined,
8590
description: 'Regenerates all changelog files based on the current JSON content.'
8691
});
92+
93+
// NPM registry related parameters
8794
this._registryUrl = this.defineStringParameter({
8895
parameterLongName: '--registry',
8996
parameterShortName: '-r',
@@ -108,6 +115,22 @@ export default class PublishAction extends BaseRushAction {
108115
`the package is older than the current latest, so in publishing workflows for older releases, providing ` +
109116
`a tag is important. When hotfix changes are made, this parameter defaults to 'hotfix'.`
110117
});
118+
119+
// NPM pack tarball related parameters
120+
this._pack = this.defineFlagParameter({
121+
parameterLongName: '--pack',
122+
description:
123+
`Packs projects into tarballs instead of publishing to npm repository. It can only be used when ` +
124+
`--include-all is specified. If this flag is specified, NPM registry related parameters will be ignored.`
125+
});
126+
this._releaseFolder = this.defineStringParameter({
127+
parameterLongName: '--releaseFolder',
128+
key: 'RELEASEFOLDER',
129+
description:
130+
`This parameter is used with --pack parameter to provide customized location for the tarballs instead of ` +
131+
`the default value. `
132+
});
133+
111134
this._includeAll = this.defineFlagParameter({
112135
parameterLongName: '--include-all',
113136
parameterShortName: undefined,
@@ -236,7 +259,11 @@ export default class PublishAction extends BaseRushAction {
236259
if (packageConfig.shouldPublish &&
237260
(!this._versionPolicy.value || this._versionPolicy.value === packageConfig.versionPolicyName)
238261
) {
239-
if (this._force.value || !this._packageExists(packageConfig)) {
262+
if (this._pack.value) {
263+
// packs to tarball instead of publishing to NPM repository
264+
this._npmPack(packageName, packageConfig);
265+
} else if (this._force.value || !this._packageExists(packageConfig)) {
266+
// Publish to npm repository
240267
this._npmPublish(packageName, packageConfig.projectFolder);
241268
git.addTag(!!this._publish.value && !this._registryUrl.value, packageName, packageConfig.packageJson.version);
242269
updated = true;
@@ -307,4 +334,37 @@ export default class PublishAction extends BaseRushAction {
307334
env);
308335
return publishedVersions.indexOf(packageConfig.packageJson.version) >= 0;
309336
}
337+
338+
private _npmPack(packageName: string, project: RushConfigurationProject): void {
339+
const args: string[] = ['pack'];
340+
const env: { [key: string]: string | undefined } = PublishUtilities.getEnvArgs();
341+
342+
PublishUtilities.execCommand(
343+
!!this._publish.value,
344+
this.rushConfiguration.packageManagerToolFilename,
345+
args,
346+
project.projectFolder,
347+
env
348+
);
349+
350+
if (!!this._publish.value) {
351+
// Copy the tarball the release folder
352+
const tarballName: string = this._calculateTarballName(project);
353+
const tarballPath: string = path.join(project.projectFolder, tarballName);
354+
const destFolder: string = this._releaseFolder.value ?
355+
this._releaseFolder.value : path.join(this.rushConfiguration.commonTempFolder, 'artifacts', 'packages');
356+
357+
fsx.copySync(tarballPath, path.join(destFolder, tarballName));
358+
fsx.unlinkSync(tarballPath);
359+
}
360+
}
361+
362+
private _calculateTarballName(project: RushConfigurationProject): string {
363+
// Same logic as how npm forms the tarball name
364+
const packageName: string = project.packageName;
365+
const name: string = packageName[0] === '@' ?
366+
packageName.substr(1).replace(/\//g, '-') : packageName;
367+
368+
return `${name}-${project.packageJson.version}.tgz`;
369+
}
310370
}

0 commit comments

Comments
 (0)