-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.js
71 lines (55 loc) · 2.29 KB
/
template.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
const { join, dirname } = require('path')
const { promisify } = require('util')
const { readFile, writeFile } = require('fs')
const glob = require('fast-glob')
const mkdirp = require('mkdirp')
const questions = require('./questions')
// promise helpers
const read = promisify(readFile)
const write = promisify(writeFile)
// path to template directory
const template = join(__dirname, '..', 'template')
// main
module.exports = async function main(argv) {
// create root folder
await mkdirp(argv.path)
let pkg = {}
try {
pkg = require(join(process.cwd(), 'package.json')) // eslint-disable-line import/no-dynamic-require
} catch (e) {} // eslint-disable-line no-empty
// collect info about the project
const answers = await questions(pkg)
// update author info
const license = answers.license.licenseText
.replace(/< ?year ?>/i, new Date().getFullYear())
.replace(/<copyright holders?>/i, 'TELUS Telecommunications Inc.')
// update answers object
answers.license = answers.license.key
answers.private = !answers.oss
answers.access = answers.oss ? 'public' : 'restricted'
answers.keywords = JSON.stringify(['telus'].concat(answers.topics))
// write license file
await write(join(argv.path, 'LICENSE'), `${license}\n`)
// get all template files
const stream = glob.stream('**/*', { cwd: template, dot: true })
stream.on('data', async (file) => {
// Needed to add package to be renamed to package.json because
// whenever we were about to publish this package, the package.json
// of the template will affect what the main package.json will be able
// to add to the published package.
// and also while trying not to set developers to fail by publishing everything
// themselves.
const rename = { gitignore: '.gitignore', package: 'package.json' }
const dir = dirname(file)
// create directory if none-exists
if (dir !== '.') await mkdirp(join(argv.path, dir))
// read file content
let content = await read(join(template, file), 'utf8')
// replace each template key
Object.entries(answers).forEach(([key, value]) => {
content = content.replace(new RegExp(`\\[${key}\\]`, 'g'), value)
})
// write file to destination
write(join(argv.path, Object.keys(rename).includes(file) ? rename[file] : file), content)
})
}