Skip to content

Commit

Permalink
refactor: Use templates from examples in create
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Feb 4, 2022
1 parent 77f8e56 commit 76a60ae
Show file tree
Hide file tree
Showing 383 changed files with 437 additions and 24,826 deletions.
146 changes: 41 additions & 105 deletions lib/plugins/create/create.js
Expand Up @@ -2,30 +2,18 @@

const BbPromise = require('bluebird');
const path = require('path');
const fse = require('fs-extra');
const untildify = require('untildify');

const ServerlessError = require('../../serverless-error');
const cliCommandsSchema = require('../../cli/commands-schema');
const createFromTemplate = require('../../utils/create-from-template');
const recommendedTemplatesList = require('../../templates/recommended-list');
const humanReadableTemplatesList = require('../../templates/recommended-list/human-readable');
const download = require('../../utils/download-template-from-repo');
const downloadTemplateFromExamples = require('../../utils/download-template-from-examples');
const renameService = require('../../utils/rename-service').renameService;
const copyDirContentsSync = require('../../utils/fs/copy-dir-contents-sync');
const dirExistsSync = require('../../utils/fs/dir-exists-sync');
const { progress, log, style } = require('@serverless/utils/log');

const handleServiceCreationError = (error) => {
if (error.code !== 'EACCESS') throw error;
const errorMessage = [
'Error unable to create a service in this directory. ',
'Please check that you have the required permissions to write to the directory',
].join('');

throw new ServerlessError(errorMessage, 'UNABLE_TO_CREATE_SERVICE');
};

const mainProgress = progress.get('main');

class Create {
Expand All @@ -46,7 +34,46 @@ class Create {

async create() {
if ('template' in this.options) {
return this.createFromTemplate();
mainProgress.notice('Setting up a new project', { isMainEvent: true });

if (!recommendedTemplatesList.includes(this.options.template)) {
const errorMessage = [
`Template "${this.options.template}" is not supported.`,
` Supported templates are: ${recommendedTemplatesList
.map((tmpl) => `"${tmpl}"`)
.join(', ')}.`,
].join('');
throw new ServerlessError(errorMessage, 'NOT_SUPPORTED_TEMPLATE');
}

await downloadTemplateFromExamples({
template: this.options.template,
name: this.options.name,
path: this.options.path,
isLegacy: true,
});

log.notice();
log.notice.success(
`Project sucessfully created in "${this.options.path || './'}" from "${
this.options.template
}" template ${style.aside(
`(${Math.floor(
(Date.now() - this.serverless.pluginManager.commandRunStartTime) / 1000
)}s)`
)}`
);

const isPluginTemplate = this.options.template === 'plugin';

if (!(this.options.path || this.options.name) && !isPluginTemplate) {
log.notice();
log.notice(
style.aside(
'Please update the "service" property in serverless.yml with your service name'
)
);
}
} else if ('template-url' in this.options) {
// We only show progress in case of setup from `template-url` as setting up from local files is fast
mainProgress.notice('Setting up new project', { isMainEvent: true });
Expand Down Expand Up @@ -99,97 +126,6 @@ class Create {
}
return BbPromise.resolve();
}

async createFromTemplate() {
const notPlugin = this.options.template !== 'plugin';

if (!recommendedTemplatesList.includes(this.options.template)) {
const errorMessage = [
`Template "${this.options.template}" is not supported.`,
` Supported templates are: ${humanReadableTemplatesList}.`,
].join('');
throw new ServerlessError(errorMessage, 'NOT_SUPPORTED_TEMPLATE');
}

// store the custom options for the service if given
const boilerplatePath = this.options.path && String(this.options.path);
const serviceName = this.options.name && String(this.options.name);
const templateSrcDir = path.join(
this.serverless.config.serverlessPath,
'plugins',
'create',
'templates',
this.options.template
);

// create (if not yet present) and chdir into the directory for the service
if (boilerplatePath) {
const newPath = path.resolve(process.cwd(), untildify(boilerplatePath));

if (this.serverless.utils.dirExistsSync(newPath)) {
const errorMessage = [
`The directory "${newPath}" already exists, and serverless will not overwrite it. `,
'Rename or move the directory and try again if you want serverless to create it"',
].join('');

throw new ServerlessError(errorMessage, 'TARGET_FOLDER_ALREADY_EXISTS');
}

fse.mkdirsSync(newPath);
process.chdir(newPath);
} else {
// ensure no template file already exists in cwd that we may overwrite
const templateFullFilePaths = this.serverless.utils.walkDirSync(templateSrcDir);

templateFullFilePaths.forEach((ffp) => {
const filename = path.basename(ffp);
if (this.serverless.utils.fileExistsSync(path.join(process.cwd(), filename))) {
const errorMessage = [
`The file "${filename}" already exists, and serverless will not overwrite it. `,
`Move the file and try again if you want serverless to write a new "${filename}"`,
].join('');

throw new ServerlessError(errorMessage, 'TEMPLATE_FILE_ALREADY_EXISTS');
}
});
}

if (!notPlugin) {
return BbPromise.try(() => {
try {
fse.copySync(
path.join(__dirname, '../../../lib/plugins/create/templates', this.options.template),
process.cwd()
);
} catch (error) {
handleServiceCreationError(error);
}
});
}

this.serverless.serviceDir = process.cwd();

return createFromTemplate(this.options.template, process.cwd(), { name: serviceName }).then(
() => {
log.notice();
log.notice.success(
`Project sucessfully created in "${this.options.path || './'}" from "${
this.options.template
}" template`
);

if (!(boilerplatePath || serviceName) && notPlugin) {
log.notice();
log.notice(
style.aside(
'Please update the "service" property in serverless.yml with your service name'
)
);
}
},
handleServiceCreationError
);
}
}

module.exports = Create;
6 changes: 0 additions & 6 deletions lib/plugins/create/templates/aliyun-nodejs/.gitignore

This file was deleted.

10 changes: 0 additions & 10 deletions lib/plugins/create/templates/aliyun-nodejs/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions lib/plugins/create/templates/aliyun-nodejs/package.json

This file was deleted.

36 changes: 0 additions & 36 deletions lib/plugins/create/templates/aliyun-nodejs/serverless.yml

This file was deleted.

9 changes: 0 additions & 9 deletions lib/plugins/create/templates/aws-alexa-typescript/.gitignore

This file was deleted.

10 changes: 0 additions & 10 deletions lib/plugins/create/templates/aws-alexa-typescript/handler.ts

This file was deleted.

23 changes: 0 additions & 23 deletions lib/plugins/create/templates/aws-alexa-typescript/package.json

This file was deleted.

53 changes: 0 additions & 53 deletions lib/plugins/create/templates/aws-alexa-typescript/serverless.yml

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 76a60ae

Please sign in to comment.