Skip to content

Commit

Permalink
refactor(CLI): Modern logs for create command
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Oct 7, 2021
1 parent 82dd1e4 commit 05f937f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 47 deletions.
43 changes: 36 additions & 7 deletions lib/plugins/create/create.js
Expand Up @@ -14,6 +14,7 @@ const download = require('../../utils/downloadTemplateFromRepo');
const renameService = require('../../utils/renameService').renameService;
const copyDirContentsSync = require('../../utils/fs/copyDirContentsSync');
const dirExistsSync = require('../../utils/fs/dirExistsSync');
const { legacy, progress, log, style } = require('@serverless/utils/log');

const handleServiceCreationError = (error) => {
if (error.code !== 'EACCESS') throw error;
Expand All @@ -25,6 +26,8 @@ const handleServiceCreationError = (error) => {
throw new ServerlessError(errorMessage, 'UNABLE_TO_CREATE_SERVICE');
};

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

class Create {
constructor(serverless, options) {
this.serverless = serverless;
Expand All @@ -42,11 +45,13 @@ class Create {
}

async create() {
this.serverless.cli.log('Generating boilerplate...');
legacy.log('Generating boilerplate...');

if ('template' in this.options) {
return this.createFromTemplate();
} 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 });
return download
.downloadTemplateFromRepo(
this.options['template-url'],
Expand All @@ -63,7 +68,17 @@ class Create {
}`,
].join('');

this.serverless.cli.log(message);
legacy.log(message);
log.notice();
log.notice.success(
`Project successfully created in "${
this.options.path || `./${serviceName}`
}" ${style.aside(
`(${Math.floor(
(Date.now() - this.serverless.pluginManager.commandRunStartTime) / 1000
)}s)`
)}`
);
})
.catch((err) => {
throw new ServerlessError(err, 'BOILERPLATE_GENERATION_ERROR');
Expand All @@ -83,6 +98,10 @@ class Create {
if (this.options.name) {
renameService(this.options.name, serviceDir);
}
log.notice();
log.notice.success(
`Project sucessfully created in "${this.options.path || `./${this.options.name}`}"`
);
} else {
const errorMessage = [
'You must either pass a template name (--template), ',
Expand Down Expand Up @@ -128,7 +147,7 @@ class Create {
throw new ServerlessError(errorMessage, 'TARGET_FOLDER_ALREADY_EXISTS');
}

this.serverless.cli.log(`Generating boilerplate in "${newPath}"`);
legacy.log(`Generating boilerplate in "${newPath}"`);

fse.mkdirsSync(newPath);
process.chdir(newPath);
Expand Down Expand Up @@ -166,15 +185,25 @@ class Create {

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

legacy.log(`Successfully generated boilerplate for template: "${this.options.template}"`);

if (!(boilerplatePath || serviceName) && notPlugin) {
this.serverless.cli.log(
legacy.log(
'NOTE: Please update the "service" property in serverless.yml with your service name'
);
log.notice();
log.notice(
style.aside(
'Please update the "service" property in serverless.yml with your service name'
)
);
}
},
handleServiceCreationError
Expand Down
40 changes: 0 additions & 40 deletions test/unit/lib/plugins/create/create.test.js
Expand Up @@ -19,14 +19,12 @@ const templatesPath = path.resolve(__dirname, '../../../../../lib/plugins/create

describe('Create', () => {
let create;
let logSpy;

before(() => {
const serverless = new Serverless();
const options = {};
create = new Create(serverless, options);
create.serverless.cli = new serverless.classes.CLI();
logSpy = sinon.spy(create.serverless.cli, 'log');
});

describe('#constructor()', () => {
Expand Down Expand Up @@ -80,35 +78,6 @@ describe('Create', () => {
expect(downloadStub).to.have.been.calledOnce;
});
});

it('should resolve if download succeeds', () => {
downloadStub.resolves('serverless');
create.options['template-url'] = 'https://github.com/serverless/serverless';

return create.create().then(() => {
const installationMessage = logSpy.args.filter((arg) =>
arg[0].includes('installed "serverless"')
);

expect(downloadStub).to.have.been.calledOnce;
expect(installationMessage[0]).to.have.lengthOf(1);
});
});

it('should resolve if download succeeds and display the desired service name', () => {
downloadStub.resolves('serverless');
create.options['template-url'] = 'https://github.com/serverless/serverless';
create.options.name = 'sls';

return create.create().then(() => {
const installationMessage = logSpy.args.filter((arg) =>
arg[0].includes('installed "serverless" as "sls"')
);

expect(downloadStub).to.have.been.calledOnce;
expect(installationMessage[0]).to.have.lengthOf(1);
});
});
});

it('should throw error if user passed unsupported template', () => {
Expand Down Expand Up @@ -136,15 +105,6 @@ describe('Create', () => {
});
});

it('should display ascii greeting', () => {
process.chdir(tmpDir);

const greetingStub = sinon.stub(create.serverless.cli, 'asciiGreeting');
return create.create().then(() => {
expect(greetingStub.callCount).to.be.equal(1);
});
});

it('should generate scaffolding for "aws-clojure-gradle" template', () => {
process.chdir(tmpDir);
create.options.template = 'aws-clojure-gradle';
Expand Down

0 comments on commit 05f937f

Please sign in to comment.