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 | 1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
4x
2x
2x
2x
1x
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
8x
7x
8x
8x
| module.exports.usage = `Set up a new service
Usage: api init
Asks a series of questions which will be used to generate a new service in the current directory`;
module.exports.category = 'basic';
module.exports.weight = 1;
require('colors');
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const utils = require('../utils/utils');
const validName = require('validate-npm-package-name');
const createEnquirer = require('../lib/enquirer');
const packageJson = require('../lib/package-json');
const exit = require('../utils/exit');
module.exports.run = () => {
const enquirer = createEnquirer();
console.log('');
console.log('Let\'s create your new API!'.cyan);
console.log('');
console.log('ReadMe Build is a really, really simple way to create awesome APIs.');
console.log(`Learn more at ${'https://readme.build'.underline}`.grey);
console.log('');
let existingPackageJson;
try {
existingPackageJson = require(path.join(process.cwd(), 'package.json'));
} catch (e) {
existingPackageJson = {};
}
return enquirer.ask(module.exports.questions(existingPackageJson)).then(module.exports.init);
};
module.exports.questions = (existingPackageJson) => {
return [
{
type: 'input',
name: 'name',
message: 'Name your service',
default: existingPackageJson.name || path.basename(process.cwd()).toLowerCase(),
validate: (input) => {
const valid = validName(input);
Eif (!valid.validForNewPackages) {
// npms package used to allow this, so the warning
// isn't very good for our case
return (valid.errors || valid.warnings.map(warning => warning.replace('can no longer', 'cannot'))).join('\n');
}
return true;
},
},
{
type: 'input',
name: 'version',
message: 'Version number',
default: existingPackageJson.version || '0.0.1',
validate: (v) => {
if (!semver.valid(v)) {
return `${v} is not a valid semver version`;
}
return true;
},
},
{
type: 'input',
name: 'action',
message: 'What is the name of your first endpoint?',
default: 'helloWorld',
},
{
type: 'list',
name: 'continue',
message: 'It seems like there are already files here. Do you want to create a new directory?',
choices: ['yes', 'no', 'abort'],
when: () => fs.readdirSync(process.cwd()).length,
},
];
};
module.exports.init = (answers) => {
Iif (answers.continue === 'abort') {
exit(0);
}
const data = fs.readFileSync(path.join(__dirname, '../utils/stub.js'), 'utf8');
const stub = data.replace(/<<action>>/g, answers.action);
let chdirMessage = '';
Iif (answers.continue === 'yes') {
const newPath = `./${answers.name}`;
fs.mkdirSync(newPath);
process.chdir(newPath);
chdirMessage = `\n ${'$'.grey} ${'cd'.yellow} ${answers.name.yellow}\n`;
}
fs.mkdirSync('./endpoints');
fs.writeFileSync(`./endpoints/${answers.action}.js`, stub);
// Need to initialise the package.json object down here
// so that process.cwd() is different in tests
const pjson = packageJson();
pjson.set('name', answers.name);
pjson.set('version', answers.version);
pjson.write();
// Only add a readme if one does not exist
if (!fs.existsSync('readme.md')) {
fs.writeFileSync('readme.md', `# ${answers.name}\n\nWelcome to ${answers.name}\n`);
}
Eif (process.env.NODE_ENV === 'testing') {
return undefined;
}
const filename = `./endpoints/${answers.action}.js`;
const name = (utils.getGitConfig('user.name') || 'Julie').split(' ')[0];
return console.log(`\n${'============================='.grey}
${'Great! We\'ve set up your api!'.green}
1. Try running it locally first:
${chdirMessage}
${'$'.grey} ${(`api local ${answers.action} name=${name}`).yellow}
2. Edit ${filename.cyan} and build your API!
3. When you're ready to release, run:
${'$'.grey} ${('api deploy').yellow}
`);
};
|