Skip to content

Commit 01cb894

Browse files
committed
Use pure child_process.exec instead of execa
1 parent 08fa1c6 commit 01cb894

File tree

7 files changed

+42
-122
lines changed

7 files changed

+42
-122
lines changed

package-lock.json

Lines changed: 10 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"compression": "^1.7.3",
3131
"dotenv": "^8.0.0",
3232
"dotenv-flow": "^2.0.0",
33-
"execa": "^1.0.0",
3433
"express": "^4.16.4",
3534
"express-github-webhook": "^1.0.6",
3635
"fs-extra": "^6.0.1",

src/Server.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import { __PROD__, credentials, httpPort, httpsPort, webhookOptions } from 'conf
88
import http from 'http';
99
import https from 'https';
1010
import { Hierarchy } from 'models';
11-
import execa from 'execa';
1211
import * as Tracers from 'tracers';
1312
import { errorHandlerMiddleware, frontendMiddleware, redirectMiddleware } from 'middlewares';
14-
import { pull } from 'utils/misc';
13+
import { execute, pull } from 'utils/misc';
1514
import { frontendBuildDir, frontendBuiltDir, frontendDir, rootDir } from 'config/paths';
1615

1716
const Webhook = require('express-github-webhook');
@@ -76,13 +75,13 @@ export default class Server {
7675

7776
async update(commit?: string) {
7877
await pull(rootDir, 'server', commit);
79-
await execa.shell('npm install', {cwd: rootDir});
78+
await execute('npm install', {cwd: rootDir});
8079
process.exit(0);
8180
};
8281

8382
async updateFrontend(commit?: string) {
8483
await pull(frontendDir, 'algorithm-visualizer', commit);
85-
await execa.shell([
84+
await execute([
8685
'npm install',
8786
'npm run build',
8887
`rm -rf ${frontendBuiltDir}`,

src/models/Hierarchy.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { GitHubApi } from 'utils/apis';
44
import { Algorithm, Category, File } from 'models';
55
import { Author } from 'models/File';
66
import { algorithmsDir } from 'config/paths';
7-
import { pull } from 'utils/misc';
8-
import execa from 'execa';
7+
import { execute, pull } from 'utils/misc';
98

109
type CommitAuthors = {
1110
[sha: string]: Author,
@@ -52,7 +51,7 @@ export class Hierarchy {
5251

5352
async cacheContributors(files: File[], commitAuthors: CommitAuthors) {
5453
for (const file of files) {
55-
const stdout = await execa('git', ['--no-pager', 'log', '--follow', '--no-merges', '--format="%H"', '--', `"${file.path}"`], {
54+
const stdout = await execute(`git --no-pager log --follow --no-merges --format="%H" -- "${path.relative(this.path, file.path)}"`, {
5655
cwd: this.path,
5756
});
5857
const output = stdout.toString().replace(/\n$/, '');

src/tracers/DockerTracer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import uuid from 'uuid';
55
import fs from 'fs-extra';
66
import { memoryLimit, timeLimit } from 'config/constants';
77
import { codesDir } from 'config/paths';
8-
import execa = require('execa');
8+
import { execute } from 'utils/misc';
99

1010
export class DockerTracer extends Tracer {
1111
private readonly directory: string;
@@ -21,7 +21,7 @@ export class DockerTracer extends Tracer {
2121

2222
build(release: Release) {
2323
const {tag_name} = release;
24-
return execa('docker', ['build', '-t', this.imageName, '.', '--build-arg', `tag_name=${tag_name}`], {cwd: this.directory});
24+
return execute(`docker build -t ${this.imageName} . --build-arg tag_name=${tag_name}`, {cwd: this.directory});
2525
}
2626

2727
route(router: express.Router) {
@@ -33,19 +33,19 @@ export class DockerTracer extends Tracer {
3333
const containerName = uuid.v4();
3434
let killed = false;
3535
const timer = setTimeout(() => {
36-
execa('docker', ['kill', containerName]).then(() => {
36+
execute(`docker kill ${containerName}`).then(() => {
3737
killed = true;
3838
});
3939
}, timeLimit);
40-
return execa('docker', [
41-
'run', '--rm',
40+
return execute([
41+
'docker run --rm',
4242
`--name=${containerName}`,
4343
'-w=/usr/visualization',
4444
`-v=${tempPath}:/usr/visualization:rw`,
4545
`-m=${memoryLimit}m`,
46-
'-e', 'ALGORITHM_VISUALIZER=1',
46+
'-e ALGORITHM_VISUALIZER=1',
4747
this.imageName,
48-
]).catch(error => {
48+
].join(' ')).catch(error => {
4949
if (killed) throw new Error('Time Limit Exceeded');
5050
throw error;
5151
}).finally(() => clearTimeout(timer));

src/utils/misc.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import axios from 'axios';
22
import fs from 'fs-extra';
3-
import execa from 'execa';
43
import { File } from 'models';
54
import removeMarkdown from 'remove-markdown';
5+
import * as child_process from 'child_process';
6+
import { ExecOptions } from 'child_process';
67

78
export function download(url: string, localPath: string) {
89
return axios({url, method: 'GET', responseType: 'stream'})
@@ -16,11 +17,11 @@ export function download(url: string, localPath: string) {
1617

1718
export async function pull(dir: string, repo: string, commit = 'origin/master') {
1819
if (fs.pathExistsSync(dir)) {
19-
await execa.shell(`git fetch`, {cwd: dir});
20+
await execute(`git fetch`, {cwd: dir});
2021
} else {
21-
await execa.shell(`git clone https://github.com/algorithm-visualizer/${repo}.git ${dir}`);
22+
await execute(`git clone https://github.com/algorithm-visualizer/${repo}.git ${dir}`);
2223
}
23-
await execa.shell(`git reset --hard ${commit}`, {cwd: dir});
24+
await execute(`git reset --hard ${commit}`, {cwd: dir});
2425
}
2526

2627
export function getDescription(files: File[]) {
@@ -34,6 +35,18 @@ export function getDescription(files: File[]) {
3435
return removeMarkdown(descriptionLines.join(' '));
3536
}
3637

37-
export function rethrow(err: any) {
38-
throw err;
38+
type ExecuteOptions = ExecOptions & {
39+
stdout?: NodeJS.WriteStream;
40+
stderr?: NodeJS.WriteStream;
41+
}
42+
43+
export function execute(command: string, {stdout, stderr, ...options}: ExecuteOptions = {}): Promise<string> {
44+
return new Promise((resolve, reject) => {
45+
const child = child_process.exec(command, options, (error, stdout, stderr) => {
46+
if (error) return reject(error.code ? new Error(stderr) : error);
47+
resolve(stdout);
48+
});
49+
if (child.stdout && stdout) child.stdout.pipe(stdout);
50+
if (child.stderr && stderr) child.stderr.pipe(stderr);
51+
});
3952
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
},
1919
"exclude": [
2020
"node_modules",
21+
"public"
2122
]
2223
}

0 commit comments

Comments
 (0)