-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·224 lines (190 loc) · 5.54 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
const program = require('commander')
const lodash = require('lodash')
const fs = require('fs-extra')
const path = require('path')
const camelCase = require('uppercamelcase')
const readDir = require('recursive-readdir')
const directoryExists = require('directory-exists')
const validateNpmPackageName = require('validate-npm-package-name')
const chalk = require('chalk')
const ora = require('ora')
const inquirer = require('inquirer')
const columnify = require('columnify')
const pkg = require('./package.json')
const exec = require('child_process').exec
lodash.templateSettings.interpolate = /<%=([\s\S]+?)%>/g
const spinner = ora()
const originalCwd = process.cwd()
let organization
let libraryName
let packageName
let componentName
let repository
let repositoryFull
let repositoryUser
let repositoryName
let templatePath
let projectCwd
program
.version(pkg.version)
.arguments('<library-name>')
.action(name => {
createProject(name)
})
program.parse(process.argv)
if (!libraryName) {
program.help()
}
function validateLibraryName() {
const validation = validateNpmPackageName(libraryName)
if (validation.errors) {
console.error(chalk.red('Project name can only contain URL-friendly characters'))
process.exit(1)
}
}
function validateProjectDir() {
const exists = directoryExists.sync(projectCwd)
if (exists) {
console.error(chalk.red(`Folder ${libraryName} already exists`))
process.exit(1)
}
}
function createProject(name) {
console.log('')
console.log(`${chalk.yellow(pkg.name)} v${pkg.version}\n`)
libraryName = name
componentName = camelCase(libraryName)
projectCwd = path.join(process.cwd(), libraryName)
templatePath = path.resolve(__dirname, 'template')
validateLibraryName()
validateProjectDir()
inquirer.prompt([
{
type: 'input',
name: 'organization',
message: 'NPM organization?',
default: '5rabbits',
validate: answer => (
!!validateNpmPackageName(answer).errors
? 'Invalid organization name. It should be URL friendly.'
: true
)
},
{
type: 'input',
name: 'repository',
message: 'Github repository?',
default: `5rabbits/${name}`,
validate: answer => (
answer.split('/').length === 2
? true
: 'Invalid repository name. Should be in format USER/REPOSITORY_NAME.'
)
},
])
.then(answers => {
const repositorySegments = answers.repository.split('/')
organization = answers.organization
repository = answers.repository
packageName = `@${organization}/${libraryName}`
repositoryUser = repositorySegments[0]
repositoryName = repositorySegments[1]
repositoryFull = `https://github.com/${repository}`
})
.then(createProjectFiles)
.then(installDependencies)
.then(prettify)
.then(createGitRepo)
.then(printSuccess)
}
function printSuccess() {
console.log('')
console.log(chalk.green(`🦄 Your new project ${chalk.bold(libraryName)} is ready to start!`))
console.log('')
console.log(columnify({
[chalk.bold.yellow('Library name:')]: packageName,
[chalk.bold.yellow('Repository url:')]: repositoryFull,
}, {
showHeaders: false,
}))
console.log('')
console.log(chalk.underline('What now?'))
console.log('')
console.log('🤯 Move to the project directory')
console.log(chalk.cyan(` $ cd ${libraryName}`))
console.log('')
console.log('🤖 Start developing')
console.log(chalk.cyan(' $ yarn start'))
console.log('')
console.log('🐛 Run the tests')
console.log(chalk.cyan(' $ yarn test --watch'))
console.log('')
console.log('🎉 Publish a new version to the world')
console.log(chalk.cyan(' $ yarn publish'))
console.log('')
return Promise.resolve()
}
function prettify() {
spinner.start('Prettifying code')
return projectExec('yarn prettify')
.then(() => spinner.succeed('Code prettified'))
}
function installDependencies() {
spinner.start('Installing dependencies')
return projectExec('yarn')
.then(() => spinner.succeed('Dependencies installed'))
}
function createGitRepo() {
spinner.start('Creating git repo')
return projectExec('git init')
.then(() => projectExec('git add .'))
.then(() => projectExec('git commit -m "Initial commit"'))
.then(() => projectExec(`git remote add origin git@github.com:${repository}.git`))
.then(() => spinner.succeed('Git repo initialized'))
}
function createProjectFiles(name) {
return new Promise(resolve => {
spinner.start(`Creating project ${chalk.bold(libraryName)}`)
const templateValues = {
libraryName,
packageName,
componentName,
organization,
repository,
repositoryFull,
repositoryUser,
repositoryName,
}
readDir(templatePath)
.then(files => {
files.forEach(file => {
const filePath = file.replace(templatePath, '')
const fileContents = fs.readFileSync(file)
fs.outputFileSync(
path.join(projectCwd, evaluateFilePath(filePath)),
lodash.template(fileContents)(templateValues)
)
})
spinner.succeed(`Project ${chalk.bold(libraryName)} created`)
resolve()
})
})
}
function projectExec(command) {
return new Promise((resolve, reject) => {
exec(command, { cwd: projectCwd }, function(error, stdout) {
if (error) {
reject(error)
}
else {
resolve(stdout)
}
})
})
}
function evaluateFilePath(filePath) {
return filePath
.replace(/_COMPONENT_/g, componentName)
.replace(/_RENAME_/g, '')
}