1
|
|
|
import fs from 'fs'; |
2
|
|
|
import path from 'path'; |
3
|
|
|
import process from 'process'; |
4
|
|
|
import { all, promisify, reject } from 'bluebird'; |
5
|
|
|
import { |
6
|
|
|
append, |
7
|
|
|
dropWhile, |
8
|
|
|
equals, |
9
|
|
|
join, |
10
|
|
|
juxt, |
11
|
|
|
last, |
12
|
|
|
map, |
13
|
|
|
merge, |
14
|
|
|
pick, |
15
|
|
|
prop, |
16
|
|
|
replace, |
17
|
|
|
split |
18
|
|
|
} from 'ramda'; |
19
|
|
|
import semver from 'semver'; |
20
|
|
|
import superagent from 'superagent'; |
21
|
|
|
import inquirer from 'inquirer'; |
22
|
|
|
import { emitSuccess } from './input'; |
23
|
|
|
|
24
|
|
|
const request = superagent.agent(); |
25
|
|
|
const createFolder = promisify(fs.mkdir); |
26
|
|
|
const createFile = promisify(fs.writeFile); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Formats a formatted String |
30
|
|
|
* |
31
|
|
|
* @param {String} source |
32
|
|
|
* @return {String} |
33
|
|
|
*/ |
34
|
|
|
const format = replace(/\n {8}/g, '\n') |
35
|
|
|
& dropWhile(equals('\n')) |
36
|
|
|
& append('\n') |
37
|
|
|
& join(''); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Generate the answers from the stdin. |
41
|
|
|
* |
42
|
|
|
* @param {IO} io |
|
|
|
|
43
|
|
|
* @return {Promise} |
44
|
|
|
*/ |
45
|
|
|
function askQuestions() { |
46
|
|
|
return request.get('https://app.rung.com.br/api/categories') |
47
|
|
|
.then(prop('body') & map(({ name, alias: value }) => ({ name, value }))) |
|
|
|
|
48
|
|
|
.then(categories => [ |
49
|
|
|
{ name: 'name', message: 'Project name', default: process.cwd() | split('/') | last }, |
50
|
|
|
{ name: 'version', message: 'Version', default: '1.0.0', validate: semver.valid & Boolean }, |
51
|
|
|
{ name: 'title', message: 'Title', default: 'Untitled' }, |
52
|
|
|
{ name: 'description', message: 'Description' }, |
53
|
|
|
{ name: 'category', type: 'list', message: 'Category', default: 'miscellaneous', choices: categories }, |
54
|
|
|
{ name: 'license', message: 'license', default: 'MIT' } |
55
|
|
|
]) |
56
|
|
|
.then(inquirer.createPromptModule()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates the folder for the boilerplate based on package name. If the folder |
61
|
|
|
* already exists, throw an error |
62
|
|
|
* Queria estar morta |
63
|
|
|
* |
64
|
|
|
* @param {Object} answers |
65
|
|
|
* @return {Promise} |
66
|
|
|
*/ |
67
|
|
|
function createBoilerplateFolder(answers) { |
68
|
|
|
return createFolder(answers.name) |
69
|
|
|
.catch(~reject(new Error(`Unable to create folder ${answers.name}`))) |
70
|
|
|
.return(answers); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns an object in the format { filename :: String, content :: String } |
75
|
|
|
* containing meta-informations about the file |
76
|
|
|
* |
77
|
|
|
* @param {Object} answers |
78
|
|
|
* @return {Object} |
79
|
|
|
*/ |
80
|
|
|
function getPackageMetaFile(answers) { |
81
|
|
|
const packageFields = ['name', 'version', 'license', 'category']; |
82
|
|
|
const packageObject = merge(pick(packageFields, answers), |
83
|
|
|
{ dependencies: { 'rung-cli': '0.9.4' } }); |
84
|
|
|
|
85
|
|
|
return { |
86
|
|
|
filename: path.join(answers.name, 'package.json'), |
87
|
|
|
content: JSON.stringify(packageObject, null, 2) |
88
|
|
|
}; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Content about README.md file |
93
|
|
|
* |
94
|
|
|
* @param {Object} answers |
95
|
|
|
* @return {Object} |
96
|
|
|
*/ |
97
|
|
|
function getReadMeMetaFile(answers) { |
98
|
|
|
const content = format(` |
99
|
|
|
# Rung ─ ${answers.title} |
100
|
|
|
|
101
|
|
|
# Development |
102
|
|
|
|
103
|
|
|
- Use \`yarn\` to install the dependencies |
104
|
|
|
- Use \`rung run\` to start the CLI wizard |
105
|
|
|
`); |
106
|
|
|
|
107
|
|
|
return { filename: path.join(answers.name, 'README.md'), content }; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Content about index.js file |
112
|
|
|
* |
113
|
|
|
* @param {Object} answers |
114
|
|
|
* @return {Object} |
115
|
|
|
*/ |
116
|
|
|
function getIndexFile(answers) { |
117
|
|
|
const content = format(` |
118
|
|
|
import { create } from 'rung-sdk'; |
119
|
|
|
import { String as Text } from 'rung-cli/dist/types'; |
120
|
|
|
|
121
|
|
|
function render(name) { |
122
|
|
|
return <b>{ _('Hello {{name}}', { name }) }</b>; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
function main(context) { |
126
|
|
|
const { name } = context.params; |
127
|
|
|
return { |
128
|
|
|
alerts: [{ |
129
|
|
|
title: _('Welcome'), |
130
|
|
|
content: render(name), |
131
|
|
|
resources: [] |
132
|
|
|
}] |
133
|
|
|
}; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
const params = { |
137
|
|
|
name: { |
138
|
|
|
description: _('What is your name?'), |
139
|
|
|
type: Text |
140
|
|
|
} |
141
|
|
|
}; |
142
|
|
|
|
143
|
|
|
export default create(main, { |
144
|
|
|
params, |
145
|
|
|
primaryKey: true, |
146
|
|
|
title: _(${JSON.stringify(answers.title)}), |
147
|
|
|
description: _(${JSON.stringify(answers.description)}), |
148
|
|
|
preview: render('Trixie') |
149
|
|
|
}); |
150
|
|
|
`); |
151
|
|
|
|
152
|
|
|
return { filename: path.join(answers.name, 'index.js'), content }; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Creates a boilerplate project |
157
|
|
|
* |
158
|
|
|
* @return {Promise} |
159
|
|
|
*/ |
160
|
|
|
export default function boilerplate() { |
161
|
|
|
return askQuestions() |
162
|
|
|
.then(createBoilerplateFolder) |
163
|
|
|
.then(juxt([getPackageMetaFile, getReadMeMetaFile, getIndexFile])) |
164
|
|
|
.then(map(({ filename, content }) => createFile(filename, content)) & all) |
165
|
|
|
.then(~emitSuccess('project generated')); |
166
|
|
|
} |
167
|
|
|
|