forked from graphql/graphiql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canary-release.js
96 lines (85 loc) · 2.48 KB
/
canary-release.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable */
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');
function getNewVersion(version, type) {
const gitHash = cp
.spawnSync('git', ['rev-parse', '--short', 'HEAD'])
.stdout.toString()
.trim();
return semver.inc(version, `pre${type}`, true, 'canary-' + gitHash);
}
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 items = listModifiedFiles
.filter(f => f.startsWith('.changeset'))
.map(f => basename(f, '.md'));
return items;
}
async function updateVersions() {
const cwd = process.cwd();
const packages = await getPackages(cwd);
const config = await readConfig(cwd, packages);
const modifiedChangesets = getRelevantChangesets(config.baseBranch);
const changesets = (await readChangesets(cwd)).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!`,
);
process.exit(1);
} else {
const releasePlan = assembleReleasePlan(
changesets,
packages,
config,
[],
false,
);
if (releasePlan.releases.length === 0) {
console.warn(
`Unable to find any relevant package for canary releasing. Please make sure changesets exists!`,
);
process.exit(1);
} else {
for (const release of releasePlan.releases) {
if (release.type !== 'none') {
release.newVersion = getNewVersion(release.oldVersion, release.type);
}
}
await applyReleasePlan(
releasePlan,
packages,
{
...config,
commit: false,
},
false,
true,
);
}
}
}
updateVersions()
.then(() => {
console.info(`Done!`);
})
.catch(err => {
console.error(err);
process.exit(1);
});