forked from projen/projen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.ts
49 lines (41 loc) · 1.53 KB
/
version.ts
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
import { JsonFile } from './json';
import { NodeProject } from './node-project';
import { Semver } from './semver';
import * as fs from 'fs-extra';
const VERSION_FILE = 'version.json';
export class Version {
constructor(project: NodeProject) {
project.addScript('no-changes', '(git log --oneline -1 | grep -q "chore(release):") && echo "No changes to release."');
project.addScript('bump', 'yarn --silent no-changes || standard-version');
project.addScript('release', 'yarn --silent no-changes || (yarn bump && git push --follow-tags origin master)');
project.addDevDependencies({
'standard-version': Semver.caret('9.0.0'),
});
project.npmignore.exclude('/.versionrc.json');
project.gitignore.include(VERSION_FILE);
new JsonFile(project, '.versionrc.json', {
obj: {
packageFiles: [ { filename: VERSION_FILE, type: 'json' } ],
bumpFiles: [ { filename: VERSION_FILE, type: 'json' } ],
commitAll: true,
scripts: {
// run projen after release to update package.json
postbump: 'yarn projen && git add .',
},
},
});
}
/**
* Returns the current version of the project.
*/
public resolveVersion(outdir: string) {
const versionFile = `${outdir}/${VERSION_FILE}`;
if (!fs.existsSync(versionFile)) {
if (!fs.existsSync(outdir)) {
fs.mkdirpSync(outdir);
}
fs.writeFileSync(versionFile, JSON.stringify({ version: '0.0.0' }));
}
return JSON.parse(fs.readFileSync(versionFile, 'utf-8')).version;
}
}