diff --git a/bin/pull.sh b/bin/pull.sh new file mode 100755 index 0000000000000000000000000000000000000000..93501632f3d02495c5fae74d9545f5f90a0bda60 --- /dev/null +++ b/bin/pull.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +git fetch && +git reset --hard origin/master && +npm install && +npm run build diff --git a/src/backend/common/util.js b/src/backend/common/util.js index 208b45c098933bbf2e3277ddb1d4de76a514d1e9..50d03dc4fe235a2720a982c44cfc1d91a36a6acf 100644 --- a/src/backend/common/util.js +++ b/src/backend/common/util.js @@ -12,12 +12,21 @@ const execute = (command, cwd, { stdout = process.stdout, stderr = process.stder if (stderr) child.stderr.pipe(stderr); }); +const spawn = (command, args, cwd, { stdout = process.stdout, stderr = process.stderr } = {}) => new Promise((resolve, reject) => { + if (!cwd) return reject(new Error('CWD Not Specified')); + const child = child_process.spawn(command, args, { cwd }); + child.on('close', code => code ? reject(new Error(`Process exited with code: ${code}`)) : resolve()); + if (stdout) child.stdout.pipe(stdout); + if (stderr) child.stderr.pipe(stderr); +}); + const createKey = name => name.toLowerCase().replace(/ /g, '-'); const listFiles = dirPath => fs.readdirSync(dirPath).filter(fileName => !fileName.startsWith('.')); export { execute, + spawn, createKey, listFiles, }; diff --git a/src/backend/index.js b/src/backend/index.js index 4b2867f93db6c878194a37682206c0998c8d72dc..d33b64820e1a5cf41968a5f5aa3ca697c1678263 100644 --- a/src/backend/index.js +++ b/src/backend/index.js @@ -6,7 +6,7 @@ import bodyParser from 'body-parser'; import * as controllers from '/controllers'; import { ClientError, ForbiddenError, NotFoundError, UnauthorizedError } from '/common/error'; import webhook from '/common/webhook'; -import { execute } from '/common/util'; +import { spawn } from '/common/util'; const app = express(); app.use(morgan('tiny')); @@ -34,13 +34,7 @@ const rootPath = path.resolve(__dirname, '..', '..'); webhook.on('algorithm-visualizer', event => { switch (event) { case 'push': - execute([ - 'git fetch', - 'git reset --hard origin/master', - 'npm install', - 'npm run build', - 'pm2 startOrRestart ./pm2.config.js', - ].join(' && '), rootPath); + spawn('sh', ['./bin/pull.sh'], rootPath).then(() => process.exit(0)); break; } });