@friggframework/devtools 2.0.0--canary.707.df80bfd.0 → 2.0.0--canary.680.d670696.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.
- package/frigg-cli/build-command/index.js +1 -32
- package/frigg-cli/db-setup-command/index.js +0 -25
- package/frigg-cli/deploy-command/index.js +2 -50
- package/frigg-cli/doctor-command/index.js +0 -7
- package/frigg-cli/generate-iam-command.js +0 -7
- package/frigg-cli/repair-command/index.js +0 -7
- package/frigg-cli/start-command/index.js +0 -58
- package/infrastructure/__tests__/template-generation.test.js +0 -7
- package/infrastructure/create-frigg-infrastructure.js +0 -14
- package/infrastructure/domains/shared/types/app-definition.js +1 -15
- package/infrastructure/domains/shared/utilities/base-definition-factory.js +3 -5
- package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +0 -24
- package/infrastructure/domains/shared/utilities/prisma-layer-manager.js +0 -10
- package/infrastructure/infrastructure-composer.js +1 -13
- package/infrastructure/infrastructure-composer.test.js +0 -7
- package/infrastructure/scripts/build-prisma-layer.js +5 -32
- package/infrastructure/scripts/build-prisma-layer.test.js +0 -35
- package/package.json +7 -7
- package/frigg-cli/__tests__/unit/commands/provider-dispatch.test.js +0 -140
- package/frigg-cli/utils/provider-helper.js +0 -80
- package/infrastructure/__tests__/compose-extension-prisma.test.js +0 -168
- package/infrastructure/__tests__/create-frigg-infrastructure-provider.test.js +0 -64
- package/infrastructure/compose-extension-prisma.js +0 -73
- package/infrastructure/provider-build.js +0 -126
- package/test/npm-install-guard.js +0 -104
- package/test/npm-install-guard.test.js +0 -53
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Provider dispatch (friggframework/frigg#545): appDefinition.provider
|
|
3
|
-
* decides whether a CLI command runs the AWS/serverless path or the
|
|
4
|
-
* provider plugin. Uses real temp projects and the real provider-netlify.
|
|
5
|
-
*/
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const os = require('os');
|
|
8
|
-
const path = require('path');
|
|
9
|
-
|
|
10
|
-
function writeProject(definitionSource) {
|
|
11
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-dispatch-'));
|
|
12
|
-
const backend = path.join(dir, 'backend');
|
|
13
|
-
fs.mkdirSync(backend);
|
|
14
|
-
fs.writeFileSync(path.join(backend, 'package.json'), '{"name":"backend"}');
|
|
15
|
-
fs.writeFileSync(path.join(backend, 'index.js'), `module.exports = { Definition: ${definitionSource} };`);
|
|
16
|
-
return { dir, backend };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const NETLIFY_APP = `{
|
|
20
|
-
name: 'dispatch-app',
|
|
21
|
-
provider: 'netlify',
|
|
22
|
-
integrations: [],
|
|
23
|
-
database: { postgres: { enable: true } },
|
|
24
|
-
}`;
|
|
25
|
-
|
|
26
|
-
describe('provider-helper', () => {
|
|
27
|
-
const { loadProviderForCli, loadCliAppDefinition, isNonAwsProject } = require('../../../utils/provider-helper');
|
|
28
|
-
const dirs = [];
|
|
29
|
-
afterAll(() => dirs.forEach((d) => fs.rmSync(d, { recursive: true, force: true })));
|
|
30
|
-
|
|
31
|
-
it('returns null without an app definition', () => {
|
|
32
|
-
expect(loadProviderForCli({ cwd: os.tmpdir() })).toBeNull();
|
|
33
|
-
expect(loadCliAppDefinition('/nonexistent/path')).toBeNull();
|
|
34
|
-
expect(isNonAwsProject({ cwd: os.tmpdir() })).toBe(false);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it('treats a missing provider as aws with no plugin', () => {
|
|
38
|
-
const { dir, backend } = writeProject(`{ integrations: [] }`);
|
|
39
|
-
dirs.push(dir);
|
|
40
|
-
expect(loadProviderForCli({ cwd: backend })).toEqual(
|
|
41
|
-
expect.objectContaining({ providerName: 'aws', provider: null, backendDir: backend })
|
|
42
|
-
);
|
|
43
|
-
expect(isNonAwsProject({ cwd: backend })).toBe(false);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('resolves the netlify plugin from the project root or from backend/', () => {
|
|
47
|
-
const { dir, backend } = writeProject(NETLIFY_APP);
|
|
48
|
-
dirs.push(dir);
|
|
49
|
-
for (const cwd of [dir, backend]) {
|
|
50
|
-
const result = loadProviderForCli({ cwd });
|
|
51
|
-
expect(result.providerName).toBe('netlify');
|
|
52
|
-
expect(result.provider.name).toBe('netlify');
|
|
53
|
-
expect(result.backendDir).toBe(backend);
|
|
54
|
-
}
|
|
55
|
-
expect(isNonAwsProject({ cwd: backend })).toBe(true);
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
describe('buildWithProvider', () => {
|
|
60
|
-
const { buildWithProvider } = require('../../../../infrastructure/provider-build');
|
|
61
|
-
|
|
62
|
-
beforeEach(() => {
|
|
63
|
-
jest.spyOn(console, 'log').mockImplementation();
|
|
64
|
-
jest.spyOn(console, 'warn').mockImplementation();
|
|
65
|
-
jest.spyOn(console, 'error').mockImplementation();
|
|
66
|
-
});
|
|
67
|
-
afterEach(() => jest.restoreAllMocks());
|
|
68
|
-
|
|
69
|
-
it('writes netlify.toml, functions and lib shims beside backend/, where their requires resolve', () => {
|
|
70
|
-
const { dir, backend } = writeProject(NETLIFY_APP);
|
|
71
|
-
try {
|
|
72
|
-
const appDefinition = require(path.join(backend, 'index.js')).Definition;
|
|
73
|
-
const { projectDir } = buildWithProvider({ appDefinition, backendDir: backend });
|
|
74
|
-
|
|
75
|
-
expect(projectDir).toBe(dir);
|
|
76
|
-
expect(fs.existsSync(path.join(dir, 'netlify.toml'))).toBe(true);
|
|
77
|
-
const auth = fs.readFileSync(path.join(dir, 'netlify/functions/auth.js'), 'utf8');
|
|
78
|
-
expect(auth).toContain("require('../../backend/index.js')");
|
|
79
|
-
expect(fs.existsSync(path.join(dir, 'netlify/lib/create-netlify-app-handler.js'))).toBe(true);
|
|
80
|
-
// Nothing is written into the backend directory itself.
|
|
81
|
-
expect(fs.existsSync(path.join(backend, 'netlify'))).toBe(false);
|
|
82
|
-
} finally {
|
|
83
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('stops on validation errors and writes nothing', () => {
|
|
88
|
-
const exit = jest.fn();
|
|
89
|
-
const { dir, backend } = writeProject(`{ provider: 'netlify', integrations: [] }`);
|
|
90
|
-
try {
|
|
91
|
-
const appDefinition = require(path.join(backend, 'index.js')).Definition;
|
|
92
|
-
const { written } = buildWithProvider({ appDefinition, backendDir: backend, exit });
|
|
93
|
-
expect(exit).toHaveBeenCalledWith(1);
|
|
94
|
-
expect(written).toEqual([]);
|
|
95
|
-
expect(fs.existsSync(path.join(dir, 'netlify.toml'))).toBe(false);
|
|
96
|
-
} finally {
|
|
97
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
describe('frigg build dispatch', () => {
|
|
103
|
-
beforeEach(() => {
|
|
104
|
-
jest.resetModules();
|
|
105
|
-
jest.spyOn(console, 'log').mockImplementation();
|
|
106
|
-
jest.spyOn(console, 'warn').mockImplementation();
|
|
107
|
-
});
|
|
108
|
-
afterEach(() => {
|
|
109
|
-
jest.restoreAllMocks();
|
|
110
|
-
jest.dontMock('child_process');
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('builds a netlify app through the plugin, never through osls', async () => {
|
|
114
|
-
const spawnSync = jest.fn(() => ({ status: 0 }));
|
|
115
|
-
jest.doMock('child_process', () => ({ ...jest.requireActual('child_process'), spawnSync }));
|
|
116
|
-
const { dir, backend } = writeProject(NETLIFY_APP);
|
|
117
|
-
try {
|
|
118
|
-
jest.spyOn(process, 'cwd').mockReturnValue(backend);
|
|
119
|
-
await require('../../../build-command').buildCommand({});
|
|
120
|
-
expect(spawnSync).not.toHaveBeenCalled();
|
|
121
|
-
expect(fs.existsSync(path.join(dir, 'netlify/functions/worker-background.js'))).toBe(true);
|
|
122
|
-
} finally {
|
|
123
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('keeps the serverless path for an aws app', async () => {
|
|
128
|
-
const spawnSync = jest.fn(() => ({ status: 0 }));
|
|
129
|
-
jest.doMock('child_process', () => ({ ...jest.requireActual('child_process'), spawnSync }));
|
|
130
|
-
const { dir, backend } = writeProject(`{ integrations: [] }`);
|
|
131
|
-
try {
|
|
132
|
-
jest.spyOn(process, 'cwd').mockReturnValue(backend);
|
|
133
|
-
await require('../../../build-command').buildCommand({});
|
|
134
|
-
expect(spawnSync).toHaveBeenCalledWith('osls', expect.any(Array), expect.any(Object));
|
|
135
|
-
expect(fs.existsSync(path.join(dir, 'netlify'))).toBe(false);
|
|
136
|
-
} finally {
|
|
137
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
});
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CLI Provider Helper (friggframework/frigg#545, 88cf983e / c8705715)
|
|
3
|
-
*
|
|
4
|
-
* Loads the app definition from the user's project and resolves its
|
|
5
|
-
* provider plugin, so CLI commands can delegate to it (Netlify, ...) or fall
|
|
6
|
-
* through to the AWS/serverless behaviour.
|
|
7
|
-
*/
|
|
8
|
-
const path = require('path');
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Find the backend: <cwd>/backend/index.js, then <cwd>/index.js.
|
|
13
|
-
* @param {string} cwd
|
|
14
|
-
* @returns {{ appDefinition: Object, backendDir: string } | null}
|
|
15
|
-
*/
|
|
16
|
-
function loadCliBackend(cwd) {
|
|
17
|
-
for (const dir of [path.join(cwd, 'backend'), cwd]) {
|
|
18
|
-
const indexPath = path.join(dir, 'index.js');
|
|
19
|
-
if (!fs.existsSync(indexPath)) continue;
|
|
20
|
-
try {
|
|
21
|
-
const exported = require(indexPath);
|
|
22
|
-
if (exported && exported.Definition) {
|
|
23
|
-
return { appDefinition: exported.Definition, backendDir: dir };
|
|
24
|
-
}
|
|
25
|
-
} catch {
|
|
26
|
-
// Failed to load, try next
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @param {string} cwd
|
|
34
|
-
* @returns {Object|null} the app Definition
|
|
35
|
-
*/
|
|
36
|
-
function loadCliAppDefinition(cwd) {
|
|
37
|
-
return loadCliBackend(cwd)?.appDefinition || null;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Load the app definition and resolve the provider plugin for CLI commands.
|
|
42
|
-
*
|
|
43
|
-
* @param {Object} [options]
|
|
44
|
-
* @param {string} [options.cwd] - Working directory (default: process.cwd())
|
|
45
|
-
* @returns {{ appDefinition: Object, backendDir: string, provider: Object|null, providerName: string } | null}
|
|
46
|
-
* null when no app definition is found; provider null for 'aws' (the
|
|
47
|
-
* caller keeps its existing behaviour).
|
|
48
|
-
*/
|
|
49
|
-
function loadProviderForCli(options = {}) {
|
|
50
|
-
const cwd = options.cwd || process.cwd();
|
|
51
|
-
const backend = loadCliBackend(cwd);
|
|
52
|
-
if (!backend) {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const providerName = backend.appDefinition.provider || 'aws';
|
|
57
|
-
if (providerName === 'aws') {
|
|
58
|
-
return { ...backend, provider: null, providerName: 'aws' };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const {
|
|
62
|
-
resolveProvider,
|
|
63
|
-
} = require('@friggframework/core/providers/resolve-provider');
|
|
64
|
-
return { ...backend, provider: resolveProvider(backend.appDefinition), providerName };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* True when the project's app definition names a non-AWS provider. Used to
|
|
69
|
-
* guard the AWS-only commands (doctor, repair, generate-iam).
|
|
70
|
-
*/
|
|
71
|
-
function isNonAwsProject(options = {}) {
|
|
72
|
-
try {
|
|
73
|
-
const result = loadProviderForCli(options);
|
|
74
|
-
return Boolean(result && result.providerName !== 'aws');
|
|
75
|
-
} catch {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
module.exports = { loadProviderForCli, loadCliAppDefinition, loadCliBackend, isNonAwsProject };
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The build step that composes core extension schemas into the backend's
|
|
3
|
-
* installed @friggframework/core. Uses a temp backend whose node_modules holds
|
|
4
|
-
* a core with the real composer and a minimal PostgreSQL schema.
|
|
5
|
-
*/
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const os = require('os');
|
|
8
|
-
const path = require('path');
|
|
9
|
-
const { composeAppExtensionPrisma } = require('../compose-extension-prisma');
|
|
10
|
-
const { buildWithProvider } = require('../provider-build');
|
|
11
|
-
|
|
12
|
-
const REAL_CORE = path.dirname(
|
|
13
|
-
require.resolve('@friggframework/core/package.json')
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
const BASE = `datasource db {
|
|
17
|
-
provider = "postgresql"
|
|
18
|
-
url = env("DATABASE_URL")
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
model User {
|
|
22
|
-
id Int @id
|
|
23
|
-
}
|
|
24
|
-
`;
|
|
25
|
-
|
|
26
|
-
function makeBackend({ withCore = true } = {}) {
|
|
27
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-compose-ext-'));
|
|
28
|
-
const backend = path.join(dir, 'backend');
|
|
29
|
-
fs.mkdirSync(backend);
|
|
30
|
-
fs.writeFileSync(path.join(backend, 'package.json'), '{"name":"backend"}');
|
|
31
|
-
const extSchema = path.join(dir, 'ext.prisma');
|
|
32
|
-
fs.writeFileSync(
|
|
33
|
-
extSchema,
|
|
34
|
-
'model OAuthAppCredential {\n id Int @id\n}\n'
|
|
35
|
-
);
|
|
36
|
-
let core = null;
|
|
37
|
-
if (withCore) {
|
|
38
|
-
core = path.join(backend, 'node_modules', '@friggframework', 'core');
|
|
39
|
-
fs.mkdirSync(path.join(core, 'prisma-postgresql'), { recursive: true });
|
|
40
|
-
fs.writeFileSync(
|
|
41
|
-
path.join(core, 'package.json'),
|
|
42
|
-
'{"name":"@friggframework/core"}'
|
|
43
|
-
);
|
|
44
|
-
fs.writeFileSync(
|
|
45
|
-
path.join(core, 'prisma-postgresql', 'schema.prisma'),
|
|
46
|
-
BASE
|
|
47
|
-
);
|
|
48
|
-
fs.symlinkSync(
|
|
49
|
-
path.join(REAL_CORE, 'extensions'),
|
|
50
|
-
path.join(core, 'extensions'),
|
|
51
|
-
'dir'
|
|
52
|
-
);
|
|
53
|
-
fs.symlinkSync(
|
|
54
|
-
path.join(REAL_CORE, 'management-api'),
|
|
55
|
-
path.join(core, 'management-api'),
|
|
56
|
-
'dir'
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
return { dir, backend, core, extSchema };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const appDefinition = (extSchema) => ({
|
|
63
|
-
name: 'compose-app',
|
|
64
|
-
provider: 'netlify',
|
|
65
|
-
integrations: [],
|
|
66
|
-
database: { postgres: { enable: true } },
|
|
67
|
-
coreExtensions: [{ name: 'db-credentials', schema: extSchema }],
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe('composeAppExtensionPrisma', () => {
|
|
71
|
-
const dirs = [];
|
|
72
|
-
afterAll(() =>
|
|
73
|
-
dirs.forEach((d) => fs.rmSync(d, { recursive: true, force: true }))
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
it("composes into the backend's installed core, not devtools' copy", () => {
|
|
77
|
-
const { dir, backend, core, extSchema } = makeBackend();
|
|
78
|
-
dirs.push(dir);
|
|
79
|
-
const generated = [];
|
|
80
|
-
const result = composeAppExtensionPrisma({
|
|
81
|
-
appDefinition: appDefinition(extSchema),
|
|
82
|
-
backendDir: backend,
|
|
83
|
-
generate: ({ corePath }) => generated.push(corePath),
|
|
84
|
-
log: () => {},
|
|
85
|
-
});
|
|
86
|
-
expect(result.changed).toBe(true);
|
|
87
|
-
expect(generated).toEqual([core]);
|
|
88
|
-
expect(
|
|
89
|
-
fs.readFileSync(
|
|
90
|
-
path.join(core, 'prisma-postgresql', 'schema.prisma'),
|
|
91
|
-
'utf-8'
|
|
92
|
-
)
|
|
93
|
-
).toContain('model OAuthAppCredential');
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('is a no-op without an installed core when no extension declares a schema', () => {
|
|
97
|
-
const { dir, backend } = makeBackend({ withCore: false });
|
|
98
|
-
dirs.push(dir);
|
|
99
|
-
expect(
|
|
100
|
-
composeAppExtensionPrisma({
|
|
101
|
-
appDefinition: { coreExtensions: [{ name: 'x' }] },
|
|
102
|
-
backendDir: backend,
|
|
103
|
-
})
|
|
104
|
-
).toEqual({ changed: false, results: [] });
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('refuses to build without an installed core when an extension declares a schema', () => {
|
|
108
|
-
const { dir, backend, extSchema } = makeBackend({ withCore: false });
|
|
109
|
-
dirs.push(dir);
|
|
110
|
-
expect(() =>
|
|
111
|
-
composeAppExtensionPrisma({
|
|
112
|
-
appDefinition: appDefinition(extSchema),
|
|
113
|
-
backendDir: backend,
|
|
114
|
-
})
|
|
115
|
-
).toThrow(
|
|
116
|
-
/declare Prisma schemas, but @friggframework\/core is not installed/
|
|
117
|
-
);
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
describe('buildWithProvider composes extension schemas', () => {
|
|
122
|
-
const plugin = {
|
|
123
|
-
validate: () => ({ errors: [], warnings: [] }),
|
|
124
|
-
generateConfig: () => '# config',
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
it('composes before writing the provider files', () => {
|
|
128
|
-
const { dir, backend } = makeBackend({ withCore: false });
|
|
129
|
-
try {
|
|
130
|
-
const order = [];
|
|
131
|
-
const def = { provider: 'netlify', integrations: [] };
|
|
132
|
-
buildWithProvider({
|
|
133
|
-
appDefinition: def,
|
|
134
|
-
backendDir: backend,
|
|
135
|
-
provider: {
|
|
136
|
-
...plugin,
|
|
137
|
-
generateConfig: () => (order.push('config'), '# config'),
|
|
138
|
-
},
|
|
139
|
-
composePrisma: (args) =>
|
|
140
|
-
order.push([
|
|
141
|
-
'compose',
|
|
142
|
-
args.appDefinition,
|
|
143
|
-
args.backendDir,
|
|
144
|
-
]),
|
|
145
|
-
});
|
|
146
|
-
expect(order).toEqual([['compose', def, backend], 'config']);
|
|
147
|
-
} finally {
|
|
148
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('does not compose when validation fails', () => {
|
|
153
|
-
const { dir, backend } = makeBackend({ withCore: false });
|
|
154
|
-
try {
|
|
155
|
-
const compose = jest.fn();
|
|
156
|
-
buildWithProvider({
|
|
157
|
-
appDefinition: { provider: 'netlify' },
|
|
158
|
-
backendDir: backend,
|
|
159
|
-
provider: { validate: () => ({ errors: ['bad'] }) },
|
|
160
|
-
composePrisma: compose,
|
|
161
|
-
exit: () => {},
|
|
162
|
-
});
|
|
163
|
-
expect(compose).not.toHaveBeenCalled();
|
|
164
|
-
} finally {
|
|
165
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
});
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `node infrastructure.js package` (what a backend's `npm run build` runs)
|
|
3
|
-
* for an app with provider 'netlify': createFriggInfrastructure() builds
|
|
4
|
-
* through provider-netlify and returns an empty serverless definition
|
|
5
|
-
* instead of composing CloudFormation (friggframework/frigg#545, 302ab9ba).
|
|
6
|
-
*/
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
const os = require('os');
|
|
9
|
-
const path = require('path');
|
|
10
|
-
|
|
11
|
-
jest.mock('../infrastructure-composer', () => ({
|
|
12
|
-
composeServerlessDefinition: jest.fn(async () => ({ service: 'aws-app' })),
|
|
13
|
-
}));
|
|
14
|
-
jest.mock('@friggframework/core', () => ({
|
|
15
|
-
findNearestBackendPackageJson: jest.fn(),
|
|
16
|
-
}));
|
|
17
|
-
|
|
18
|
-
const { composeServerlessDefinition } = require('../infrastructure-composer');
|
|
19
|
-
const { findNearestBackendPackageJson } = require('@friggframework/core');
|
|
20
|
-
const { createFriggInfrastructure } = require('../create-frigg-infrastructure');
|
|
21
|
-
|
|
22
|
-
function writeProject(definitionSource) {
|
|
23
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-infra-'));
|
|
24
|
-
const backend = path.join(dir, 'backend');
|
|
25
|
-
fs.mkdirSync(backend);
|
|
26
|
-
fs.writeFileSync(path.join(backend, 'package.json'), '{"name":"backend"}');
|
|
27
|
-
fs.writeFileSync(path.join(backend, 'index.js'), `module.exports = { Definition: ${definitionSource} };`);
|
|
28
|
-
findNearestBackendPackageJson.mockReturnValue(path.join(backend, 'package.json'));
|
|
29
|
-
return dir;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
describe('createFriggInfrastructure with a non-AWS provider', () => {
|
|
33
|
-
beforeEach(() => {
|
|
34
|
-
jest.clearAllMocks();
|
|
35
|
-
jest.spyOn(console, 'log').mockImplementation();
|
|
36
|
-
jest.spyOn(console, 'warn').mockImplementation();
|
|
37
|
-
});
|
|
38
|
-
afterEach(() => jest.restoreAllMocks());
|
|
39
|
-
|
|
40
|
-
it('generates the Netlify project and composes no serverless definition', async () => {
|
|
41
|
-
const dir = writeProject(`{
|
|
42
|
-
provider: 'netlify', integrations: [], database: { postgres: { enable: true } }
|
|
43
|
-
}`);
|
|
44
|
-
try {
|
|
45
|
-
await expect(createFriggInfrastructure()).resolves.toEqual({});
|
|
46
|
-
expect(composeServerlessDefinition).not.toHaveBeenCalled();
|
|
47
|
-
expect(fs.existsSync(path.join(dir, 'netlify.toml'))).toBe(true);
|
|
48
|
-
expect(fs.existsSync(path.join(dir, 'netlify/functions/auth.js'))).toBe(true);
|
|
49
|
-
expect(fs.existsSync(path.join(dir, 'netlify/lib/create-netlify-app-handler.js'))).toBe(true);
|
|
50
|
-
} finally {
|
|
51
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('keeps composing the serverless definition for AWS', async () => {
|
|
56
|
-
const dir = writeProject(`{ provider: 'aws', integrations: [] }`);
|
|
57
|
-
try {
|
|
58
|
-
await expect(createFriggInfrastructure()).resolves.toEqual({ service: 'aws-app' });
|
|
59
|
-
expect(fs.existsSync(path.join(dir, 'netlify.toml'))).toBe(false);
|
|
60
|
-
} finally {
|
|
61
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
});
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Build-time step: put the app's core extension models (and PostgreSQL
|
|
3
|
-
* migrations) into the Prisma client of the @friggframework/core the backend
|
|
4
|
-
* installed. See core's extensions/prisma-composer.js.
|
|
5
|
-
*
|
|
6
|
-
* It must run on the backend's own core, not devtools' copy: that is the
|
|
7
|
-
* package the functions bundle and the Lambda layer copy their client from.
|
|
8
|
-
*/
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Directory of the @friggframework/core the backend resolves.
|
|
14
|
-
* @param {string} backendDir
|
|
15
|
-
*/
|
|
16
|
-
function resolveBackendCorePath(backendDir) {
|
|
17
|
-
return path.dirname(
|
|
18
|
-
require.resolve('@friggframework/core/package.json', {
|
|
19
|
-
paths: [backendDir],
|
|
20
|
-
})
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @param {Object} params
|
|
26
|
-
* @param {Object} params.appDefinition
|
|
27
|
-
* @param {string} params.backendDir - directory holding the backend's package.json
|
|
28
|
-
* @param {Function} [params.generate] - injected prisma generate (tests)
|
|
29
|
-
* @param {Function} [params.log]
|
|
30
|
-
* @returns {{ changed: boolean, results: Array<Object> }}
|
|
31
|
-
*/
|
|
32
|
-
function composeAppExtensionPrisma({
|
|
33
|
-
appDefinition,
|
|
34
|
-
backendDir,
|
|
35
|
-
generate,
|
|
36
|
-
log,
|
|
37
|
-
}) {
|
|
38
|
-
const declares = (appDefinition?.coreExtensions || []).some(
|
|
39
|
-
(ext) => ext && (ext.schema || ext.migrations)
|
|
40
|
-
);
|
|
41
|
-
let corePath;
|
|
42
|
-
try {
|
|
43
|
-
corePath = resolveBackendCorePath(backendDir);
|
|
44
|
-
} catch (error) {
|
|
45
|
-
// No installed core to compose into. Fine when nothing needs composing.
|
|
46
|
-
if (!declares) return { changed: false, results: [] };
|
|
47
|
-
throw new Error(
|
|
48
|
-
`Core extensions declare Prisma schemas, but @friggframework/core is not installed for ${backendDir}`,
|
|
49
|
-
{ cause: error }
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
const composerPath = path.join(
|
|
53
|
-
corePath,
|
|
54
|
-
'extensions',
|
|
55
|
-
'prisma-composer.js'
|
|
56
|
-
);
|
|
57
|
-
if (!fs.existsSync(composerPath)) {
|
|
58
|
-
if (!declares) return { changed: false, results: [] };
|
|
59
|
-
throw new Error(
|
|
60
|
-
`@friggframework/core at ${corePath} cannot compose extension schemas; ` +
|
|
61
|
-
'upgrade it to the version devtools was built with'
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
const { composeExtensionPrisma } = require(composerPath);
|
|
65
|
-
return composeExtensionPrisma({
|
|
66
|
-
appDefinition,
|
|
67
|
-
corePath,
|
|
68
|
-
...(generate ? { generate } : {}),
|
|
69
|
-
...(log ? { log } : {}),
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
module.exports = { composeAppExtensionPrisma, resolveBackendCorePath };
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Build for a non-AWS provider (friggframework/frigg#545, 302ab9ba /
|
|
3
|
-
* c8705715).
|
|
4
|
-
*
|
|
5
|
-
* Instead of the serverless/CloudFormation pipeline, the provider plugin
|
|
6
|
-
* (resolved from appDefinition.provider) does the build. First, core
|
|
7
|
-
* extensions' Prisma models are composed into the backend's Prisma client
|
|
8
|
-
* (compose-extension-prisma.js), since the functions bundle that client:
|
|
9
|
-
* 1. validate(appDefinition) - errors abort, warnings print
|
|
10
|
-
* 2. generateConfig(appDefinition) - e.g. netlify.toml
|
|
11
|
-
* 3. getFunctionEntryPoints(appDefinition) - written to <project>/netlify/functions
|
|
12
|
-
* 4. getLibEntryPoints(appDefinition) - written to <project>/netlify/lib
|
|
13
|
-
*
|
|
14
|
-
* <project> is the directory that holds backend/: the generated entry points
|
|
15
|
-
* require '../../backend/...', so they must sit beside it. #545 had two
|
|
16
|
-
* copies of this, and the CLI one wrote into the backend directory itself,
|
|
17
|
-
* where those relative requires do not resolve.
|
|
18
|
-
*
|
|
19
|
-
* Shared by createFriggInfrastructure() (`node infrastructure.js package`,
|
|
20
|
-
* what a backend's `npm run build` runs) and `frigg build`.
|
|
21
|
-
*/
|
|
22
|
-
const path = require('path');
|
|
23
|
-
const fs = require('fs');
|
|
24
|
-
const { composeAppExtensionPrisma } = require('./compose-extension-prisma');
|
|
25
|
-
|
|
26
|
-
const CONFIG_FILE_NAMES = { netlify: 'netlify.toml' };
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @param {Object} params
|
|
30
|
-
* @param {Object} params.appDefinition
|
|
31
|
-
* @param {string} params.backendDir - directory containing the backend's index.js
|
|
32
|
-
* @param {Object} [params.provider] - resolved plugin; defaults to resolveProvider(appDefinition)
|
|
33
|
-
* @param {boolean} [params.verbose]
|
|
34
|
-
* @param {Function} [params.exit] - called with 1 on validation errors (default process.exit)
|
|
35
|
-
* @param {Function} [params.composePrisma] - extension Prisma composition (injected in tests)
|
|
36
|
-
* @returns {{ projectDir: string, written: string[] }}
|
|
37
|
-
*/
|
|
38
|
-
function buildWithProvider({
|
|
39
|
-
appDefinition,
|
|
40
|
-
backendDir,
|
|
41
|
-
provider,
|
|
42
|
-
verbose = false,
|
|
43
|
-
exit = (code) => process.exit(code),
|
|
44
|
-
composePrisma = composeAppExtensionPrisma,
|
|
45
|
-
}) {
|
|
46
|
-
const providerName = appDefinition.provider;
|
|
47
|
-
const plugin =
|
|
48
|
-
provider ||
|
|
49
|
-
require('@friggframework/core/providers/resolve-provider').resolveProvider(
|
|
50
|
-
appDefinition
|
|
51
|
-
);
|
|
52
|
-
const projectDir = path.dirname(path.resolve(backendDir));
|
|
53
|
-
const written = [];
|
|
54
|
-
|
|
55
|
-
console.log(`Building for ${providerName} provider (skipping AWS infrastructure)...`);
|
|
56
|
-
|
|
57
|
-
if (typeof plugin.validate === 'function') {
|
|
58
|
-
const validation = plugin.validate(appDefinition);
|
|
59
|
-
if (validation.errors?.length > 0) {
|
|
60
|
-
console.error(`\nValidation errors for ${providerName}:`);
|
|
61
|
-
for (const error of validation.errors) {
|
|
62
|
-
console.error(` - ${error}`);
|
|
63
|
-
}
|
|
64
|
-
exit(1);
|
|
65
|
-
return { projectDir, written };
|
|
66
|
-
}
|
|
67
|
-
for (const warning of validation.warnings || []) {
|
|
68
|
-
console.warn(` Warning: ${warning}`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
composePrisma({ appDefinition, backendDir });
|
|
73
|
-
|
|
74
|
-
const write = (filePath, content) => {
|
|
75
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
76
|
-
fs.writeFileSync(filePath, content, 'utf-8');
|
|
77
|
-
written.push(filePath);
|
|
78
|
-
if (verbose) console.log(` Written ${path.relative(projectDir, filePath)}`);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
if (typeof plugin.generateConfig === 'function') {
|
|
82
|
-
const fileName = CONFIG_FILE_NAMES[providerName] || `${providerName}.config`;
|
|
83
|
-
write(path.join(projectDir, fileName), plugin.generateConfig(appDefinition));
|
|
84
|
-
console.log(` Written ${fileName}`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (typeof plugin.getFunctionEntryPoints === 'function') {
|
|
88
|
-
const entryPoints = plugin.getFunctionEntryPoints(appDefinition);
|
|
89
|
-
for (const [fileName, content] of Object.entries(entryPoints)) {
|
|
90
|
-
write(path.join(projectDir, 'netlify', 'functions', fileName), content);
|
|
91
|
-
}
|
|
92
|
-
console.log(` Generated ${Object.keys(entryPoints).length} function entry points`);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (typeof plugin.getLibEntryPoints === 'function') {
|
|
96
|
-
const libEntryPoints = plugin.getLibEntryPoints(appDefinition);
|
|
97
|
-
for (const [fileName, content] of Object.entries(libEntryPoints)) {
|
|
98
|
-
write(path.join(projectDir, 'netlify', 'lib', fileName), content);
|
|
99
|
-
}
|
|
100
|
-
console.log(` Generated ${Object.keys(libEntryPoints).length} lib entry points`);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (typeof plugin.generateEnvTemplate === 'function') {
|
|
104
|
-
const missing = Object.entries(plugin.generateEnvTemplate(appDefinition)).filter(
|
|
105
|
-
([key]) => !process.env[key]
|
|
106
|
-
);
|
|
107
|
-
if (missing.length > 0) {
|
|
108
|
-
console.log('\n Environment variables not set locally (configure them on the platform):');
|
|
109
|
-
for (const [key] of missing) console.log(` - ${key}`);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
console.log(`\nBuild complete for ${providerName}.`);
|
|
114
|
-
return { projectDir, written };
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* True when the app definition names a provider other than AWS.
|
|
119
|
-
* @param {Object} [appDefinition]
|
|
120
|
-
*/
|
|
121
|
-
function isNonAwsProvider(appDefinition) {
|
|
122
|
-
const name = appDefinition?.provider;
|
|
123
|
-
return Boolean(name) && name !== 'aws';
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
module.exports = { buildWithProvider, isNonAwsProvider };
|