diff --git a/README.md b/README.md index 748baa2..fba78fe 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,25 @@ Optionnaly additional files can be added to also have their version bumped } ``` -Note that the path for the `--files` option is relative to your current root directory +> **NOTE** +> the path for the `--files` option is relative to your current root directory Then, to publish a new version execute the following command: + ```bash npm run release ``` +By default, this will publish a patch version, if you want to control the semver option you can pass an optional 'patch', 'minor' or 'major' type argument. + +**Example :** + +```bash +npm run release -- --type=minor +``` + +> **NOTE** +> The double `--` is necessary, this is how npm script propagates its arguments [npm-image]: https://badge.fury.io/js/mcfly-semantic-release.svg diff --git a/bin/mcfly-semantic-release.js b/bin/mcfly-semantic-release.js index 822e722..cc9b020 100644 --- a/bin/mcfly-semantic-release.js +++ b/bin/mcfly-semantic-release.js @@ -7,7 +7,7 @@ const args = require('yargs').argv; const chalk = require('chalk'); const changelogScript = require('../lib/changelog-script'); const gitHelper = require('../lib/githelper'); -const githubHelper = require('../lib/githubHelper'); +const githubHelper = require('mcfly-github'); const inquirer = require('inquirer'); const path = require('path'); const versionHelper = require('../lib/versionHelper'); diff --git a/lib/githubHelper.js b/lib/githubHelper.js deleted file mode 100644 index 2bbb5fa..0000000 --- a/lib/githubHelper.js +++ /dev/null @@ -1,219 +0,0 @@ -'use strict'; -global.Promise = require('bluebird'); -var GitHubApi = require('github'); -var jsonfile = require('jsonfile'); -var fs = require('fs'); -var execAsync = Promise.promisify(require('child_process').exec); -/** - * Gets the git user name - * @returns {Promise} The username - */ -var getUsername = function() { - return execAsync('git config user.email') - .then((username) => { - if (username && username.length > 0) { - username = username.trim(); - } - return username; - }); -}; -/** - * Gets a connected github client - * @param {String} username The github username - * @param {String} password The github password - * @returns {Object} The github client - */ -var buildClient = function(username, password) { - var github = new GitHubApi({ - // required - version: '3.0.0', - // optional - debug: false, - protocol: 'https', - host: 'api.github.com', // should be api.github.com for GitHub - pathPrefix: '', // for some GHEs; none for GitHub - timeout: 5000 - //headers: { - // 'user-agent': 'Some cool app' // GitHub is happy with a unique user agent - //} - }); - if (username && password) { - github.authenticate({ - type: 'basic', - username: username, - password: password - }); - return github; - } - if (process.env.GITHUB_TOKEN) { - - github.authenticate({ - type: 'oauth', - token: process.env.GITHUB_TOKEN - }); - return github; - } - - if (fs.existsSync('./files/testAuth.json')) { - var oauth = jsonfile.readFileSync('./files/testAuth.json'); - - github.authenticate({ - type: 'oauth', - token: oauth.token - }); - return github; - } - - return github; -}; - -/** - * Checks the validity of the credentials - * @param {Object} github The github client - * @returns {Promise} The github client - */ -var checkClient = function(github) { - return new Promise(function(resolve, reject) { - github.misc.getRateLimit({}, function(err, res) { - if (err) { - reject(err); - } else { - resolve(github); - } - }); - }); -}; - -var getClient = function(username, password) { - var github = buildClient(username, password); - return checkClient(github); -}; - -/** - * Gets a specific repo - * @param {Object} github The github client - * @param {Object} param An object with the following properties: user, repo - * @returns {Promise} The resulting repository - */ -var getRepo = function(github, param) { - - var user = param.user; - var repo = param.repo; - - if (repo.includes('/')) { - var params = param.repo.split('/'); - user = params[0]; - repo = params[1]; - } - - return Promise.fromCallback(function(callback) { - return github.repos.get({ - user: user, - repo: repo - }, callback); - }); -}; - -/** - * Gets all the repo - * @param {Object} github The github client - * @param {Object} param An object with the following properties: per_page, page - * @returns {Promise} An array of the repos found - */ -var getAllRepos = function(github, param) { - return Promise.fromCallback(function(callback) { - return github.repos.getAll({ - per_page: param.per_page, - page: param.page - }, callback); - }); -}; - -/** - * Gets the content of package.json - * @param {Object} github The github client - * @param {Object} param An object with the following properties: user, repo - * @returns {Promise} An array of the repos found - */ -var getPackageJson = function(github, param) { - var user = param.user; - var repo = param.repo; - - if (repo.includes('/')) { - var params = param.repo.split('/'); - user = params[0]; - repo = params[1]; - } - return Promise.fromCallback(function(callback) { - return github.repos.getContent({ - user: user, - repo: repo, - path: 'package.json' - }, callback); - }) - .then(response => { - var b64string = response.content; - var buf = new Buffer(b64string, 'base64'); - var packageJson = JSON.parse(buf.toString()); - return packageJson; - }) - .catch(err => null); -}; - -/** - * Creates a token file - * @param {String} username The github username - * @param {String} password The github password - * @param {Strong} tokenName The name of the token (visible in Person access tokens on https://github.com/settings/tokens) - * @param {String} filename The filename to store the resulting token - * @returns {Promise} The result of the api call to create the token - */ -var createTokenFile = function(username, password, tokenName, filename) { - var github = buildClient(username, password); - return Promise.fromCallback(function(callback) { - return github.authorization.create({ - scopes: ['user', 'public_repo', 'repo', 'repo:status', 'gist'], - note: tokenName - }, callback); - - }) - .then(res => { - var jsonObject = { - token: res.token - }; - jsonfile.writeFileSync(filename, jsonObject, { - spaces: 2 - }); - return res; - }); - -}; - -var createRelease = function(param) { - return new Promise(function(resolve, reject) { - param.github.repos.createRelease({ - user: param.owner, - repo: param.repo, - tag_name: param.nextVersion, - name: 'v' + param.nextVersion, - body: param.changelogContent - }, function(err, res) { - if (err) { - reject(err); - } else { - resolve(res); - } - }); - }); -}; - -module.exports = { - getUsername, - buildClient, - getClient, - getRepo, - getAllRepos, - getPackageJson, - createTokenFile, - createRelease -}; diff --git a/package.json b/package.json index e8af770..d45797b 100644 --- a/package.json +++ b/package.json @@ -1,59 +1,60 @@ { - "name": "mcfly-semantic-release", - "version": "1.0.7", - "description": "A cli tool to bump version and publish to github", - "bin": { - "mcfly-semantic-release": "./bin/mcfly-semantic-release.js" - }, - "dependencies": { - "bluebird": "^3.4.0", - "chalk": "^1.1.3", - "github": "^1.1.2", - "inquirer": "^1.0.3", - "jsonfile": "^2.3.1", - "lodash": "4.13.1", - "memory-stream": "0.0.3", - "node-jsxml": "^0.7.0", - "semver": "^5.1.0", - "simple-git": "^1.37.0", - "strip-json-comments": "^2.0.1", - "yargs": "^4.7.1" - }, - "devDependencies": { - "chai": "^3.5.0", - "coveralls": "^2.11.9", - "eslint": "^2.10.2", - "eslint-plugin-json": "^1.2.0", - "eslint-plugin-nodeca": "^1.0.3", - "istanbul": "^0.4.3", - "mocha": "^2.5.3", - "sinon": "^1.17.4", - "sinon-chai": "^2.8.0" - }, - "scripts": { - "eslint": "eslint .", - "lint": "npm run eslint", - "pretest": "npm run eslint", - "test": "npm run mocha", - "mocha": "istanbul cover --root . --include-all-sources -x **/coverage/** -x **/client/** -x **/files/** -x **/node_modules/** --dir ./coverage/mocha --report text --report text-summary --report lcov --print none _mocha -- test/mocha/**/*.spec.js --reporter spec --timeout 10000", - "mocha:watch": "mocha test/mocha/**/*.spec.js -R nyan -w --timeout 10000", - "release": "node bin/mcfly-semantic-release.js" - }, - "files": [ - "lib", - "bin" - ], - "engines": { - "node": ">= 4.2.1 < 5" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/mcfly-io/mcfly-semantic-release.git" - }, - "author": "", - "license": "MIT", - "bugs": { - "url": "https://github.com/mcfly-io/mcfly-semantic-release/issues" - }, - "homepage": "https://github.com/mcfly-io/mcfly-semantic-release#readme" -} \ No newline at end of file + "name": "mcfly-semantic-release", + "version": "1.0.7", + "description": "A cli tool to bump version and publish to github", + "bin": { + "mcfly-semantic-release": "./bin/mcfly-semantic-release.js" + }, + "dependencies": { + "bluebird": "^3.4.0", + "chalk": "^1.1.3", + "github": "^1.1.2", + "inquirer": "^1.0.3", + "jsonfile": "^2.3.1", + "lodash": "4.13.1", + "mcfly-github": "^1.0.2", + "memory-stream": "0.0.3", + "node-jsxml": "^0.7.0", + "semver": "^5.1.0", + "simple-git": "^1.37.0", + "strip-json-comments": "^2.0.1", + "yargs": "^4.7.1" + }, + "devDependencies": { + "chai": "^3.5.0", + "coveralls": "^2.11.9", + "eslint": "^2.10.2", + "eslint-plugin-json": "^1.2.0", + "eslint-plugin-nodeca": "^1.0.3", + "istanbul": "^0.4.3", + "mocha": "^2.5.3", + "sinon": "^1.17.4", + "sinon-chai": "^2.8.0" + }, + "scripts": { + "eslint": "eslint .", + "lint": "npm run eslint", + "pretest": "npm run eslint", + "test": "npm run mocha", + "mocha": "istanbul cover --root . --include-all-sources -x **/coverage/** -x **/client/** -x **/files/** -x **/node_modules/** --dir ./coverage/mocha --report text --report text-summary --report lcov --print none _mocha -- test/mocha/**/*.spec.js --reporter spec --timeout 10000", + "mocha:watch": "mocha test/mocha/**/*.spec.js -R nyan -w --timeout 10000", + "release": "node bin/mcfly-semantic-release.js" + }, + "files": [ + "lib", + "bin" + ], + "engines": { + "node": ">= 4.2.1 < 5" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/mcfly-io/mcfly-semantic-release.git" + }, + "author": "", + "license": "MIT", + "bugs": { + "url": "https://github.com/mcfly-io/mcfly-semantic-release/issues" + }, + "homepage": "https://github.com/mcfly-io/mcfly-semantic-release#readme" +} diff --git a/test/mocha/githubHelper.spec.js b/test/mocha/githubHelper.spec.js deleted file mode 100644 index 26e12eb..0000000 --- a/test/mocha/githubHelper.spec.js +++ /dev/null @@ -1,118 +0,0 @@ -'use strict'; - -var githubHelper = require('../../lib/githubHelper'); -var expect = require('chai').expect; - -describe('githubHelper', () => { - - describe('getUsername()', () => { - // disable this test when running on travis - if (process.env.CI !== 'true') { - it('should succeed', (done) => { - githubHelper.getUsername() - .then(username => { - expect(username).not.to.be.null; - done(); - }) - .catch(done); - }); - } - - }); - - describe('buildClient()', () => { - it('with json token file should succeed', () => { - var github = githubHelper.buildClient(); - expect(github.auth.token).to.exist; - }); - - it('with invalid token should throw an error', (done) => { - var github = githubHelper.buildClient('totoxxx', 'totoyyyy'); - githubHelper.getAllRepos(github, {}) - .catch(err => { - expect(err).to.exist; - done(); - }); - }); - }); - - describe('getClient()', () => { - it('with invalid credentials should fail', (done) => { - githubHelper.getClient('xxxxx', 'yyyyyy') - .then(() => { - done(new Error('Did not throw an error')); - }) - .catch(err => { - expect(err).to.exist; - done(); - }); - }); - it('with valid credential returns the client', (done) => { - githubHelper.getClient() - .then((github) => { - expect(github.auth.token).to.exist; - done(); - }) - .catch(done); - }); - }); - - describe('getAllRepos()', () => { - it('should succeed', (done) => { - var github = githubHelper.buildClient(); - githubHelper.getAllRepos(github, {}) - .then(repos => { - // check that we get a full page of 30 results (default paging for the gitub api) - expect(repos.length).to.be.equal(30); - done(); - }) - .catch(done); - }); - - }); - - describe('getRepo()', () => { - it('should succeed', (done) => { - var github = githubHelper.buildClient(); - var repoName = 'mcfly-io/ngux-loader'; - githubHelper.getRepo(github, { - repo: repoName - }) - .then(repo => { - expect(repo.full_name).to.equal(repoName); - done(); - }) - .catch(done); - }); - }); - - describe('getPackageJson()', () => { - it('when package.json exist it should succeed', (done) => { - var github = githubHelper.buildClient(); - var repoName = 'mcfly-io/ngux-loader'; - githubHelper.getPackageJson(github, { - repo: repoName - }) - .then(packageJson => { - expect(packageJson).to.be.an('object'); - expect(packageJson.name).to.equal('ngux-loader'); - done(); - }) - .catch(done); - }); - - it('when package.json does not exist it should return null', (done) => { - var github = githubHelper.buildClient(); - var repoName = 'mcfly-io/wiki'; - githubHelper.getPackageJson(github, { - repo: repoName - }) - .then(packageJson => { - expect(packageJson).to.be.null; - done(); - }) - .catch(done); - }); - }); - -});