diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5b59df2..a930841 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -69,8 +69,10 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} run: | - for path in ${{ steps.get-workspaces.outputs.ws }}; do - cd $path + topdir=$(pwd) + for workspace in ${{ steps.get-workspaces.outputs.ws }}; do + echo "Potentially going to version and release $workspace" + cd $workspace mkdir .git PCKG_NAME=`node -pe "require('./package.json').name"` @@ -80,38 +82,35 @@ jobs: if [ "$CUR_VERSION_NO" != "0.0.0" ]; then FROM_PARAM="--from ${PCKG_NAME}_v${CUR_VERSION_NO}" fi - + VERSION=`npx auto version $FROM_PARAM` if [ ! -z "$VERSION" ]; then + echo "::notice title=✅ Detected $VERSION version change for $PCKG_NAME::Bumping version" npx auto changelog --base-branch ${{ steps.get-workspaces.outputs.branch }} $FROM_PARAM - - unpushed_commits=$(git log origin/${{ steps.get-workspaces.outputs.branch }}..${{ steps.get-workspaces.outputs.branch }}) - - if [ -z "$unpushed_commits" ]; then - echo "No changelog was generated for $PCKG_NAME" - else - npm version $VERSION -m "chore: bump release version to %s [skip ci]" - NEW_VERSION_NO=`node -pe "require('./package.json').version"` - git tag -d v$NEW_VERSION_NO - NEW_TAG=${PCKG_NAME}_v$NEW_VERSION_NO - echo "Going to create a new release for $NEW_TAG" - git add -A - git commit -m "chore: release v$NEW_VERSION_NO [skip ci]" - git tag -a $NEW_TAG -m "chore: tag v$NEW_VERSION_NO [skip ci]" - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY" HEAD:${{ steps.get-workspaces.outputs.branch }} --follow-tags - npx auto release --use-version $NEW_TAG $FROM_PARAM --base-branch ${{ steps.get-workspaces.outputs.branch }} - IS_PRIVATE=`node -pe "require('./package.json').private"` - if [ "$IS_PRIVATE" != "true" ]; then - npm publish ./dist - fi + npm version $VERSION -m "chore: bump release version to %s [skip ci]" + NEW_VERSION_NO=`node -pe "require('./package.json').version"` + git tag -d v$NEW_VERSION_NO + NEW_TAG=${PCKG_NAME}_v$NEW_VERSION_NO + echo "Going to create a new release for $NEW_TAG" + git add -A + git commit -m "chore: release v$NEW_VERSION_NO [skip ci]" + git tag -a $NEW_TAG -m "chore: tag v$NEW_VERSION_NO [skip ci]" + git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY" HEAD:${{ steps.get-workspaces.outputs.branch }} --follow-tags + npx auto release --use-version $NEW_TAG $FROM_PARAM --base-branch ${{ steps.get-workspaces.outputs.branch }} + IS_PRIVATE=`node -pe "require('./package.json').private"` + rm -rf .git + if [ "$IS_PRIVATE" != "true" ]; then + cd $topdir + echo "publishing $workspace/dist" + npm publish ./$workspace/dist + echo "::notice title=🚀 ${PCKG_NAME} v$NEW_VERSION_NO::Package versioned and published" fi else - echo 'Auto versioning failed.' + echo "::notice title=Versioning of $PCKG_NAME skipped::No relevant changes detected." exit 1 fi - rm -rf .git - cd - + cd $topdir done - if: ${{ always() }} diff --git a/.vscode/launch.json b/.vscode/launch.json index bc4f10b..fa0fc17 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,32 @@ "changelog", "-d", "--from", - "@restfulhead/npm-auto-plugin-example-test-package_v0.0.2" + "@restfulhead/npm-auto-plugin-example-test-package_v0.0.3" + ], + "internalConsoleOptions": "openOnSessionStart", + "sourceMaps": true, + "outputCapture": "std", + "smartStep": true, + "skipFiles": [ + "/**", + "node_modules/**" + ], + "resolveSourceMapLocations": [ + "${workspaceFolder}/**", + "!**/node_modules/**" + ], + "envFile": "${workspaceFolder}/.env" + }, + { + "name": "Auto version", + "type": "node", + "request": "launch", + "cwd": "${workspaceFolder}/packages/example-test-package", + "program": "${workspaceFolder}/node_modules/auto/dist/bin/auto", + "args": [ + "version", + "--from", + "@restfulhead/npm-auto-plugin-example-test-package_v0.0.3" ], "internalConsoleOptions": "openOnSessionStart", "sourceMaps": true, @@ -36,7 +61,7 @@ "args": [ "${relativeFile}", "--config", - "${workspaceFolder}/packages/filter-by-workspace-path/package.json", + "${workspaceFolder}/packages/filter-by-workspace-path/jest.debug.config.js", "--testTimeout", "300000", "--no-cache", diff --git a/config/.eslintrc.js b/config/.eslintrc.js index 72587bc..1b03ed3 100644 --- a/config/.eslintrc.js +++ b/config/.eslintrc.js @@ -17,8 +17,6 @@ module.exports = { rules: { 'unused-imports/no-unused-imports': 'error', 'require-await': 'error', - }, - settings: { '@typescript-eslint/adjacent-overload-signatures': 'error', '@typescript-eslint/array-type': ['error'], '@typescript-eslint/await-thenable': 'error', @@ -90,6 +88,10 @@ module.exports = { format: ['camelCase'], leadingUnderscore: 'forbid', }, + { + selector: 'import', + format: ['camelCase', 'PascalCase'], + }, { selector: 'default', format: ['camelCase'], diff --git a/packages/filter-by-workspace-path/README.md b/packages/filter-by-workspace-path/README.md index a7cfda9..7d28cba 100644 --- a/packages/filter-by-workspace-path/README.md +++ b/packages/filter-by-workspace-path/README.md @@ -24,16 +24,16 @@ However, carefully read the following caveats section. ## Caveats -* Note that this plugin also ommits commits that +* Note that this plugin also omits commits that * do not have a related pull request (e.g. pushed directly to the release branch) * belong to pull requests that has the `skip-release` label attached * have `[skip ci]` in their commit message -* By default, `auto version` seems to set the version to `patch` instead of `noVersion` even if all commits were filtered out. - * Therefore, to detect if there were no changes in this case, you have to use the `auto changelog` command instead and then check whether - a changelog was actually generated. :-/ +* This plugin modifies the behavior of `auto version`. By default, it seems to set the version to `patch` instead of `noVersion` even if all + commits were omitted. With this plugin, `noVersion` is returned if there are no relevant commits. * While the title in the GitHub release notes is correct, the version is missing in the `CHANGELOG.md` file for currently unknown reasons * You can't use the `shipit` command, because for example, the version in each package should only contain the version number - (e.g. `v1.0.0`), but the tag and Github release must include the package name to avoid ambigious release names/tags. + (e.g. `v1.0.0`), but the tag and Github release must include the package name to avoid ambigious release names/tags. Maybe this + customization can be added to the plugin in future. (Contributions welcome!) * However, you can use individual commands to make this work. To see how to setup a release process that takes all of this into account, take a look at the [release.yml GitHub action](../../.github/workflows/release.yml) in this repository. * If you create merge-commits against your release/target branch, then changes to files in your release/target branch become part of the diff --git a/packages/filter-by-workspace-path/src/index.ts b/packages/filter-by-workspace-path/src/index.ts index efd776c..f8bfb3c 100644 --- a/packages/filter-by-workspace-path/src/index.ts +++ b/packages/filter-by-workspace-path/src/index.ts @@ -1,7 +1,6 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { Auto, IExtendedCommit, ILogger, IPlugin } from '@auto-it/core' import { execSync } from 'child_process' import * as path from 'path' +import { Auto, IExtendedCommit, ILogger, IPlugin, SEMVER } from '@auto-it/core' function shouldOmitCommit(currentDir: string, currentWorkspace: string, commit: IExtendedCommit, logger: ILogger): boolean { if (!commit.pullRequest) { @@ -31,16 +30,29 @@ export default class FilterByWorkspacePathPlugin implements IPlugin { /** The name of the plugin */ name = 'filter-by-workspace-path-plugin' - apply(auto: Auto) { + apply(auto: Auto): void { const currentDir = path.resolve('.') const npmResult = execSync('npm ls --omit=dev --depth 1 -json', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'ignore'] }) const workspaceDeps: any = JSON.parse(npmResult).dependencies + // eslint-disable-next-line @typescript-eslint/no-magic-numbers const currentWorkspace = workspaceDeps[Object.keys(workspaceDeps)[0] as any].resolved.substring(11) auto.hooks.onCreateLogParse.tap(this.name, (logParse) => { - logParse.hooks.omitCommit.tap(this.name, (commit) => { - return shouldOmitCommit(currentDir, currentWorkspace, commit, auto.logger) === true ? true : undefined - }) + logParse.hooks.omitCommit.tap(this.name, (commit) => + shouldOmitCommit(currentDir, currentWorkspace, commit, auto.logger) ? true : undefined + ) + }) + + auto.hooks.onCreateRelease.tap(this.name, (release) => { + const origGetVersion = release.getSemverBump.bind(release) + release.getSemverBump = async (from: string, to?: string): Promise => { + const commits = await release.getCommits(from, to) + if (commits.length === 0) { + auto.logger.verbose.log('No commits found. Skipping release.') + return SEMVER.noVersion + } + return origGetVersion(from, to) + } }) } } diff --git a/packages/filter-by-workspace-path/test/index.spec.ts b/packages/filter-by-workspace-path/test/index.spec.ts index 3b89386..831a445 100644 --- a/packages/filter-by-workspace-path/test/index.spec.ts +++ b/packages/filter-by-workspace-path/test/index.spec.ts @@ -1,12 +1,11 @@ +import * as path from 'path' import Auto, { SEMVER } from '@auto-it/core' import makeCommitFromMsg from '@auto-it/core/dist/__tests__/make-commit-from-msg' import LogParse from '@auto-it/core/dist/log-parse' -import { makeChangelogHooks, makeHooks, makeLogParseHooks, makeReleaseHooks } from '@auto-it/core/dist/utils/make-hooks' -import FilterByPathPlugin from '../src' -import * as path from 'path' +import { makeHooks, makeLogParseHooks, makeReleaseHooks } from '@auto-it/core/dist/utils/make-hooks' import createLog from '@auto-it/core/dist/utils/logger' import Release from '@auto-it/core/dist/release' -import Changelog from '@auto-it/core/dist/changelog' +import FilterByPathPlugin from '../src' const setup = () => { const plugin = new FilterByPathPlugin() @@ -14,12 +13,14 @@ const setup = () => { const logger = createLog() const logParseHooks = makeLogParseHooks() const releaseHooks = makeReleaseHooks() - const changeLogHooks = makeChangelogHooks() plugin.apply({ hooks, logger } as Auto) hooks.onCreateLogParse.call({ hooks: logParseHooks } as LogParse) - hooks.onCreateRelease.call({ hooks: releaseHooks } as Release) - hooks.onCreateChangelog.call({ hooks: changeLogHooks } as Changelog, { bump: SEMVER.major }) + hooks.onCreateRelease.call({ + hooks: releaseHooks, + getSemverBump: (from: string, to?: string): Promise => Promise.resolve(SEMVER.patch), + } as Release) + return logParseHooks }