├── .codeclimate.yml ├── .editorconfig ├── .eslintrc ├── .gitattributes ├── .github ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING ├── ISSUE_TEMPLATE └── PULL_REQUEST_TEMPLATE ├── .gitignore ├── .npmignore ├── .readme └── contents.md ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── package.json └── src ├── bin.js └── index.js /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by a `metapak` 2 | # module. Do not change it elsewhere, changes would 3 | # be overriden. 4 | engines: 5 | eslint: 6 | enabled: true 7 | 8 | ratings: 9 | paths: 10 | - "src/**.js" 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by a `metapak` 2 | # module. Do not change it elsewhere, changes would 3 | # be overriden. 4 | 5 | # EditorConfig is awesome: http://EditorConfig.org 6 | 7 | # top-most EditorConfig file 8 | root = true 9 | 10 | # Unix-style newlines with a newline ending every file 11 | [*] 12 | end_of_line = lf 13 | insert_final_newline = true 14 | 15 | # Matches multiple files with brace expansion notation 16 | # Set default charset 17 | # 2 space indentation 18 | [*.{js,css}] 19 | charset = utf-8 20 | indent_style = space 21 | trim_trailing_whitespace = true 22 | indent_size = 2 23 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | extends: 'eslint-config-simplifield/lib/backend', 3 | parserOptions: { 4 | sourceType: 'script', 5 | modules: true, 6 | }, 7 | rules: { 8 | 'no-magic-numbers': 0, 9 | }, 10 | } 11 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.md text eol=lf 2 | *.html text eol=lf 3 | *.js text eol=lf 4 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | Be kind, except if i behave like an asshole, 4 | if so, tell me by linking to this file. 5 | 6 | I try hard to automate things so that you cannot 7 | create noises without really willing to do so. 8 | 9 | This is why i'll just delete issues/comments 10 | making be sad. 11 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING: -------------------------------------------------------------------------------- 1 | Contributing to this project requires you to be 2 | a gentleman. 3 | 4 | By contributing you must agree with publishing your 5 | changes int the same license than the actual code. 6 | 7 | You will find the license in the LICENSE file at 8 | the root of this repository. 9 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE: -------------------------------------------------------------------------------- 1 | ## Issue 2 | 16 | 17 | I'm a gentledev i: 18 | - [ ] fully read the README recently 19 | - [ ] searched for existing issues 20 | - [ ] checked i'm up to date with the latest version of the project 21 | 22 | ### Expected behavior 23 | 24 | ### Actual behavior 25 | 26 | ### Steps to reproduce the behavior 27 | 28 | ### Debugging informations 29 | - `node -v` result: 30 | ``` 31 | 32 | ``` 33 | 34 | - `npm -v` result: 35 | ``` 36 | 37 | ``` 38 | If the result is lower than 6.9.5, there is 39 | poor chances i even have a look to it. Please, 40 | use the last [NodeJS LTS version](https://nodejs.org/en/). 41 | 42 | ## Feature request 43 | 54 | 55 | ### Feature description 56 | 57 | ### Use cases 58 | 59 | - [ ] I will/have implement the feature 60 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE: -------------------------------------------------------------------------------- 1 | 10 | 11 | Fixes # 12 | 13 | ### Proposed changes 14 | - 15 | - 16 | 17 | 18 | 19 | ### Code quality 20 | - [ ] I made some tests for my changes 21 | - [ ] I added my name in the 22 | [contributors](https://docs.npmjs.com/files/package.json#people-fields-author-contributors) 23 | field of the package.json file 24 | 25 | ### License 26 | To get your contribution merged, you must check the following. 27 | 28 | - [ ] I read the project license in the LICENSE file 29 | - [ ] I agree with publishing under this project license 30 | 31 | 45 | ### Join 46 | - [ ] I wish to join the core team 47 | - [ ] I agree that with great powers comes responsibilities 48 | - [ ] I'm a nice person 49 | 50 | My NPM username: 51 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by a `metapak` 2 | # module. Do not change it elsewhere, changes would 3 | # be overriden. 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | 10 | # Runtime data 11 | pids 12 | *.pid 13 | *.seed 14 | 15 | # Directory for instrumented libs generated by jscoverage/JSCover 16 | lib-cov 17 | 18 | # Coverage directory used by tools like istanbul 19 | coverage 20 | 21 | # nyc test coverage 22 | .nyc_output 23 | 24 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 25 | .grunt 26 | 27 | # node-waf configuration 28 | .lock-wscript 29 | 30 | # Compiled binary addons (http://nodejs.org/api/addons.html) 31 | build/Release 32 | 33 | # Dependency directories 34 | node_modules 35 | jspm_packages 36 | 37 | # Optional npm cache directory 38 | .npm 39 | 40 | # Optional REPL history 41 | .node_repl_history 42 | 43 | # Coveralls key 44 | .coveralls.yml 45 | 46 | # Dist files 47 | dist 48 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.swp 3 | node_modules/ 4 | .git 5 | -------------------------------------------------------------------------------- /.readme/contents.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | You can use this from the command-line or within your build system. 4 | 5 | ### Command-line 6 | 7 | First, install `buildbranch` globally: 8 | 9 | ```shell 10 | npm install buildbranch -g 11 | ``` 12 | 13 | Then, from your master branch, run `buildbranch` with the branch and directory 14 | you want to publish. It will default to `gh-pages` and `www`. 15 | 16 | ```shell 17 | buildbranch gh-pages www example.com 18 | ``` 19 | 20 | All arguments are optional, the defaults being: 21 | 22 | * branch: `'gh-pages'` 23 | * folder: `'www'´ 24 | * domain: `''` 25 | 26 | #### Skipping the pre-commit hook 27 | 28 | buildbranch will run `git commit` in the given branch. You might want to skip the pre-commit hook if there is one. This can be archived by adding another command line argument: 29 | 30 | ```shell 31 | buildbranch gh-pages www example.com no-verify 32 | ``` 33 | 34 | Actually, anytime four arguments are passed, `git commit` will be run with `--no-verify`, skipping the pre-commit and commit-msg hook, it does not matter if you pass `no-verify` or any other non-empty string as the fourth argument. 35 | 36 | ```shell 37 | buildbranch gh-pages www example.com no-verify 38 | ``` 39 | 40 | If you want to skip have `--no-verify` but do not want to specify a domain, use 41 | 42 | ```shell 43 | buildbranch gh-pages www '' no-verify 44 | ``` 45 | 46 | ### Build System 47 | 48 | First, install `buildbranch` as a development dependency: 49 | 50 | ```shell 51 | npm install buildbranch --save-dev 52 | ``` 53 | 54 | Then, use it in your build system: 55 | 56 | ```javascript 57 | buildBranch({ 58 | branch: 'gh-pages', 59 | remote: 'origin', 60 | ignore: ['.git', 'www', 'node_modules'], 61 | folder: 'www', 62 | domain: 'example.com', 63 | noVerify: false 64 | }, function(err) { 65 | if(err) { 66 | throw err; 67 | } 68 | console.log('Published!'); 69 | }); 70 | ``` 71 | 72 | For example in gulp you can do it like this: 73 | 74 | ```javascript 75 | var gulp = require('gulp'); 76 | var buildBranch = require('buildbranch'); 77 | 78 | gulp.task('gh', ['build'], function(done) { 79 | buildBranch({ folder: 'dist' }, done); 80 | }); 81 | ``` 82 | 83 | ## API 84 | 85 | ### buildBranch(options, callback) 86 | 87 | #### options 88 | Type: `Object` 89 | 90 | Required. An object containing the following options. 91 | 92 | ##### options.branch 93 | Type: `String` 94 | Default: 'gh-pages' 95 | 96 | The branch on wich to publish. 97 | 98 | ##### options.remote 99 | Type: `String` 100 | Default: 'origin' 101 | 102 | The remote repository on wich to publish. 103 | 104 | ##### options.folder 105 | Type: `String` 106 | Default: 'www' 107 | 108 | The folder in wich the build is. 109 | 110 | ##### options.domain 111 | Type: `String` 112 | 113 | The domain name that will fill the cname file. 114 | 115 | ##### options.cname 116 | Type: `String` 117 | Default: 'CNAME' 118 | 119 | The name of the file enabling custom domain name on you build platform. 120 | 121 | ##### options.commit 122 | Type: `String` 123 | Default: 'Build $' 124 | 125 | The commit label, the first `$` occurrence in the given string will be replaced 126 | by the current date. 127 | 128 | ##### options.cwd 129 | Type: `String` 130 | Default: process.cwd() 131 | 132 | The working directory (root of the git repo). 133 | 134 | ##### options.ignore 135 | Type: `Array` 136 | 137 | A list of files to ignore. 'node_modules' and '.git' will be automatically 138 | added to the ignore list. 139 | 140 | ##### options.noVerify 141 | Type: `Boolean` 142 | Default: false 143 | 144 | Whether or not to add `--no-verify` when running `git commit` (skipping the pre-commit and commit-msg hook). 145 | 146 | #### callback 147 | Type: `Function` 148 | 149 | Required. Called when the publication is done. 150 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by a `metapak` 2 | # module. Do not change it elsewhere, changes would 3 | # be overriden. 4 | 5 | language: node_js 6 | node_js: 7 | - 6 8 | - 6.9.5 9 | - 7 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | ### v2.0.0 (2017/02/18 05:32 +00:00) 3 | - [ba25e2b](https://github.com/nfroidure/buildbranch/commit/ba25e2bf7caa23e4ecf64fdf654ca5b21821a543) 2.0.0 (@nfroidure) 4 | - [7dfe459](https://github.com/nfroidure/buildbranch/commit/7dfe459e513105e05aa9fa17925f11b5f2efa71f) Adding metapak-nfroidure (@nfroidure) 5 | - [3dad25a](https://github.com/nfroidure/buildbranch/commit/3dad25a3a2d0b8e871f10804cd0aadbfaf0cceb7) Adds an option to avoid hooks at build fix #12 (@nfroidure) 6 | 7 | ### v1.0.0 (2016/02/21 11:23 +00:00) 8 | - [783d0fd](https://github.com/nfroidure/buildbranch/commit/783d0fdf7643bbf7995ffdcf89309cd42e0a1c97) 1.0.0 (@nfroidure) 9 | - [5899e2e](https://github.com/nfroidure/buildbranch/commit/5899e2e26f18073458ee6410922c6c8338575935) Fixing lil tweaks and adding linting rules (@nfroidure) 10 | - [#7](https://github.com/nfroidure/buildbranch/pull/7) Added options.remote for alternative remote repos (@nksoff) 11 | - [8d3cfdd](https://github.com/nfroidure/buildbranch/commit/8d3cfdd5a17f1ee2664bdd3fdf06211905bd5de6) add options.remote for alternative remote repos (@nksoff) 12 | 13 | ### v0.0.3 (2015/03/05 10:45 +00:00) 14 | - [be08ae5](https://github.com/nfroidure/buildbranch/commit/be08ae5d60f2b87f9102c83e0db411b9628cce40) 0.0.3 (@nfroidure) 15 | - [65f8f83](https://github.com/nfroidure/buildbranch/commit/65f8f83b3697179c8a2b6efa8d81fb2d70065ee7) Adding a domain option (@nfroidure) 16 | 17 | ### v0.0.2 (2015/02/15 16:22 +00:00) 18 | - [b59a368](https://github.com/nfroidure/buildbranch/commit/b59a368dd31a147585a846854d2530ec223851eb) 0.0.2 (@nfroidure) 19 | - [#3](https://github.com/nfroidure/buildbranch/pull/3) adding note about using in gulp (@matmuchrapna) 20 | - [5036436](https://github.com/nfroidure/buildbranch/commit/5036436bd85457f19df9d4161769b84f4a6e1c8d) adding note about using in gulp (@iamstarkov) 21 | - [#2](https://github.com/nfroidure/buildbranch/pull/2) Add Simple Command Line Interface (@jlord) 22 | - [a1824bc](https://github.com/nfroidure/buildbranch/commit/a1824bca601d3c0e880d17b52faee7c58d8c5b05) update readme 23 | - [2fe2583](https://github.com/nfroidure/buildbranch/commit/2fe258376b32a76c30ab92efcc133511360e6d9e) add bin to package.json 24 | - [6f7d9d7](https://github.com/nfroidure/buildbranch/commit/6f7d9d7bc25260d7113661705cce600bb8e5d002) add command line file 25 | - [#1](https://github.com/nfroidure/buildbranch/pull/1) Update Readme: use --save-dev instead of --save (@robinboehm) 26 | - [8266b5f](https://github.com/nfroidure/buildbranch/commit/8266b5fae8e5f75b806827305c00ac76a4871d9f) Update Readme: use --save-dev instead of --save (@robinboehm) 27 | - [a7b6407](https://github.com/nfroidure/buildbranch/commit/a7b6407b1368692535417491f63e4a18b96342b0) First commit (@nfroidure) 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright © 2017 Nicolas Froidure 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the “Software”), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 6 | # buildbranch 7 | > Publish a folder to the given build branch (like gh-pages). 8 | 9 | [![NPM version](https://badge.fury.io/js/buildbranch.svg)](https://npmjs.org/package/buildbranch) 10 | [![Dependency Status](https://david-dm.org/nfroidure/buildbranch.svg)](https://david-dm.org/nfroidure/buildbranch) 11 | [![devDependency Status](https://david-dm.org/nfroidure/buildbranch/dev-status.svg)](https://david-dm.org/nfroidure/buildbranch#info=devDependencies) 12 | [![Coverage Status](https://coveralls.io/repos/nfroidure/buildbranch/badge.svg?branch=master)](https://coveralls.io/r/nfroidure/buildbranch?branch=master) 13 | [![Dependency Status](https://dependencyci.com/github/nfroidure/buildbranch/badge)](https://dependencyci.com/github/nfroidure/buildbranch) 14 | 15 | ## Usage 16 | 17 | You can use this from the command-line or within your build system. 18 | 19 | ### Command-line 20 | 21 | First, install `buildbranch` globally: 22 | 23 | ```shell 24 | npm install buildbranch -g 25 | ``` 26 | 27 | Then, from your master branch, run `buildbranch` with the branch and directory 28 | you want to publish. It will default to `gh-pages` and `www`. 29 | 30 | ```shell 31 | buildbranch gh-pages www example.com 32 | ``` 33 | 34 | All arguments are optional, the defaults being: 35 | 36 | * branch: `'gh-pages'` 37 | * folder: `'www'´ 38 | * domain: `''` 39 | 40 | #### Skipping the pre-commit hook 41 | 42 | buildbranch will run `git commit` in the given branch. You might want to skip the pre-commit hook if there is one. This can be archived by adding another command line argument: 43 | 44 | ```shell 45 | buildbranch gh-pages www example.com no-verify 46 | ``` 47 | 48 | Actually, anytime four arguments are passed, `git commit` will be run with `--no-verify`, skipping the pre-commit and commit-msg hook, it does not matter if you pass `no-verify` or any other non-empty string as the fourth argument. 49 | 50 | ```shell 51 | buildbranch gh-pages www example.com no-verify 52 | ``` 53 | 54 | If you want to skip have `--no-verify` but do not want to specify a domain, use 55 | 56 | ```shell 57 | buildbranch gh-pages www '' no-verify 58 | ``` 59 | 60 | ### Build System 61 | 62 | First, install `buildbranch` as a development dependency: 63 | 64 | ```shell 65 | npm install buildbranch --save-dev 66 | ``` 67 | 68 | Then, use it in your build system: 69 | 70 | ```javascript 71 | buildBranch({ 72 | branch: 'gh-pages', 73 | remote: 'origin', 74 | ignore: ['.git', 'www', 'node_modules'], 75 | folder: 'www', 76 | domain: 'example.com', 77 | noVerify: false 78 | }, function(err) { 79 | if(err) { 80 | throw err; 81 | } 82 | console.log('Published!'); 83 | }); 84 | ``` 85 | 86 | For example in gulp you can do it like this: 87 | 88 | ```javascript 89 | var gulp = require('gulp'); 90 | var buildBranch = require('buildbranch'); 91 | 92 | gulp.task('gh', ['build'], function(done) { 93 | buildBranch({ folder: 'dist' }, done); 94 | }); 95 | ``` 96 | 97 | ## API 98 | 99 | ### buildBranch(options, callback) 100 | 101 | #### options 102 | Type: `Object` 103 | 104 | Required. An object containing the following options. 105 | 106 | ##### options.branch 107 | Type: `String` 108 | Default: 'gh-pages' 109 | 110 | The branch on wich to publish. 111 | 112 | ##### options.remote 113 | Type: `String` 114 | Default: 'origin' 115 | 116 | The remote repository on wich to publish. 117 | 118 | ##### options.folder 119 | Type: `String` 120 | Default: 'www' 121 | 122 | The folder in wich the build is. 123 | 124 | ##### options.domain 125 | Type: `String` 126 | 127 | The domain name that will fill the cname file. 128 | 129 | ##### options.cname 130 | Type: `String` 131 | Default: 'CNAME' 132 | 133 | The name of the file enabling custom domain name on you build platform. 134 | 135 | ##### options.commit 136 | Type: `String` 137 | Default: 'Build $' 138 | 139 | The commit label, the first `$` occurrence in the given string will be replaced 140 | by the current date. 141 | 142 | ##### options.cwd 143 | Type: `String` 144 | Default: process.cwd() 145 | 146 | The working directory (root of the git repo). 147 | 148 | ##### options.ignore 149 | Type: `Array` 150 | 151 | A list of files to ignore. 'node_modules' and '.git' will be automatically 152 | added to the ignore list. 153 | 154 | ##### options.noVerify 155 | Type: `Boolean` 156 | Default: false 157 | 158 | Whether or not to add `--no-verify` when running `git commit` (skipping the pre-commit and commit-msg hook). 159 | 160 | #### callback 161 | Type: `Function` 162 | 163 | Required. Called when the publication is done. 164 | 165 | # License 166 | [MIT](https://github.com/nfroidure/buildbranch/blob/master/LICENSE) 167 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "buildbranch", 3 | "description": "Publish a folder to the given build branch (like gh-pages).", 4 | "version": "2.0.0", 5 | "homepage": "https://github.com/nfroidure/buildbranch", 6 | "author": "Nicolas Froidure", 7 | "bin": { 8 | "buildbranch": "./src/bin.js" 9 | }, 10 | "metapak": { 11 | "configs": [ 12 | "readme" 13 | ], 14 | "data": { 15 | "files": "src/**.js" 16 | } 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "git://github.com/nfroidure/buildbranch.git" 21 | }, 22 | "scripts": { 23 | "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", 24 | "cli": "env NODE_ENV=${NODE_ENV:-cli}", 25 | "cz": "env NODE_ENV=${NODE_ENV:-cli} git cz", 26 | "lint": "eslint src/**.js", 27 | "metapak": "metapak", 28 | "preversion": "npm t && npm run lint && npm run metapak -s", 29 | "test": "exit 0", 30 | "version": "npm run changelog && git add CHANGELOG.md" 31 | }, 32 | "bugs": { 33 | "url": "https://github.com/nfroidure/buildbranch/issues" 34 | }, 35 | "license": "MIT", 36 | "main": "src/index", 37 | "engines": { 38 | "node": ">=6.9.5" 39 | }, 40 | "keywords": [ 41 | "gulpfriendly", 42 | "gruntfriendly", 43 | "ghpages", 44 | "gh-pages", 45 | "build", 46 | "publish", 47 | "git", 48 | "github" 49 | ], 50 | "dependencies": { 51 | "rimraf": "^2.5.2", 52 | "shell-escape": "^0.2.0" 53 | }, 54 | "devDependencies": { 55 | "commitizen": "^2.9.6", 56 | "conventional-changelog-cli": "^1.3.5", 57 | "coveralls": "2.11.15", 58 | "cz-conventional-changelog": "^2.0.0", 59 | "eslint": "3.16.0", 60 | "eslint-config-simplifield": "4.1.1", 61 | "istanbul": "0.4.5", 62 | "metapak": "1.0.3", 63 | "metapak-nfroidure": "^1.0.2", 64 | "mocha": "3.2.0", 65 | "mocha-lcov-reporter": "1.3.0" 66 | }, 67 | "config": { 68 | "commitizen": { 69 | "path": "./node_modules/cz-conventional-changelog" 70 | } 71 | }, 72 | "greenkeeper": { 73 | "ignore": [ 74 | "commitizen", 75 | "cz-conventional-changelog", 76 | "conventional-changelog-cli" 77 | ] 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict'; 4 | 5 | const buildBranch = require('./index.js'); 6 | const branch = process.argv[2]; 7 | const dir = process.argv[3]; 8 | const domain = process.argv[4]; 9 | const noVerify = process.argv[5]; 10 | 11 | buildBranch({ 12 | branch: branch || 'gh-pages', 13 | folder: dir || 'www', 14 | domain: domain || '', 15 | noVerify: !!noVerify, 16 | }, function buildBranchCLICb(err) { 17 | if(err) { 18 | throw err; 19 | } 20 | console.log('Published!'); // eslint-disable-line 21 | }); 22 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const exec = require('child_process').exec; 4 | const path = require('path'); 5 | const rimraf = require('rimraf'); 6 | const fs = require('fs'); 7 | const shellescape = require('shell-escape') 8 | 9 | function buildBranch(options, callback) { 10 | 11 | let curBranch = 'master'; 12 | const execOptions = {}; 13 | let command = ''; 14 | 15 | // Checking options 16 | options.folder = shellescape([options.folder]) || 'www'; 17 | options.branch = shellescape([options.branch]) || 'gh-pages'; 18 | options.remote = shellescape([options.remote]) || 'origin'; 19 | options.ignore = options.ignore || []; 20 | options.ignore.push('.git', 'node_modules', options.folder); 21 | options.cname = shellescape([options.cname]) || 'CNAME'; 22 | options.commit = (shellescape([options.commit]) || 'Build $').replace('$', (new Date()).toISOString()); 23 | options.cwd = shellescape([options.cwd]) || process.cwd(); 24 | options.noVerify = shellescape([options.noVerify]) || false; 25 | execOptions.cwd = options.cwd; 26 | 27 | // Remember the current branch 28 | command = 'git rev-parse --abbrev-ref HEAD'; 29 | 30 | exec(command, execOptions, (err, stdout) => { 31 | if(err) { 32 | callback(err); return; 33 | } 34 | 35 | curBranch = stdout.trim(); 36 | 37 | // Switch to ghpages branch 38 | command = 'git branch -D ' + options.branch + ';' + 39 | ' git checkout --orphan ' + options.branch + ';' + 40 | ' git rm -r --cached .'; 41 | 42 | exec(command, execOptions, (err) => { 43 | let ignore; 44 | 45 | if(err) { 46 | callback(err); return; 47 | } 48 | 49 | // delete all files except the untracked ones 50 | ignore = options.ignore.slice(0); 51 | fs.readdirSync(options.cwd).forEach((file) => { 52 | if(-1 === ignore.indexOf(file)) { 53 | rimraf.sync(path.join(options.cwd, file)); 54 | } 55 | }); 56 | // Check if build folder exists and is not empty 57 | if(!fs.existsSync(options.folder) || !fs.readdirSync(options.folder).length) { 58 | callback(new Error('Build folder doesn\'t exist or is empty.')); return; 59 | } 60 | fs.readdirSync(path.join(options.cwd, options.folder)) 61 | .forEach((file) => { 62 | fs.renameSync(path.join(options.cwd, options.folder, file), 63 | path.join(options.cwd, file)); 64 | }); 65 | fs.rmdirSync(path.join(options.cwd, options.folder)); 66 | 67 | // Add the domain cname field 68 | if(options.domain) { 69 | fs.writeFileSync(path.join(options.cwd, options.cname), options.domain); 70 | } 71 | 72 | // Add a new ignore file 73 | ignore.push('.gitignore'); 74 | fs.writeFileSync(path.join(options.cwd, '.gitignore'), ignore.join('\n')); 75 | 76 | // Commit files 77 | command = 'git add .;' + 78 | ' git commit -m "' + options.commit.replace('"', '\\"') + '"' + 79 | (options.noVerify ? ' --no-verify' : ''); 80 | exec(command, execOptions, (err) => { 81 | if(err) { 82 | callback(err); return; 83 | } 84 | 85 | // Pushing commit 86 | command = 'git push -f ' + options.remote + ' ' + options.branch + ';' + 87 | ' git checkout ' + curBranch + ' ;' + 88 | ' git checkout .'; 89 | 90 | exec(command, execOptions, (err) => { 91 | if(err) { 92 | callback(err); return; 93 | } 94 | 95 | callback(); 96 | 97 | }); 98 | }); 99 | }); 100 | }); 101 | } 102 | 103 | module.exports = buildBranch; 104 | --------------------------------------------------------------------------------