-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#547): add command for uploading extension libs
- Loading branch information
1 parent
8b5e62e
commit 3b13e40
Showing
4 changed files
with
135 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const environment = require('../lib/environment'); | ||
const pouch = require('../lib/db'); | ||
const log = require('../lib/log'); | ||
const attachmentsFromDir = require('../lib/attachments-from-dir'); | ||
const warnUploadOverwrite = require('../lib/warn-upload-overwrite'); | ||
const insertOrReplace = require('../lib/insert-or-replace'); | ||
const projectPaths = require('../lib/project-paths'); | ||
|
||
const DOC_ID = 'extension-libs'; | ||
|
||
module.exports = { | ||
requiresInstance: true, | ||
execute: async () => { | ||
const configurationDir = `${environment.pathToProject}/${projectPaths.EXTENSION_LIBS_PATH}`; | ||
|
||
const attachments = attachmentsFromDir(configurationDir); | ||
if (!Object.keys(attachments).length) { | ||
log.info(`No configuration found at "${configurationDir}" - not uploading extension-libs`); | ||
return; | ||
} | ||
|
||
log.info(`Found extension-libs: ${Object.keys(attachments).join(', ')}`); | ||
const doc = { | ||
_id: DOC_ID, | ||
_attachments: attachments | ||
}; | ||
|
||
const db = pouch(environment.apiUrl); | ||
const changes = await warnUploadOverwrite.preUploadDoc(db, doc); | ||
|
||
if (!changes) { | ||
log.info('Extension libs not uploaded as already up to date'); | ||
return; | ||
} | ||
|
||
await insertOrReplace(db, doc); | ||
log.info('Extension libs upload complete'); | ||
|
||
return await warnUploadOverwrite.postUploadDoc(db, doc); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
const path = require('path'); | ||
// Paths Relative to the Project | ||
module.exports = { | ||
APP_SETTINGS_DIR_PATH: 'app_settings', | ||
APP_SETTINGS_JSON_PATH: 'app_settings.json', | ||
APP_FORMS_PATH: path.join('forms', 'app'), | ||
CONTACT_FORMS_PATH: path.join('forms', 'contact'), | ||
COLLECT_FORMS_PATH: path.join('forms', 'collect'), | ||
TRAINING_FORMS_PATH: path.join('forms', 'training'), | ||
TRANSLATIONS_DIR_PATH: 'translations', | ||
RESOURCE_CONFIG_PATH: 'resources.json', | ||
RESOURCES_DIR_PATH: 'resources' | ||
}; | ||
APP_SETTINGS_DIR_PATH: 'app_settings', | ||
APP_SETTINGS_JSON_PATH: 'app_settings.json', | ||
APP_FORMS_PATH: path.join('forms', 'app'), | ||
CONTACT_FORMS_PATH: path.join('forms', 'contact'), | ||
COLLECT_FORMS_PATH: path.join('forms', 'collect'), | ||
TRAINING_FORMS_PATH: path.join('forms', 'training'), | ||
TRANSLATIONS_DIR_PATH: 'translations', | ||
RESOURCE_CONFIG_PATH: 'resources.json', | ||
RESOURCES_DIR_PATH: 'resources', | ||
EXTENSION_LIBS_PATH: 'extension-libs', | ||
}; |
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,81 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
const rewire = require('rewire'); | ||
|
||
const log = require('../../src/lib/log'); | ||
const warnUploadOverwrite = require('../../src/lib/warn-upload-overwrite'); | ||
|
||
const uploadExtensionLibs = rewire('../../src/fn/upload-extension-libs'); | ||
|
||
describe('Upload extension libs', () => { | ||
|
||
let attachmentsFromDir; | ||
let insertOrReplace; | ||
|
||
beforeEach(() => { | ||
sinon.stub(log, 'info'); | ||
sinon.stub(warnUploadOverwrite, 'preUploadDoc'); | ||
sinon.stub(warnUploadOverwrite, 'postUploadDoc'); | ||
attachmentsFromDir = sinon.stub(); | ||
insertOrReplace = sinon.stub().resolves(); | ||
uploadExtensionLibs.__set__('pouch', () => {}); | ||
uploadExtensionLibs.__set__('environment', { | ||
apiUrl: 'test', | ||
pathToProject: '/testpath' | ||
}); | ||
uploadExtensionLibs.__set__('attachmentsFromDir', attachmentsFromDir); | ||
uploadExtensionLibs.__set__('insertOrReplace', insertOrReplace); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('log and skip when dir is empty', async () => { | ||
attachmentsFromDir.returns({}); | ||
await uploadExtensionLibs.execute(); | ||
expect(attachmentsFromDir.callCount).to.equal(1); | ||
expect(attachmentsFromDir.args[0][0]).to.equal('/testpath/extension-libs'); | ||
expect(insertOrReplace.callCount).to.equal(0); | ||
expect(log.info.callCount).to.equal(1); | ||
expect(log.info.args[0][0]).to.equal('No configuration found at "/testpath/extension-libs" - not uploading extension-libs'); | ||
}); | ||
|
||
it('does nothing if doc matches remote', async () => { | ||
attachmentsFromDir.returns({ 'script.js': {}, 'data.json': {} }); | ||
warnUploadOverwrite.preUploadDoc.resolves(false); | ||
|
||
await uploadExtensionLibs.execute(); | ||
expect(attachmentsFromDir.callCount).to.equal(1); | ||
expect(attachmentsFromDir.args[0][0]).to.equal('/testpath/extension-libs'); | ||
expect(warnUploadOverwrite.preUploadDoc.callCount).to.equal(1); | ||
expect(warnUploadOverwrite.postUploadDoc.callCount).to.equal(0); | ||
expect(insertOrReplace.callCount).to.equal(0); | ||
expect(log.info.callCount).to.equal(2); | ||
expect(log.info.args[0][0]).to.equal('Found extension-libs: script.js, data.json'); | ||
expect(log.info.args[1][0]).to.equal('Extension libs not uploaded as already up to date'); | ||
}); | ||
|
||
it('should update doc when attachments found', async () => { | ||
attachmentsFromDir.returns({ 'script.js': {}, 'data.json': {} }); | ||
warnUploadOverwrite.preUploadDoc.resolves(true); | ||
|
||
await uploadExtensionLibs.execute(); | ||
expect(attachmentsFromDir.callCount).to.equal(1); | ||
expect(attachmentsFromDir.args[0][0]).to.equal('/testpath/extension-libs'); | ||
expect(warnUploadOverwrite.preUploadDoc.callCount).to.equal(1); | ||
expect(warnUploadOverwrite.postUploadDoc.callCount).to.equal(1); | ||
expect(insertOrReplace.callCount).to.equal(1); | ||
expect(insertOrReplace.args[0][1]).to.deep.equal({ | ||
_id: 'extension-libs', | ||
_attachments: { | ||
'data.json': {}, | ||
'script.js': {} | ||
} | ||
}); | ||
expect(log.info.callCount).to.equal(2); | ||
expect(log.info.args[0][0]).to.equal('Found extension-libs: script.js, data.json'); | ||
expect(log.info.args[1][0]).to.equal('Extension libs upload complete'); | ||
}); | ||
|
||
}); |