Skip to content

Commit ed3fdaf

Browse files
authored
refactor: promisify instead promisify-child-process (#517)
1 parent dddd2a0 commit ed3fdaf

File tree

8 files changed

+32
-60
lines changed

8 files changed

+32
-60
lines changed

package-lock.json

-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"dependencies": {
3737
"@aeternity/aepp-sdk": "^14.0.0",
3838
"commander": "^12.1.0",
39-
"promisify-child-process": "^4.1.2",
4039
"prompts": "^2.4.2"
4140
},
4241
"devDependencies": {

src/env/env.js

+18-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
import { spawn, exec } from "promisify-child-process";
2-
import { print, printError } from "../utils/utils.js";
3-
4-
let dockerComposeCmd = "docker compose";
1+
import { print, printError, exec } from "../utils/utils.js";
52

63
async function getDockerCompose() {
7-
const dockerSpaceCompose = await spawn("docker", ["compose"]).catch(() => ({
8-
code: 1,
9-
}));
10-
if (dockerSpaceCompose.code === 0) return;
11-
const dockerMinusCompose = await spawn("docker-compose").catch(() => ({
12-
code: 1,
13-
}));
14-
15-
if (dockerMinusCompose.code === 0) {
16-
dockerComposeCmd = "docker-compose";
17-
return;
4+
if (getDockerCompose._cmd) return getDockerCompose._cmd;
5+
6+
for (const cmd of ["docker compose", "docker-compose"]) {
7+
if (await exec(cmd).catch(() => false)) {
8+
getDockerCompose._cmd = cmd;
9+
return cmd;
10+
}
1811
}
1912

2013
throw new Error("===== docker compose is not installed! =====");
@@ -66,8 +59,8 @@ async function stopEnv(running) {
6659

6760
print("===== stopping env =====");
6861

69-
await getDockerCompose();
70-
await exec(`${dockerComposeCmd} down -v`);
62+
const cmd = await getDockerCompose();
63+
await exec(`${cmd} down -v`);
7164

7265
print("===== Env was successfully stopped! =====");
7366
}
@@ -80,8 +73,8 @@ async function restartEnv(running) {
8073

8174
print("===== restarting env =====");
8275

83-
await getDockerCompose();
84-
await exec(`${dockerComposeCmd} restart`);
76+
const cmd = await getDockerCompose();
77+
await exec(`${cmd} restart`);
8578

8679
print("===== env was successfully restarted! =====");
8780
}
@@ -98,9 +91,9 @@ async function startEnv(option) {
9891
print(`using versions as specified: ${versionTags}`);
9992
else print("using versions from docker-compose.yml");
10093

101-
await getDockerCompose();
102-
await exec(`${versionTags} ${dockerComposeCmd} pull`);
103-
await exec(`${versionTags} ${dockerComposeCmd} up -d --wait`);
94+
const cmd = await getDockerCompose();
95+
await exec(`${versionTags} ${cmd} pull`);
96+
await exec(`${versionTags} ${cmd} up -d --wait`);
10497

10598
const isRunning = await isEnvRunning();
10699
await printInfo(isRunning, true);
@@ -119,9 +112,9 @@ async function printInfo(running, imagesOnly = false) {
119112
}
120113

121114
async function getInfo(cwd = "./", imagesOnly = false) {
122-
await getDockerCompose();
123-
const ps = await exec(`${dockerComposeCmd} ps`, { cwd });
124-
const images = await exec(`${dockerComposeCmd} images`, { cwd });
115+
const cmd = await getDockerCompose();
116+
const ps = await exec(`${cmd} ps`, { cwd });
117+
const images = await exec(`${cmd} images`, { cwd });
125118

126119
if (ps && images && ps.stdout && images.stdout) {
127120
return imagesOnly ? images.stdout : `${ps.stdout}\n${images.stdout}`;

src/init/init.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from "fs";
22
import path from "path";
3-
import { exec } from "promisify-child-process";
4-
import { print } from "../utils/utils.js";
3+
import { print, exec } from "../utils/utils.js";
54
import { copyFolderRecursive, deleteWithPrompt } from "../utils/fs-utils.js";
65
import { fileURLToPath } from "url";
76
import { readFile } from "fs/promises";

src/test/test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exec } from "promisify-child-process";
1+
import { spawn } from "child_process";
22
import { print } from "../utils/utils.js";
33

44
async function run() {
@@ -10,11 +10,9 @@ async function run() {
1010
async function test() {
1111
print("===== Starting Tests =====");
1212

13-
const child = exec("npm test");
13+
const proc = spawn("npm", ["test"], { stdio: "inherit" });
1414

15-
child.stdout.on("data", (out) => process.stdout.write(out));
16-
child.stderr.on("data", (err) => process.stderr.write(err));
17-
await child;
15+
await new Promise((resolve) => proc.on("close", resolve));
1816
}
1917

2018
export default { run };

src/utils/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
import { promisify } from "util";
2+
import { exec as execCb } from "child_process";
3+
4+
export const exec = promisify(execCb);
5+
16
export const print = console.log;
27
export const printError = console.error;

tests/cli.test.mjs

+4-15
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,21 @@ describe("command line usage", () => {
1111
afterAll(() => cleanLocal());
1212

1313
it("help", async () => {
14-
const res = await exec("aeproject help", { cwd });
15-
assert.equal(res.code, 0);
14+
await exec("aeproject help", { cwd });
1615
});
1716

1817
it("version", async () => {
1918
const res = await exec("aeproject --version", { cwd });
20-
assert.equal(res.code, 0);
2119
assert.include(res.stdout, version);
2220
});
2321

2422
for (const folder of [null, "testprojectfolder"]) {
2523
// eslint-disable-next-line vitest/valid-title
2624
it(folder ? `init ${folder}` : "init", async () => {
27-
const res = await exec(
25+
await exec(
2826
folder ? `aeproject init ${folder}` : "aeproject init",
2927
{ cwd },
3028
);
31-
assert.equal(res.code, 0);
3229

3330
// link to use local aeproject utils
3431
await linkLocalLib(folder);
@@ -48,8 +45,7 @@ describe("command line usage", () => {
4845
}
4946

5047
it("env", async () => {
51-
const res = await exec("aeproject env", { cwd });
52-
assert.equal(res.code, 0);
48+
await exec("aeproject env", { cwd });
5349
assert.isTrue(await isEnvRunning(cwd));
5450

5551
// don't run for all gh-action matrix tests
@@ -75,14 +71,11 @@ describe("command line usage", () => {
7571

7672
it("env --info", async () => {
7773
const res = await exec("aeproject env --info", { cwd });
78-
assert.equal(res.code, 0);
79-
8074
print(res.stdout);
8175
});
8276

8377
it("test", async () => {
8478
const res = await exec("aeproject test", { cwd });
85-
assert.equal(res.code, 0);
8679
assert.equal(res.stderr, "");
8780
assert.include(res.stdout, "2 passing");
8881
});
@@ -94,7 +87,6 @@ describe("command line usage", () => {
9487
// link to use local aeproject utils
9588
await linkLocalLib();
9689

97-
assert.equal(res.code, 0);
9890
assert.equal(res.stderr, "");
9991
assert.include(
10092
res.stdout,
@@ -115,21 +107,18 @@ describe("command line usage", () => {
115107
);
116108

117109
const resEnv = await exec("aeproject env", { cwd });
118-
assert.equal(resEnv.code, 0);
119110
assert.isTrue(await isEnvRunning(cwd));
120111

121112
assert.include(resEnv.stdout, "aeternity/aeternity latest");
122113
assert.include(resEnv.stdout, "aeternity/aesophia_http latest");
123114

124115
const resTest = await exec("aeproject test", { cwd });
125-
assert.equal(resTest.code, 0);
126116
assert.include(resTest.stdout, "2 passing");
127117
} else console.log("skipping next test for auxiliary test run");
128118
});
129119

130120
it("env --stop", async () => {
131-
const res = await exec("aeproject env --stop", { cwd });
132-
assert.equal(res.code, 0);
121+
await exec("aeproject env --stop", { cwd });
133122
assert.isFalse(await isEnvRunning(cwd));
134123
});
135124
});

tests/util.mjs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from "path";
2-
3-
import { exec as execP } from "promisify-child-process";
42
import fs from "fs";
3+
import { exec as execP } from "../src/utils/utils";
54

65
export const cwd = path.join(process.cwd(), ".testdir");
76

0 commit comments

Comments
 (0)