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

Speed up cleanup process #462

Merged
merged 1 commit into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/cleanup.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');
const fse = require('fs-extra');

module.exports = {
cleanup() {
const webpackOutputPath = this.webpackOutputPath;

const keepOutputDirectory = this.keepOutputDirectory;
const cli = this.options.verbose ? this.serverless.cli : { log: _.noop };

if (!keepOutputDirectory) {
this.options.verbose && this.serverless.cli.log(`Remove ${webpackOutputPath}`);
cli.log(`Remove ${webpackOutputPath}`);
if (this.serverless.utils.dirExistsSync(webpackOutputPath)) {
fse.removeSync(webpackOutputPath);
// Remove async to speed up process
fse
.remove(webpackOutputPath)
.then(() => cli.log(`Removing ${webpackOutputPath} done`))
.catch(error => cli.log(`Error occurred while removing ${webpackOutputPath}: ${error}`));
}
} else {
this.options.verbose && this.serverless.cli.log(`Keeping ${webpackOutputPath}`);
cli.log(`Keeping ${webpackOutputPath}`);
}

return BbPromise.resolve();
Expand Down
57 changes: 48 additions & 9 deletions tests/cleanup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const expect = chai.expect;

const FseMock = sandbox => ({
copy: sandbox.stub(),
removeSync: sandbox.stub()
remove: sandbox.stub()
});

describe('cleanup', () => {
Expand Down Expand Up @@ -47,14 +47,17 @@ describe('cleanup', () => {
serverless = new Serverless();
serverless.cli = {
log: sandbox.stub(),
error: sandbox.stub(),
consoleLog: sandbox.stub()
};
dirExistsSyncStub = sandbox.stub(serverless.utils, 'dirExistsSync');

module = _.assign(
{
serverless,
options: {},
options: {
verbose: true
},
webpackOutputPath: 'my/Output/Path'
},
baseModule
Expand All @@ -64,42 +67,78 @@ describe('cleanup', () => {
afterEach(() => {
// This will reset the webpackMock too
sandbox.restore();
fseMock.remove.reset();
serverless.cli.log.reset();
});

it('should remove output dir if it exists', () => {
dirExistsSyncStub.returns(true);
fseMock.removeSync.reset();
fseMock.remove.resolves(true);

return expect(module.cleanup()).to.be.fulfilled.then(() => {
expect(dirExistsSyncStub).to.have.been.calledOnce;
expect(dirExistsSyncStub).to.have.been.calledWith('my/Output/Path');
expect(fseMock.removeSync).to.have.been.calledOnce;
expect(fseMock.remove).to.have.been.calledOnce;
expect(serverless.cli.log).to.have.been.calledWith('Removing my/Output/Path done');
return null;
});
});

it('should not call removeSync if output dir does not exists', () => {
it('should log nothing is verbose is false', () => {
dirExistsSyncStub.returns(true);
fseMock.remove.resolves(true);

module = _.assign(
{
serverless,
options: {
verbose: false
},
webpackOutputPath: 'my/Output/Path'
},
baseModule
);

return expect(module.cleanup()).to.be.fulfilled.then(() => {
expect(dirExistsSyncStub).to.have.been.calledOnce;
expect(dirExistsSyncStub).to.have.been.calledWith('my/Output/Path');
expect(fseMock.remove).to.have.been.calledOnce;
expect(serverless.cli.log).not.to.have.been.called;
return null;
});
});

it('should log an error if it occurs', () => {
dirExistsSyncStub.returns(true);
fseMock.remove.rejects('remove error');

return expect(module.cleanup()).to.be.fulfilled.then(() => {
expect(serverless.cli.log).to.have.been.calledWith('Error occurred while removing my/Output/Path: remove error');

return null;
});
});

it('should not call remove if output dir does not exists', () => {
dirExistsSyncStub.returns(false);
fseMock.removeSync.reset();

return expect(module.cleanup()).to.be.fulfilled.then(() => {
expect(dirExistsSyncStub).to.have.been.calledOnce;
expect(dirExistsSyncStub).to.have.been.calledWith('my/Output/Path');
expect(fseMock.removeSync).to.not.have.been.called;
expect(fseMock.remove).to.not.have.been.called;
return null;
});
});

it('should keep output dir if keepOutputDir = true', () => {
dirExistsSyncStub.returns(true);
fseMock.removeSync.reset();

const configuredModule = _.assign({}, module, {
keepOutputDirectory: true
});
return expect(configuredModule.cleanup()).to.be.fulfilled.then(() => {
expect(dirExistsSyncStub).to.not.have.been.calledOnce;
expect(fseMock.removeSync).to.not.have.been.called;
expect(fseMock.remove).to.not.have.been.called;
return null;
});
});
Expand Down