Skip to content

Commit

Permalink
feat: store & bump version in version.json
Browse files Browse the repository at this point in the history
Otherwise, projen will override the "version" field after the bump.
  • Loading branch information
Elad Ben-Israel committed May 11, 2020
1 parent 8c40191 commit a861b00
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ tsconfig.json
.jsii
*.d.ts
*.js
!projen.js
!projen.js
!.versionrc.json
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ projen.js
*.ts
tsconfig.json
dist
.versionrc.json
!*.js
!*.d.ts
!.jsii
18 changes: 18 additions & 0 deletions .versionrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"packageFiles": [
{
"filename": "version.json",
"type": "json"
}
],
"bumpFiles": [
{
"filename": "version.json",
"type": "json"
}
],
"commitAll": true,
"scripts": {
"postbump": "yarn projen && git add ."
}
}
30 changes: 30 additions & 0 deletions lib/bump.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Construct } from 'constructs';
import { JsonFile } from './json';
import { NodeProject } from './node-project';
import { Semver } from './semver';

export class BumpScript extends Construct {
constructor(project: NodeProject) {
super(project, 'bump-script');

project.addScripts({
bump: 'standard-version',
});

project.addDevDependencies({
'standard-version': Semver.caret('8.0.0'),
});

project.gitignore.include('.versionrc.json');
project.npmignore.exclude('.versionrc.json');

new JsonFile(this, '.versionrc.json', {
packageFiles: [ { filename: 'version.json', type: 'json' } ],
bumpFiles: [ { filename: 'version.json', type: 'json' } ],
commitAll: true,
scripts: {
postbump: 'yarn projen && git add .',
},
});
}
}
5 changes: 2 additions & 3 deletions lib/jsii-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Semver } from './semver';
import { Eslint } from './eslint';
import { GithubWorkflow } from './github-workflow';
import { Construct } from 'constructs';
import { BumpScript } from './bump';

export interface JsiiProjectOptions extends CommonOptions {
/**
Expand All @@ -11,7 +12,6 @@ export interface JsiiProjectOptions extends CommonOptions {
readonly rootdir?: string;

readonly name: string;
readonly version: string;
readonly description?: string;
readonly repository: string;
readonly authorName: string;
Expand Down Expand Up @@ -67,7 +67,6 @@ export class JsiiProject extends NodeProject {
test: 'echo ok',
compat: 'npx jsii-diff npm:$(node -p "require(\'./package.json\').name")',
package: 'jsii-pacmak',
bump: 'standard-version',
build: 'yarn compile && yarn test && yarn run package',
});

Expand Down Expand Up @@ -125,7 +124,6 @@ export class JsiiProject extends NodeProject {
'jsii-diff': options.jsiiVersion,
'jsii-pacmak': options.jsiiVersion,
'jsii-release': Semver.caret('0.1.5'),
'standard-version': Semver.caret('8.0.0'),
'@types/node': Semver.caret('13.13.5'),
});

Expand Down Expand Up @@ -156,6 +154,7 @@ export class JsiiProject extends NodeProject {
this.npmignore.include('.jsii');

new JsiiBuildWorkflow(this);
new BumpScript(this);
}
}

Expand Down
14 changes: 12 additions & 2 deletions lib/node-project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Project, ProjectOptions } from './project';
import * as fs from 'fs';
import { JsonFile } from './json';
import { Semver } from './semver';
import { IgnoreFile } from './ignore-file';
Expand All @@ -16,7 +17,6 @@ export interface CommonOptions {

export interface NodeProjectOptions extends ProjectOptions, CommonOptions {
readonly name: string;
readonly version?: string;
readonly description?: string;
readonly repository?: string;
readonly authorName?: string;
Expand Down Expand Up @@ -48,7 +48,7 @@ export class NodeProject extends Project {
this.manifest = {
'//': GENERATION_DISCLAIMER,
name: options.name,
version: options.version ?? '0.0.0',
version: this.resolveVersion(),
description: options.description,
main: 'lib/index.js',
repository: !options.repository ? undefined : {
Expand Down Expand Up @@ -150,6 +150,16 @@ export class NodeProject extends Project {
}
}

private resolveVersion() {
const versionFile = `${this.outdir}/version.json`;
if (!fs.existsSync(versionFile)) {
fs.writeFileSync(versionFile, { version: '0.0.0' });
}


return JSON.parse(fs.readFileSync(versionFile, 'utf-8')).version;
}

private addDefaultGitIgnore() {

this.gitignore.exclude(
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"test": "echo ok",
"compat": "npx jsii-diff npm:$(node -p \"require('./package.json').name\")",
"package": "jsii-pacmak",
"bump": "standard-version",
"build": "yarn compile && yarn test && yarn run package"
"build": "yarn compile && yarn test && yarn run package",
"bump": "standard-version"
},
"author": {
"name": "Elad Ben-Israel",
Expand All @@ -31,14 +31,14 @@
"jsii-diff": "^1.5.0",
"jsii-pacmak": "^1.5.0",
"jsii-release": "^0.1.5",
"standard-version": "^8.0.0",
"@types/node": "^13.13.5",
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "^2.19.2",
"eslint": "^6.8.0",
"eslint-import-resolver-node": "^0.3.3",
"eslint-import-resolver-typescript": "^2.0.0",
"eslint-plugin-import": "^2.20.2"
"eslint-plugin-import": "^2.20.2",
"standard-version": "^8.0.0"
},
"peerDependencies": {
"constructs": "^3.0.3"
Expand All @@ -56,4 +56,4 @@
"outdir": "dist",
"targets": {}
}
}
}
1 change: 0 additions & 1 deletion projen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { Semver } = require('./lib/semver');
const project = new JsiiProject({
name: 'projen',
jsiiVersion: Semver.caret('1.5.0'),
version: '0.1.0',
description: 'A new generation of project generators',
repository: 'https://github.com/eladb/projen.git',
authorName: 'Elad Ben-Israel',
Expand Down
3 changes: 3 additions & 0 deletions version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "0.1.1"
}

0 comments on commit a861b00

Please sign in to comment.