Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
264 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const _ = require('lodash'); | ||
|
||
const statsFileName = 'stats.json'; | ||
|
||
function loadStatsFromFile(webpackOutputPath) { | ||
const statsFile = getStatsFilePath(webpackOutputPath); | ||
const data = fs.readFileSync(statsFile); | ||
const stats = JSON.parse(data); | ||
|
||
if (!stats.stats || !stats.stats.length) { | ||
throw new this.serverless.classes.Error('Packaging: No stats information found'); | ||
} | ||
|
||
const mappedStats = _.map(stats.stats, s => | ||
_.assign({}, s, { outputPath: path.resolve(webpackOutputPath, s.outputPath) }) | ||
); | ||
|
||
return { stats: mappedStats }; | ||
} | ||
|
||
const getStatsFilePath = webpackOutputPath => path.join(webpackOutputPath, statsFileName); | ||
|
||
module.exports = { | ||
getCompileStats() { | ||
const stats = this.stats || loadStatsFromFile.call(this, this.webpackOutputPath); | ||
|
||
return stats; | ||
}, | ||
saveCompileStats(stats) { | ||
const statsJson = _.invokeMap(stats.stats, 'toJson'); | ||
|
||
this.stats = { stats: statsJson }; | ||
|
||
const normalisedStats = _.map(statsJson, s => { | ||
return _.assign({}, s, { outputPath: path.relative(this.webpackOutputPath, s.outputPath) }); | ||
}); | ||
|
||
const statsFile = getStatsFilePath(this.webpackOutputPath); | ||
|
||
fs.writeFileSync(statsFile, JSON.stringify({ stats: normalisedStats }, null, 2)); | ||
|
||
return; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const BbPromise = require('bluebird'); | ||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
const path = require('path'); | ||
const Serverless = require('serverless'); | ||
|
||
// Mocks | ||
const fsMockFactory = require('../tests/mocks/fs.mock'); | ||
const mockery = require('mockery'); | ||
|
||
chai.use(require('chai-as-promised')); | ||
chai.use(require('sinon-chai')); | ||
|
||
const expect = chai.expect; | ||
|
||
describe('compileStats', () => { | ||
let baseModule; | ||
let module; | ||
let sandbox; | ||
let serverless; | ||
let fsMock; | ||
|
||
before(() => { | ||
sandbox = sinon.createSandbox(); | ||
sandbox.usingPromise(BbPromise.Promise); | ||
|
||
fsMock = fsMockFactory.create(sandbox); | ||
|
||
mockery.enable({ warnOnUnregistered: false }); | ||
mockery.registerMock('fs', fsMock); | ||
|
||
baseModule = require('./compileStats'); | ||
Object.freeze(baseModule); | ||
}); | ||
|
||
beforeEach(() => { | ||
serverless = new Serverless(); | ||
serverless.cli = { | ||
log: sandbox.stub() | ||
}; | ||
module = _.assign( | ||
{ | ||
serverless, | ||
options: {} | ||
}, | ||
baseModule | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
fsMock.writeFileSync.reset(); | ||
fsMock.readFileSync.reset(); | ||
mockery.disable(); | ||
mockery.deregisterAll(); | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('getCompileStats', () => { | ||
it('should return this.stats if available', () => { | ||
const stats = { stats: [{}] }; | ||
module.stats = stats; | ||
|
||
const result = module.getCompileStats(); | ||
|
||
expect(result).to.equal(stats); | ||
}); | ||
|
||
it('should load stats from file if this.stats is not present', () => { | ||
const webpackOutputPath = '.webpack'; | ||
|
||
const statsFile = { stats: [{ outputPath: 'service/path' }] }; | ||
const mappedFile = { stats: [{ outputPath: path.resolve(webpackOutputPath, 'service', 'path') }] }; | ||
module.webpackOutputPath = webpackOutputPath; | ||
|
||
const fullStatsPath = path.join(webpackOutputPath, 'stats.json'); | ||
|
||
fsMock.readFileSync.withArgs(fullStatsPath).returns(JSON.stringify(statsFile)); | ||
|
||
const stats = module.getCompileStats(); | ||
|
||
expect(fsMock.readFileSync).to.be.calledWith(fullStatsPath); | ||
expect(stats).to.deep.equal(mappedFile); | ||
}); | ||
|
||
it('should fail if compile stats are not loaded', () => { | ||
const webpackOutputPath = '.webpack'; | ||
|
||
const statsFile = { stats: [] }; | ||
|
||
module.webpackOutputPath = webpackOutputPath; | ||
|
||
const fullStatsPath = path.join(webpackOutputPath, 'stats.json'); | ||
|
||
fsMock.readFileSync.withArgs(fullStatsPath).returns(JSON.stringify(statsFile)); | ||
|
||
expect(() => module.getCompileStats()).to.throw(/Packaging: No stats information found/); | ||
}); | ||
}); | ||
|
||
describe('saveCompileStats', () => { | ||
it('should set this.stats', () => { | ||
const webpackOutputPath = '.webpack'; | ||
module.webpackOutputPath = webpackOutputPath; | ||
|
||
const stats = { stats: [{ toJson: () => ({ outputPath: '.webpack/service/path' }) }] }; | ||
|
||
module.saveCompileStats(stats); | ||
|
||
expect(module.stats).to.deep.equal({ stats: [{ outputPath: '.webpack/service/path' }] }); | ||
}); | ||
|
||
it('should write stats to a file', () => { | ||
const webpackOutputPath = '/tmp/.webpack'; | ||
module.webpackOutputPath = webpackOutputPath; | ||
|
||
const stats = { stats: [{ toJson: () => ({ outputPath: '/tmp/.webpack/service/path' }) }] }; | ||
|
||
const fullStatsPath = path.join(webpackOutputPath, 'stats.json'); | ||
|
||
const fileContent = JSON.stringify({ stats: [{ outputPath: path.join('service', 'path') }] }, null, 2); | ||
|
||
module.saveCompileStats(stats); | ||
|
||
expect(fsMock.writeFileSync).to.be.calledWith(fullStatsPath, fileContent); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.