Skip to content

Commit

Permalink
✨ Create sample file on install
Browse files Browse the repository at this point in the history
Issue: #34

 - Create .git-coauthors file with sample file after install if it doesn't exist
 - function to check if authors file exists

Co-authored-by: Jack Bittiner <jackbittiner@hotmail.com>
Co-authored-by: Richard Kotze <rkotze@findmypast.com>
  • Loading branch information
3 people committed Jan 14, 2019
1 parent 62aac4e commit 4b8150b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"test": "env-cmd test-helpers/.env ava --verbose --serial && npm run js-lint",
"test:w": "env-cmd test-helpers/.env ava --watch",
"js-lint": "xo",
"preversion": "npm test -s"
"preversion": "npm test -s",
"postinstall": "node ./src/install/create-author-file.js"
},
"bin": {
"git-mob": "bin/mob.js",
Expand Down
12 changes: 8 additions & 4 deletions src/git-authors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function gitAuthors(readFilePromise, writeFilePromise) {
}
}

async function writeToFile(path) {
async function writeToFile(path, content) {
const writeToPromise = writeFilePromise || promisify(fs.appendFile);
try {
return await writeToPromise(path, 'utf8');
return await writeToPromise(path, content, 'utf8');
} catch (err) {
throw new Error(err.message);
}
Expand All @@ -37,12 +37,16 @@ function gitAuthors(readFilePromise, writeFilePromise) {

write: async authorJson => {
try {
return writeToFile(gitCoauthorsPath, JSON.stringify(authorJson));
return writeToFile(gitCoauthorsPath, JSON.stringify(authorJson, null, 2));
} catch (err) {
throw new Error('Invalid JSON ' + err.message);
}
},

fileExists: () => {
return fs.existsSync(gitCoauthorsPath);
},

coAuthors(authorInitials, authorJson) {
const { coauthors } = authorJson;
return authorInitials.map(initials => {
Expand All @@ -68,4 +72,4 @@ function missingAuthorError(initials, coauthors) {
}
}

module.exports = { gitAuthors };
module.exports = { gitAuthors, gitCoauthorsPath };
18 changes: 17 additions & 1 deletion src/install/create-author-file.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
const DUMMY_CONTENT = {
const { gitAuthors, gitCoauthorsPath } = require('../git-authors');

const SAMPLE_CONTENT = {
coauthors: {
hh: {
name: 'Hulk Hogan',
email: 'hulk_hogan22@hotmail.org',
},
},
};

createFileIfNotExist();

async function createFileIfNotExist() {
const instance = gitAuthors();
if (!instance.fileExists()) {
try {
await instance.write(SAMPLE_CONTENT);
console.log('Add co-authors to: ', gitCoauthorsPath);
} catch (err) {
console.log('Someting went wrong adding a new co-authors file, error: ', err);
}
}
}

0 comments on commit 4b8150b

Please sign in to comment.