Skip to content

Commit

Permalink
feat(#547): add command for uploading extension libs
Browse files Browse the repository at this point in the history
  • Loading branch information
garethbowen committed Apr 4, 2023
1 parent 8b5e62e commit 3b13e40
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 10 deletions.
41 changes: 41 additions & 0 deletions src/fn/upload-extension-libs.js
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);
}
};
2 changes: 2 additions & 0 deletions src/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const defaultActions = [
'upload-partners',
'upload-custom-translations',
'upload-privacy-policies',
'upload-extension-libs',
];
const defaultArchiveActions = [
'compile-app-settings',
Expand All @@ -58,6 +59,7 @@ const defaultArchiveActions = [
'upload-partners',
'upload-custom-translations',
'upload-privacy-policies',
'upload-extension-libs',
];

module.exports = async (argv, env) => {
Expand Down
21 changes: 11 additions & 10 deletions src/lib/project-paths.js
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',
};
81 changes: 81 additions & 0 deletions test/fn/upload-extension-libs.spec.js
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');
});

});

0 comments on commit 3b13e40

Please sign in to comment.