-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (53 loc) · 2.45 KB
/
Copy pathindex.js
File metadata and controls
62 lines (53 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { spawn } from 'child_process';
/**
* A Vite plugin to automatically scaffold AWS Fargate infrastructure
* via deploy-stack after a successful production build.
*/
export default function deployStack(options = {}) {
let viteConfig;
return {
name: 'vite-plugin-deploy-stack',
// We strictly apply this only during `vite build`, not `vite dev`
apply: 'build',
configResolved(resolvedConfig) {
// Capture the user's resolved Vite configuration
viteConfig = resolvedConfig;
},
async closeBundle() {
// 1. Identify the output directory (defaults to 'dist')
const outDir = viteConfig.build.outDir || 'dist';
const root = viteConfig.root || process.cwd();
console.log(`\n☁️ [vite-plugin-deploy-stack] Build complete. Preparing AWS architecture for "${outDir}"...`);
// 2. Base arguments for the CLI
const args = [
'--yes',
'deploy-stack',
'--headless',
'--framework=static'
]
// 3. Dynamically map any plugin options the user passed to the CLI flags
for (const [key, value] of Object.entries(options)) {
if (typeof value === 'boolean') {
if (value) args.push(`--${key}`); // e.g., { enablePrPreviews: true } -> --enablePrPreviews
} else if (value) {
args.push(`--${key}=${value}`); // e.g., { region: 'us-east-1' } -> --region=us-east-1
}
}
// 4. Spawn the deploy-stack CLI silently in the background
const command = process.platform === 'win32' ? 'npx.cmd' : 'npx';
const child = spawn(command, args, {
cwd: root,
stdio: 'inherit', // Pipes the deploy-stack CLI colors and logs directly to the user's terminal
shell: false
});
child.on('close', (code) => {
if (code !== 0) {
console.error(`\n❌ [vite-plugin-deploy-stack] Infrastructure generation failed (Exit Code: ${code}).`);
} else {
console.log(`\n✅ [vite-plugin-deploy-stack] AWS Infrastructure and CI/CD generated successfully!`);
console.log(` Run "npx --yes deploy-stack apply" to provision it in your AWS account.`);
}
});
}
};
}