Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(create-compas): write template tar to temp directory before extracting #2144

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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