Skip to content

Commit 329cc20

Browse files
committed
Fix cp promise returns by converting the exec callbacks into promises
1 parent b351ddb commit 329cc20

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

src/cp.ts

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,33 @@ export class Cp {
3535
command.push(srcPath);
3636
const writerStream = fs.createWriteStream(tmpFileName);
3737
const errStream = new WritableStreamBuffer();
38-
this.execInstance.exec(
39-
namespace,
40-
podName,
41-
containerName,
42-
command,
43-
writerStream,
44-
errStream,
45-
null,
46-
false,
47-
async ({ status }) => {
48-
writerStream.close();
49-
if (status === 'Failure' || errStream.size()) {
50-
throw new Error(`Error from cpFromPod - details: \n ${errStream.getContentsAsString()}`);
51-
}
52-
await tar.x({
53-
file: tmpFileName,
54-
cwd: tgtPath,
55-
});
56-
},
57-
);
38+
return new Promise<void>((resolve, reject) => {
39+
this.execInstance.exec(
40+
namespace,
41+
podName,
42+
containerName,
43+
command,
44+
writerStream,
45+
errStream,
46+
null,
47+
false,
48+
async ({ status }) => {
49+
try {
50+
writerStream.close();
51+
if (status === 'Failure' || errStream.size()) {
52+
throw new Error(`Error from cpFromPod - details: \n ${errStream.getContentsAsString()}`);
53+
}
54+
await tar.x({
55+
file: tmpFileName,
56+
cwd: tgtPath,
57+
});
58+
resolve();
59+
} catch (e) {
60+
reject(e);
61+
}
62+
},
63+
).catch(reject);
64+
});
5865
}
5966

6067
/**
@@ -78,20 +85,24 @@ export class Cp {
7885
await tar.c({ file: tmpFileName, cwd }, [srcPath]);
7986
const readStream = fs.createReadStream(tmpFileName);
8087
const errStream = new WritableStreamBuffer();
81-
this.execInstance.exec(
82-
namespace,
83-
podName,
84-
containerName,
85-
command,
86-
null,
87-
errStream,
88-
readStream,
89-
false,
90-
async ({ status }) => {
91-
if (status === 'Failure' || errStream.size()) {
92-
throw new Error(`Error from cpToPod - details: \n ${errStream.getContentsAsString()}`);
93-
}
94-
},
95-
);
88+
return new Promise<void>((resolve, reject) => {
89+
this.execInstance.exec(
90+
namespace,
91+
podName,
92+
containerName,
93+
command,
94+
null,
95+
errStream,
96+
readStream,
97+
false,
98+
async ({ status }) => {
99+
if (status === 'Failure' || errStream.size()) {
100+
reject(new Error(`Error from cpToPod - details: \n ${errStream.getContentsAsString()}`));
101+
} else {
102+
resolve();
103+
}
104+
},
105+
).catch(reject);
106+
});
96107
}
97108
}

0 commit comments

Comments
 (0)