Skip to content

Commit

Permalink
Able to release with custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jun 24, 2021
1 parent 7d3e300 commit 46f7efa
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ on:
- master
workflow_dispatch:
inputs:
confirm:
onDemand:
description: 'Are you sure?'
required: true
default: 'yes'
npmTag:
description: 'NPM Tag'
required: true
default: 'alpha'
npmVersionSuffix:
description: 'NPM Version Suffix'
required: true
default: 'alpha-${GITHUB_SHA}'
jobs:
publish-canary:
name: Publish Canary
runs-on: ubuntu-latest
if: github.event.pull_request.head.repo.full_name == github.repository || github.event.inputs.confirm == 'yes'
if: github.event.pull_request.head.repo.full_name == github.repository || github.event.inputs.onDemand == 'yes'
steps:
- name: Checkout Master
uses: actions/checkout@v2
Expand Down Expand Up @@ -50,7 +58,9 @@ jobs:
npm-script: 'yarn release:canary'
changesets: true
env:
ON_DEMAND: ${{github.event.inputs.confirm}}
ON_DEMAND: ${{github.event.inputs.onDemand}}
NPM_TAG: ${{github.event.inputs.npmTag || 'alpha'}}
NPM_VERSION_SUFFIX: ${{github.event.inputs.npmVersionSuffix}}
- name: Publish a message
if: steps.canary.outputs.released
uses: 'kamilkisiela/pr-comment@master'
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"node_modules": true,
"node_modules": false,
"test-lib": true,
"lib": true,
"coverage": true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prerelease": "yarn build",
"prerelease-canary": "yarn build",
"release": "changeset publish",
"release:canary": "(node scripts/canary-release.js && yarn build && yarn changeset publish --tag alpha) || echo Skipping Canary..."
"release:canary": "(node scripts/canary-release.js && yarn build && yarn changeset publish --tag $NPM_TAG) || echo Skipping Canary..."
},
"repository": {
"type": "git",
Expand Down
49 changes: 32 additions & 17 deletions scripts/canary-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ const semver = require('semver');
const cp = require('child_process');
const { basename } = require('path');

const { read: readConfig } = require("@changesets/config");
const readChangesets = require("@changesets/read").default;
const assembleReleasePlan = require("@changesets/assemble-release-plan").default;
const applyReleasePlan = require("@changesets/apply-release-plan").default;
const { getPackages } = require("@manypkg/get-packages");
const { read: readConfig } = require('@changesets/config');
const readChangesets = require('@changesets/read').default;
const assembleReleasePlan = require('@changesets/assemble-release-plan').default;
const applyReleasePlan = require('@changesets/apply-release-plan').default;
const { getPackages } = require('@manypkg/get-packages');

function getNewVersion(version, type) {
const gitHash = cp.spawnSync('git', ['rev-parse', '--short', 'HEAD']).stdout.toString().trim();

return semver.inc(version, `pre${type}`, true, 'alpha-' + gitHash);
let npmVersionSuffix = process.env.NPM_VERSION_SUFFIX;
if (!npmVersionSuffix) {
const gitHash = cp.spawnSync('git', ['rev-parse', '--short', 'HEAD']).stdout.toString().trim();
npmVersionSuffix = `alpha-${gitHash}`;
}
return semver.inc(version, `pre${type}`, true, npmVersionSuffix);
}

function getRelevantChangesets(baseBranch) {
const comparePoint = cp.spawnSync('git', ['merge-base', `origin/${baseBranch}`, 'HEAD']).stdout.toString().trim();
const listModifiedFiles = cp.spawnSync('git', ['diff', '--name-only', comparePoint]).stdout.toString().trim().split('\n');
const comparePoint = cp
.spawnSync('git', ['merge-base', `origin/${baseBranch}`, 'HEAD'])
.stdout.toString()
.trim();
const listModifiedFiles = cp
.spawnSync('git', ['diff', '--name-only', comparePoint])
.stdout.toString()
.trim()
.split('\n');

return listModifiedFiles.filter(f => f.startsWith('.changeset')).map(f => basename(f, '.md'));
}
Expand All @@ -28,7 +38,10 @@ async function updateVersions() {
const config = await readConfig(cwd, packages);
const modifiedChangesets = getRelevantChangesets(config.baseBranch);
const allChangesets = await readChangesets(cwd);
const changesets = process.env.ON_DEMAND ? allChangesets : allChangesets.filter(change => modifiedChangesets.includes(change.id));
const changesets =
process.env.ON_DEMAND === 'yes'
? allChangesets
: allChangesets.filter(change => modifiedChangesets.includes(change.id));

if (changesets.length === 0) {
console.warn(`Unable to find any relevant package for canary publishing. Please make sure changesets exists!`);
Expand Down Expand Up @@ -60,9 +73,11 @@ async function updateVersions() {
}
}

updateVersions().then(() => {
console.info(`Done!`)
}).catch(err => {
console.error(err);
process.exit(1);
});
updateVersions()
.then(() => {
console.info(`Done!`);
})
.catch(err => {
console.error(err);
process.exit(1);
});

0 comments on commit 46f7efa

Please sign in to comment.