Skip to content

Commit d58a934

Browse files
authored
feat: enable frontendless Function uploads (#2223)
* refactor: pin node to v20 * feat: add support for more function templates * feat: add support for all available function event types * feat: only validate index.html files when they exist, or when no functions are uploaded * refactor: don't include examples as templates
1 parent d145fa7 commit d58a934

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
lts/*
1+
v20

packages/contentful--app-scripts/src/upload/validate-bundle.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,28 @@ describe('validateBundle', () => {
1111
(console.warn as SinonStub).restore();
1212
});
1313

14-
it('throws when there is no index.html', () => {
14+
it('throws when there is no index.html & no functions or actions', () => {
1515
const fsstub = { readdirSync: () => [] };
1616
const { validateBundle } = proxyquire('./validate-bundle', { fs: fsstub });
1717
try {
1818
validateBundle('build', {});
1919
} catch (e: any) {
2020
assert.strictEqual(
2121
e.message,
22-
'Make sure your bundle includes a valid index.html file in its root folder.'
22+
'Ensure your bundle includes a valid index.html file in its root folder, or a valid Contentful Function entrypoint (defined in your contentful-app-manifest.json file).'
2323
);
2424
}
2525
});
26+
it('does not throw when there is no index.html but a function is defined', () => {
27+
const mockedSettings = {
28+
functions: [{ id: 'myFunc', path: 'functions/myFunc.js' }],
29+
} as Pick<UploadSettings, 'functions' | 'actions'>;
30+
const fsstub = {
31+
readdirSync: () => ['functions', 'functions/myFunc.js'],
32+
};
33+
const { validateBundle } = proxyquire('./validate-bundle', { fs: fsstub });
34+
validateBundle('build', mockedSettings);
35+
});
2636

2737
it('warns when the index.html contains an absolute path', () => {
2838
const fsstub = {

packages/contentful--app-scripts/src/upload/validate-bundle.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@ export const validateBundle = (
2020
const files = fs.readdirSync(buildFolder, { recursive: true, encoding: 'utf-8' });
2121
const entry = getEntryFile(files);
2222

23-
if (!entry) {
24-
throw new Error('Make sure your bundle includes a valid index.html file in its root folder.');
23+
if (!entry && !functions && !actions) {
24+
throw new Error(
25+
'Ensure your bundle includes a valid index.html file in its root folder, or a valid Contentful Function entrypoint (defined in your contentful-app-manifest.json file).'
26+
);
2527
}
2628

27-
const entryFile = fs.readFileSync(Path.join(buildFolder, entry), { encoding: 'utf8' });
29+
if (entry) {
30+
const entryFile = fs.readFileSync(Path.join(buildFolder, entry), { encoding: 'utf8' });
2831

29-
if (fileContainsAbsolutePath(entryFile)) {
30-
console.log('----------------------------');
31-
console.warn(
32-
`${chalk.red(
33-
'Warning:'
34-
)} This bundle uses absolute paths. Please use relative paths instead for correct rendering. See more details here https://www.contentful.com/developers/docs/extensibility/app-framework/app-bundle/#limitations`
35-
);
36-
console.log('----------------------------');
32+
if (fileContainsAbsolutePath(entryFile)) {
33+
console.log('----------------------------');
34+
console.warn(
35+
`${chalk.red(
36+
'Warning:'
37+
)} This bundle uses absolute paths. Please use relative paths instead for correct rendering. See more details here https://www.contentful.com/developers/docs/extensibility/app-framework/app-bundle/#limitations`
38+
);
39+
console.log('----------------------------');
40+
}
3741
}
3842

3943
if (functions) {

packages/contentful--app-scripts/src/utils.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import { ContentfulFunction, FunctionAppAction } from './types';
99
const DEFAULT_MANIFEST_PATH = './contentful-app-manifest.json';
1010

1111
const functionEvents = {
12+
appActionCall: 'appaction.call',
13+
appEventFilter: 'appevent.filter',
14+
appEventHandler: 'appevent.handler',
15+
appEventTransformation: 'appevent.transformation',
1216
fieldMappingEvent: 'graphql.field.mapping',
1317
resourceTypeMappingEvent: 'graphql.resourcetype.mapping',
1418
queryEvent: 'graphql.query',
1519
resourceLinksSearchEvent: 'resources.search',
16-
resourceLinksLookupEvent: 'resources.lookup',
17-
appEventFilter: 'appevent.filter',
18-
appEventHandler: 'appevent.handler',
19-
appEventTransformation: 'appevent.transformation',
20-
appActionCall: 'appaction.call',
20+
resourceLinksLookupEvent: 'resources.lookup'
2121
};
2222

2323
export const throwValidationException = (subject: string, message?: string, details?: string) => {
@@ -163,9 +163,7 @@ export function getEntityFromManifest<Type extends 'actions' | 'functions'>(
163163
}
164164
if (hasInvalidEvent) {
165165
console.log(
166-
`${chalk.red(
167-
'Error:'
168-
)} Invalid events ${hasInvalidEvent} found in the accepts array for ${type} "${
166+
`${chalk.red('Error:')} Invalid events found in the accepts array for ${type} "${
169167
item.name
170168
}".`
171169
);

packages/contentful--create-contentful-app/src/includeFunction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ const addBuildCommand = getAddBuildCommandFn({
1212
});
1313

1414
const VALID_FUNCTION_TEMPLATES_DIRS = [
15-
'external-references',
1615
'appevent-filter',
1716
'appevent-handler',
1817
'appevent-transformation',
18+
'external-references',
19+
'comment-bot',
1920
];
2021

2122
function functionTemplateFromName(functionName: string) {

0 commit comments

Comments
 (0)