Skip to content

Commit

Permalink
Merge pull request #462 from neilime/patch-1
Browse files Browse the repository at this point in the history
Speed up cleanup process
  • Loading branch information
miguel-a-calles-mba authored Jul 30, 2020
2 parents a114524 + 1a0f0d4 commit ea029ba
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
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

0 comments on commit ea029ba

Please sign in to comment.