R-Nozawa commited on
Commit
a53b27f
·
verified ·
1 Parent(s): 9fad30c

Upload install_and_run.js

Browse files
Files changed (1) hide show
  1. install_and_run.js +20 -18
install_and_run.js CHANGED
@@ -1,25 +1,27 @@
1
- const { exec } = require('child_process');
2
- const fs = require('fs').promises;
3
- const path = require('path');
 
 
 
 
4
 
5
  const zipUrl = process.env.ZIP_URL;
6
  const downloadPath = '/tmp/app.zip';
7
  const extractPath = '/app';
8
 
9
  async function runCommand(command) {
10
- return new Promise((resolve, reject) => {
11
- exec(command, (error, stdout, stderr) => {
12
- if (error) {
13
- console.error(`Error executing command: ${command}`);
14
- console.error(stderr);
15
- reject(error);
16
- return;
17
- }
18
- console.log(`Command executed successfully: ${command}`);
19
- console.log(stdout);
20
- resolve(stdout);
21
- });
22
- });
23
  }
24
 
25
  async function main() {
@@ -36,7 +38,7 @@ async function main() {
36
  await runCommand(`unzip -o ${downloadPath} -d ${extractPath}`);
37
 
38
  console.log("Removing downloaded ZIP file...");
39
- await fs.unlink(downloadPath);
40
 
41
  console.log("Starting the application...");
42
  await runCommand('npm start');
@@ -47,4 +49,4 @@ async function main() {
47
  }
48
  }
49
 
50
- main();
 
1
+ import { exec } from 'child_process';
2
+ import { promisify } from 'util';
3
+ import { unlink } from 'fs/promises';
4
+ import path from 'path';
5
+ import { fileURLToPath } from 'url';
6
+
7
+ const execAsync = promisify(exec);
8
 
9
  const zipUrl = process.env.ZIP_URL;
10
  const downloadPath = '/tmp/app.zip';
11
  const extractPath = '/app';
12
 
13
  async function runCommand(command) {
14
+ try {
15
+ const { stdout, stderr } = await execAsync(command);
16
+ console.log(`Command executed successfully: ${command}`);
17
+ if (stdout) console.log(stdout);
18
+ if (stderr) console.error(stderr);
19
+ return stdout;
20
+ } catch (error) {
21
+ console.error(`Error executing command: ${command}`);
22
+ console.error(error.stderr || error.message);
23
+ throw error;
24
+ }
 
 
25
  }
26
 
27
  async function main() {
 
38
  await runCommand(`unzip -o ${downloadPath} -d ${extractPath}`);
39
 
40
  console.log("Removing downloaded ZIP file...");
41
+ await unlink(downloadPath);
42
 
43
  console.log("Starting the application...");
44
  await runCommand('npm start');
 
49
  }
50
  }
51
 
52
+ main();