-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
service.js
105 lines (95 loc) · 3.4 KB
/
service.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
'use strict';
const { join } = require('path');
const chalk = require('chalk');
const fs = require('fs');
const inquirer = require('@serverless/utils/inquirer');
const resolveConfigurationPath = require('../resolve-configuration-path');
const readConfiguration = require('../../configuration/read');
const createFromTemplate = require('../../utils/createFromTemplate');
const resolveVariables = require('../../configuration/variables');
const { confirm } = require('./utils');
const createFromLocalTemplate = require('../../utils/create-from-local-template');
const isValidServiceName = RegExp.prototype.test.bind(/^[a-zA-Z][a-zA-Z0-9-]{0,100}$/);
const initializeProjectChoices = [
{ name: 'AWS Node.js', value: 'aws-nodejs' },
{ name: 'AWS Python', value: 'aws-python3' },
{ name: 'Other', value: 'other' },
];
const projectTypeChoice = async () =>
(
await inquirer.prompt({
message: 'What do you want to make?',
type: 'list',
name: 'projectType',
choices: initializeProjectChoices,
})
).projectType;
const projectNameInput = async (workingDir) =>
(
await inquirer.prompt({
message: 'What do you want to call this project?',
type: 'input',
name: 'projectName',
validate: async (input) => {
input = input.trim();
if (!isValidServiceName(input)) {
return (
'Project name is not valid.\n' +
' - It should only contain alphanumeric and hyphens.\n' +
' - It should start with an alphabetic character.\n' +
" - Shouldn't exceed 128 characters"
);
}
try {
await fs.promises.access(join(workingDir, input));
return `Path ${input} is already taken`;
} catch {
return true;
}
},
})
).projectName.trim();
module.exports = {
isApplicable({ serviceDir }) {
return !serviceDir;
},
async run(context) {
const workingDir = context.cwd || process.cwd();
const isConfirmed = await confirm('No project detected. Do you want to create a new one?', {
name: 'shouldCreateNewProject',
});
if (!isConfirmed) return;
let projectDir;
// TOOD: CLEANUP
let projectName;
if (context.options && context.options['template-path']) {
projectName = await projectNameInput(workingDir);
projectDir = join(workingDir, projectName);
createFromLocalTemplate({
templatePath: context.options['template-path'],
projectDir,
projectName,
});
} else {
const projectType = await projectTypeChoice();
if (projectType === 'other') {
process.stdout.write(
'\nRun “serverless create --help” to view available templates and create a new project ' +
'from one of those templates.\n'
);
return;
}
projectName = await projectNameInput(workingDir);
projectDir = join(workingDir, projectName);
await createFromTemplate(projectType, projectDir);
}
process.stdout.write(
`\n${chalk.green(`Project successfully created in '${projectName}' folder.`)}\n`
);
context.serviceDir = projectDir;
const configurationPath = await resolveConfigurationPath({ cwd: projectDir, options: {} });
context.configurationFilename = configurationPath.slice(projectDir.length + 1);
context.configuration = await readConfiguration(configurationPath);
await resolveVariables(context);
},
};