@friggframework/devtools 2.0.0--canary.691.01b391d.0 → 2.0.0--canary.690.33ca435.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/__tests__/unit/commands/provider-dispatch.test.js +140 -0
- package/frigg-cli/build-command/index.js +32 -1
- package/frigg-cli/deploy-command/index.js +50 -2
- package/frigg-cli/doctor-command/index.js +7 -0
- package/frigg-cli/generate-iam-command.js +7 -0
- package/frigg-cli/repair-command/index.js +7 -0
- package/frigg-cli/start-command/index.js +58 -0
- package/frigg-cli/utils/provider-helper.js +80 -0
- package/infrastructure/__tests__/create-frigg-infrastructure-provider.test.js +64 -0
- package/infrastructure/__tests__/management-api-gateway-routes.test.js +335 -0
- package/infrastructure/__tests__/template-generation.test.js +7 -0
- package/infrastructure/create-frigg-infrastructure.js +14 -0
- package/infrastructure/domains/admin-scripts/admin-script-builder.js +2 -6
- package/infrastructure/domains/admin-scripts/admin-script-builder.test.js +0 -30
- package/infrastructure/domains/shared/types/app-definition.js +15 -1
- package/infrastructure/domains/shared/utilities/base-definition-factory.js +28 -10
- package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +85 -1
- package/infrastructure/infrastructure-composer.test.js +7 -0
- package/infrastructure/provider-build.js +119 -0
- package/infrastructure/scripts/build-prisma-layer.js +32 -5
- package/infrastructure/scripts/build-prisma-layer.test.js +35 -0
- package/package.json +7 -7
- package/test/npm-install-guard.js +104 -0
- package/test/npm-install-guard.test.js +53 -0
|
@@ -0,0 +1,140 @@
|
|
|
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,7 +1,22 @@
|
|
|
1
1
|
const { spawnSync } = require('child_process');
|
|
2
2
|
const path = require('path');
|
|
3
3
|
|
|
4
|
-
async function buildCommand(options) {
|
|
4
|
+
async function buildCommand(options = {}) {
|
|
5
|
+
// A non-AWS provider (appDefinition.provider) builds through its plugin:
|
|
6
|
+
// platform config + function entry points, no serverless package
|
|
7
|
+
// (friggframework/frigg#545).
|
|
8
|
+
const providerResult = loadProviderIfConfigured();
|
|
9
|
+
if (providerResult) {
|
|
10
|
+
const { buildWithProvider } = require('../../infrastructure/provider-build');
|
|
11
|
+
buildWithProvider({
|
|
12
|
+
appDefinition: providerResult.appDefinition,
|
|
13
|
+
backendDir: providerResult.backendDir,
|
|
14
|
+
provider: providerResult.provider,
|
|
15
|
+
verbose: options.verbose,
|
|
16
|
+
});
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
5
20
|
console.log('Building the serverless application...');
|
|
6
21
|
|
|
7
22
|
// Suppress AWS SDK warning message about maintenance mode
|
|
@@ -63,4 +78,20 @@ async function buildCommand(options) {
|
|
|
63
78
|
// });
|
|
64
79
|
}
|
|
65
80
|
|
|
81
|
+
/**
|
|
82
|
+
* The resolved non-AWS provider, or null for AWS / no app definition found.
|
|
83
|
+
*/
|
|
84
|
+
function loadProviderIfConfigured() {
|
|
85
|
+
const { loadProviderForCli } = require('../utils/provider-helper');
|
|
86
|
+
let result;
|
|
87
|
+
try {
|
|
88
|
+
result = loadProviderForCli();
|
|
89
|
+
} catch (error) {
|
|
90
|
+
// A non-AWS provider whose package is missing: say so, don't build for AWS.
|
|
91
|
+
console.error(error.message);
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
return result && result.provider ? result : null;
|
|
95
|
+
}
|
|
96
|
+
|
|
66
97
|
module.exports = { buildCommand };
|
|
@@ -310,9 +310,16 @@ async function pushOffloadedParametersOrAbort(appDefinition, options) {
|
|
|
310
310
|
}
|
|
311
311
|
|
|
312
312
|
async function deployCommand(options) {
|
|
313
|
-
console.log('Deploying the serverless application...');
|
|
314
|
-
|
|
315
313
|
const appDefinition = loadAppDefinition();
|
|
314
|
+
|
|
315
|
+
// A non-AWS provider (appDefinition.provider) deploys through its plugin
|
|
316
|
+
// (friggframework/frigg#545).
|
|
317
|
+
const providerName = appDefinition?.provider;
|
|
318
|
+
if (providerName && providerName !== 'aws') {
|
|
319
|
+
return deployWithProvider(appDefinition, providerName, options);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
console.log('Deploying the serverless application...');
|
|
316
323
|
const environment = validateAndBuildEnvironment(appDefinition, options);
|
|
317
324
|
|
|
318
325
|
await pushOffloadedParametersOrAbort(appDefinition, options);
|
|
@@ -346,4 +353,45 @@ async function deployCommand(options) {
|
|
|
346
353
|
}
|
|
347
354
|
}
|
|
348
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Deploy with a provider plugin: validate(), then deploy() (which runs its
|
|
358
|
+
* own preflightCheck()).
|
|
359
|
+
*/
|
|
360
|
+
async function deployWithProvider(appDefinition, providerName, options = {}) {
|
|
361
|
+
let provider;
|
|
362
|
+
try {
|
|
363
|
+
const { resolveProvider } = require('@friggframework/core/providers/resolve-provider');
|
|
364
|
+
provider = resolveProvider(appDefinition);
|
|
365
|
+
} catch (error) {
|
|
366
|
+
console.error(`Failed to load provider '${providerName}': ${error.message}`);
|
|
367
|
+
process.exit(1);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
console.log(`Deploying with ${providerName} provider...`);
|
|
371
|
+
|
|
372
|
+
if (typeof provider.validate === 'function') {
|
|
373
|
+
const validation = provider.validate(appDefinition);
|
|
374
|
+
if (validation.errors?.length > 0) {
|
|
375
|
+
console.error(`\nValidation errors for ${providerName}:`);
|
|
376
|
+
for (const error of validation.errors) console.error(` - ${error}`);
|
|
377
|
+
process.exit(1);
|
|
378
|
+
}
|
|
379
|
+
for (const warning of validation.warnings || []) console.warn(` Warning: ${warning}`);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
try {
|
|
383
|
+
const result = await provider.deploy(appDefinition, {
|
|
384
|
+
stage: options.stage,
|
|
385
|
+
prod: options.stage === 'production' || options.stage === 'prod',
|
|
386
|
+
dryRun: options.dryRun || false,
|
|
387
|
+
});
|
|
388
|
+
console.log('\nDeployment completed successfully.');
|
|
389
|
+
if (result?.url) console.log(` URL: ${result.url}`);
|
|
390
|
+
} catch (error) {
|
|
391
|
+
console.error(`\nDeployment failed: ${error.message}`);
|
|
392
|
+
for (const item of error.missing || []) console.error(` - ${item}`);
|
|
393
|
+
process.exit(1);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
349
397
|
module.exports = { deployCommand };
|
|
@@ -246,6 +246,13 @@ async function promptForStackSelection(region) {
|
|
|
246
246
|
*/
|
|
247
247
|
async function doctorCommand(stackName, options = {}) {
|
|
248
248
|
try {
|
|
249
|
+
// AWS-only (CloudFormation); a non-AWS provider has nothing to inspect.
|
|
250
|
+
if (require('../utils/provider-helper').isNonAwsProject()) {
|
|
251
|
+
output.error('The doctor command is only available for AWS deployments.');
|
|
252
|
+
output.log('Your appDefinition uses a non-AWS provider.');
|
|
253
|
+
process.exit(1);
|
|
254
|
+
}
|
|
255
|
+
|
|
249
256
|
// Extract options with defaults
|
|
250
257
|
const region = options.region || process.env.AWS_REGION || 'us-east-1';
|
|
251
258
|
const format = options.format || 'console';
|
|
@@ -9,6 +9,13 @@ const { generateIAMCloudFormation, getFeatureSummary } = require('../infrastruct
|
|
|
9
9
|
*/
|
|
10
10
|
async function generateIamCommand(options = {}) {
|
|
11
11
|
try {
|
|
12
|
+
// AWS-only (CloudFormation); a non-AWS provider has nothing to inspect.
|
|
13
|
+
if (require('./utils/provider-helper').isNonAwsProject()) {
|
|
14
|
+
console.error('The generate-iam command is only available for AWS deployments.');
|
|
15
|
+
console.log('Your appDefinition uses a non-AWS provider.');
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
|
|
12
19
|
console.log('🔍 Finding Frigg application...');
|
|
13
20
|
|
|
14
21
|
// Find the backend package.json
|
|
@@ -453,6 +453,13 @@ async function handleReconcileRepair(stackIdentifier, report, options) {
|
|
|
453
453
|
*/
|
|
454
454
|
async function repairCommand(stackName, options = {}) {
|
|
455
455
|
try {
|
|
456
|
+
// AWS-only (CloudFormation); a non-AWS provider has nothing to inspect.
|
|
457
|
+
if (require('../utils/provider-helper').isNonAwsProject()) {
|
|
458
|
+
output.error('The repair command is only available for AWS deployments.');
|
|
459
|
+
output.log('Your appDefinition uses a non-AWS provider.');
|
|
460
|
+
process.exit(1);
|
|
461
|
+
}
|
|
462
|
+
|
|
456
463
|
// Validate required parameter
|
|
457
464
|
if (!stackName) {
|
|
458
465
|
console.error('Error: Stack name is required');
|
|
@@ -37,6 +37,12 @@ async function startCommand(options) {
|
|
|
37
37
|
console.log(chalk.green('✓ Database checks passed\n'));
|
|
38
38
|
console.log('Starting backend and optional frontend...');
|
|
39
39
|
|
|
40
|
+
// A non-AWS provider runs its own local dev server (friggframework/frigg#545).
|
|
41
|
+
const providerResult = loadNonAwsProvider();
|
|
42
|
+
if (providerResult) {
|
|
43
|
+
return startWithProvider(providerResult, options);
|
|
44
|
+
}
|
|
45
|
+
|
|
40
46
|
// Suppress AWS SDK warning message about maintenance mode
|
|
41
47
|
process.env.AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE = '1';
|
|
42
48
|
// Skip AWS discovery for local development
|
|
@@ -146,4 +152,56 @@ async function performDatabaseChecks(verbose) {
|
|
|
146
152
|
// connection when it starts.
|
|
147
153
|
}
|
|
148
154
|
|
|
155
|
+
function loadNonAwsProvider() {
|
|
156
|
+
try {
|
|
157
|
+
const { loadProviderForCli } = require('../utils/provider-helper');
|
|
158
|
+
const result = loadProviderForCli();
|
|
159
|
+
return result && result.provider ? result : null;
|
|
160
|
+
} catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Local dev server per provider: Netlify -> `netlify dev` (reads netlify.toml
|
|
167
|
+
* and serves the generated functions), run from the project root that holds
|
|
168
|
+
* backend/ and netlify.toml.
|
|
169
|
+
*/
|
|
170
|
+
function startWithProvider({ providerName, backendDir }, options = {}) {
|
|
171
|
+
const DEV_COMMANDS = {
|
|
172
|
+
netlify: { command: 'netlify', args: ['dev'] },
|
|
173
|
+
};
|
|
174
|
+
const devCmd = DEV_COMMANDS[providerName];
|
|
175
|
+
if (!devCmd) {
|
|
176
|
+
console.error(chalk.red(
|
|
177
|
+
`Provider '${providerName}' does not have a local dev server configured.\n` +
|
|
178
|
+
` Supported providers for local dev: ${Object.keys(DEV_COMMANDS).join(', ')}, aws`
|
|
179
|
+
));
|
|
180
|
+
process.exit(1);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const cwd = require('node:path').dirname(backendDir);
|
|
184
|
+
console.log(chalk.blue(`Starting local dev server (${providerName})...`));
|
|
185
|
+
if (options.verbose) {
|
|
186
|
+
console.log(`Executing command: ${devCmd.command} ${devCmd.args.join(' ')}`);
|
|
187
|
+
console.log(`Working directory: ${cwd}`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const childProcess = spawn(devCmd.command, devCmd.args, {
|
|
191
|
+
cwd,
|
|
192
|
+
stdio: 'inherit',
|
|
193
|
+
env: { ...process.env },
|
|
194
|
+
});
|
|
195
|
+
childProcess.on('error', (error) => {
|
|
196
|
+
if (error.code === 'ENOENT') {
|
|
197
|
+
console.error(chalk.red(`'${devCmd.command}' not found. Install it with: npm install -g ${devCmd.command}-cli`));
|
|
198
|
+
} else {
|
|
199
|
+
console.error(`Error executing command: ${error.message}`);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
childProcess.on('close', (code) => {
|
|
203
|
+
if (code !== 0) console.log(`Child process exited with code ${code}`);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
149
207
|
module.exports = { startCommand };
|
|
@@ -0,0 +1,80 @@
|
|
|
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 };
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
});
|