CMU Coding Bootcamp
1/* eslint-disable no-undef */
2// Import the exec function from the child_process module
3const { exec } = require("child_process");
4
5// Define an async function to handle the deployment process
6async function deploy() {
7 try {
8 // Build the application
9 console.log("Building application...");
10 await execCommand("npm run build"); // Execute the "npm run build" command
11
12 // Optimize images
13 console.log("Optimizing images...");
14 await execCommand("npm run optimize-images"); // Execute the "npm run optimize-images" command
15
16 // Generate service worker
17 console.log("Generating service worker...");
18 await execCommand("npm run generate-sw"); // Execute the "npm run generate-sw" command
19
20 // Run tests
21 console.log("Running tests...");
22 await execCommand("npm run test"); // Execute the "npm run test" command
23
24 // Deploy to hosting
25 console.log("Deploying to hosting...");
26 await execCommand("npm run deploy-hosting"); // Execute the "npm run deploy-hosting" command
27
28 // Log a success message
29 console.log("Deployment complete!");
30 } catch (error) { // Catch any errors that occur during the deployment process
31 console.error("Deployment failed:", error); // Log the error message
32 process.exit(1); // Exit the process with a non-zero exit code to indicate failure
33 }
34}
35
36// Define a function to execute a command and return a promise
37function execCommand(command) {
38 return new Promise((resolve, reject) => { // Create a new promise
39 // Execute the command using the exec function
40 exec(command, (error, stdout, stderr) => { // Callback function to handle the command execution result
41 if (error) { // Check if there was an error
42 console.error(`Error: ${error.message}`); // Log the error message
43 console.error(`Stderr: ${stderr}`); // Log the standard error output
44 reject(error); // Reject the promise with the error
45 return; // Return to prevent further execution
46 }
47 console.log(stdout); // Log the standard output
48 resolve(); // Resolve the promise indicating successful execution
49 });
50 });
51}
52
53
54// Call the deploy function to start the deployment process
55deploy();