This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
152 lines (120 loc) · 3.46 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var fs = require('fs')
var path = require('path')
var util = require('util')
var extend = util._extend
var EventEmitter = require('events').EventEmitter
var camelCase = require('camel-case')
var cd = require('shelljs').cd
var exec = require('shelljs').exec
var fixpack = require('fixpack')
var mkdirp = require('mkdirp')
var templates = require('./templates')
var OPTIONS = {
required: [
'pkgName',
'usrName',
'usrEmail',
'usrGithub'
],
valid: {
pkgLicense: ['Apache-2.0', 'BSD-3-Clause', 'CC0-1.0', 'ISC', 'MIT', 'UNLICENSED'],
pkgLinter: ['standard', 'semistandard']
},
defaults: {
'dir': process.cwd(),
'gitInit': true,
'pkgContributing': true,
'pkgLicense': 'ISC',
'pkgLinter': 'standard',
'pkgVersion': '1.0.0',
'npmInstall': true
}
}
function ModuleInit (data, cb) {
if (!(this instanceof ModuleInit)) return new ModuleInit(data, cb)
this.set(data)
this.cb = cb || function noop () {}
}
util.inherits(ModuleInit, EventEmitter)
ModuleInit.OPTIONS = OPTIONS
ModuleInit.prototype.set = function set (data) {
var defaults = extend({}, OPTIONS.defaults)
if (this.data) extend(this.data, data || {})
else this.data = extend(defaults, data || {})
}
ModuleInit.prototype.validate = function validate () {
var data = this.data
var missing = []
var invalid = []
function checkMissing (opt) {
if (!data[opt]) missing.push(opt)
}
function validateInput (opt) {
if (OPTIONS.valid[opt].indexOf(data[opt]) === -1) {
invalid.push(opt)
}
}
OPTIONS.required.forEach(checkMissing)
Object.keys(OPTIONS.valid).forEach(validateInput)
return {
invalid: invalid,
missing: missing
}
}
ModuleInit.prototype.run = function run () {
var cwd = process.cwd()
var differentDir = this.data.dir !== cwd
var errors = this.validate()
var err
if (errors.missing.length) {
err = new Error('missing required options: ' + errors.missing.join(', '))
this.emit('err', err)
return this.cb(err)
}
if (errors.invalid.length) {
err = new Error('invalid options: ' + errors.invalid.join(', '))
this.emit('err', err)
return this.cb(err)
}
this.data.nodeName = this.data.nodeName || camelCase(this.data.pkgName)
this.data.year = this.data.year || new Date().getFullYear()
if (differentDir) {
mkdirp.sync(this.data.dir)
cd(this.data.dir)
}
if (this.data.gitInit) exec('git init')
createIndex.apply(this)
createTestDir.apply(this)
templates.forEach(createFileFromTemplate.bind(this))
fixpack(path.resolve('package.json'), { quiet: true })
if (this.data.npmInstall) exec('npm install')
if (differentDir) cd(cwd)
this.emit('done', this.data)
return this.cb(null, this.data)
}
function createFileFromTemplate (tpl) {
if (this.data[tpl.name] === false) return
var filePath = path.resolve(tpl.file)
if (fs.existsSync(filePath)) {
return this.emit('warn', tpl.file + ' already exists')
}
fs.writeFileSync(filePath, tpl.template(this.data))
this.emit('create', tpl.file)
}
function createIndex () {
var filePath = path.resolve('index.js')
if (fs.existsSync(filePath)) {
return this.emit('warn', 'index.js already exists')
}
fs.writeFileSync(filePath, '\n')
this.emit('create', 'index.js')
}
function createTestDir () {
var dirPath = path.resolve('test')
if (fs.existsSync(dirPath)) {
return this.emit('warn', 'test directory already exists')
}
fs.mkdirSync(dirPath)
this.emit('create', 'test/')
}
module.exports = ModuleInit