@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
@@ -1,50 +0,0 @@
1
- const assert = require('assert');
2
- const { stub, match } = require('sinon');
3
- const proxyquire = require('proxyquire');
4
- describe('createAppUpload', () => {
5
- let createAppUpload, clientMock;
6
- const uploadMock = { sys: { id: 'test-id' } };
7
- const mockedSettings = {
8
- accessToken: 'token',
9
- organization: { value: 'id' },
10
- definition: { value: 'id' },
11
- };
12
- beforeEach(() => {
13
- stub(console, 'log');
14
- });
15
-
16
- afterEach(() => {
17
- console.log.restore();
18
- });
19
-
20
- beforeEach(() => {
21
- clientMock = {
22
- getOrganization: () => ({
23
- createAppUpload: () => uploadMock,
24
- }),
25
- };
26
-
27
- ({ createAppUpload } = proxyquire('./create-app-upload', {
28
- 'contentful-management': {
29
- createClient: () => {
30
- return clientMock;
31
- },
32
- },
33
- './create-zip-from-directory': { createZipFileFromDirectory: () => 'zip_file' },
34
- './validate-bundle': { validateBundle: () => {} },
35
- }));
36
- });
37
-
38
- it('successfully creates an app upload from settings', async () => {
39
- const appUpload = await createAppUpload(mockedSettings);
40
- assert.strictEqual(appUpload, uploadMock);
41
- });
42
- it('shows creation error when createAppUpload throws', async () => {
43
- clientMock.getOrganization = () => ({
44
- createAppUpload: stub().rejects(new Error()),
45
- });
46
- await createAppUpload(mockedSettings);
47
-
48
- assert(console.log.calledWith(match(/Creation error:/)));
49
- });
50
- });
@@ -1,45 +0,0 @@
1
- const sinon = require('sinon');
2
- const proxyquire = require('proxyquire');
3
- const assert = require('assert');
4
-
5
- describe('validateBundle', () => {
6
- beforeEach(() => {
7
- sinon.stub(console, 'warn');
8
- });
9
- afterEach(() => {
10
- console.warn.restore();
11
- });
12
-
13
- it('throws when there is no index.html', () => {
14
- const fsstub = { readdirSync: () => [] };
15
- const { validateBundle } = proxyquire('./validate-bundle', { fs: fsstub });
16
- try {
17
- validateBundle('build');
18
- } catch (e) {
19
- assert.strictEqual(
20
- e.message,
21
- 'Make sure your bundle includes a valid index.html file in its root folder.'
22
- );
23
- }
24
- });
25
-
26
- it('warns when the index.html contains an absolute path', () => {
27
- const fsstub = {
28
- readdirSync: () => ['index.html'],
29
- readFileSync: () => '<html><script src="/absolute/path"></script></html>',
30
- };
31
- const { validateBundle } = proxyquire('./validate-bundle', { fs: fsstub });
32
- validateBundle('build');
33
- assert(console.warn.calledWith(sinon.match(/absolute paths/)));
34
- });
35
-
36
- it('does not warn when the index.html contains only relative paths', () => {
37
- const fsstub = {
38
- readdirSync: () => ['index.html'],
39
- readFileSync: () => '<html><script src="./relative/path"></script></html>',
40
- };
41
- const { validateBundle } = proxyquire('./validate-bundle', { fs: fsstub });
42
- validateBundle('build');
43
- sinon.assert.notCalled(console.warn)
44
- });
45
- });
package/lib/utils.test.js DELETED
@@ -1,323 +0,0 @@
1
- const assert = require('assert');
2
- const { stub } = require('sinon');
3
- const proxyquire = require('proxyquire');
4
-
5
- const { isValidNetwork, stripProtocol } = require('./utils');
6
-
7
- describe('isValidIpAddress', () => {
8
- it('returns true for a valid IP address', () => {
9
- const result = isValidNetwork('192.168.0.1');
10
- assert.strictEqual(result, true);
11
- });
12
-
13
- it('returns true for a valid domain', () => {
14
- const result = isValidNetwork('google.com');
15
- assert.strictEqual(result, true);
16
- });
17
-
18
- it('returns true for a valid ipv6 address', () => {
19
- const result = isValidNetwork('2001:0db8:85a3:0000:0000:8a2e:0370:7334');
20
- assert.strictEqual(result, true);
21
- });
22
-
23
- it('returns true for a valid domain with port', () => {
24
- const result = isValidNetwork('google.com:4000');
25
- assert.strictEqual(result, true);
26
- });
27
-
28
- it('returns true for a valid IP address with port', () => {
29
- const result = isValidNetwork('192.168.0.1:2000');
30
- assert.strictEqual(result, true);
31
- });
32
-
33
- it('returns false for an invalid IP address', () => {
34
- const result = isValidNetwork('not an ip address');
35
- assert.strictEqual(result, false);
36
- });
37
-
38
- it('returns false for an invalid IP address', () => {
39
- const result = isValidNetwork('427.0.0.1');
40
- assert.strictEqual(result, false);
41
- });
42
-
43
- it('returns true for an valid ipv6 address with port', () => {
44
- const result = isValidNetwork('[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:443');
45
- assert.strictEqual(result, true);
46
- });
47
- });
48
-
49
- describe('removeProtocolFromUrl', () => {
50
- it('returns the host of a URL without a protocol', () => {
51
- const result = stripProtocol('http://example.com');
52
- assert.strictEqual(result, 'example.com');
53
- });
54
-
55
- it('returns valid ipv6 address', () => {
56
- const result = stripProtocol('http://[2001:0db8:85a3:0000:0000:8a2e:0370]:7334');
57
- assert.strictEqual(result, '[2001:0db8:85a3:0000:0000:8a2e:0370]:7334');
58
- });
59
-
60
- it('returns valid domain', () => {
61
- const result = stripProtocol('example.com');
62
- assert.strictEqual(result, 'example.com');
63
- });
64
-
65
- it('returns valid domain with port', () => {
66
- const result = stripProtocol('example.com:40');
67
- assert.strictEqual(result, 'example.com:40');
68
- });
69
- });
70
-
71
- describe('get actions from manifest', () => {
72
- let fs, exitStub, consoleLog, chalk;
73
-
74
- const actionMock = {
75
- name: 'name',
76
- category: 'Custom',
77
- description: 'descriptoin',
78
- type: 'function',
79
- path: 'actions/mock.js',
80
- entryFile: './actions/mock.ts',
81
- parameters: [],
82
- allowNetworks: ['127.0.0.1', 'some.domain.tld'],
83
- };
84
- // eslint-disable-next-line no-unused-vars
85
- const { entryFile: _, ...resultMock } = actionMock;
86
-
87
- fs = {
88
- existsSync: stub(),
89
- readFileSync: stub(),
90
- };
91
- chalk = {
92
- bold: stub(),
93
- red: stub(),
94
- };
95
-
96
- let { getEntityFromManifest } = proxyquire('./utils', { fs, chalk });
97
-
98
- beforeEach(() => {
99
- exitStub = stub(process, 'exit');
100
- consoleLog = stub(console, 'log');
101
- });
102
- afterEach(() => {
103
- exitStub.restore();
104
- consoleLog.restore();
105
- });
106
-
107
- it('should return undefined if manifest does not exist', () => {
108
- fs.existsSync.returns(false);
109
-
110
- const result = getEntityFromManifest('actions');
111
-
112
- assert.equal(result, undefined);
113
- });
114
-
115
- it('should return undefined if manifest has no actions', () => {
116
- fs.existsSync.returns(true);
117
- fs.readFileSync.returns(JSON.stringify({ actions: [] }));
118
-
119
- const result = getEntityFromManifest('actions');
120
- assert.equal(result, undefined);
121
- });
122
-
123
- it('should return an array of actions if manifest is valid', () => {
124
- fs.existsSync.returns(true);
125
- fs.readFileSync.returns(
126
- JSON.stringify({
127
- actions: [actionMock],
128
- }),
129
- );
130
-
131
- const result = getEntityFromManifest('actions');
132
-
133
- assert.deepEqual(result, [resultMock]);
134
- assert.ok(consoleLog.called);
135
- });
136
-
137
- it('should strip the protocol when a domain has a protocol in allowNetworks', () => {
138
- const mockAction = {
139
- ...actionMock,
140
- allowNetworks: ['http://some.domain.tld'],
141
- };
142
- // eslint-disable-next-line no-unused-vars
143
- const { entryFile: _, ...resultMock } = mockAction;
144
- fs.existsSync.returns(true);
145
- fs.readFileSync.returns(
146
- JSON.stringify({
147
- actions: [mockAction],
148
- }),
149
- );
150
-
151
- const result = getEntityFromManifest('actions');
152
-
153
- assert.deepEqual(result, [{ ...resultMock, allowNetworks: ['some.domain.tld'] }]);
154
- assert.ok(consoleLog.called);
155
- });
156
-
157
- it('should return an array of actions without entryFile prop if manifest is valid', () => {
158
- fs.existsSync.returns(true);
159
- fs.readFileSync.returns(
160
- JSON.stringify({
161
- actions: [actionMock],
162
- }),
163
- );
164
-
165
- const result = getEntityFromManifest('actions');
166
-
167
- assert.notDeepEqual(result, [actionMock]);
168
- });
169
-
170
- it('should exit with error if invalid network is found in allowNetworks', () => {
171
- fs.existsSync.returns(true);
172
- fs.readFileSync.returns(
173
- JSON.stringify({
174
- actions: [
175
- {
176
- name: 'action1',
177
- entryFile: 'entry1',
178
- allowNetworks: ['412.1.1.1'],
179
- },
180
- ],
181
- }),
182
- );
183
-
184
- getEntityFromManifest('actions');
185
-
186
- assert.ok(exitStub.calledOnceWith(1));
187
- });
188
-
189
- it('should exit with error if manifest is invalid JSON', () => {
190
- fs.existsSync.returns(true);
191
- fs.readFileSync.throws();
192
-
193
- getEntityFromManifest('actions');
194
-
195
- assert.ok(exitStub.calledOnceWith(1));
196
- });
197
- });
198
-
199
- describe('get delivery functions from manifest', () => {
200
- let fs, exitStub, consoleLog, chalk;
201
-
202
- const deliveryFnMock = {
203
- id: 'test',
204
- name: 'name',
205
- description: 'descriptoin',
206
- path: 'delivery-functions/mock.js',
207
- entryFile: './delivery-functions/mock.ts',
208
- allowNetworks: ['127.0.0.1', 'some.domain.tld'],
209
- };
210
- // eslint-disable-next-line no-unused-vars
211
- const { entryFile: _, ...resultMock } = deliveryFnMock;
212
-
213
- fs = {
214
- existsSync: stub(),
215
- readFileSync: stub(),
216
- };
217
- chalk = {
218
- bold: stub(),
219
- red: stub(),
220
- };
221
-
222
- let { getEntityFromManifest } = proxyquire('./utils', { fs, chalk });
223
-
224
- beforeEach(() => {
225
- exitStub = stub(process, 'exit');
226
- consoleLog = stub(console, 'log');
227
- });
228
- afterEach(() => {
229
- exitStub.restore();
230
- consoleLog.restore();
231
- });
232
-
233
- it('should return undefined if manifest does not exist', () => {
234
- fs.existsSync.returns(false);
235
-
236
- const result = getEntityFromManifest('deliveryFunctions');
237
-
238
- assert.equal(result, undefined);
239
- });
240
-
241
- it('should return undefined if manifest has no delivery functions', () => {
242
- fs.existsSync.returns(true);
243
- fs.readFileSync.returns(JSON.stringify({ deliveryFunctions: [] }));
244
-
245
- const result = getEntityFromManifest('deliveryFunctions');
246
- assert.equal(result, undefined);
247
- });
248
-
249
- it('should return an array of delivery functions if manifest is valid', () => {
250
- fs.existsSync.returns(true);
251
- fs.readFileSync.returns(
252
- JSON.stringify({
253
- deliveryFunctions: [deliveryFnMock],
254
- }),
255
- );
256
-
257
- const result = getEntityFromManifest('deliveryFunctions');
258
-
259
- assert.deepEqual(result, [resultMock]);
260
- assert.ok(consoleLog.called);
261
- });
262
-
263
- it('should strip the protocol when a domain has a protocol in allowNetworks', () => {
264
- const mockDeliveryFn = {
265
- ...deliveryFnMock,
266
- allowNetworks: ['http://some.domain.tld'],
267
- };
268
- // eslint-disable-next-line no-unused-vars
269
- const { entryFile: _, ...resultMock } = mockDeliveryFn;
270
- fs.existsSync.returns(true);
271
- fs.readFileSync.returns(
272
- JSON.stringify({
273
- deliveryFunctions: [mockDeliveryFn],
274
- }),
275
- );
276
-
277
- const result = getEntityFromManifest('deliveryFunctions');
278
-
279
- assert.deepEqual(result, [{ ...resultMock, allowNetworks: ['some.domain.tld'] }]);
280
- assert.ok(consoleLog.called);
281
- });
282
-
283
- it('should return an array of delivery functions without entryFile prop if manifest is valid', () => {
284
- fs.existsSync.returns(true);
285
- fs.readFileSync.returns(
286
- JSON.stringify({
287
- deliveryFunctions: [deliveryFnMock],
288
- }),
289
- );
290
-
291
- const result = getEntityFromManifest('deliveryFunctions');
292
-
293
- assert.notDeepEqual(result, [deliveryFnMock]);
294
- });
295
-
296
- it('should exit with error if invalid network is found in allowNetworks', () => {
297
- fs.existsSync.returns(true);
298
- fs.readFileSync.returns(
299
- JSON.stringify({
300
- deliveryFunctions: [
301
- {
302
- name: 'test resolver fn',
303
- entryFile: 'entry1',
304
- allowNetworks: ['412.1.1.1'],
305
- },
306
- ],
307
- }),
308
- );
309
-
310
- getEntityFromManifest('deliveryFunctions');
311
-
312
- assert.ok(exitStub.calledOnceWith(1));
313
- });
314
-
315
- it('should exit with error if manifest is invalid JSON', () => {
316
- fs.existsSync.returns(true);
317
- fs.readFileSync.throws();
318
-
319
- getEntityFromManifest('deliveryFunctions');
320
-
321
- assert.ok(exitStub.calledOnceWith(1));
322
- });
323
- });
@@ -1,33 +0,0 @@
1
- const assert = require('assert');
2
- const { validateArguments } = require('./validate-arguments');
3
- const { stub } = require('sinon');
4
-
5
- describe('validateArguments', () => {
6
- const options = {
7
- firstKey: 'anything',
8
- secondKey: 'anything',
9
- };
10
- const requiredOptions = {
11
- firstKey: '--anything',
12
- secondKey: '--anything',
13
- thirdKey: '--anything',
14
- };
15
-
16
- beforeEach(() => {
17
- stub(console, 'log');
18
- });
19
-
20
- afterEach(() => {
21
- console.log.restore();
22
- });
23
-
24
- it('throws an error when options does not contain requiredOptions', () => {
25
- const throwing = () => validateArguments(requiredOptions, options);
26
- assert.throws(throwing, Error);
27
- });
28
-
29
- it('does not throw an error when options does contain requiredOptions', () => {
30
- const throwing = () => validateArguments(requiredOptions, { ...options, thirdKey: 'anything' });
31
- assert.doesNotThrow(throwing, Error);
32
- });
33
- });
@@ -1,36 +0,0 @@
1
- const Analytics = require('analytics-node');
2
-
3
- // Public write key scoped to data source
4
- const SEGMENT_WRITE_KEY = 'IzCq3j4dQlTAgLdMykRW9oBHQKUy1xMm';
5
-
6
- /**
7
- *
8
- * @param {object} properties tracking properties
9
- * @param {string} properties.command triggered command e.g create-app-definition, upload, etc.
10
- * @param {boolean} properties.ci value if --ci flag has been set
11
- * @returns
12
- */
13
- function track(properties) {
14
- if (process.env.DISABLE_ANALYTICS) {
15
- return;
16
- }
17
-
18
- try {
19
- const client = new Analytics(SEGMENT_WRITE_KEY, {
20
- errorHandler: () => {
21
- // noop
22
- },
23
- });
24
- client.track({
25
- event: 'app-cli-app-scripts',
26
- properties,
27
- anonymousId: Date.now(), // generate a random id
28
- timestamp: new Date(),
29
- });
30
- // eslint-disable-next-line no-empty
31
- } catch (e) {
32
- // ignore any error, to not block the call
33
- }
34
- }
35
-
36
- module.exports = track;
@@ -1,81 +0,0 @@
1
- const dotenv = require('dotenv');
2
- const fs = require('fs');
3
- const { EOL } = require('os');
4
- const ignore = require('ignore')();
5
- const chalk = require('chalk');
6
- const { DOTENV_FILE } = require('../constants')
7
-
8
-
9
- const fsPromises = fs.promises;
10
- const fsConstants = fs.constants;
11
-
12
- // Always export set env vars by default
13
- dotenv.config();
14
-
15
- async function removeOldEnv(envKey) {
16
- const envFileData = await fsPromises.readFile(DOTENV_FILE, {encoding: 'utf-8',});
17
- const envVarsData = dotenv.parse(envFileData);
18
-
19
- if (envVarsData[envKey]) {
20
- delete envVarsData[envKey];
21
- const stringifiedEnvData = Object.keys(envVarsData).map(key => `${key}=${envVarsData[key]}`).join(EOL);
22
- await fsPromises.writeFile(DOTENV_FILE, stringifiedEnvData, {
23
- encoding: 'utf-8'
24
- });
25
- }
26
- }
27
-
28
- function addEnvFileToGitIgnore() {
29
- try {
30
- if (!ignore.ignores(DOTENV_FILE)) {
31
- ignore.add(DOTENV_FILE);
32
- }
33
- } catch(err) {
34
- console.log(`${
35
- chalk.yellow('Warning: could not add .env file to .gitignore. Please don\'t forget to add it manually.')
36
- }`)
37
- }
38
- }
39
-
40
- async function checkFileEnvExists() {
41
- try {
42
- await fsPromises.access(DOTENV_FILE, fsConstants.F_OK);
43
- return true
44
- } catch(_) {
45
- return false
46
- }
47
- }
48
-
49
- async function cacheEnvVars(envObj) {
50
- try {
51
- let envVars = '';
52
- let envFileExists = await checkFileEnvExists();
53
-
54
- for (const [key, val] of Object.entries(envObj)) {
55
- if (envFileExists) {
56
- await removeOldEnv(key);
57
- }
58
- envVars += `${EOL}${key}=${val}`
59
- }
60
-
61
- await fsPromises.writeFile(DOTENV_FILE, envVars, {
62
- encoding: 'utf-8',
63
- flag: 'a'
64
- });
65
-
66
- addEnvFileToGitIgnore();
67
-
68
- console.log(`
69
- Saved new Environment variable(s) locally: ${ chalk.cyan(Object.keys(envObj).join(", ")) }.
70
- `)
71
- } catch(err) {
72
- console.log(`
73
- ${chalk.red('Couldn\'t save environment variables locally.')}
74
- ${err.message}
75
- `)
76
- }
77
- }
78
-
79
- module.exports = {
80
- cacheEnvVars,
81
- }
@@ -1,73 +0,0 @@
1
- const proxyquire = require('proxyquire');
2
- const { DOTENV_FILE, ACCESS_TOKEN_ENV_KEY } = require('../constants');
3
- const assert = require('assert');
4
-
5
- const mockFs = () => {
6
- let data = {};
7
-
8
- return {
9
- promises: {
10
- async access(name){
11
- if (data[name]) {
12
- return undefined;
13
- }
14
- throw new Error();
15
- },
16
- async writeFile(name, fileData, opts = {}){
17
- if (opts.flag === 'a') {
18
- data[name] = `${data[name] || ''}${fileData}`;
19
- } else {
20
- data[name] = fileData
21
- }
22
- },
23
- async readFile(name){ return data[name] },
24
- },
25
- constants: {
26
- F_OK: true
27
- },
28
- resetData() {
29
- data = {}
30
- },
31
- };
32
- }
33
- const mockedFs = mockFs();
34
- const { cacheEnvVars } = proxyquire('./index', {'fs': mockedFs});
35
-
36
- describe('Caching environment variables', () => {
37
-
38
- beforeEach(() => {
39
- mockedFs.resetData();
40
- })
41
-
42
- it('should create .env file if its missing', async () => {
43
- await cacheEnvVars({[ACCESS_TOKEN_ENV_KEY]: 'test_value'});
44
-
45
- const fileExists = await mockedFs.promises.access(DOTENV_FILE, mockedFs.constants.F_OK) === undefined;
46
- assert.ok(fileExists);
47
- })
48
-
49
- it('should replace old variable in .env file if it exists', async () => {
50
- const envData = `CONTENTFUL_APP_DEF_ID=some_app_def_id\n
51
- ${ACCESS_TOKEN_ENV_KEY}=old_value`;
52
- const expectedData = `CONTENTFUL_APP_DEF_ID=some_app_def_id
53
- ${ACCESS_TOKEN_ENV_KEY}=new_value`;
54
-
55
- await mockedFs.promises.writeFile(DOTENV_FILE, envData, {encoding: 'utf-8'});
56
-
57
- await cacheEnvVars({[ACCESS_TOKEN_ENV_KEY]: 'new_value'});
58
- const fileData = await mockedFs.promises.readFile(DOTENV_FILE, {encoding: 'utf-8'});
59
- assert.strictEqual(fileData, expectedData);
60
- })
61
-
62
- it('should add variable in .env file if variable is missing', async () => {
63
- const envData = `CONTENTFUL_APP_DEF_ID=some_app_def_id`;
64
- const expectedData = `CONTENTFUL_APP_DEF_ID=some_app_def_id
65
- ${ACCESS_TOKEN_ENV_KEY}=new_value_2`;
66
-
67
- await mockedFs.promises.writeFile(DOTENV_FILE, envData, {encoding: 'utf-8'});
68
-
69
- await cacheEnvVars({[ACCESS_TOKEN_ENV_KEY]: 'new_value_2'});
70
- const fileData = await mockedFs.promises.readFile(DOTENV_FILE, {encoding: 'utf-8'});
71
- assert.strictEqual(fileData, expectedData);
72
- })
73
- });
@@ -1,21 +0,0 @@
1
- const DOTENV_FILE = '.env';
2
- const ACCESS_TOKEN_ENV_KEY = 'CONTENTFUL_ACCESS_TOKEN';
3
- const ORG_ID_ENV_KEY = 'CONTENTFUL_ORG_ID';
4
- const APP_DEF_ENV_KEY = 'CONTENTFUL_APP_DEF_ID';
5
-
6
- const DEFAULT_BUNDLES_TO_KEEP = 50;
7
- const DEFAULT_BUNDLES_TO_FETCH = 1000;
8
- const MAX_CONCURRENT_DELETION_CALLS = 5;
9
-
10
- const DEFAULT_CONTENTFUL_API_HOST = 'api.contentful.com';
11
-
12
- module.exports = {
13
- DOTENV_FILE,
14
- ACCESS_TOKEN_ENV_KEY,
15
- ORG_ID_ENV_KEY,
16
- APP_DEF_ENV_KEY,
17
- DEFAULT_BUNDLES_TO_KEEP,
18
- DEFAULT_BUNDLES_TO_FETCH,
19
- MAX_CONCURRENT_DELETION_CALLS,
20
- CONTENTFUL_API_HOST: DEFAULT_CONTENTFUL_API_HOST,
21
- };