From dff557a9da0be23d9e03eda26a4dcc7c423a0418 Mon Sep 17 00:00:00 2001 From: Jason Park Date: Tue, 4 Dec 2018 17:35:35 -0500 Subject: [PATCH] Fix webhook pull --- bin/pull.sh | 6 ++++++ src/backend/common/util.js | 9 +++++++++ src/backend/index.js | 10 ++-------- 3 files changed, 17 insertions(+), 8 deletions(-) create mode 100755 bin/pull.sh diff --git a/bin/pull.sh b/bin/pull.sh new file mode 100755 index 0000000..9350163 --- /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 208b45c..50d03dc 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 4b2867f..d33b648 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; } }); -- GitLab