Skip to content

Commit

Permalink
feat: ✅ add --push option
Browse files Browse the repository at this point in the history
  • Loading branch information
edbzn committed Dec 5, 2020
1 parent 6ccfbcc commit 2dc9aa6
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/semver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
63 changes: 60 additions & 3 deletions packages/semver/src/builders/version/builder.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,6 +17,50 @@ async function getProjectRoot(context: BuilderContext): Promise<string> {
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
Expand All @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions packages/semver/src/builders/version/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export interface VersionBuilderSchema extends JsonObject {
dryRun?: boolean;
noVerify?: boolean;
firstRelease?: boolean;
push?: boolean;
remote?: string;
baseBranch?: string;
}
21 changes: 18 additions & 3 deletions packages/semver/src/builders/version/schema.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
{
"$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
},
"first-release": {
"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": []
Expand Down
27 changes: 25 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 2dc9aa6

Please sign in to comment.