Skip to content

Commit 7477a3c

Browse files
authored
Merge pull request microsoft#579 from Microsoft/qz2017/pack
Support packing packages into tarballs in rush publish action
2 parents cffcd57 + a38b1c7 commit 7477a3c

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

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

Lines changed: 78 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: '--release-folder',
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,
@@ -156,6 +179,8 @@ export default class PublishAction extends BaseRushAction {
156179
return Promise.resolve();
157180
}
158181

182+
this._validate();
183+
159184
if (this._includeAll.value) {
160185
this._publishAll(allPackages);
161186
} else {
@@ -167,6 +192,21 @@ export default class PublishAction extends BaseRushAction {
167192
return Promise.resolve();
168193
}
169194

195+
/**
196+
* Validate some input parameters
197+
*/
198+
private _validate(): void {
199+
if (this._pack.value && !this._includeAll.value) {
200+
throw new Error('--pack can only be used with --include-all');
201+
}
202+
if (this._releaseFolder.value && !this._pack.value) {
203+
throw new Error(`--release-folder can only be used with --pack`);
204+
}
205+
if (this._registryUrl.value && this._pack.value) {
206+
throw new Error(`--registry cannot be used with --pack`);
207+
}
208+
}
209+
170210
private _publishChanges(allPackages: Map<string, RushConfigurationProject>): void {
171211
const changeManager: ChangeManager = new ChangeManager(this.rushConfiguration);
172212
changeManager.load(this.rushConfiguration.changesFolder,
@@ -236,7 +276,11 @@ export default class PublishAction extends BaseRushAction {
236276
if (packageConfig.shouldPublish &&
237277
(!this._versionPolicy.value || this._versionPolicy.value === packageConfig.versionPolicyName)
238278
) {
239-
if (this._force.value || !this._packageExists(packageConfig)) {
279+
if (this._pack.value) {
280+
// packs to tarball instead of publishing to NPM repository
281+
this._npmPack(packageName, packageConfig);
282+
} else if (this._force.value || !this._packageExists(packageConfig)) {
283+
// Publish to npm repository
240284
this._npmPublish(packageName, packageConfig.projectFolder);
241285
git.addTag(!!this._publish.value && !this._registryUrl.value, packageName, packageConfig.packageJson.version);
242286
updated = true;
@@ -307,4 +351,37 @@ export default class PublishAction extends BaseRushAction {
307351
env);
308352
return publishedVersions.indexOf(packageConfig.packageJson.version) >= 0;
309353
}
354+
355+
private _npmPack(packageName: string, project: RushConfigurationProject): void {
356+
const args: string[] = ['pack'];
357+
const env: { [key: string]: string | undefined } = PublishUtilities.getEnvArgs();
358+
359+
PublishUtilities.execCommand(
360+
!!this._publish.value,
361+
this.rushConfiguration.packageManagerToolFilename,
362+
args,
363+
project.projectFolder,
364+
env
365+
);
366+
367+
if (!!this._publish.value) {
368+
// Copy the tarball the release folder
369+
const tarballName: string = this._calculateTarballName(project);
370+
const tarballPath: string = path.join(project.projectFolder, tarballName);
371+
const destFolder: string = this._releaseFolder.value ?
372+
this._releaseFolder.value : path.join(this.rushConfiguration.commonTempFolder, 'artifacts', 'packages');
373+
374+
fsx.copySync(tarballPath, path.join(destFolder, tarballName));
375+
fsx.unlinkSync(tarballPath);
376+
}
377+
}
378+
379+
private _calculateTarballName(project: RushConfigurationProject): string {
380+
// Same logic as how npm forms the tarball name
381+
const packageName: string = project.packageName;
382+
const name: string = packageName[0] === '@' ?
383+
packageName.substr(1).replace(/\//g, '-') : packageName;
384+
385+
return `${name}-${project.packageJson.version}.tgz`;
386+
}
310387
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Add --pack option to \"rush publish\" to support packing packages into tarballs instead of publishing to NPM registry",
5+
"packageName": "@microsoft/rush",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "qz2017@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)