-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
125 lines (99 loc) · 3.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
const fs = require('fs');
const util = require('util');
const path = require('path');
const mkdirp = require('mkdirp');
const empty = require('empty-dir');
const isGitUrl = require('is-git-url');
const cp = require('child_process');
const git = fp => path.resolve(fp, '.git');
const defaults = {
message: 'first commit',
file: { path: '.gitkeep', contents: '' }
};
const firstCommit = async(cwd, options, callback) => {
const stats = util.promisify(fs.stat);
const mkdir = util.promisify(mkdirp);
const exec = util.promisify(cp.exec);
if (typeof cwd !== 'string') {
return firstCommit(process.cwd(), cwd, options);
}
if (typeof options === 'function') {
return firstCommit(cwd, null, options);
}
const opts = Object.assign({ cwd: cwd }, options);
const execOpts = Object.assign({}, opts.exec, { cwd: opts.cwd });
const promise = stats(git(opts.cwd))
.then(stat => {
throw new Error('.git repository already exists in: ' + git(opts.cwd));
})
.catch(err => {
if (err.code === 'ENOENT') {
return mkdir(opts.cwd);
}
return Promise.reject(err);
})
.then(async() => {
return await exec(createArgs(opts), execOpts);
});
if (typeof callback === 'function') {
promise.then(res => callback(null, res.stdout, res.stderr)).catch(callback);
return;
}
return promise;
};
firstCommit.sync = function(cwd, options) {
if (typeof cwd !== 'string') {
return firstCommit.sync(process.cwd(), cwd);
}
const opts = Object.assign({ cwd: cwd }, options);
const execOpts = Object.assign({}, opts.exec, { cwd: opts.cwd });
if (fs.existsSync(git(opts.cwd))) {
throw new Error('.git repository already exists in: ' + git(opts.cwd));
}
if (!fs.existsSync(cwd)) {
mkdirp.sync(cwd);
}
return cp.execSync(createArgs(opts), execOpts);
};
function createArgs(options) {
const opts = Object.assign({}, defaults, options);
const args = ['git init'];
const files = opts.files ? arrayify(opts.files).join(' ') : '.';
let message = opts.message || 'First commit';
if (message[0] !== '"' && message.slice(-1) !== '"') {
message = `"${message}"`;
}
// backwards compatibility
if (opts.skipCommit === true) {
opts.commit = false;
}
if (opts.forceFile === true || (opts.file !== false && isEmpty(opts.cwd))) {
args.push('touch "' + opts.file.path + '"');
if (opts.file.contents) {
args.push('echo "' + opts.file.contents.toString() + '" >> ' + opts.file.path);
}
}
if (opts.commit !== false) {
args.push(`git add ${files}`);
args.push(`git commit -m ${message}`);
}
if (typeof opts.remote === 'string' && isGitUrl(opts.remote)) {
args.push(`git remote add origin ${opts.remote}`);
if (opts.push === true) {
args.push('git push --force origin master:master');
}
}
return args.join(' && ');
}
function isEmpty(cwd) {
return empty.sync(cwd, garbage);
}
function garbage(filepath) {
return !/(Thumbs\.db|\.DS_Store)$/i.test(filepath);
}
function arrayify(val) {
return val != null ? (Array.isArray(val) ? val : [val]) : [];
}
firstCommit.createArgs = createArgs;
module.exports = firstCommit;