From e5553ac3075f27d221e9a4bef29e242d134bc39a Mon Sep 17 00:00:00 2001 From: Ronny Unger Date: Wed, 15 Jan 2020 15:36:27 +0000 Subject: [PATCH 1/2] set preferred line endings --- .gitattributes | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..cad1c32 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +# Set the default behavior, in case people don't have core.autocrlf set +* text=auto + +# Require Unix line endings +* text eol=lf From 1ea4280718e7d166a1f23f476b57aa7179d286cf Mon Sep 17 00:00:00 2001 From: Ronny Unger Date: Wed, 15 Jan 2020 17:07:57 +0100 Subject: [PATCH 2/2] fixed some formatting and documentation issues --- README.md | 21 ++++++++++++++------- bin/branch.js | 43 +++++++++++++++++++++---------------------- package.json | 2 +- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index d7d6885..99f3f6e 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,25 @@ [![Build Status](https://travis-ci.org/silentport/branch.svg?branch=master)](https://travis-ci.org/silentport/branch) undefined -> Ensure that script commands in package. JSON are only allowed to execute on specified git branches -### usage ->Maybe you should cooperate with cross-env +Ensure that script commands in `package.json` are only allowed to execute on specified git branches -`npm i check-git-branch -D` -`npm i cross-env -D` +### Installation -### package.json in your project +This module should be installed as one of your project's `devDependencies`: + +`npm install --save-dev check-git-branch` + +You might also want to consider using `cross-env`: + +`npm install --save-dev cross-env` + +### Usage + +example of the `scripts` section in your project's package.json: ```javascript "scripts": { "build": "branch master && cross-env NODE_ENV=production node build/build.js", "build-test": "branch dev && cross-env NODE_ENV=sandbox node build/build.js" }, -``` \ No newline at end of file +``` diff --git a/bin/branch.js b/bin/branch.js index 33128a9..2a961c2 100644 --- a/bin/branch.js +++ b/bin/branch.js @@ -1,49 +1,48 @@ #!/usr/bin/env node -const chalk = require ('chalk'); -const util = require ('util'); +const chalk = require('chalk'); +const util = require('util'); const log = console.log; const error = chalk.bold.red; const warn = chalk.bold.yellow; const success = chalk.bold.green; -const exec = util.promisify (require ('child_process').exec); +const exec = util.promisify(require('child_process').exec); const groupStart = () => { console.group("---------------------------------------------------"); -} +}; const groupEnd = () => { console.groupEnd(); log("---------------------------------------------------\n"); -} +}; const execCommand = async (cmd, b) => { - const ls = await exec (cmd); - const current = ls.stdout.trim (); - + const ls = await exec(cmd); + const current = ls.stdout.trim(); + groupStart(); - log (chalk.green ('Current Branch: ') + `${current}\n`); + log(chalk.green('Current Branch: ') + `${current}\n`); if (!b) { - log(warn('Warning: ') - + - `Maybe you should assign a specific branch for current + log(warn('Warning: ') + + + `Maybe you should assign a specific branch for current script, such as use ${chalk.green('branch master && node xxx')} in scripts - of your package.json `); + of your package.json`); groupEnd(); - return ; + return; } if (current !== b) { - log ( - error (`Error: `) + - `The script you try to execute is only available in - ${chalk.green(b)} branch, but current branch is ${chalk.blue(current)}` + log(error('Error: ') + + + `The script you try to execute is only available in + ${chalk.green(b)} branch, but current branch is ${chalk.blue(current)}` ); groupEnd(); - process.exitCode = 1; - process.exit (); + process.exit(1); } - log(success("Success: ") + 'you passed the branch check √') + log(success('Success: ') + 'you passed the branch check √'); groupEnd(); }; -execCommand ('git symbolic-ref --short -q HEAD', process.argv[2]); +execCommand('git symbolic-ref --short -q HEAD', process.argv[2]); diff --git a/package.json b/package.json index 969fe71..a98663b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "check git current branch", "main": "./branch.js", "scripts": { - "test": "node branch.js master" + "test": "node branch.js master" }, "bin": { "branch": "./bin/branch.js"