Skip to content

Commit 7dd7578

Browse files
committed
added made an example config optional
1 parent 28c305d commit 7dd7578

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

man/git-better-init.1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
git better-init \- Creates a config file based off of an example config
44
.SH SYNOPSIS
55
.B git better-init
6-
.IR [--global]\ exampleName
6+
.IR [--global]\ [exampleName]
77
.SH DESCRIPTION
88
.B
9-
Creates a config file based off of an example config within the current repo. When passing the `--global` flag, it will create a config file in the user's home directory for use in all projects.
9+
Creates a config file based off of an example config within the current repo. If no example is provided a blank config file is created. When passing the `--global` flag, it will create a config file in the user's home directory for use in all projects.
1010
.SH OPTIONS
1111
.TP
1212
.BR \-g ", " \-\-global

src/config/index.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ module.exports.getConfig = async() => {
5050
/**
5151
* Initializes a config for the tool by copying an example config
5252
*
53-
* @param {string} example - name of the example to base the config on
53+
* @param {string} [example] - name of the example to base the config on
5454
* @param {Object} [options] - any options required
5555
* @param {boolean} [options.global] - if true, the config will be placed in the current users home directory
5656
* @returns {Promise<void>} - When complete
5757
*/
58-
module.exports.initialize = async function(example, options = {}) {
58+
module.exports.initialize = async function(example = false, options = {}) {
5959
const exampleDir = `${__dirname}/../../example-configs`;
6060
let path;
6161

@@ -73,14 +73,29 @@ module.exports.initialize = async function(example, options = {}) {
7373
throw new Error('Config file already exists. Please remove it if you\'d like to initialize a new config.');
7474
}
7575

76-
// check to see if theres an example for the example name the user passed
77-
const examples = fs.readdirSync(exampleDir)
78-
.map((filename) => _.replace(filename, '.json', ''));
7976

80-
if (!examples.includes(example)) {
81-
throw new Error(`An example by that name does not exist. Possible examples are: ${examples.join(', ')}`);
77+
78+
// no example provided
79+
if (!example) {
80+
81+
// lets just make a blank config file
82+
fs.writeFileSync(path, '{\n\n}');
83+
84+
// if we want to use an example
85+
} else {
86+
87+
// check to see if theres an example for the example name the user passed
88+
const examples = fs.readdirSync(exampleDir)
89+
.map((filename) => _.replace(filename, '.json', ''));
90+
91+
if (examples.includes(example)) {
92+
93+
// copy the example to the desired location
94+
fs.copyFileSync(`${exampleDir}/${example}.json`, path);
95+
} else {
96+
throw new Error(`An example by that name does not exist. Possible examples are: ${examples.join(', ')}`);
97+
}
8298
}
8399

84-
// copy the example to the desired location
85-
fs.copyFileSync(`${exampleDir}/${example}.json`, path);
100+
86101
};

0 commit comments

Comments
 (0)