-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·93 lines (78 loc) · 2.22 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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
function log (message) {
console.log(`@geut/create-module - ${message}`)
}
function mkdir (dir) {
try {
fs.mkdirSync(dir, { recursive: true, mode: 0o755 })
} catch (err) {
if (err.code !== 'EEXIST') {
throw err
}
}
}
function copyDir (src, dest) {
mkdir(dest)
const files = fs.readdirSync(src)
for (let i = 0; i < files.length; i++) {
const current = fs.lstatSync(path.join(src, files[i]))
if (current.isDirectory()) {
copyDir(path.join(src, files[i]), path.join(dest, files[i]))
} else if (current.isSymbolicLink()) {
const symlink = fs.readlinkSync(path.join(src, files[i]))
fs.symlinkSync(symlink, path.join(dest, files[i]))
} else {
let filename = files[i]
if (filename === '_package.json') {
filename = 'package.json'
} else if (filename.startsWith('_')) {
filename = '.' + filename.slice(1)
}
const pathname = path.join(dest, filename)
fs.copyFileSync(path.join(src, files[i]), pathname)
log(`Copy ${pathname}`)
}
}
}
function dirIsEmpty (directory) {
try {
const files = fs.readdirSync(directory)
return !files.length
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
return true
}
function editFile (filepath, transform) {
fs.writeFileSync(filepath, transform(fs.readFileSync(filepath)))
}
const moduleName = process.argv[2]
if (!moduleName) {
log('You need to set a name for your module.')
process.exit(1)
}
const dest = path.join(process.cwd(), process.argv[2] || '.')
try {
log(`Creating module in ${dest}.`)
if (dirIsEmpty(dest)) {
copyDir(path.join(__dirname, 'templates'), dest)
;['CONTRIBUTING.md', 'package.json', 'README.md'].forEach(filepath => {
editFile(path.join(dest, filepath), data => {
return data.toString('utf-8').replace(/<moduleName>/gi, moduleName)
})
})
editFile(path.join(dest, 'LICENSE'), data => {
return data.toString('utf-8').replace(/<year>/gi, new Date().getFullYear())
})
log('Module created.')
} else {
log('Destination directory is not empty.')
}
} catch (err) {
log(err.message)
process.exit(1)
}