-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
68 lines (60 loc) · 1.84 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
#!/usr/bin/env node
var fs = require('fs')
var hostedGitInfo = require('hosted-git-info')
var path = require('path')
var yargs = require('yargs')
.usage('Usage: $0 [repository] [options]')
.example('$0 wealljs/weallcontribute > contrib.md', 'Output a contrib doc to stdout and pipe to contrib.md')
.example('$0 -o .', 'Read project repo from package.json or git config, then write `CONTRIBUTING.md` to current directory (.).')
.alias('o', 'output')
.nargs('o', 1)
.normalize('o')
.describe('o', 'Output to directory or make new file.')
.help()
var repo = yargs.argv._[0]
if (!repo) {
try {
repo = require(path.join(process.cwd(), 'package.json')).repository
repo = repo.url || repo
console.warn('Using project repo from package.json')
} catch (e) { console.error(e) }
}
if (!repo) {
try {
repo = require('child_process').spawnSync('git', ['remote', '-v'], {encoding: 'utf8'}).output[1].match(/origin\s+([^\s]+)\s/)[1]
console.warn('Using project repo from current git repo')
} catch (e) {}
}
if (!repo) {
console.error('Unable to figure out your repo.\n')
yargs.showHelp()
process.exit(1)
}
repo = hostedGitInfo.fromUrl(repo)
if (!repo) {
console.error('Invalid repository url or shorthand.\n')
yargs.showHelp()
process.exit(1)
}
if (repo.type !== 'github') {
console.error('Only github repositories are supported (for now).\n')
yargs.showHelp()
process.exit(1)
}
var contrib = fs.readFileSync(
path.join(__dirname, 'CONTRIBUTING.md'),
'utf8'
).replace(/wealljs\/weallcontribute/gi, repo.path())
if (!yargs.argv.output) {
console.log(contrib)
process.exit(0)
}
var target = path.resolve(yargs.argv.output)
try {
if (fs.statSync(target).isDirectory()) {
target = path.join(target, 'CONTRIBUTING.md')
}
} catch (e) {}
fs.writeFileSync(target, contrib)
console.log('Contrib docs written to', target)
process.exit(0)