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
8 changed files
with
116 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,24 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const BbPromise = require('bluebird'); | ||
const fse = require('fs-extra'); | ||
const getTmpDirPath = require('../../../utils/fs/getTmpDirPath'); | ||
const createZipFile = require('../../../utils/fs/createZipFile'); | ||
const getEnsureArtifact = require('../../../utils/getEnsureArtifact'); | ||
const ensureArtifact = require('../../../utils/ensureArtifact'); | ||
|
||
const srcDirPath = path.join(__dirname, 'resources'); | ||
|
||
const artifactName = 'custom-resources.zip'; | ||
|
||
const ensureCustomResourcesArtifact = getEnsureArtifact(artifactName, (cachePath) => | ||
BbPromise.try(() => { | ||
module.exports = async () => { | ||
const resultPath = await ensureArtifact(artifactName, async (cachePath) => { | ||
const tmpDirPath = getTmpDirPath(); | ||
const tmpInstalledLambdaPath = path.resolve(tmpDirPath, 'resource-lambda'); | ||
const tmpZipFilePath = path.resolve(tmpDirPath, 'resource-lambda.zip'); | ||
const cachedZipFilePath = path.resolve(cachePath, artifactName); | ||
return fse | ||
.copy(srcDirPath, tmpInstalledLambdaPath) | ||
.then(() => createZipFile(tmpInstalledLambdaPath, tmpZipFilePath)) | ||
.then(() => fse.move(tmpZipFilePath, cachedZipFilePath)); | ||
}) | ||
); | ||
|
||
module.exports = () => | ||
ensureCustomResourcesArtifact().then((cachePath) => path.resolve(cachePath, artifactName)); | ||
await fse.copy(srcDirPath, tmpInstalledLambdaPath); | ||
await createZipFile(tmpInstalledLambdaPath, tmpZipFilePath); | ||
await fse.move(tmpZipFilePath, cachedZipFilePath); | ||
}); | ||
return path.resolve(resultPath, artifactName); | ||
}; |
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,17 @@ | ||
'use strict'; | ||
|
||
const memoizee = require('memoizee'); | ||
const { version } = require('../../package'); | ||
const ensureExists = require('./ensureExists'); | ||
const path = require('path'); | ||
const os = require('os'); | ||
|
||
const cachePath = path.resolve(os.homedir(), '.serverless/artifacts', version); | ||
|
||
module.exports = memoizee( | ||
async (filename, generate) => { | ||
await ensureExists(path.resolve(cachePath, filename), generate); | ||
return cachePath; | ||
}, | ||
{ length: 1, promise: true } | ||
); |
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,20 @@ | ||
'use strict'; | ||
|
||
const fse = require('fs-extra'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = async (filename, generate) => { | ||
const cacheDir = path.dirname(filename); | ||
try { | ||
const stats = await fs.promises.lstat(filename); | ||
if (stats.isFile()) { | ||
return; | ||
} | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') throw err; | ||
} | ||
|
||
await fse.ensureDir(cacheDir); | ||
await generate(cacheDir); | ||
}; |
This file was deleted.
Oops, something went wrong.
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,34 @@ | ||
'use strict'; | ||
|
||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const ensureExists = require('../../../../lib/utils/ensureExists'); | ||
const { getTmpDirPath } = require('../../../utils/fs'); | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
const crypto = require('crypto'); | ||
|
||
const expect = chai.expect; | ||
chai.use(require('chai-as-promised')); | ||
chai.use(require('sinon-chai')); | ||
|
||
describe('test/unit/lib/utils/ensureExists.test.js', () => { | ||
const testCacheDir = getTmpDirPath(); | ||
|
||
it('Should call generate if file missing', async () => { | ||
const testFileName = `test-${crypto.randomBytes(2).toString('hex')}`; | ||
const generateStub = sinon.stub().resolves(); | ||
await ensureExists(path.resolve(testCacheDir, testFileName), generateStub); | ||
expect(generateStub.calledOnce).to.be.true; | ||
}); | ||
|
||
it('Should not call generate if file exists', async () => { | ||
const testFileName = `test-${crypto.randomBytes(2).toString('hex')}`; | ||
await fs.promises.writeFile(path.resolve(testCacheDir, testFileName), ''); | ||
const generateStub = sinon.stub().resolves(); | ||
await ensureExists(path.resolve(testCacheDir, testFileName), generateStub); | ||
expect(generateStub.calledOnce).to.be.false; | ||
}); | ||
}); |
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