From 2dc9aa6ee2cd90d2f1d5de14b32ff344fc303de0 Mon Sep 17 00:00:00 2001 From: Edouard Bozon Date: Sat, 5 Dec 2020 09:37:51 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=85=20add=20`--push`=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/semver/package.json | 3 +- .../semver/src/builders/version/builder.ts | 63 ++++++++++++++++++- .../semver/src/builders/version/schema.d.ts | 3 + .../semver/src/builders/version/schema.json | 21 ++++++- yarn.lock | 27 +++++++- 5 files changed, 108 insertions(+), 9 deletions(-) diff --git a/packages/semver/package.json b/packages/semver/package.json index 0764f9961..bb77042ac 100644 --- a/packages/semver/package.json +++ b/packages/semver/package.json @@ -12,6 +12,7 @@ "standard-version": "^9.0.0" }, "devDependencies": { - "@types/standard-version": "^7.0.0" + "@types/standard-version": "^7.0.0", + "@lerna/child-process": "^3.16.5" } } diff --git a/packages/semver/src/builders/version/builder.ts b/packages/semver/src/builders/version/builder.ts index f8120d18d..5355f2d3a 100644 --- a/packages/semver/src/builders/version/builder.ts +++ b/packages/semver/src/builders/version/builder.ts @@ -1,7 +1,13 @@ -import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect'; +import { + BuilderContext, + BuilderOutput, + createBuilder, +} from '@angular-devkit/architect'; +import { noop, Rule } from '@angular-devkit/schematics'; +import * as childProcess from '@lerna/child-process'; import { resolve } from 'path'; -import { from, Observable, of } from 'rxjs'; -import { catchError, mapTo, switchMap } from 'rxjs/operators'; +import { from, Observable, of, defer } from 'rxjs'; +import { catchError, mapTo, switchMap, switchMapTo } from 'rxjs/operators'; import * as standardVersion from 'standard-version'; import { VersionBuilderSchema } from './schema'; @@ -11,6 +17,50 @@ async function getProjectRoot(context: BuilderContext): Promise { return metadata.root as string; } +function pushToGitRemote( + remote: string, + branch: string, + context: BuilderContext +): Rule { + return childProcess + .exec('git', [ + 'push', + '--follow-tags', + '--no-verify', + '--atomic', + remote, + branch, + ]) + .catch((error) => { + // @see https://github.com/sindresorhus/execa/blob/v1.0.0/index.js#L159-L179 + // the error message _should_ be on stderr except when GIT_REDIRECT_STDERR has been configured to redirect + // to stdout. More details in https://git-scm.com/docs/git#Documentation/git.txt-codeGITREDIRECTSTDERRcode + if ( + /atomic/.test(error.stderr) || + (process.env.GIT_REDIRECT_STDERR === '2>&1' && + /atomic/.test(error.stdout)) + ) { + // --atomic is only supported in git >=2.4.0, which some crusty CI environments deem unnecessary to upgrade. + // so let's try again without attempting to pass an option that is almost 5 years old as of this writing... + context.logger.warn('git push ' + error.stderr); + context.logger.info( + 'git push --atomic failed, attempting non-atomic push' + ); + + return childProcess.exec('git', [ + 'push', + '--follow-tags', + '--no-verify', + remote, + branch, + ]); + } + + // ensure unexpected errors still break chain + throw error; + }); +} + export function runBuilder( options: VersionBuilderSchema, context: BuilderContext @@ -28,6 +78,13 @@ export function runBuilder( bumpFiles: [resolve(projectRoot, 'package.json')], }) ), + options.push + ? switchMapTo( + defer(() => + pushToGitRemote(options.remote, options.baseBranch, context) + ) + ) + : mapTo(noop()), mapTo({ success: true }), catchError(() => { context.reportStatus('Error'); diff --git a/packages/semver/src/builders/version/schema.d.ts b/packages/semver/src/builders/version/schema.d.ts index 7c39eea4c..6b0d74e79 100644 --- a/packages/semver/src/builders/version/schema.d.ts +++ b/packages/semver/src/builders/version/schema.d.ts @@ -4,4 +4,7 @@ export interface VersionBuilderSchema extends JsonObject { dryRun?: boolean; noVerify?: boolean; firstRelease?: boolean; + push?: boolean; + remote?: string; + baseBranch?: string; } diff --git a/packages/semver/src/builders/version/schema.json b/packages/semver/src/builders/version/schema.json index 97876e2d8..4cefae48e 100644 --- a/packages/semver/src/builders/version/schema.json +++ b/packages/semver/src/builders/version/schema.json @@ -1,17 +1,17 @@ { "$schema": "https://json-schema.org/draft-07/schema", "$id": "https://json-schema.org/draft-07/schema", - "title": "Version builder", + "title": "JSCutlery Version builder", "description": "Automate versioning and CHANGELOG generation.", "type": "object", "properties": { "dry-run": { - "description": "See the commands that running standard-version would run", + "description": "See the commands that running standard-version would run.", "type": "boolean", "default": false }, "no-verify": { - "description": "Bypass pre-commit or commit-msg git hooks during the commit phase", + "description": "Bypass pre-commit or commit-msg git hooks during the commit phase.", "type": "boolean", "default": false }, @@ -19,6 +19,21 @@ "description": "Generate your CHANGELOG for your first release.", "type": "boolean", "default": false + }, + "push": { + "description": "Pushes to the git remote.", + "type": "boolean", + "default": false + }, + "remote": { + "description": "Pushes against git remote repository.", + "type": "string", + "default": "origin" + }, + "baseBranch": { + "description": "Pushes against git base branch.", + "type": "string", + "default": "main" } }, "required": [] diff --git a/yarn.lock b/yarn.lock index 9ff3e47e7..0d684252b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -529,6 +529,15 @@ "@types/yargs" "^15.0.0" chalk "^4.0.0" +"@lerna/child-process@^3.16.5": + version "3.16.5" + resolved "https://registry.yarnpkg.com/@lerna/child-process/-/child-process-3.16.5.tgz#38fa3c18064aa4ac0754ad80114776a7b36a69b2" + integrity sha512-vdcI7mzei9ERRV4oO8Y1LHBZ3A5+ampRKg1wq5nutLsUA4mEBN6H7JqjWOMY9xZemv6+kATm2ofjJ3lW5TszQg== + dependencies: + chalk "^2.3.1" + execa "^1.0.0" + strong-log-transformer "^2.0.0" + "@nodelib/fs.scandir@2.1.3": version "2.1.3" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b" @@ -1938,7 +1947,7 @@ caseless@~0.12.0: resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc" integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw= -chalk@2.4.2, chalk@^2.0.0, chalk@^2.3.0, chalk@^2.4.1, chalk@^2.4.2: +chalk@2.4.2, chalk@^2.0.0, chalk@^2.3.0, chalk@^2.3.1, chalk@^2.4.1, chalk@^2.4.2: version "2.4.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ== @@ -2898,6 +2907,11 @@ dotgitignore@^2.1.0: find-up "^3.0.0" minimatch "^3.0.4" +duplexer@^0.1.1: + version "0.1.2" + resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6" + integrity sha512-jtD6YG370ZCIi/9GTaJKQxWTZD045+4R4hTk/x1UyoqadyJ9x9CgSi1RlVDQF8U2sxLLSnFkCaMihqljHIWgMg== + duplexify@^3.4.2, duplexify@^3.6.0: version "3.7.1" resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.7.1.tgz#2a4df5317f6ccfd91f86d6fd25d8d8a103b88309" @@ -7741,6 +7755,15 @@ strip-json-comments@^3.1.0, strip-json-comments@^3.1.1: resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006" integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig== +strong-log-transformer@^2.0.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/strong-log-transformer/-/strong-log-transformer-2.1.0.tgz#0f5ed78d325e0421ac6f90f7f10e691d6ae3ae10" + integrity sha512-B3Hgul+z0L9a236FAUC9iZsL+nVHgoCJnqCbN588DjYxvGXaXaaFbfmQ/JhvKjZwsOukuR72XbHv71Qkug0HxA== + dependencies: + duplexer "^0.1.1" + minimist "^1.2.0" + through "^2.3.4" + supports-color@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" @@ -7878,7 +7901,7 @@ through2@^4.0.0: dependencies: readable-stream "3" -through@2, "through@>=2.2.7 <3", through@^2.3.6: +through@2, "through@>=2.2.7 <3", through@^2.3.4, through@^2.3.6: version "2.3.8" resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=