From 6626c67586b91a6e946edc8abc1e14e8eda5efad Mon Sep 17 00:00:00 2001 From: Dirk de Visser Date: Tue, 18 Oct 2022 09:21:53 +0200 Subject: [PATCH] fix(create-compas): write template tar to temp directory before extracting Closes #2105 --- packages/create-compas/src/template.js | 36 ++++++++++++++------------ 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/create-compas/src/template.js b/packages/create-compas/src/template.js index 65fd862184..d6aa0d15ac 100644 --- a/packages/create-compas/src/template.js +++ b/packages/create-compas/src/template.js @@ -1,9 +1,11 @@ +import { createWriteStream } from "fs"; import { mkdir, readFile, writeFile } from "fs/promises"; import https from "https"; +import os from "os"; import { normalize } from "path"; import { Readable } from "stream"; import { pipeline } from "stream/promises"; -import { AppError, environment, exec, pathJoin, spawn } from "@compas/stdlib"; +import { AppError, environment, exec, pathJoin, spawn, uuid } from "@compas/stdlib"; import tar from "tar"; /** @@ -80,19 +82,23 @@ export async function templateGetAndExtractStream(logger, options) { await mkdir(options.outputDirectory, { recursive: true }); - let stream = Readable.from(Buffer.from("")); + const tmpFile = pathJoin(os.tmpdir(), `create-compas-${uuid()}`); + let httpStream = Readable.from(Buffer.from("")); if (options.template.provider === "github") { logger.info(`Resolving remote template...`); // @ts-expect-error - stream = await templateGetHttpStream( + httpStream = await templateGetHttpStream( `https://codeload.github.com/${options.template.repository}/tar.gz${ options.template.ref ? `/${options.template.ref}` : "" }`, ); } + logger.info("Downloading template..."); + await pipeline(httpStream, createWriteStream(tmpFile)); + let dirToExtract = options.template.path; if (options.template.provider === "github") { @@ -113,19 +119,17 @@ export async function templateGetAndExtractStream(logger, options) { } } - logger.info(`Downloading and extracting template...`); - - await pipeline( - stream, - tar.extract( - { - cwd: options.outputDirectory, - strip: options.template.path - ? normalize(options.template.path).split("/").length + 1 - : 1, - }, - [dirToExtract], - ), + logger.info(`Extracting template...`); + + await tar.extract( + { + file: tmpFile, + cwd: options.outputDirectory, + strip: options.template.path + ? normalize(options.template.path).split("/").length + 1 + : 1, + }, + [dirToExtract], ); }