├── .editorconfig
├── .eslintrc.json
├── .gitattributes
├── .github
└── contributing.md
├── .gitignore
├── .travis.yml
├── .verb.md
├── LICENSE
├── README.md
├── appveyor.yml
├── index.js
├── package.json
└── test
├── create-args.js
└── test.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org/
2 | root = true
3 |
4 | [*]
5 | charset = utf-8
6 | end_of_line = lf
7 | indent_size = 2
8 | indent_style = space
9 | insert_final_newline = true
10 | trim_trailing_whitespace = true
11 |
12 | [{**/{actual,fixtures,expected,templates}/**,*.md}]
13 | trim_trailing_whitespace = false
14 | insert_final_newline = false
15 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": [
3 | "eslint:recommended"
4 | ],
5 |
6 | "env": {
7 | "browser": false,
8 | "es6": true,
9 | "node": true,
10 | "mocha": true
11 | },
12 |
13 | "parserOptions":{
14 | "ecmaVersion": 9,
15 | "sourceType": "module",
16 | "ecmaFeatures": {
17 | "modules": true,
18 | "experimentalObjectRestSpread": true
19 | }
20 | },
21 |
22 | "globals": {
23 | "document": false,
24 | "navigator": false,
25 | "window": false
26 | },
27 |
28 | "rules": {
29 | "accessor-pairs": 2,
30 | "arrow-spacing": [2, { "before": true, "after": true }],
31 | "block-spacing": [2, "always"],
32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }],
33 | "comma-dangle": [2, "never"],
34 | "comma-spacing": [2, { "before": false, "after": true }],
35 | "comma-style": [2, "last"],
36 | "constructor-super": 2,
37 | "curly": [2, "multi-line"],
38 | "dot-location": [2, "property"],
39 | "eol-last": 2,
40 | "eqeqeq": [2, "allow-null"],
41 | "generator-star-spacing": [2, { "before": true, "after": true }],
42 | "handle-callback-err": [2, "^(err|error)$" ],
43 | "indent": [2, 2, { "SwitchCase": 1 }],
44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
45 | "keyword-spacing": [2, { "before": true, "after": true }],
46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }],
47 | "new-parens": 2,
48 | "no-array-constructor": 2,
49 | "no-caller": 2,
50 | "no-class-assign": 2,
51 | "no-cond-assign": 2,
52 | "no-const-assign": 2,
53 | "no-control-regex": 2,
54 | "no-debugger": 2,
55 | "no-delete-var": 2,
56 | "no-dupe-args": 2,
57 | "no-dupe-class-members": 2,
58 | "no-dupe-keys": 2,
59 | "no-duplicate-case": 2,
60 | "no-empty-character-class": 2,
61 | "no-eval": 2,
62 | "no-ex-assign": 2,
63 | "no-extend-native": 2,
64 | "no-extra-bind": 2,
65 | "no-extra-boolean-cast": 2,
66 | "no-extra-parens": [2, "functions"],
67 | "no-fallthrough": 2,
68 | "no-floating-decimal": 2,
69 | "no-func-assign": 2,
70 | "no-implied-eval": 2,
71 | "no-inner-declarations": [2, "functions"],
72 | "no-invalid-regexp": 2,
73 | "no-irregular-whitespace": 2,
74 | "no-iterator": 2,
75 | "no-label-var": 2,
76 | "no-labels": 2,
77 | "no-lone-blocks": 2,
78 | "no-mixed-spaces-and-tabs": 2,
79 | "no-multi-spaces": 2,
80 | "no-multi-str": 2,
81 | "no-multiple-empty-lines": [2, { "max": 1 }],
82 | "no-native-reassign": 0,
83 | "no-negated-in-lhs": 2,
84 | "no-new": 2,
85 | "no-new-func": 2,
86 | "no-new-object": 2,
87 | "no-new-require": 2,
88 | "no-new-wrappers": 2,
89 | "no-obj-calls": 2,
90 | "no-octal": 2,
91 | "no-octal-escape": 2,
92 | "no-proto": 0,
93 | "no-redeclare": 2,
94 | "no-regex-spaces": 2,
95 | "no-return-assign": 2,
96 | "no-self-compare": 2,
97 | "no-sequences": 2,
98 | "no-shadow-restricted-names": 2,
99 | "no-spaced-func": 2,
100 | "no-sparse-arrays": 2,
101 | "no-this-before-super": 2,
102 | "no-throw-literal": 2,
103 | "no-trailing-spaces": 0,
104 | "no-undef": 2,
105 | "no-undef-init": 2,
106 | "no-unexpected-multiline": 2,
107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }],
108 | "no-unreachable": 2,
109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }],
110 | "no-useless-call": 0,
111 | "no-with": 2,
112 | "one-var": [0, { "initialized": "never" }],
113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
114 | "padded-blocks": [0, "never"],
115 | "quotes": [2, "single", "avoid-escape"],
116 | "radix": 2,
117 | "semi": [2, "always"],
118 | "semi-spacing": [2, { "before": false, "after": true }],
119 | "space-before-blocks": [2, "always"],
120 | "space-before-function-paren": [2, "never"],
121 | "space-in-parens": [2, "never"],
122 | "space-infix-ops": 2,
123 | "space-unary-ops": [2, { "words": true, "nonwords": false }],
124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
125 | "use-isnan": 2,
126 | "valid-typeof": 2,
127 | "wrap-iife": [2, "any"],
128 | "yoda": [2, "never"]
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Enforce Unix newlines
2 | * text eol=lf
3 |
4 | # binaries
5 | *.ai binary
6 | *.psd binary
7 | *.jpg binary
8 | *.gif binary
9 | *.png binary
10 | *.jpeg binary
11 |
--------------------------------------------------------------------------------
/.github/contributing.md:
--------------------------------------------------------------------------------
1 | # Contributing to gfc
2 |
3 | First and foremost, thank you! We appreciate that you want to contribute to gfc, your time is valuable, and your contributions mean a lot to us.
4 |
5 | **What does "contributing" mean?**
6 |
7 | Creating an issue is the simplest form of contributing to a project. But there are many ways to contribute, including the following:
8 |
9 | - Updating or correcting documentation
10 | - Feature requests
11 | - Bug reports
12 |
13 | If you'd like to learn more about contributing in general, the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) has a lot of useful information.
14 |
15 | **Showing support for gfc**
16 |
17 | Please keep in mind that open source software is built by people like you, who spend their free time creating things the rest the community can use.
18 |
19 | Don't have time to contribute? No worries, here are some other ways to show your support for gfc:
20 |
21 | - star the [project](https://github.com/jonschlinkert/gfc)
22 | - tweet your support for gfc
23 |
24 | ## Issues
25 |
26 | ### Before creating an issue
27 |
28 | Please try to determine if the issue is caused by an underlying library, and if so, create the issue there. Sometimes this is difficult to know. We only ask that you attempt to give a reasonable attempt to find out. Oftentimes the readme will have advice about where to go to create issues.
29 |
30 | Try to follow these guidelines
31 |
32 | - **Investigate the issue**:
33 | - **Check the readme** - oftentimes you will find notes about creating issues, and where to go depending on the type of issue.
34 | - Create the issue in the appropriate repository.
35 |
36 | ### Creating an issue
37 |
38 | Please be as descriptive as possible when creating an issue. Give us the information we need to successfully answer your question or address your issue by answering the following in your issue:
39 |
40 | - **version**: please note the version of gfc are you using
41 | - **extensions, plugins, helpers, etc** (if applicable): please list any extensions you're using
42 | - **error messages**: please paste any error messages into the issue, or a [gist](https://gist.github.com/)
43 |
44 | ## Above and beyond
45 |
46 | Here are some tips for creating idiomatic issues. Taking just a little bit extra time will make your issue easier to read, easier to resolve, more likely to be found by others who have the same or similar issue in the future.
47 |
48 | - read the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing)
49 | - take some time to learn basic markdown. This [markdown cheatsheet](https://gist.github.com/jonschlinkert/5854601) is super helpful, as is the GitHub guide to [basic markdown](https://help.github.com/articles/markdown-basics/).
50 | - Learn about [GitHub Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/). And if you want to really go above and beyond, read [mastering markdown](https://guides.github.com/features/mastering-markdown/).
51 | - use backticks to wrap code. This ensures that code will retain its format, making it much more readable to others
52 | - use syntax highlighting by adding the correct language name after the first "code fence"
53 |
54 |
55 | [node-glob]: https://github.com/isaacs/node-glob
56 | [micromatch]: https://github.com/jonschlinkert/micromatch
57 | [so]: http://stackoverflow.com/questions/tagged/gfc
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # always ignore files
2 | *.DS_Store
3 | .idea
4 | .vscode
5 | *.sublime-*
6 |
7 | # test related, or directories generated by tests
8 | test/actual
9 | actual
10 | coverage
11 | .nyc*
12 |
13 | # npm
14 | node_modules
15 | npm-debug.log
16 |
17 | # yarn
18 | yarn.lock
19 | yarn-error.log
20 |
21 | # misc
22 | _gh_pages
23 | _draft
24 | _drafts
25 | bower_components
26 | vendor
27 | temp
28 | tmp
29 | TODO.md
30 | package-lock.json
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | os:
3 | - linux
4 | - osx
5 | language: node_js
6 | node_js:
7 | - node
8 | - '9'
9 | - '8'
10 | - '7'
11 | - '6'
12 | before_install:
13 | - git config --global user.name 'gfc-test'
14 | - git config --global user.email 'travis@gfc'
15 |
--------------------------------------------------------------------------------
/.verb.md:
--------------------------------------------------------------------------------
1 | ## Usage
2 |
3 | ```js
4 | const firstCommit = require('{%= name %}');
5 | ```
6 |
7 | The main export is an async function that takes a [callback](#async-usage) or returns a [promise](#promise-usage) when no callback is passed. A [.sync](#sync-usage) method is also exposed.
8 |
9 | **Default behavior**
10 |
11 | The following steps can be customized with [options](#options):
12 |
13 | 1. Creates a new git repository
14 | 1. Adds a `.gitkeep` file if the cwd is empty.
15 | 1. `git add .`
16 | 1. do a first commit with the message `"first commit"`
17 |
18 |
19 | ### promise usage
20 |
21 | Returns a promise if a [callback](#async-usage) is not passed.
22 |
23 | ```js
24 | firstCommit(cwd[, options])
25 | .then(res => {
26 | console.log('stdout: ' + res.stdout);
27 | console.log('stderr: ' + res.stderr);
28 | })
29 | .catch(err => console.log('Error: ', err));
30 | ```
31 |
32 |
33 | ### async usage
34 |
35 | ```js
36 | firstCommit(cwd[, options], function(err, stdout, stderr) {
37 | if (err) {
38 | console.error('exec error: ' + err);
39 | return;
40 | }
41 | console.log('stdout: ' + stdout);
42 | console.log('stderr: ' + stderr);
43 | });
44 | ```
45 |
46 | ### sync usage
47 |
48 | ```js
49 | firstCommit.sync(cwd[, options]);
50 | ```
51 |
52 | ## Options
53 |
54 | ### options.file
55 |
56 | **Type**: `object|boolean`
57 |
58 | **Default**: `{ path: '.gitkeep', contents: '' }`
59 |
60 |
61 |
62 | ```js
63 | firstCommit.sync('foo/bar', { file: false })
64 | ```
65 |
66 | ### options.message
67 |
68 | **Type**: `string`
69 |
70 | **Default**: `'first commit'`
71 |
72 | ```js
73 | var options = {message: 'my amazing first commit'};
74 |
75 | firstCommit('foo/bar', options, function(err) {
76 | if (err) {
77 | console.log(err);
78 | } else {
79 | console.log('done!');
80 | }
81 | });
82 | ```
83 |
84 | ### options.exec
85 |
86 | **Type**: `object`
87 |
88 | **Default**: `undefined`
89 |
90 | Options to pass to [execSync](https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options).
91 |
92 | ```js
93 | var options = {
94 | message: 'my amazing first commit',
95 | exec: {
96 | timeout: 3000,
97 | killSignal: 'SIGTERM'
98 | }
99 | };
100 |
101 | firstCommit.sync('foo/bar', options);
102 | ```
103 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2017-2018, Jon Schlinkert.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # gfc [](https://www.npmjs.com/package/gfc) [](https://npmjs.org/package/gfc) [](https://npmjs.org/package/gfc) [](https://travis-ci.org/jonschlinkert/gfc) [](https://ci.appveyor.com/project/jonschlinkert/gfc)
2 |
3 | > Simple way to initialize a new git repository in an empty directory, add a file and do a first commit (or skip that part in a directory with files). Useful for unit tests and generators.
4 |
5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
6 |
7 | ## Install
8 |
9 | Install with [npm](https://www.npmjs.com/):
10 |
11 | ```sh
12 | $ npm install --save gfc
13 | ```
14 |
15 | ## Usage
16 |
17 | ```js
18 | const firstCommit = require('gfc');
19 | ```
20 |
21 | The main export is an async function that takes a [callback](#async-usage) or returns a [promise](#promise-usage) when no callback is passed. A [.sync](#sync-usage) method is also exposed.
22 |
23 | **Default behavior**
24 |
25 | The following steps can be customized with [options](#options):
26 |
27 | 1. Creates a new git repository
28 | 2. Adds a `.gitkeep` file if the cwd is empty.
29 | 3. `git add .`
30 | 4. do a first commit with the message `"first commit"`
31 |
32 | ### promise usage
33 |
34 | Returns a promise if a [callback](#async-usage) is not passed.
35 |
36 | ```js
37 | firstCommit(cwd[, options])
38 | .then(res => {
39 | console.log('stdout: ' + res.stdout);
40 | console.log('stderr: ' + res.stderr);
41 | })
42 | .catch(err => console.log('Error: ', err));
43 | ```
44 |
45 | ### async usage
46 |
47 | ```js
48 | firstCommit(cwd[, options], function(err, stdout, stderr) {
49 | if (err) {
50 | console.error('exec error: ' + err);
51 | return;
52 | }
53 | console.log('stdout: ' + stdout);
54 | console.log('stderr: ' + stderr);
55 | });
56 | ```
57 |
58 | ### sync usage
59 |
60 | ```js
61 | firstCommit.sync(cwd[, options]);
62 | ```
63 |
64 | ## Options
65 |
66 | ### options.file
67 |
68 | **Type**: `object|boolean`
69 |
70 | **Default**: `{ path: '.gitkeep', contents: '' }`
71 |
72 | ```js
73 | firstCommit.sync('foo/bar', { file: false })
74 | ```
75 |
76 | ### options.message
77 |
78 | **Type**: `string`
79 |
80 | **Default**: `'first commit'`
81 |
82 | ```js
83 | var options = {message: 'my amazing first commit'};
84 |
85 | firstCommit('foo/bar', options, function(err) {
86 | if (err) {
87 | console.log(err);
88 | } else {
89 | console.log('done!');
90 | }
91 | });
92 | ```
93 |
94 | ### options.exec
95 |
96 | **Type**: `object`
97 |
98 | **Default**: `undefined`
99 |
100 | Options to pass to [execSync](https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options).
101 |
102 | ```js
103 | var options = {
104 | message: 'my amazing first commit',
105 | exec: {
106 | timeout: 3000,
107 | killSignal: 'SIGTERM'
108 | }
109 | };
110 |
111 | firstCommit.sync('foo/bar', options);
112 | ```
113 |
114 | ## About
115 |
116 |
117 | Contributing
118 |
119 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
120 |
121 | Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.
122 |
123 |
124 |
125 |
126 | Running Tests
127 |
128 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
129 |
130 | ```sh
131 | $ npm install && npm test
132 | ```
133 |
134 |
135 |
136 |
137 | Building docs
138 |
139 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
140 |
141 | To generate the readme, run the following command:
142 |
143 | ```sh
144 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
145 | ```
146 |
147 |
148 |
149 | ### Related projects
150 |
151 | You might also be interested in these projects:
152 |
153 | * [git-branch](https://www.npmjs.com/package/git-branch): Get the current branch for a local git repository. | [homepage](https://github.com/jonschlinkert/git-branch "Get the current branch for a local git repository.")
154 | * [git-user-name](https://www.npmjs.com/package/git-user-name): Get a user's name from git config at the project or global scope, depending on… [more](https://github.com/jonschlinkert/git-user-name) | [homepage](https://github.com/jonschlinkert/git-user-name "Get a user's name from git config at the project or global scope, depending on what git uses in the current context.")
155 | * [git-username](https://www.npmjs.com/package/git-username): Get the username (or 'owner' name) from a git/GitHub remote origin URL. | [homepage](https://github.com/jonschlinkert/git-username "Get the username (or 'owner' name) from a git/GitHub remote origin URL.")
156 | * [list-git-remotes](https://www.npmjs.com/package/list-git-remotes): List the remotes for a local git repository. Sync and async. | [homepage](https://github.com/jonschlinkert/list-git-remotes "List the remotes for a local git repository. Sync and async.")
157 |
158 | ### Contributors
159 |
160 | | **Commits** | **Contributor** |
161 | | --- | --- |
162 | | 6 | [johno](https://github.com/johno) |
163 | | 6 | [jonschlinkert](https://github.com/jonschlinkert) |
164 |
165 | ### Author
166 |
167 | **Jon Schlinkert**
168 |
169 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
170 | * [GitHub Profile](https://github.com/jonschlinkert)
171 | * [Twitter Profile](https://twitter.com/jonschlinkert)
172 |
173 | ### License
174 |
175 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
176 | Released under the [MIT License](LICENSE).
177 |
178 | ***
179 |
180 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 09, 2018._
--------------------------------------------------------------------------------
/appveyor.yml:
--------------------------------------------------------------------------------
1 | # Test against this version of Node.js
2 | environment:
3 | matrix:
4 | # node.js
5 | - nodejs_version: "9.0"
6 | - nodejs_version: "8.0"
7 | - nodejs_version: "7.0"
8 | - nodejs_version: "6.0"
9 |
10 | # Install scripts. (runs after repo cloning)
11 | install:
12 | # Get the latest stable version of Node.js or io.js
13 | - ps: Install-Product node $env:nodejs_version
14 | # install modules
15 | - npm install
16 | - git config --global user.email "gfc@fakeemail.com"
17 | - git config --global user.name "gfc-test"
18 |
19 | # Post-install test scripts.
20 | test_script:
21 | # Output useful info for debugging.
22 | - node --version
23 | - npm --version
24 | # run tests
25 | - npm test
26 |
27 | # Don't actually build.
28 | build: off
29 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const fs = require('fs');
4 | const util = require('util');
5 | const path = require('path');
6 | const mkdirp = require('mkdirp');
7 | const empty = require('empty-dir');
8 | const isGitUrl = require('is-git-url');
9 | const cp = require('child_process');
10 | const git = fp => path.resolve(fp, '.git');
11 | const defaults = {
12 | message: 'first commit',
13 | file: { path: '.gitkeep', contents: '' }
14 | };
15 |
16 | const firstCommit = async(cwd, options, callback) => {
17 | const stats = util.promisify(fs.stat);
18 | const mkdir = util.promisify(mkdirp);
19 | const exec = util.promisify(cp.exec);
20 |
21 | if (typeof cwd !== 'string') {
22 | return firstCommit(process.cwd(), cwd, options);
23 | }
24 |
25 | if (typeof options === 'function') {
26 | return firstCommit(cwd, null, options);
27 | }
28 |
29 | const opts = Object.assign({ cwd: cwd }, options);
30 | const execOpts = Object.assign({}, opts.exec, { cwd: opts.cwd });
31 |
32 | const promise = stats(git(opts.cwd))
33 | .then(stat => {
34 | throw new Error('.git repository already exists in: ' + git(opts.cwd));
35 | })
36 | .catch(err => {
37 | if (err.code === 'ENOENT') {
38 | return mkdir(opts.cwd);
39 | }
40 | return Promise.reject(err);
41 | })
42 | .then(async() => {
43 | return await exec(createArgs(opts), execOpts);
44 | });
45 |
46 | if (typeof callback === 'function') {
47 | promise.then(res => callback(null, res.stdout, res.stderr)).catch(callback);
48 | return;
49 | }
50 |
51 | return promise;
52 | };
53 |
54 | firstCommit.sync = function(cwd, options) {
55 | if (typeof cwd !== 'string') {
56 | return firstCommit.sync(process.cwd(), cwd);
57 | }
58 |
59 | const opts = Object.assign({ cwd: cwd }, options);
60 | const execOpts = Object.assign({}, opts.exec, { cwd: opts.cwd });
61 |
62 | if (fs.existsSync(git(opts.cwd))) {
63 | throw new Error('.git repository already exists in: ' + git(opts.cwd));
64 | }
65 |
66 | if (!fs.existsSync(cwd)) {
67 | mkdirp.sync(cwd);
68 | }
69 |
70 | return cp.execSync(createArgs(opts), execOpts);
71 | };
72 |
73 | function createArgs(options) {
74 | const opts = Object.assign({}, defaults, options);
75 | const args = ['git init'];
76 | const files = opts.files ? arrayify(opts.files).join(' ') : '.';
77 | let message = opts.message || 'First commit';
78 |
79 | if (message[0] !== '"' && message.slice(-1) !== '"') {
80 | message = `"${message}"`;
81 | }
82 |
83 | // backwards compatibility
84 | if (opts.skipCommit === true) {
85 | opts.commit = false;
86 | }
87 |
88 | if (opts.forceFile === true || (opts.file !== false && isEmpty(opts.cwd))) {
89 | args.push('touch "' + opts.file.path + '"');
90 |
91 | if (opts.file.contents) {
92 | args.push('echo "' + opts.file.contents.toString() + '" >> ' + opts.file.path);
93 | }
94 | }
95 |
96 | if (opts.commit !== false) {
97 | args.push(`git add ${files}`);
98 | args.push(`git commit -m ${message}`);
99 | }
100 |
101 | if (typeof opts.remote === 'string' && isGitUrl(opts.remote)) {
102 | args.push(`git remote add origin ${opts.remote}`);
103 |
104 | if (opts.push === true) {
105 | args.push('git push --force origin master:master');
106 | }
107 | }
108 |
109 | return args.join(' && ');
110 | }
111 |
112 | function isEmpty(cwd) {
113 | return empty.sync(cwd, garbage);
114 | }
115 |
116 | function garbage(filepath) {
117 | return !/(Thumbs\.db|\.DS_Store)$/i.test(filepath);
118 | }
119 |
120 | function arrayify(val) {
121 | return val != null ? (Array.isArray(val) ? val : [val]) : [];
122 | }
123 |
124 | firstCommit.createArgs = createArgs;
125 | module.exports = firstCommit;
126 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "gfc",
3 | "description": "Simple way to initialize a new git repository in an empty directory, add a file and do a first commit (or skip that part in a directory with files). Useful for unit tests and generators.",
4 | "version": "2.0.2",
5 | "homepage": "https://github.com/jonschlinkert/gfc",
6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
7 | "repository": "jonschlinkert/gfc",
8 | "bugs": {
9 | "url": "https://github.com/jonschlinkert/gfc/issues"
10 | },
11 | "license": "MIT",
12 | "files": [
13 | "index.js"
14 | ],
15 | "main": "index.js",
16 | "engines": {
17 | "node": ">=6"
18 | },
19 | "scripts": {
20 | "test": "mocha"
21 | },
22 | "dependencies": {
23 | "empty-dir": "^1.0.0",
24 | "is-git-url": "^1.0.0",
25 | "mkdirp": "^0.5.1"
26 | },
27 | "devDependencies": {
28 | "mocha": "^3.5.3",
29 | "delete": "^1.1.0",
30 | "rimraf": "^2.6.2",
31 | "gitty": "^3.6.0",
32 | "gulp-format-md": "^1.0.0"
33 | },
34 | "keywords": [
35 | "add",
36 | "commit",
37 | "first",
38 | "first-commit",
39 | "git",
40 | "git-add",
41 | "git-commit",
42 | "git-init",
43 | "init"
44 | ],
45 | "verb": {
46 | "toc": false,
47 | "layout": "default",
48 | "tasks": [
49 | "readme"
50 | ],
51 | "plugins": [
52 | "gulp-format-md"
53 | ],
54 | "related": {
55 | "list": [
56 | "git-branch",
57 | "git-user-name",
58 | "git-username",
59 | "list-git-remotes"
60 | ]
61 | },
62 | "lint": {
63 | "reflinks": true
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/test/create-args.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | require('mocha');
4 | const path = require('path');
5 | const assert = require('assert');
6 | const del = require('delete');
7 | const mkdirp = require('mkdirp');
8 | const rimraf = require('rimraf');
9 | const { createArgs } = require('..');
10 | const fixtures = path.join.bind(path, __dirname, 'fixtures');
11 | const cwd = process.cwd();
12 |
13 | describe('.createArgs', function() {
14 | beforeEach(cb => {
15 | rimraf.sync(fixtures());
16 | mkdirp(fixtures('temp'), cb);
17 | });
18 |
19 | afterEach(() => rimraf.sync(fixtures()));
20 |
21 | it('should create exec args with defaults', function() {
22 | const actual = createArgs({ cwd: fixtures('temp') });
23 | const expected = 'git init && touch ".gitkeep" && git add . && git commit -m "first commit"';
24 | assert.equal(actual, expected);
25 | });
26 |
27 | it('should create exec args with custom file.path', function() {
28 | const actual = createArgs({ file: { path: 'foo' }, cwd: fixtures('temp') });
29 | const expected = 'git init && touch "foo" && git add . && git commit -m "first commit"';
30 | assert.equal(actual, expected);
31 | });
32 |
33 | it('should create exec args with custom commit message', function() {
34 | const actual = createArgs({ message: 'foo', cwd: fixtures('temp') });
35 | const expected = 'git init && touch ".gitkeep" && git add . && git commit -m "foo"';
36 | assert.equal(actual, expected);
37 | });
38 |
39 | it('should add a git remote origin', function() {
40 | const actual = createArgs({ remote: 'https://github.com/jonschlinkert/gfc.git', cwd: fixtures('temp') });
41 | const expected = 'git init && touch ".gitkeep" && git add . && git commit -m "first commit" && git remote add origin https://github.com/jonschlinkert/gfc.git';
42 | assert.equal(actual, expected);
43 | });
44 |
45 | it('should add push command if enabled', function() {
46 | const actual = createArgs({ remote: 'https://github.com/jonschlinkert/gfc.git', push: true, cwd: fixtures('temp') });
47 | const expected = 'git init && touch ".gitkeep" && git add . && git commit -m "first commit" && git remote add origin https://github.com/jonschlinkert/gfc.git && git push --force origin master:master';
48 | assert.equal(actual, expected);
49 | });
50 | });
51 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | require('mocha');
4 | const fs = require('fs');
5 | const util = require('util');
6 | const path = require('path');
7 | const cp = require('child_process');
8 | const assert = require('assert');
9 | const rimraf = require('rimraf');
10 | const git = require('gitty');
11 | const firstCommit = require('..');
12 | const fixtures = path.join.bind(path, __dirname, 'fixtures');
13 | let repo;
14 |
15 | describe('git-add-remote', function() {
16 | beforeEach(() => rimraf.sync(fixtures()));
17 | afterEach(() => rimraf.sync(fixtures()));
18 |
19 | describe('async', function() {
20 | it('should throw an error when .git directory exists', function(cb) {
21 | firstCommit(fixtures('temp'), (err, stdout, stderr) => {
22 | if (err) return cb(err);
23 |
24 | firstCommit(fixtures('temp'), err => {
25 | assert(err);
26 |
27 | if (!(err instanceof Error)) {
28 | cb(new Error('expected an error'));
29 | return;
30 | }
31 | cb();
32 | });
33 | });
34 | });
35 |
36 | it('should create a git repository', function(cb) {
37 | firstCommit(fixtures('temp'), function(err) {
38 | if (err) {
39 | cb(err);
40 | return;
41 | }
42 | verify('temp', cb);
43 | });
44 | });
45 |
46 | it('should add a file', function(cb) {
47 | firstCommit(fixtures('temp'), function(err) {
48 | if (err) {
49 | cb(err);
50 | return;
51 | }
52 |
53 | verify('temp', function(log, files) {
54 | assert(Array.isArray(files));
55 | assert.equal(files.length, 1);
56 | assert.equal(files[0], '.gitkeep');
57 | }, cb);
58 | });
59 | });
60 |
61 | it('should add a first commit', function(cb) {
62 | firstCommit(fixtures('temp'), function(err) {
63 | if (err) {
64 | cb(err);
65 | return;
66 | }
67 |
68 | verify('temp', function(log) {
69 | assert(Array.isArray(log));
70 | assert.equal(log.length, 1);
71 | assert.equal(log[0].message, 'first commit');
72 | }, cb);
73 | });
74 | });
75 |
76 | it('should not add a first commit when told not to', function(cb) {
77 | firstCommit(fixtures('temp'), { skipCommit: true }, function(err) {
78 | if (err) {
79 | cb(err);
80 | return;
81 | }
82 |
83 | verify('temp', null, function(err) {
84 | assert(err);
85 | cb();
86 | });
87 | });
88 | });
89 |
90 | it('should customize first commit message', function(cb) {
91 | firstCommit(fixtures('temp'), {message: 'foo'}, function(err) {
92 | if (err) {
93 | cb(err);
94 | return;
95 | }
96 |
97 | verify('temp', function(log) {
98 | assert(Array.isArray(log));
99 | assert.equal(log.length, 1);
100 | assert.equal(log[0].message, 'foo');
101 | }, cb);
102 | });
103 | });
104 | });
105 |
106 | describe('promise', function() {
107 | const verifyP = util.promisify(verify);
108 |
109 | it('should throw an error when .git directory exists', function() {
110 | return firstCommit(fixtures('temp')).then(() => {
111 | return firstCommit(fixtures('temp'))
112 | .then(() => new Error('expected an error'))
113 | .catch(() => {});
114 | });
115 | });
116 |
117 | it('should create a git repository', function(cb) {
118 | return firstCommit(fixtures('temp')).then(() => verify('temp', cb));
119 | });
120 |
121 | it('should add a file', function(cb) {
122 | return firstCommit(fixtures('temp'))
123 | .then(() => {
124 | verify('temp', function(log, files) {
125 | assert(Array.isArray(files));
126 | assert.equal(files.length, 1);
127 | assert.equal(files[0], '.gitkeep');
128 | }, cb);
129 | });
130 | });
131 |
132 | it('should add a first commit', function(cb) {
133 | return firstCommit(fixtures('temp'))
134 | .then(() => {
135 | verify('temp', function(log, files) {
136 | assert(Array.isArray(log));
137 | assert.equal(log.length, 1);
138 | assert.equal(log[0].message, 'first commit');
139 | }, cb);
140 | });
141 | });
142 |
143 | it('should customize first commit message', function(cb) {
144 | return firstCommit(fixtures('temp'), { message: 'foo' })
145 | .then(() => {
146 | verify('temp', function(log, files) {
147 | assert(Array.isArray(log));
148 | assert.equal(log.length, 1);
149 | assert.equal(log[0].message, 'foo');
150 | }, cb);
151 | });
152 | });
153 |
154 | it('should disable first commit', function(cb) {
155 | return firstCommit(fixtures('temp'), { commit: false })
156 | .then(() => verifyP('temp'))
157 | .catch(err => assert(err))
158 | .then(cb);
159 | });
160 | });
161 |
162 | describe('sync', function() {
163 | it('should create a git repository', function(cb) {
164 | firstCommit.sync(fixtures('temp'));
165 | verify('temp', cb);
166 | });
167 |
168 | it('should add a file', function(cb) {
169 | firstCommit.sync(fixtures('temp'));
170 | verify('temp', function(log, files) {
171 | assert(Array.isArray(files));
172 | assert.equal(files.length, 1);
173 | assert.equal(files[0], '.gitkeep');
174 | }, cb);
175 | });
176 |
177 | it('should add a first commit', function(cb) {
178 | firstCommit.sync(fixtures('temp'));
179 | verify('temp', function(log) {
180 | assert(Array.isArray(log));
181 | assert.equal(log.length, 1);
182 | assert.equal(log[0].message, 'first commit');
183 | }, cb);
184 | });
185 |
186 | it('should customize first commit message', function(cb) {
187 | firstCommit.sync(fixtures('temp'), {message: 'foo'});
188 | verify('temp', function(log) {
189 | assert(Array.isArray(log));
190 | assert.equal(log.length, 1);
191 | assert.equal(log[0].message, 'foo');
192 | }, cb);
193 | });
194 |
195 | it('should customize file name', function(cb) {
196 | firstCommit.sync(fixtures('temp'), { file: { path: '.gitkeep' } });
197 | verify('temp', function(log, files) {
198 | assert(Array.isArray(log));
199 | assert.equal(log.length, 1);
200 |
201 | assert(Array.isArray(files));
202 | assert.equal(files.length, 1);
203 | assert.equal(files[0], '.gitkeep');
204 | }, cb);
205 | });
206 | });
207 | });
208 |
209 | function verify(dir, fn, cb) {
210 | var cwd = fixtures(dir);
211 | repo = git(cwd);
212 | if (typeof cb !== 'function') {
213 | cb = fn;
214 | fn = function() {};
215 | }
216 |
217 | fs.stat(fixtures(dir, '.git'), function(err, stat) {
218 | if (err) {
219 | cb(err);
220 | return;
221 | }
222 |
223 | assert(stat);
224 | assert(stat.isDirectory());
225 |
226 | repo.log(function(err, log) {
227 | if (err) {
228 | cb(err);
229 | return;
230 | }
231 |
232 | list(cwd, function(err, files, stderr) {
233 | if (err) {
234 | cb(err, null, stderr);
235 | return;
236 | }
237 |
238 | fn(log, files);
239 | cb();
240 | });
241 | });
242 | });
243 | }
244 |
245 | function list(cwd, cb) {
246 | cp.exec('git ls-files', {cwd: cwd}, function(err, stdout, stderr) {
247 | if (err) {
248 | cb(err, null, stderr);
249 | return;
250 | }
251 | cb(null, stdout.split('\n').filter(Boolean));
252 | });
253 | }
254 |
--------------------------------------------------------------------------------