-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·88 lines (74 loc) · 2.09 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
#!/usr/bin/env node
'use strict'
/**
* Module dependencies.
*/
var fs = require('fs');
var path = require('path');
var program = require('commander');
var multiline = require('multiline');
var _ = require('underscore');
var iconv = require('iconv-lite');
var extend = require('node.extend');
var gh = require("github-url-to-object");
// Set underscore.template options.
_.options = {
matcher: /<%%([^%]+)%>/g,
detecter: /<%%?[^%]+%>/,
start: '<%',
end: '%>'
};
var defaultParameters = {
name: '',
description: '',
repository: {url: '', type: ''},
scripts: {build: '', test: ''},
dependencies: {},
devDependencies: {},
bugs: '',
travisUrl: '',
license: ''
}
program
.version('0.0.1')
.option('-t, --travis', 'add travis badge (default: false)', false)
.option('-f, --file <type>', 'location of package.json', 'package.json');
program.on('--help', function() {
console.log(multiline(function(){/*
Examples:
$ readmify --travis > README.md
$ readmify --file path/to/package.json > README.md
*/}));
});
program.parse(process.argv);
var packageFile = program.file
var pkgPath = path.resolve(process.cwd(), packageFile)
try {
var pkgJson = require(pkgPath);
} catch(e) {
return console.error('Can\'t load ' + pkgPath + '. please check your json syntax.');
}
pkgJson = extend(true, defaultParameters, pkgJson);
if(program.travis){
if(pkgJson.repository.url && gh(pkgJson.repository.url)){
pkgJson.travisUrl = gh(pkgJson.repository.url).travis_url
}else{
return console.error('You must set repository.url option in your package.json with --travis option');
}
}
var tpl = fs.readFileSync(__dirname + "/template.tpl").toString();
var body = _.template(tpl)(pkgJson);
process.stdout.write(body)
function readFile(filepath) {
var contents;
try {
contents = fs.readFileSync(String(filepath));
contents = iconv.decode(contents, 'utf8');
if (contents.charCodeAt(0) === 0xFEFF) {
contents = contents.substring(1);
}
return contents;
} catch (e) {
throw new Error('Unable to read "' + filepath + '" file (Error code: ' + e.code + ').');
}
}