Skip to content

Commit

Permalink
feat(semantic-release): adds option to install semantic-release
Browse files Browse the repository at this point in the history
  • Loading branch information
sharvit committed Jan 6, 2019
1 parent d534a01 commit 11570c2
Show file tree
Hide file tree
Showing 16 changed files with 370 additions and 76 deletions.
2 changes: 0 additions & 2 deletions other/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ We haven‘t filled this out yet though. Care to help? See [`contributing.md`](.

## Want to do

- Add option to install [semantic-release](https://github.com/semantic-release/semantic-release)

## Might do

## Won‘t do
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"lodash.kebabcase": "^4.1.1",
"make-dir": "^1.1.0",
"npm-profile": "^4.0.1",
"opener": "^1.5.1",
"request-promise": "^4.2.2",
"uuid": "^3.3.2",
"yeoman-generator": "^3.1.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [Travis CI](https://travis-ci.org) configuration _(optional)_
- [Coveralls](http://coveralls.io) configuration _(optional)_
- Automatically deploy to [npm registry](https://www.npmjs.com) with [Travis CI](https://travis-ci.org) _(optional)_
- Automates versioning releases with [semantic-release](https://github.com/semantic-release/semantic-release) and [commitizen](https://github.com/commitizen/cz-cli) _(optional)_
- Friendly for contributions using [Issue, Pull Request, and Contributing templates](https://github.com/blog/2111-issue-and-pull-request-templates) and some extras _(optional)_

## Installation
Expand Down
25 changes: 15 additions & 10 deletions src/app/generators/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@ export default class extends BaseGenerator {
}
}

async end() {
async install() {
if (this.options.createGithubRepository) {
const {
data: { html_url: url },
} = await createRepository({
name: this.options.projectName,
description: this.options.description,
});

this.log('\n\n');
this.log(`Repository created: ${url}`);
await this._createGithubRepository();
}
}

async _createGithubRepository() {
this.log('Creating github repository...');
const {
data: { html_url: url },
} = await createRepository({
name: this.options.projectName,
description: this.options.description,
});

this.log('\n\n');
this.log(`Repository created: ${url}`);
}
}
21 changes: 19 additions & 2 deletions src/app/generators/github/templates/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ yarn lint
yarn lint --fix
```

<% if (semanticRelease) { %>
Run linter to validate your commit message:

```sh
yarn lint:commit
```
<% } %>

## Committing and Pushing changes

## Committing and Pushing changes

Create a branch and start hacking:
Expand All @@ -48,13 +58,20 @@ git checkout -b my-branch

Commit and push your changes:

<% if (semanticRelease) { %>`generator-node-mdl` uses [commitizen](https://github.com/commitizen/cz-cli) to create commit messages so [semantic-release](https://github.com/semantic-release/semantic-release) can automatically create releases.<% } %>

```sh
git add my/changed/files
git add .
<% if (semanticRelease) { %>
yarn commit
# answer the questions
<% } else { %>
git commit
<% } %>
git push origin my-branch
```
Open this project on [GitHub](https://github.com/<%= githubUsername %>/<%= projectName %>), then click “Compare & pull request”.
Open this project on [GitHub](https://github.com/sharvit/generator-node-mdl), then click “Compare & pull request”.
## Help needed
Expand Down
3 changes: 2 additions & 1 deletion src/app/generators/project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default class extends BaseGenerator {
bower: false,
npm: !hasYarn,
yarn: hasYarn,
skipMessage: true,
});
}

Expand All @@ -82,7 +83,7 @@ export default class extends BaseGenerator {
this.spawnCommandSync('git', [
'commit',
'-m',
'Generated by generator-node-mdl 🔥',
'chore(init): Generated by generator-node-mdl 🔥',
'--quiet',
]);
}
Expand Down
34 changes: 26 additions & 8 deletions src/app/generators/project/templates/_package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "<%= projectName %>",
"version": "0.0.0",
"name": "<%= projectName %>",<% if (semanticRelease) { %>
"version": "0.0.0-semantic-release",<% } else { %>
"version": "0.1.0",<% } %>
"description": "<%= description %>",
"license": "MIT",
"repository": "https://github.com/<%= githubUsername %>/<%= projectName %>",
Expand All @@ -18,7 +19,11 @@
"test:watch": "jest --watch",
"test": "jest --coverage",<% if (coveralls) { %>
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",<% } %>
"lint": "eslint ./src"
"lint": "eslint ./src"<% if (semanticRelease) { %>,
"lint:commit": "commitlint -e",
"lint:commit-travis": "commitlint-travis",
"commit": "git-cz",
"semantic-release": "semantic-release"<% } %>
},
"main": "dist/index.js",
"files": [
Expand All @@ -28,12 +33,17 @@
"devDependencies": {
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/preset-env": "^7.2.0",<% if (semanticRelease) { %>
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"@commitlint/travis-cli": "^7.2.1",<% } %>
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-jest": "^23.6.0",<% if (semanticRelease) { %>
"commitlint-config-cz": "^0.11.0",<% } %>
"babel-plugin-add-module-exports": "^1.0.0",<% if (coveralls) { %>
"coveralls": "^3.0.2",<% } %>
"coveralls": "^3.0.2",<% } %><% if (semanticRelease) { %>
"cz-conventional-changelog": "2.1.0",<% } %>
"eslint": "^5.10.0",
"eslint-config-prettier": "^3.3.0",
"eslint-config-standard": "^12.0.0",
Expand All @@ -45,7 +55,10 @@
"jest": "^23.6.0",
"prettier": "^1.15.3",
"rimraf": "^2.6.2"
},
},<% if (semanticRelease) { %>
"optionalDependencies": {
"semantic-release": "^15.13.1"
},<% } %>
"jest": {
"testEnvironment": "node",
"transform": {
Expand All @@ -54,6 +67,11 @@
"collectCoverageFrom": [
"src/**/*.js"
]
},
},<% if (semanticRelease) { %>
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},<% } %>
"keywords": []
}
3 changes: 2 additions & 1 deletion src/app/generators/project/templates/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

> <%= description %>
[![Package Version](https://img.shields.io/npm/v/<%= projectName %>.svg?style=flat-square)](https://www.npmjs.com/package/<%= projectName %>)
[![Package Version](https://img.shields.io/npm/v/<%= projectName %>.svg?style=flat-square)](https://www.npmjs.com/package/<%= projectName %>)<% if (semanticRelease) { %>
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)<% } %>
[![Downloads Status](https://img.shields.io/npm/dm/<%= projectName %>.svg?style=flat-square)](https://npm-stat.com/charts.html?package=<%= projectName %>&from=2016-04-01)
<% if (travisCI) { %>[![Build Status: Linux](https://img.shields.io/travis/<%= githubUsername %>/<%= projectName %>/master.svg?style=flat-square)](https://travis-ci.org/<%= githubUsername %>/<%= projectName %>)<% } %>
<% if (coveralls) { %>[![Coverage Status](https://coveralls.io/repos/github/<%= githubUsername %>/<%= projectName %>/badge.svg?branch=master)](https://coveralls.io/github/<%= githubUsername %>/<%= projectName %>?branch=master)<% } %>
Expand Down
56 changes: 45 additions & 11 deletions src/app/generators/travis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,26 @@ export default class extends BaseGenerator {
async install() {
if (process.env.NODE_ENV === 'test') return;

const { repository, npmDeploy, npmToken } = this.props;
this.log('\nInstalling TravisCI...\n');

const { npmDeploy, semanticRelease } = this.options;

this._installTravis();

if (npmDeploy) {
this._installTravisNpmToken();

if (semanticRelease) {
this._installTravisGithubToken();
}
}
}

_installTravis() {
const { repository } = this.options;

this.log('repository');
this.log(repository);

this.spawnCommandSync('gem', [
'install',
Expand All @@ -23,16 +42,31 @@ export default class extends BaseGenerator {
]);
this.spawnCommandSync('travis', ['login', '--auto']);
this.spawnCommandSync('travis', ['enable', '-r', repository]);
}

if (npmDeploy) {
this.spawnCommandSync('travis', [
'env',
'set',
'NPM_TOKEN',
npmToken,
'-r',
repository,
]);
}
_installTravisNpmToken() {
const { repository, npmToken } = this.options;

this.spawnCommandSync('travis', [
'env',
'set',
'NPM_TOKEN',
npmToken,
'-r',
repository,
]);
}

_installTravisGithubToken() {
const { repository, githubToken } = this.options;

this.spawnCommandSync('travis', [
'env',
'set',
'GH_TOKEN',
githubToken,
'-r',
repository,
]);
}
}
22 changes: 17 additions & 5 deletions src/app/generators/travis/templates/_travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ language: node_js
notifications:
email: false
node_js:
- '10'
- '8'
- '6'
script:
- 10
- 8
- 6
script:<% if (semanticRelease) { %>
- yarn lint:commit-travis<% } %>
- yarn lint
- yarn test
- yarn build
<% if (coveralls) { %>after_success: yarn coveralls<% } %><% if (npmDeploy) { %>
<% if (coveralls) { %>after_success: yarn coveralls<% } if (npmDeploy && semanticRelease) { %>
jobs:
include:
- stage: release
if: branch = master
node_js: 10
deploy:
provider: script
skip_cleanup: true
script:
- yarn semantic-release
<% } else if (npmDeploy) { %>
deploy:
skip_cleanup: true
provider: npm
Expand Down
58 changes: 58 additions & 0 deletions src/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ describe('prompts', () => {
return runAppGenerator()
.withPrompts({
createGithubRepository: false,
npmDeploy: false,
githubUsername: 'some-username',
githubPassword: 'some-password',
projectName: 'some-project-name',
Expand Down Expand Up @@ -273,13 +274,36 @@ describe('prompts', () => {
.withPrompts({
travisCI: true,
npmDeploy: true,
semanticRelease: false,
npmUsername: 'some-username',
npmPassword: 'some-password',
})
.then(() => {
assert.fileContent('.travis.yml', 'deploy:');
assert.fileContent('.travis.yml', 'provider: npm');
assert.fileContent('.travis.yml', 'api_key: $NPM_TOKEN');

assert.noFileContent('.travis.yml', 'provider: script');
assert.noFileContent('.travis.yml', 'yarn semantic-release');

assert.noFileContent(
'package.json',
'"version": "0.0.0-semantic-release"'
);
assert.noFileContent(
'package.json',
'"semantic-release": "semantic-release"'
);
assert.noFileContent('package.json', '@commitlint/cli');
assert.noFileContent(
'package.json',
'@commitlint/config-conventional'
);
assert.noFileContent('package.json', '@commitlint/travis-cli');
assert.noFileContent(
'package.json',
'"path": "./node_modules/cz-conventional-changelog"'
);
});
});

Expand All @@ -293,5 +317,39 @@ describe('prompts', () => {
assert.noFileContent('.travis.yml', 'deploy');
});
});

test('npmDeploy with semanticRelease', () => {
return runAppGenerator()
.withPrompts({
travisCI: true,
npmDeploy: true,
semanticRelease: true,
npmUsername: 'some-username',
npmPassword: 'some-password',
githubUsername: 'some-username',
githubPassword: 'some-password',
})
.then(() => {
assert.fileContent('.travis.yml', 'deploy:');
assert.fileContent('.travis.yml', 'provider: script');
assert.fileContent('.travis.yml', 'yarn semantic-release');

assert.fileContent(
'package.json',
'"version": "0.0.0-semantic-release"'
);
assert.fileContent(
'package.json',
'"semantic-release": "semantic-release"'
);
assert.fileContent('package.json', '@commitlint/cli');
assert.fileContent('package.json', '@commitlint/config-conventional');
assert.fileContent('package.json', '@commitlint/travis-cli');
assert.fileContent(
'package.json',
'"path": "./node_modules/cz-conventional-changelog"'
);
});
});
});
});
2 changes: 2 additions & 0 deletions src/app/lib/__mocks__/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export const createRepository = jest.fn(async ({ name, description }) => ({
html_url: 'some-html_url',
},
}));

export const createGithubToken = jest.fn(async () => 'some-token');
Loading

0 comments on commit 11570c2

Please sign in to comment.