Skip to content

Commit

Permalink
fix(create-compas): write template tar to temp directory before extra…
Browse files Browse the repository at this point in the history
…cting

Closes #2105
  • Loading branch information
dirkdev98 committed Oct 18, 2022
1 parent de2dbe7 commit 6626c67
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions packages/create-compas/src/template.js
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand Down Expand Up @@ -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") {
Expand All @@ -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],
);
}

Expand Down

0 comments on commit 6626c67

Please sign in to comment.