Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

[![Build Status](https://travis-ci.org/silentport/branch.svg?branch=master)](https://travis-ci.org/silentport/branch)
<a href="https://www.npmjs.com/package/check-git-branch"><img alt="undefined" src="https://img.shields.io/npm/v/check-git-branch.svg?style=flat"></a>
> 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"
},
```
```
43 changes: 21 additions & 22 deletions bin/branch.js
Original file line number Diff line number Diff line change
@@ -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]);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down