@contentful/app-scripts 1.2.6 → 1.4.2
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.
- package/lib/clean-up/clean-up-bundles.js +48 -7
- package/lib/clean-up/clean-up-bundles.test.js +21 -5
- package/lib/clean-up/get-clean-up-settings.js +1 -0
- package/lib/create-app-definition/build-app-definition-settings.js +1 -0
- package/lib/upload/create-app-bundle.js +10 -7
- package/lib/upload/get-upload-settings-args.js +1 -0
- package/package.json +3 -3
- package/utils/constants.js +2 -0
- package/LICENSE +0 -21
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
const chalk = require('chalk');
|
|
2
2
|
const ora = require('ora');
|
|
3
3
|
const Bottleneck = require('bottleneck');
|
|
4
|
-
const {
|
|
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
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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: () => ({
|
|
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: () =>
|
|
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
|
});
|
|
@@ -26,6 +26,7 @@ NOTE: This will create an app definition in your Contentful organization.
|
|
|
26
26
|
{ name: 'Entry editor (entry-editor)', value: 'entry-editor' },
|
|
27
27
|
{ name: 'Entry sidebar (entry-sidebar) ', value: 'entry-sidebar' },
|
|
28
28
|
{ name: 'Page (page) ', value: 'page' },
|
|
29
|
+
{ name: 'Home (home) ', value: 'home' },
|
|
29
30
|
],
|
|
30
31
|
},
|
|
31
32
|
{
|
|
@@ -7,7 +7,12 @@ const { createAppUpload } = require('./create-app-upload');
|
|
|
7
7
|
|
|
8
8
|
async function createAppBundleFromUpload(settings, appUploadId) {
|
|
9
9
|
const clientSpinner = ora('Verifying your upload...').start();
|
|
10
|
-
const client = createClient({
|
|
10
|
+
const client = createClient({
|
|
11
|
+
accessToken: settings.accessToken,
|
|
12
|
+
application: settings.userAgentApplication
|
|
13
|
+
? settings.userAgentApplication
|
|
14
|
+
: 'contentful.app-scripts',
|
|
15
|
+
});
|
|
11
16
|
const organization = await client.getOrganization(settings.organization.value);
|
|
12
17
|
const appDefinition = await organization.getAppDefinition(settings.definition.value);
|
|
13
18
|
clientSpinner.stop();
|
|
@@ -31,18 +36,16 @@ async function createAppBundleFromSettings(settings) {
|
|
|
31
36
|
try {
|
|
32
37
|
appUpload = await createAppUpload(settings);
|
|
33
38
|
console.log(`
|
|
34
|
-
${chalk.yellow(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
appUpload.sys.id
|
|
38
|
-
)}) has been created.`);
|
|
39
|
+
${chalk.yellow('Done!')} Your files were successfully uploaded and a new AppUpload (${chalk.dim(
|
|
40
|
+
appUpload.sys.id
|
|
41
|
+
)}) has been created.`);
|
|
39
42
|
} catch (err) {
|
|
40
43
|
showCreationError('app upload', err.message);
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
if (!appUpload) return;
|
|
44
47
|
|
|
45
|
-
console.log(
|
|
48
|
+
console.log('');
|
|
46
49
|
console.log(` ----------------------------`);
|
|
47
50
|
const appBundle = await createAppBundleFromUpload(settings, appUpload.sys.id);
|
|
48
51
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/app-scripts",
|
|
3
|
-
"version": "1.2
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "A collection of scripts for building Contentful Apps",
|
|
5
5
|
"author": "Contentful GmbH",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"bottleneck": "2.19.5",
|
|
53
53
|
"chalk": "4.1.2",
|
|
54
54
|
"commander": "9.4.1",
|
|
55
|
-
"contentful-management": "10.
|
|
55
|
+
"contentful-management": "10.23.0",
|
|
56
56
|
"dotenv": "16.0.3",
|
|
57
57
|
"ignore": "5.2.1",
|
|
58
58
|
"inquirer": "8.2.5",
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"open": "8.4.0",
|
|
61
61
|
"ora": "5.4.1"
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "4c3506be3f52c7a8aae17deaa75acefc9a805b42"
|
|
64
64
|
}
|
package/utils/constants.js
CHANGED
|
@@ -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
|
};
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 Contentful
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|