This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (56 loc) · 1.61 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
var fs = require('fs')
var path = require('path')
var format = require('util').format
var template =
'# %s Change Log\n' +
'All notable changes to this project will be documented in this file.\n' +
'This project adheres to [Semantic Versioning](http://semver.org/).\n' +
'\n' +
'## %s - %s\n' +
'* ...\n'
function getPackage (dir, cb) {
var packagePath = path.resolve(dir, 'package.json')
fs.readFile(packagePath, function (err, data) {
if (err) return cb(err)
var pkg = JSON.parse(data)
cb(null, pkg)
})
}
function getData (dir, cb) {
var name
var version
getPackage(dir, function (err, pkg) {
if (err) {
console.warn('No pacakge.json found')
name = path.basename(dir)
version = '1.0.0'
} else {
name = pkg.name
version = pkg.version
}
var now = new Date()
var dateStamp = now.toISOString().split('T')[0]
var output = format(template, name, version, dateStamp)
cb(output)
})
}
function checkForExisting (logPath, cb) {
fs.stat(logPath, function (err, stats) {
if (err && err.code === 'ENOENT') return cb()
else return cb(new Error('CHANGELOG.md already exists', stats || err))
})
}
function init (dir, opts, cb) {
var clPath = path.join(dir, 'CHANGELOG.md')
checkForExisting(clPath, function (err, msg) {
if (err && opts.force === false) return cb(err)
else if (err && opts.force === true) console.warn('Force overwriting existing changelog')
getData(dir, function (output) {
fs.writeFile(clPath, output, function (err) {
if (err) return cb(err)
cb(null, clPath)
})
})
})
}
module.exports = init