@contentful/app-scripts 1.2.5 → 1.2.7

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.
@@ -1,7 +1,10 @@
1
1
  const chalk = require('chalk');
2
2
  const ora = require('ora');
3
3
  const Bottleneck = require('bottleneck');
4
- const { MAX_CONCURRENT_DELETION_CALLS } = require('../../utils/constants');
4
+ const {
5
+ MAX_CONCURRENT_DELETION_CALLS,
6
+ DEFAULT_BUNDLES_TO_FETCH,
7
+ } = require('../../utils/constants');
5
8
  const { throwError } = require('../utils');
6
9
  const { createClient } = require('contentful-management');
7
10
 
@@ -29,7 +32,8 @@ const scheduleBundleDeletion = async (bundlesToDelete, client, settings) => {
29
32
  };
30
33
 
31
34
  async function cleanUpBundles(settings) {
32
- let bundles, definition;
35
+ let bundlesToDelete, definition;
36
+
33
37
  const client = createClient(
34
38
  {
35
39
  host: settings.host,
@@ -37,12 +41,51 @@ async function cleanUpBundles(settings) {
37
41
  },
38
42
  { type: 'plain' }
39
43
  );
40
- const bundlesSpinner = ora(`Fetching all bundles...`).start();
41
- try {
42
- bundles = await client.appBundle.getMany({
44
+
45
+ const fetchAppBundles = async (limit = DEFAULT_BUNDLES_TO_FETCH, skip = 0) => {
46
+ return await client.appBundle.getMany({
43
47
  appDefinitionId: settings.definition.value,
44
48
  organizationId: settings.organization.value,
49
+ query: {
50
+ limit,
51
+ skip,
52
+ },
45
53
  });
54
+ };
55
+
56
+ const getAppBundles = async (requestedAmount = DEFAULT_BUNDLES_TO_FETCH) => {
57
+ if (requestedAmount < 1) {
58
+ throw new Error('Requested amount of bundles to fetch must be greater than 0');
59
+ }
60
+ const getBundles = async (limit, skip) => {
61
+ const result = await fetchAppBundles(limit, skip);
62
+ const currLength = skip + result.items.length;
63
+
64
+ if (result.total > currLength) {
65
+ return [...result.items, ...(await getBundles(result.items.length, currLength))];
66
+ } else {
67
+ return result.items;
68
+ }
69
+ };
70
+
71
+ const all = await getBundles(DEFAULT_BUNDLES_TO_FETCH, 0);
72
+ return all.reverse().slice(0, requestedAmount);
73
+ };
74
+
75
+ const getAppBundleCount = async () => {
76
+ return await fetchAppBundles(1, 0);
77
+ };
78
+
79
+ const bundlesSpinner = ora(`Fetching all bundles...`).start();
80
+ try {
81
+ const { total } = await getAppBundleCount();
82
+ const amountToDelete = total - settings.keep;
83
+ if (amountToDelete < 1) {
84
+ console.log(`${chalk.yellow('Warning:')} There is nothing to delete`);
85
+ bundlesSpinner.stop();
86
+ return;
87
+ }
88
+ bundlesToDelete = await getAppBundles(amountToDelete);
46
89
  definition = await client.appDefinition.get({
47
90
  appDefinitionId: settings.definition.value,
48
91
  organizationId: settings.organization.value,
@@ -53,8 +96,6 @@ async function cleanUpBundles(settings) {
53
96
 
54
97
  bundlesSpinner.stop();
55
98
 
56
- let bundlesToDelete = bundles.items.slice(settings.keep);
57
-
58
99
  if (definition.bundle) {
59
100
  bundlesToDelete = bundlesToDelete.filter(
60
101
  (bundle) => bundle.sys.id !== definition.bundle.sys.id
@@ -1,4 +1,4 @@
1
- const { stub, match, useFakeTimers } = require('sinon');
1
+ const { stub, spy, match, useFakeTimers } = require('sinon');
2
2
  const assert = require('assert');
3
3
  const proxyquire = require('proxyquire');
4
4
 
@@ -14,8 +14,8 @@ const bundlesFixture = [
14
14
  { sys: { id: 'test-8' } },
15
15
  ];
16
16
 
17
- describe('cleanUpBundles', () => {
18
- let subject, clientMock, deleteMock;
17
+ describe.only('cleanUpBundles', () => {
18
+ let subject, clientMock, deleteMock, createClientArgs;
19
19
  let mockedBundles = bundlesFixture;
20
20
 
21
21
  beforeEach(() => {
@@ -31,7 +31,10 @@ describe('cleanUpBundles', () => {
31
31
  deleteMock = stub();
32
32
  clientMock = {
33
33
  appBundle: {
34
- getMany: () => ({ items: mockedBundles }),
34
+ getMany: () => ({
35
+ total: mockedBundles.length,
36
+ items: mockedBundles,
37
+ }),
35
38
  delete: deleteMock,
36
39
  },
37
40
  appDefinition: {
@@ -41,7 +44,10 @@ describe('cleanUpBundles', () => {
41
44
  };
42
45
  ({ cleanUpBundles: subject } = proxyquire('./clean-up-bundles', {
43
46
  'contentful-management': {
44
- createClient: () => clientMock,
47
+ createClient: (...args) => {
48
+ createClientArgs = args;
49
+ return clientMock;
50
+ },
45
51
  },
46
52
  '../../utils/constants': {
47
53
  MAX_CONCURRENT_DELETION_CALLS: 2,
@@ -84,6 +90,7 @@ describe('cleanUpBundles', () => {
84
90
  it('slow call will occupy slot until finished', async () => {
85
91
  const clock = useFakeTimers();
86
92
  mockedBundles.unshift({ sys: { id: 'slow' } });
93
+ mockedBundles = mockedBundles.reverse();
87
94
  clientMock.appBundle.delete = stub().callsFake(
88
95
  ({ appBundleId }) => new Promise((r) => setTimeout(r, appBundleId === 'slow' ? 200 : 100))
89
96
  );
@@ -99,4 +106,13 @@ describe('cleanUpBundles', () => {
99
106
 
100
107
  clock.restore();
101
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
+ });
102
118
  });
@@ -19,6 +19,7 @@ async function getCleanUpSettingsArgs(options) {
19
19
  return {
20
20
  ...appInfo,
21
21
  keep: options.keep !== undefined ? +options.keep : DEFAULT_BUNDLES_TO_KEEP,
22
+ host: options.host,
22
23
  };
23
24
  } catch (err) {
24
25
  console.log(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/app-scripts",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "description": "A collection of scripts for building Contentful Apps",
5
5
  "author": "Contentful GmbH",
6
6
  "license": "MIT",
@@ -54,11 +54,11 @@
54
54
  "commander": "9.4.1",
55
55
  "contentful-management": "10.21.4",
56
56
  "dotenv": "16.0.3",
57
- "ignore": "5.2.0",
57
+ "ignore": "5.2.1",
58
58
  "inquirer": "8.2.5",
59
59
  "lodash": "4.17.21",
60
60
  "open": "8.4.0",
61
61
  "ora": "5.4.1"
62
62
  },
63
- "gitHead": "784c471828f77203e31dbc1c770c32e17c51453d"
63
+ "gitHead": "6234b521e7987d280949352765a443e344310b2c"
64
64
  }
@@ -4,6 +4,7 @@ const ORG_ID_ENV_KEY = 'CONTENTFUL_ORG_ID';
4
4
  const APP_DEF_ENV_KEY = 'CONTENTFUL_APP_DEF_ID';
5
5
 
6
6
  const DEFAULT_BUNDLES_TO_KEEP = 50;
7
+ const DEFAULT_BUNDLES_TO_FETCH = 1000;
7
8
  const MAX_CONCURRENT_DELETION_CALLS = 5;
8
9
 
9
10
  module.exports = {
@@ -13,4 +14,5 @@ module.exports = {
13
14
  ORG_ID_ENV_KEY,
14
15
  APP_DEF_ENV_KEY,
15
16
  MAX_CONCURRENT_DELETION_CALLS,
17
+ DEFAULT_BUNDLES_TO_FETCH,
16
18
  };