mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
98 lines
3.3 KiB
JavaScript
98 lines
3.3 KiB
JavaScript
'use strict';
|
|
|
|
const { join } = require('path');
|
|
const chalk = require('chalk');
|
|
const inquirer = require('inquirer');
|
|
const installTemplate = require('../../utils/installTemplate');
|
|
const {
|
|
getConfigFilePath,
|
|
getServerlessConfigFile,
|
|
} = require('../../utils/getServerlessConfigFile');
|
|
const { confirm } = require('./utils');
|
|
|
|
const isValidServiceName = RegExp.prototype.test.bind(/^[a-zA-Z][a-zA-Z0-9-]{0,100}$/);
|
|
|
|
const projectTypeChoice = () =>
|
|
inquirer
|
|
.prompt({
|
|
message: 'What do you want to make?',
|
|
type: 'list',
|
|
name: 'projectType',
|
|
choices: [
|
|
{ name: 'AWS Node.js', value: 'aws-nodejs' },
|
|
{ name: 'AWS Python', value: 'aws-python' },
|
|
{ name: 'Other', value: 'other' },
|
|
],
|
|
})
|
|
.then(({ projectType }) => projectType);
|
|
|
|
const projectNameInput = workingDir =>
|
|
inquirer
|
|
.prompt({
|
|
message: 'What do you want to call this project?',
|
|
type: 'input',
|
|
name: 'projectName',
|
|
validate: 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"
|
|
);
|
|
}
|
|
return getConfigFilePath(join(workingDir, input)).then(configFilePath => {
|
|
return configFilePath ? `Serverless project already found at ${input} directory` : true;
|
|
});
|
|
},
|
|
})
|
|
.then(({ projectName }) => projectName.trim());
|
|
|
|
module.exports = {
|
|
check(serverless) {
|
|
return !serverless.config.servicePath;
|
|
},
|
|
run(serverless) {
|
|
const workingDir = process.cwd();
|
|
return confirm('No Serverless project detected. Do you want to create a new one?').then(
|
|
isConfirmed => {
|
|
if (!isConfirmed) return null;
|
|
return projectTypeChoice().then(projectType => {
|
|
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 null;
|
|
}
|
|
return projectNameInput(workingDir).then(projectName => {
|
|
const projectDir = join(workingDir, projectName);
|
|
return installTemplate(projectType, projectDir)
|
|
.then(() => {
|
|
process.stdout.write(
|
|
`\n${chalk.green(`Project successfully created in '${projectName}' folder.`)}\n`
|
|
);
|
|
|
|
process.chdir(projectDir);
|
|
serverless.config.servicePath = projectDir;
|
|
getServerlessConfigFile.cache.delete(serverless);
|
|
getServerlessConfigFile(serverless);
|
|
})
|
|
.then(serverlessConfigFile => {
|
|
serverless.pluginManager.serverlessConfigFile = serverlessConfigFile;
|
|
return serverless.service.load();
|
|
})
|
|
.then(() => serverless.variables.populateService())
|
|
.then(() => {
|
|
serverless.service.mergeArrays();
|
|
serverless.service.setFunctionNames();
|
|
serverless.service.validate();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
);
|
|
},
|
|
};
|