From 4aa52c84bd8cf302031e9f7f6407466aa736beb7 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 2 Feb 2024 02:35:24 +0200 Subject: [PATCH] fix: rm tmp dir too early --- src/cmd.ts | 11 +++++++++-- src/config.ts | 13 ++++++++----- src/git.ts | 7 +++---- src/scaffold.ts | 5 ++++- src/types.ts | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/cmd.ts b/src/cmd.ts index 29bf2c0..28ab47f 100644 --- a/src/cmd.ts +++ b/src/cmd.ts @@ -1,4 +1,6 @@ #!/usr/bin/env node + +import os from "node:os" import { massarg } from "massarg" import chalk from "chalk" import { LogLevel, ScaffoldCmdConfig } from "./types" @@ -19,8 +21,13 @@ export async function parseCliArgs(args = process.argv.slice(2)) { description: pkg.description, }) .main(async (config) => { - const parsed = await parseConfigFile(config) - return Scaffold(parsed) + const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`) + const parsed = await parseConfigFile(config, tmpPath) + try { + return Scaffold(parsed) + } finally { + await fs.rm(tmpPath, { recursive: true, force: true }) + } }) .option({ name: "name", diff --git a/src/config.ts b/src/config.ts index a061c8e..410d035 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,6 @@ import path from "node:path" +import os from "node:os" +import fs from "node:fs/promises" import { ConfigLoadConfig, FileResponse, @@ -48,7 +50,7 @@ function isWrappedWithQuotes(string: string): boolean { } /** @internal */ -export async function parseConfigFile(config: ScaffoldCmdConfig): Promise { +export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string): Promise { let output: ScaffoldConfig = config if (config.quiet) { @@ -69,8 +71,9 @@ export async function parseConfigFile(config: ScaffoldCmdConfig): Promise, ): Promise { - const { config: configFile, git, ...logConfig } = config as Required + const { config: configFile, git, tmpPath, ...logConfig } = config as Required log(logConfig, LogLevel.info, `Loading config from remote ${git}, file ${configFile}`) @@ -135,5 +138,5 @@ export async function getRemoteConfig( throw new Error(`Unsupported protocol ${url.protocol}`) } - return getGitConfig(url, configFile, logConfig) + return getGitConfig(url, configFile, tmpPath, logConfig) } diff --git a/src/git.ts b/src/git.ts index 66b6bfd..77c8d14 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,5 +1,4 @@ import path from "node:path" -import os from "node:os" import fs from "node:fs/promises" import { log } from "./logger" import { AsyncResolver, LogConfig, LogLevel, ScaffoldCmdConfig, ScaffoldConfigMap } from "./types" @@ -9,15 +8,15 @@ import { resolve, wrapNoopResolver } from "./utils" export async function getGitConfig( url: URL, file: string, + tmpPath: string, logConfig: LogConfig, ): Promise> { const repoUrl = `${url.protocol}//${url.host}${url.pathname}` log(logConfig, LogLevel.info, `Cloning git repo ${repoUrl}`) - const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`) - return new Promise((res, reject) => { + log(logConfig, LogLevel.debug, `Cloning git repo to ${tmpPath}`) const clone = spawn("git", ["clone", "--recurse-submodules", "--depth", "1", repoUrl, tmpPath]) clone.on("error", reject) @@ -47,6 +46,7 @@ export async function loadGitConfig({ log(logConfig, LogLevel.info, `Loading config from git repo: ${repoUrl}`) const filename = file || (await findConfigFile(tmpPath)) const absolutePath = path.resolve(tmpPath, filename) + log(logConfig, LogLevel.debug, `Resolving config file: ${absolutePath}`) const loadedConfig = await resolve(async () => (await import(absolutePath)).default as ScaffoldConfigMap, logConfig) log(logConfig, LogLevel.info, `Loaded config from git`) @@ -58,7 +58,6 @@ export async function loadGitConfig({ templates: v.templates.map((t) => path.resolve(tmpPath, t)), } } - await fs.rm(tmpPath, { recursive: true }) return wrapNoopResolver(fixedConfig) } diff --git a/src/scaffold.ts b/src/scaffold.ts index baee730..c175d74 100644 --- a/src/scaffold.ts +++ b/src/scaffold.ts @@ -5,6 +5,8 @@ * See [readme](README.md) */ import path from "node:path" +import os from "node:os" + import { handleErr, resolve } from "./utils" import { isDir, @@ -128,8 +130,9 @@ Scaffold.fromConfig = async function ( config: pathOrUrl, ...config, } + const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`) const _overrides = resolve(overrides, _cmdConfig) - const _config = await parseConfigFile(_cmdConfig) + const _config = await parseConfigFile(_cmdConfig, tmpPath) return Scaffold({ ..._config, ..._overrides }) } diff --git a/src/types.ts b/src/types.ts index 9a751a1..d46781c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -393,7 +393,7 @@ export type LogConfig = Pick export type ConfigLoadConfig = LogConfig & Pick /** @internal */ -export type RemoteConfigLoadConfig = LogConfig & Pick +export type RemoteConfigLoadConfig = LogConfig & Pick & { tmpPath: string } /** @internal */ export type MinimalConfig = Pick