Signed-off-by: bnechyporenko <bnechyporenko@bol.com>
This commit is contained in:
bnechyporenko
2024-06-25 11:43:45 +02:00
committed by blam
parent a190944647
commit 47b7ac5ade
4 changed files with 30 additions and 13 deletions
@@ -72,8 +72,11 @@ describe('serializer', () => {
const restoredWorkspaceDir = createMockDirectory();
it('should be able to archive and restore the workspace', async () => {
const workspaceBuffer = await serializeWorkspace(workspaceDir.path);
await restoreWorkspace(restoredWorkspaceDir.path, workspaceBuffer);
const workspaceBuffer = await serializeWorkspace(workspaceDir);
await restoreWorkspace({
path: restoredWorkspaceDir.path,
buffer: workspaceBuffer.contents,
});
expect(
fs.existsSync(`${restoredWorkspaceDir.path}/\$\{ESCAPE_ME\}.txt`),
@@ -23,20 +23,31 @@ const pipeline = promisify(pipelineCb);
/**
* Serializes provided path into tar archive
*
* @public
* @alpha
*/
export const serializeWorkspace = async (path: string): Promise<Buffer> => {
return new Promise<Buffer>(async resolve => {
await pipeline(tar.create({ cwd: path }, ['']), concatStream(resolve));
export const serializeWorkspace = async (opts: {
path: string;
}): Promise<{ contents: Buffer }> => {
return new Promise<{ contents: Buffer }>(async resolve => {
await pipeline(
tar.create({ cwd: opts.path }, ['']),
concatStream(buffer => {
return resolve({ contents: buffer });
}),
);
});
};
/**
* Rehydrates the provided buffer of tar archive into the provide destination path
*
* @public
* @alpha
*/
export const restoreWorkspace = async (path: string, buffer?: Buffer) => {
export const restoreWorkspace = async (opts: {
path: string;
buffer?: Buffer;
}): Promise<void> => {
const { buffer, path } = opts;
if (buffer) {
await pipeline(
Readable.from(buffer),