@contentful/app-scripts 1.12.0 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +4 -4
  2. package/lib/activate/activate-bundle.d.ts +2 -0
  3. package/lib/activate/activate-bundle.js +45 -58
  4. package/lib/activate/build-bundle-activate-settings.d.ts +2 -0
  5. package/lib/activate/build-bundle-activate-settings.js +32 -34
  6. package/lib/activate/get-activate-args.d.ts +2 -0
  7. package/lib/activate/get-activate-args.js +33 -30
  8. package/lib/activate/index.d.ts +5 -0
  9. package/lib/activate/index.js +17 -13
  10. package/lib/analytics.d.ts +11 -0
  11. package/lib/analytics.js +42 -0
  12. package/lib/bin.d.ts +2 -0
  13. package/lib/bin.js +69 -0
  14. package/lib/cache-credential/index.d.ts +1 -0
  15. package/lib/cache-credential/index.js +74 -0
  16. package/lib/clean-up/build-clean-up-settings.d.ts +2 -0
  17. package/lib/clean-up/build-clean-up-settings.js +32 -36
  18. package/lib/clean-up/clean-up-bundles.d.ts +2 -0
  19. package/lib/clean-up/clean-up-bundles.js +91 -118
  20. package/lib/clean-up/get-clean-up-settings.d.ts +2 -0
  21. package/lib/clean-up/get-clean-up-settings.js +34 -32
  22. package/lib/clean-up/index.d.ts +5 -0
  23. package/lib/clean-up/index.js +17 -13
  24. package/lib/constants.d.ts +8 -0
  25. package/lib/constants.js +11 -0
  26. package/lib/create-app-definition/build-app-definition-settings.d.ts +7 -0
  27. package/lib/create-app-definition/build-app-definition-settings.js +73 -77
  28. package/lib/create-app-definition/create-app-definition.d.ts +2 -0
  29. package/lib/create-app-definition/create-app-definition.js +80 -112
  30. package/lib/create-app-definition/index.d.ts +4 -0
  31. package/lib/create-app-definition/index.js +18 -15
  32. package/lib/definition-api.d.ts +7 -0
  33. package/lib/definition-api.js +37 -44
  34. package/lib/get-app-info.d.ts +10 -0
  35. package/lib/get-app-info.js +22 -27
  36. package/lib/get-management-token.d.ts +1 -0
  37. package/lib/get-management-token.js +53 -60
  38. package/lib/index.d.ts +6 -0
  39. package/lib/index.js +15 -15
  40. package/lib/open/index.d.ts +5 -0
  41. package/lib/open/index.js +12 -8
  42. package/lib/open/open-settings.d.ts +3 -0
  43. package/lib/open/open-settings.js +46 -46
  44. package/lib/organization-api.d.ts +7 -0
  45. package/lib/organization-api.js +39 -51
  46. package/lib/types.d.ts +73 -0
  47. package/lib/types.js +2 -0
  48. package/lib/upload/build-upload-settings.d.ts +2 -0
  49. package/lib/upload/build-upload-settings.js +53 -60
  50. package/lib/upload/create-app-bundle.d.ts +3 -0
  51. package/lib/upload/create-app-bundle.js +64 -71
  52. package/lib/upload/create-app-upload.d.ts +2 -0
  53. package/lib/upload/create-app-upload.js +31 -36
  54. package/lib/upload/create-zip-from-directory.d.ts +2 -0
  55. package/lib/upload/create-zip-from-directory.js +23 -21
  56. package/lib/upload/get-upload-settings-args.d.ts +2 -0
  57. package/lib/upload/get-upload-settings-args.js +43 -41
  58. package/lib/upload/index.d.ts +5 -0
  59. package/lib/upload/index.js +20 -18
  60. package/lib/upload/validate-bundle.d.ts +1 -0
  61. package/lib/upload/validate-bundle.js +26 -33
  62. package/lib/utils.d.ts +12 -0
  63. package/lib/utils.js +87 -121
  64. package/lib/validate-arguments.d.ts +1 -0
  65. package/lib/validate-arguments.js +20 -14
  66. package/package.json +23 -11
  67. package/bin/app-scripts +0 -78
  68. package/lib/activate/activate-bundle.test.js +0 -69
  69. package/lib/clean-up/clean-up-bundles.test.js +0 -118
  70. package/lib/create-app-definition/create-app-definition.test.js +0 -87
  71. package/lib/get-app-info.test.js +0 -57
  72. package/lib/get-management-token.test.js +0 -82
  73. package/lib/open/open-settings.test.js +0 -45
  74. package/lib/upload/create-app-bundle.test.js +0 -110
  75. package/lib/upload/create-app-upload.test.js +0 -50
  76. package/lib/upload/validate-bundle.test.js +0 -45
  77. package/lib/utils.test.js +0 -323
  78. package/lib/validate-arguments.test.js +0 -33
  79. package/utils/analytics.js +0 -36
  80. package/utils/cache-credential/index.js +0 -81
  81. package/utils/cache-credential/index.test.js +0 -73
  82. package/utils/constants.js +0 -21
package/bin/app-scripts DELETED
@@ -1,78 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const { program } = require('commander');
4
- const { version } = require('../package.json');
5
-
6
- const { createAppDefinition, upload, activate, cleanup, open, track } = require('../');
7
-
8
- async function runCommand(command, options) {
9
- const { ci } = program.opts();
10
- return ci ? await command.nonInteractive(options) : await command.interactive(options);
11
- }
12
-
13
- (async function main() {
14
- program.version(version).option('--ci', 'Execute in non-interactive mode', false);
15
-
16
- program
17
- .command('create-app-definition')
18
- .description('Create a new AppDefinition for an App')
19
- .action(async (options) => {
20
- await runCommand(createAppDefinition, options);
21
- });
22
-
23
- program
24
- .command('upload')
25
- .description('Upload your build folder and create an AppBundle')
26
- .option('--bundle-dir [directory]', 'The directory of your build folder')
27
- .option('--organization-id [orgId]', 'The id of your organization')
28
- .option('--definition-id [defId]', 'The id of your apps definition')
29
- .option('--token [accessToken]', 'Your content management access token')
30
- .option('--comment [comment]', 'Optional comment for the created bundle')
31
- .option('--skip-activation', 'A Boolean flag to skip automatic activation')
32
- .option('--host [host]', 'Contentful domain to use')
33
- .action(async (options) => {
34
- await runCommand(upload, options);
35
- });
36
-
37
- program
38
- .command('activate')
39
- .description('Mark an AppBundle as "active" for a given AppDefinition')
40
- .option('--bundle-id [bundleId]', 'The id of your bundle')
41
- .option('--organization-id [orgId]', 'The id of your organization')
42
- .option('--definition-id [defId]', 'The id of your apps definition')
43
- .option('--token [accessToken]', 'Your content management access token')
44
- .option('--host [host]', 'Contentful domain to use')
45
- .action(async (options) => {
46
- await runCommand(activate, options);
47
- });
48
-
49
- program
50
- .command('open-settings')
51
- .description('Opens the app editor for a given AppDefinition')
52
- .option('--definition-id [defId]', 'The id of your apps definition')
53
- .action(async (options) => {
54
- await runCommand(open, options);
55
- });
56
-
57
- program
58
- .command('bundle-cleanup')
59
- .description('Removes old, non-active bundles, only keeps the 50 most recent ones')
60
- .option('--organization-id [orgId]', 'The id of your organization')
61
- .option('--definition-id [defId]', 'The id of your apps definition')
62
- .option('--token [accessToken]', 'Your content management access token')
63
- .option('--keep [keepAmount]', 'The amount of bundles that should remain')
64
- .option('--host [host]', 'Contentful domain to use')
65
- .action(async (options) => {
66
- await runCommand(cleanup, options);
67
- });
68
-
69
- program.hook('preAction', (thisCommand) => {
70
- track({ command: thisCommand.args[0], ci: `${thisCommand._optionValues.ci}` });
71
- });
72
-
73
- await program.parseAsync(process.argv);
74
- })().catch((e) => {
75
- console.error(e);
76
- // eslint-disable-next-line no-process-exit
77
- process.exit(1);
78
- });
@@ -1,69 +0,0 @@
1
- const { stub, match } = require('sinon');
2
- const assert = require('assert');
3
- const proxyquire = require('proxyquire');
4
-
5
- const mockedSettings = {
6
- accessToken: 'token',
7
- organization: { value: 'id' },
8
- definition: { value: 'id' },
9
- bundleId: 'bundle-id',
10
- };
11
-
12
- describe('activate-bundle', () => {
13
- // eslint-disable-next-line no-unused-vars
14
- let activateBundle, clientMock, updateStub, createClientArgs;
15
- beforeEach(() => {
16
- stub(console, 'log');
17
- });
18
-
19
- afterEach(() => {
20
- console.log.restore();
21
- });
22
-
23
- const throwErrorStub = stub();
24
- const definitionMock = {
25
- src: 'src',
26
- bundle: undefined,
27
- locations: [],
28
- };
29
-
30
- beforeEach(() => {
31
- updateStub = stub();
32
- clientMock = {
33
- appDefinition: {
34
- get: () => definitionMock,
35
- update: () => updateStub(),
36
- },
37
- };
38
-
39
- ({ activateBundle } = proxyquire('./activate-bundle', {
40
- 'contentful-management': {
41
- createClient: (...args) => {
42
- createClientArgs = args;
43
- return clientMock;
44
- },
45
- },
46
- '../utils': {
47
- throwError: throwErrorStub,
48
- },
49
- }));
50
- });
51
-
52
- it('updates definition with bundle, sets default location, and sets src to undefined', async () => {
53
- await activateBundle(mockedSettings);
54
- assert.strictEqual(definitionMock.bundle.sys.id, mockedSettings.bundleId);
55
- assert.strictEqual(definitionMock.locations.length, 1);
56
- assert.strictEqual(definitionMock.src, undefined);
57
- assert.strictEqual(updateStub.called, true);
58
- assert(console.log.calledWith(match(/Your app bundle was activated/)));
59
- });
60
- it('shows error when update went wrong', async () => {
61
- updateStub = stub().rejects(new Error());
62
- await activateBundle(mockedSettings);
63
- assert.strictEqual(throwErrorStub.called, true);
64
- });
65
- it('supports custom defined host domain', async () => {
66
- await activateBundle({ ...mockedSettings, host: 'jane.doe.com' });
67
- assert.strictEqual(createClientArgs[0].host, 'jane.doe.com');
68
- });
69
- });
@@ -1,118 +0,0 @@
1
- const { stub, spy, match, useFakeTimers } = require('sinon');
2
- const assert = require('assert');
3
- const proxyquire = require('proxyquire');
4
-
5
- const mockedSettings = { definition: { value: 'test' }, organization: { value: 'test' } };
6
- const bundlesFixture = [
7
- { sys: { id: 'test-1' } },
8
- { sys: { id: 'test-2' } },
9
- { sys: { id: 'test-3' } },
10
- { sys: { id: 'test-4' } },
11
- { sys: { id: 'test-5' } },
12
- { sys: { id: 'test-6' } },
13
- { sys: { id: 'test-7' } },
14
- { sys: { id: 'test-8' } },
15
- ];
16
-
17
- describe('cleanUpBundles', () => {
18
- let subject, clientMock, deleteMock, createClientArgs;
19
- let mockedBundles = bundlesFixture;
20
-
21
- beforeEach(() => {
22
- stub(console, 'log');
23
- });
24
-
25
- afterEach(() => {
26
- mockedBundles = bundlesFixture;
27
- console.log.restore();
28
- });
29
-
30
- beforeEach(() => {
31
- deleteMock = stub();
32
- clientMock = {
33
- appBundle: {
34
- getMany: () => ({
35
- total: mockedBundles.length,
36
- items: mockedBundles,
37
- }),
38
- delete: deleteMock,
39
- },
40
- appDefinition: {
41
- get: () => ({ bundle: { sys: { id: 'active-bundle' } } }),
42
- delete: deleteMock,
43
- },
44
- };
45
- ({ cleanUpBundles: subject } = proxyquire('./clean-up-bundles', {
46
- 'contentful-management': {
47
- createClient: (...args) => {
48
- createClientArgs = args;
49
- return clientMock;
50
- },
51
- },
52
- '../../utils/constants': {
53
- MAX_CONCURRENT_DELETION_CALLS: 2,
54
- },
55
- }));
56
- });
57
-
58
- it('deletes the correct amount when keep is defined with number', async () => {
59
- const keep = 1;
60
- await subject({ ...mockedSettings, keep });
61
- assert.strictEqual(deleteMock.callCount, mockedBundles.length - keep);
62
- });
63
- it('deletes all when 0 to keep on a list of 4', async () => {
64
- await subject({ ...mockedSettings, keep: 0 });
65
- assert.strictEqual(deleteMock.callCount, mockedBundles.length);
66
- });
67
- it('does not delete the active one', async () => {
68
- mockedBundles = [{ sys: { id: 'test-1' } }, { sys: { id: 'active-bundle' } }];
69
- await subject({ ...mockedSettings, keep: 0 });
70
- assert.strictEqual(deleteMock.callCount, 1);
71
- });
72
- it('shows a warning when nothing will be deleted and delete is not called', async () => {
73
- await subject({ ...mockedSettings, keep: 100 });
74
- assert.strictEqual(deleteMock.called, false);
75
- assert(console.log.calledWith(match(/There is nothing to delete/)));
76
- });
77
- it('only runs specific deletion calls at a time ', async () => {
78
- const clock = useFakeTimers();
79
- clientMock.appBundle.delete = stub().callsFake(() => new Promise((r) => setTimeout(r, 100)));
80
- subject({ ...mockedSettings, keep: 0 });
81
- await clock.tickAsync(50);
82
- // Here we expect only the first ones to be called
83
- assert.strictEqual(clientMock.appBundle.delete.callCount, 2); // 2 is batch size
84
- await clock.tickAsync(150);
85
- // Here the next ones also have called
86
- assert.strictEqual(clientMock.appBundle.delete.callCount, 4); // 2 before + 2 new
87
-
88
- clock.restore();
89
- });
90
- it('slow call will occupy slot until finished', async () => {
91
- const clock = useFakeTimers();
92
- mockedBundles.unshift({ sys: { id: 'slow' } });
93
- mockedBundles = mockedBundles.reverse();
94
- clientMock.appBundle.delete = stub().callsFake(
95
- ({ appBundleId }) => new Promise((r) => setTimeout(r, appBundleId === 'slow' ? 200 : 100)),
96
- );
97
- subject({ ...mockedSettings, keep: 0 });
98
- await clock.tickAsync(50);
99
- // Here we expect only the first ones to be called
100
- assert.strictEqual(clientMock.appBundle.delete.callCount, 2); // 2 is batch size
101
- await clock.tickAsync(100);
102
- // Here there are still only two called
103
- assert.strictEqual(clientMock.appBundle.delete.callCount, 3); // 2 before + 1 new (1 still in flight and occupies slot)
104
- await clock.tickAsync(100);
105
- assert.strictEqual(clientMock.appBundle.delete.callCount, 5); // 3 before + 2 new (slot is free again)
106
-
107
- clock.restore();
108
- });
109
- it('supports custom defined host domain', async () => {
110
- await subject({ ...mockedSettings, host: 'jane.doe.com' });
111
- assert.strictEqual(createClientArgs[0].host, 'jane.doe.com');
112
- });
113
- it('queries more than 100 bundles', async () => {
114
- spy(clientMock.appBundle, 'getMany');
115
- await subject(mockedSettings);
116
- assert(clientMock.appBundle.getMany.calledWith(match({ query: { limit: 1000 } })));
117
- });
118
- });
@@ -1,87 +0,0 @@
1
- const proxyquire = require('proxyquire');
2
- const { stub, match } = require('sinon');
3
- const assert = require('assert');
4
- const {
5
- APP_DEF_ENV_KEY,
6
- } = require('../../utils/constants');
7
-
8
- const organizationId = 'orgId';
9
- const token = 'token';
10
-
11
- describe('createAppDefinition', () => {
12
- let subject, clientMock, selectFromListMock, cachedEnvVarsMock;
13
-
14
- beforeEach(() => {
15
- stub(console, 'log');
16
- });
17
-
18
- afterEach(() => {
19
- console.log.restore();
20
- });
21
-
22
- beforeEach(() => {
23
- clientMock = {
24
- getOrganization: stub(),
25
- getOrganizations: stub()
26
- };
27
-
28
- cachedEnvVarsMock = stub().resolves(undefined);
29
-
30
- selectFromListMock = stub();
31
-
32
- ({ createAppDefinition: subject } = proxyquire('./create-app-definition', {
33
- 'contentful-management': {
34
- createClient: () => {
35
- return clientMock;
36
- }
37
- },
38
- '../../utils/cache-credential': {
39
- cacheEnvVars: cachedEnvVarsMock
40
- },
41
- '../utils': {
42
- selectFromList: selectFromListMock
43
- }
44
- }));
45
- });
46
-
47
- it('throws with invalid options', () => assert.rejects(() => subject(), /TypeError/));
48
-
49
- it('throws if unable to fetch organizations', async () => {
50
- clientMock.getOrganizations = stub().rejects(new Error());
51
-
52
- await assert.rejects(() => subject(token, { locations: [] }));
53
- assert(console.log.calledWith(match(/Could not fetch your organizations/)));
54
- });
55
-
56
- it('throws if unable to create definition', async () => {
57
- clientMock.getOrganization = stub().resolves({ createAppDefinition: stub().rejects(new Error()) });
58
- clientMock.getOrganizations = stub().resolves({ items: [{ name: 'name', sys: { id: organizationId } }] });
59
- selectFromListMock.returns({ name: 'name', value: organizationId });
60
-
61
- await assert.rejects(() => subject(token, { locations: [] }));
62
- assert(console.log.calledWith(match(/Something went wrong while creating the app definition/)));
63
- });
64
-
65
- it('logs success message', async () => {
66
- const appId = 'appId';
67
- const orgSettingsLink = 'https://app.contentful.com/deeplink?link=org';
68
- const appLink = `https://app.contentful.com/deeplink?link=apps&id=${appId}`;
69
- const tutorialLink = 'https://ctfl.io/app-tutorial';
70
-
71
- clientMock.getOrganization = stub().resolves({
72
- createAppDefinition: stub().resolves({ sys: { id: 'appId' } })
73
- });
74
- clientMock.getOrganizations = stub().resolves({ items: [{ name: 'name', sys: { id: organizationId } }] });
75
- selectFromListMock.returns({ name: 'name', value: organizationId });
76
-
77
- await assert.doesNotReject(() => subject(token, { locations: [] }));
78
-
79
- const loggedMessage = console.log.getCall(0).args[0];
80
-
81
- assert(loggedMessage.includes('Success'));
82
- assert(loggedMessage.includes(orgSettingsLink));
83
- assert(loggedMessage.includes(appLink));
84
- assert(loggedMessage.includes(tutorialLink));
85
- assert.deepStrictEqual(cachedEnvVarsMock.args[0][0], {[APP_DEF_ENV_KEY]: 'appId'});
86
- });
87
- });
@@ -1,57 +0,0 @@
1
- const { stub } = require('sinon');
2
- const proxyquire = require('proxyquire');
3
- const assert = require('assert');
4
-
5
- describe('get-app-info', () => {
6
- let getAppInfo, stubs;
7
- const mockedSettings = {
8
- token: 'token',
9
- organizationId: 'test-id',
10
- definitionId: 'test-id',
11
- };
12
-
13
- beforeEach(() => {
14
- stubs = {
15
- getDefinitionByIdStub: stub().returns({ value: 'id' }),
16
- selectDefinitionStub: stub().returns({ value: 'id' }),
17
- getOrganizationByIdStub: stub().returns({ value: 'id' }),
18
- selectOrganizationStub: stub().returns({ value: 'id' }),
19
- };
20
- ({ getAppInfo } = proxyquire('./get-app-info', {
21
- './definition-api': {
22
- getDefinitionById: stubs.getDefinitionByIdStub,
23
- selectDefinition: stubs.selectDefinitionStub,
24
- },
25
- './organization-api': {
26
- getOrganizationById: stubs.getOrganizationByIdStub,
27
- selectOrganization: stubs.selectOrganizationStub,
28
- },
29
- inquirer: {
30
- prompt: stub(),
31
- },
32
- './get-management-token': {
33
- getManagementToken: stub(),
34
- },
35
- }));
36
- });
37
- it('calls select Organization when id not provided', async () => {
38
- await getAppInfo({ ...mockedSettings, organizationId: undefined });
39
- assert.strictEqual(stubs.selectOrganizationStub.called, true);
40
- assert.strictEqual(stubs.getOrganizationByIdStub.called, false);
41
- });
42
- it('calls getOrganizationById when id provided', async () => {
43
- await getAppInfo(mockedSettings);
44
- assert.strictEqual(stubs.selectDefinitionStub.called, false);
45
- assert.strictEqual(stubs.getDefinitionByIdStub.called, true);
46
- });
47
- it('calls select Definition when id not provided', async () => {
48
- await getAppInfo({ ...mockedSettings, definitionId: undefined });
49
- assert.strictEqual(stubs.selectDefinitionStub.called, true);
50
- assert.strictEqual(stubs.getDefinitionByIdStub.called, false);
51
- });
52
- it('calls getDefinitionById when id provided', async () => {
53
- await getAppInfo(mockedSettings);
54
- assert.strictEqual(stubs.selectDefinitionStub.called, false);
55
- assert.strictEqual(stubs.getDefinitionByIdStub.called, true);
56
- });
57
- });
@@ -1,82 +0,0 @@
1
- const proxyquire = require('proxyquire');
2
- const { stub, spy } = require('sinon');
3
- const assert = require('assert');
4
-
5
- describe('getManagementToken-js', () => {
6
- context('getManagementToken', () => {
7
- let subject, promptMock, openMock;
8
-
9
- beforeEach(() => {
10
- stub(console, 'log');
11
- });
12
-
13
- afterEach(() => {
14
- console.log.restore();
15
- });
16
-
17
- beforeEach(() => {
18
- promptMock = stub();
19
- openMock = stub();
20
-
21
- ({ getManagementToken: subject } = proxyquire('./get-management-token', {
22
- inquirer: { prompt: promptMock },
23
- open: openMock,
24
- 'contentful-management': {
25
- createClient() {
26
- return {
27
- async getOrganizations() {
28
- throw new Error();
29
- },
30
- };
31
- },
32
- },
33
- }));
34
- });
35
-
36
- it('throws when unable to open the browser', async () => {
37
- openMock.throws(new Error());
38
- await assert.rejects(() => subject());
39
- });
40
-
41
- it('throws when unable to get input', async () => {
42
- promptMock.throws(new Error());
43
- await assert.rejects(() => subject());
44
- });
45
-
46
- it('returns management token', async () => {
47
- const mgmtToken = 'token';
48
- promptMock.returns({ mgmtToken });
49
- assert.strictEqual(await subject(), mgmtToken);
50
- });
51
- });
52
-
53
- context('checkTokenValidity', () => {
54
- let subject, createClientSpy, promptMock, openMock;
55
- const mgmtToken = 'token';
56
- const host = 'host';
57
- const mockedSettings = {
58
- host,
59
- accessToken: mgmtToken,
60
- };
61
-
62
- beforeEach(() => {
63
- promptMock = stub();
64
- openMock = stub();
65
- createClientSpy = spy();
66
-
67
- ({ getManagementToken: subject } = proxyquire('./get-management-token', {
68
- inquirer: { prompt: promptMock },
69
- open: openMock,
70
- 'contentful-management': {
71
- createClient: createClientSpy,
72
- },
73
- }));
74
- });
75
-
76
- it('Call CMA with a host argument', async () => {
77
- promptMock.returns({ mgmtToken });
78
- assert.strictEqual(await subject(host), mgmtToken);
79
- assert.strictEqual(createClientSpy.args[0][0].host, mockedSettings.host);
80
- });
81
- });
82
- });
@@ -1,45 +0,0 @@
1
- const { stub } = require('sinon');
2
- const assert = require('assert');
3
- const { APP_DEF_ENV_KEY } = require('../../utils/constants');
4
- const proxyquire = require('proxyquire');
5
- const { REDIRECT_URL } = require('./open-settings');
6
-
7
- const TEST_DEF_ID = 'test-def-id';
8
-
9
- describe('openSettings', () => {
10
- let subject, openMock, inquirerMock;
11
- beforeEach(() => {
12
- delete process.env[APP_DEF_ENV_KEY];
13
- stub(console, 'log');
14
- });
15
-
16
- afterEach(() => {
17
- console.log.restore();
18
- delete process.env[APP_DEF_ENV_KEY];
19
- });
20
-
21
- beforeEach(() => {
22
- openMock = stub();
23
- inquirerMock = stub();
24
- ({ openSettings: subject } = proxyquire('./open-settings', {
25
- open: openMock,
26
- inquirer: { prompt: inquirerMock },
27
- }));
28
- });
29
-
30
- it('works with option passed', () => {
31
- subject({ definitionId: TEST_DEF_ID });
32
- assert(openMock.calledWith(`${REDIRECT_URL}&id=${TEST_DEF_ID}`));
33
- });
34
-
35
- it('shows prompt when no definition is provided', () => {
36
- subject({});
37
- assert.strictEqual(inquirerMock.called, true);
38
- });
39
-
40
- it('works with env variable set', () => {
41
- process.env[APP_DEF_ENV_KEY] = TEST_DEF_ID;
42
- subject({});
43
- assert(openMock.calledWith(`${REDIRECT_URL}&id=${TEST_DEF_ID}`));
44
- });
45
- });
@@ -1,110 +0,0 @@
1
- const { stub, match } = require('sinon');
2
- const assert = require('assert');
3
- const proxyquire = require('proxyquire');
4
-
5
- const bundleMock = { sys: { id: 'mocked_bundle' } };
6
- const mockedSettings = {
7
- accessToken: 'token',
8
- organization: { value: 'id' },
9
- definition: { value: 'id' },
10
- };
11
-
12
- describe('createAppBundleFromUpload', () => {
13
- // eslint-disable-next-line no-unused-vars
14
- let createAppBundleFromUpload, clientMock, createClientArgs;
15
-
16
- beforeEach(() => {
17
- stub(console, 'log');
18
- });
19
-
20
- afterEach(() => {
21
- console.log.restore();
22
- });
23
-
24
- beforeEach(() => {
25
- clientMock = {
26
- getOrganization: () => ({
27
- getAppDefinition: () => ({
28
- createAppBundle: () => bundleMock,
29
- }),
30
- }),
31
- getOrganizations: stub(),
32
- };
33
-
34
- ({ createAppBundleFromUpload } = proxyquire('./create-app-bundle', {
35
- 'contentful-management': {
36
- createClient: (...args) => {
37
- createClientArgs = args;
38
- return clientMock;
39
- },
40
- },
41
- }));
42
- });
43
- it('creates app bundle', async () => {
44
- const bundle = await createAppBundleFromUpload(mockedSettings, 'upload-id');
45
- assert.strictEqual(bundle, bundleMock);
46
- });
47
- it('shows creation error when createAppBundle throws', async () => {
48
- clientMock.getOrganization = () => ({
49
- getAppDefinition: () => ({
50
- createAppBundle: stub().rejects(new Error()),
51
- }),
52
- });
53
- await createAppBundleFromUpload(mockedSettings, 'upload-id');
54
-
55
- assert(console.log.calledWith(match(/Creation error:/)));
56
- });
57
- it('supports custom defined host domain creating appbundle from upload', async () => {
58
- await createAppBundleFromUpload({ ...mockedSettings, host: 'jane.doe.com' });
59
- assert.strictEqual(createClientArgs[0].host, 'jane.doe.com');
60
- });
61
- });
62
-
63
- describe('createAppBundleFromSettings', () => {
64
- let createAppBundleFromSettings, clientMock, uploadMock, createClientArgs;
65
-
66
- beforeEach(() => {
67
- stub(console, 'log');
68
- });
69
-
70
- afterEach(() => {
71
- console.log.restore();
72
- });
73
-
74
- beforeEach(() => {
75
- clientMock = {
76
- getOrganization: () => ({
77
- getAppDefinition: () => ({
78
- createAppBundle: () => bundleMock,
79
- }),
80
- }),
81
- getOrganizations: stub(),
82
- };
83
-
84
- uploadMock = { sys: { id: 'test-id' } };
85
-
86
- ({ createAppBundleFromSettings } = proxyquire('./create-app-bundle', {
87
- 'contentful-management': {
88
- createClient: (...args) => {
89
- createClientArgs = args;
90
- return clientMock;
91
- },
92
- },
93
- './create-app-upload': { createAppUpload: () => uploadMock },
94
- }));
95
- });
96
- it('creates app bundle', async () => {
97
- await createAppBundleFromSettings(mockedSettings);
98
- assert(console.log.calledWith(match(/Your files were successfully uploaded/)));
99
- assert(console.log.calledWith(match(/Created a new app bundle/)));
100
- });
101
- it('shows creation error when app upload did not succeed', async () => {
102
- uploadMock = stub().rejects(new Error())();
103
- await createAppBundleFromSettings(mockedSettings);
104
- assert(console.log.calledWith(match(/Creation error:/)));
105
- });
106
- it('supports custom defined host domain creating appbundle from settings', async () => {
107
- await createAppBundleFromSettings({ ...mockedSettings, host: 'jane.doe.com' });
108
- assert.strictEqual(createClientArgs[0].host, 'jane.doe.com');
109
- });
110
- });