Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't use functionName in template function file #79

Open
zamirdan opened this issue Jan 9, 2018 · 1 comment
Open

can't use functionName in template function file #79

zamirdan opened this issue Jan 9, 2018 · 1 comment

Comments

@zamirdan
Copy link

zamirdan commented Jan 9, 2018

Hi,

this variable is only available in the "template for a test file".
it's a simple fix that could really help us.
Thanks,
Dan

@zamirdan
Copy link
Author

zamirdan commented Jan 9, 2018

createAWSNodeJSFuncFile(handlerPath) {

const functionName = this.options.f || this.options.function;

const handlerInfo = path.parse(handlerPath);
const handlerDir = path.join(this.serverless.config.servicePath, handlerInfo.dir);
const handlerFile = `${handlerInfo.name}.js`;
const handlerFunction = handlerInfo.ext.replace(/^\./, '');
let templateFile = path.join(__dirname, functionTemplateFile);

if (this.serverless.service.custom &&
  this.serverless.service.custom['serverless-mocha-plugin'] &&
  this.serverless.service.custom['serverless-mocha-plugin'].functionTemplate) {
  templateFile = path.join(this.serverless.config.servicePath,
    this.serverless.service.custom['serverless-mocha-plugin'].functionTemplate);
}

const templateText = fse.readFileSync(templateFile).toString();
const jsFile = ejs.render(templateText, {

handlerFunction,
functionName

});

const filePath = path.join(handlerDir, handlerFile);

this.serverless.utils.writeFileDir(filePath);
if (this.serverless.utils.fileExistsSync(filePath)) {
  const errorMessage = [
    `File "${filePath}" already exists. Cannot create function.`,
  ].join('');
  throw new this.serverless.classes.Error(errorMessage);
}
fse.writeFileSync(path.join(handlerDir, handlerFile), jsFile);

this.serverless.cli.log(`Created function file "${path.join(handlerDir, handlerFile)}"`);
return BbPromise.resolve();

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants