fix: rm tmp dir too early

This commit is contained in:
2024-02-02 02:35:24 +02:00
committed by Chen Asraf
parent f748125ae6
commit 4aa52c84bd
5 changed files with 25 additions and 13 deletions

View File

@@ -1,4 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
import os from "node:os"
import { massarg } from "massarg" import { massarg } from "massarg"
import chalk from "chalk" import chalk from "chalk"
import { LogLevel, ScaffoldCmdConfig } from "./types" import { LogLevel, ScaffoldCmdConfig } from "./types"
@@ -19,8 +21,13 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
description: pkg.description, description: pkg.description,
}) })
.main(async (config) => { .main(async (config) => {
const parsed = await parseConfigFile(config) const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`)
return Scaffold(parsed) const parsed = await parseConfigFile(config, tmpPath)
try {
return Scaffold(parsed)
} finally {
await fs.rm(tmpPath, { recursive: true, force: true })
}
}) })
.option({ .option({
name: "name", name: "name",

View File

@@ -1,4 +1,6 @@
import path from "node:path" import path from "node:path"
import os from "node:os"
import fs from "node:fs/promises"
import { import {
ConfigLoadConfig, ConfigLoadConfig,
FileResponse, FileResponse,
@@ -48,7 +50,7 @@ function isWrappedWithQuotes(string: string): boolean {
} }
/** @internal */ /** @internal */
export async function parseConfigFile(config: ScaffoldCmdConfig): Promise<ScaffoldConfig> { export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string): Promise<ScaffoldConfig> {
let output: ScaffoldConfig = config let output: ScaffoldConfig = config
if (config.quiet) { if (config.quiet) {
@@ -69,8 +71,9 @@ export async function parseConfigFile(config: ScaffoldCmdConfig): Promise<Scaffo
const configPath = isGit ? config.git : configFilename const configPath = isGit ? config.git : configFilename
log(config, LogLevel.info, `Loading config from ${configFilename} with key ${key}`) log(config, LogLevel.info, `Loading config from ${configFilename} with key ${key}`)
const configPromise = await (config.git
? getRemoteConfig({ git: configPath, config: configFilename, logLevel: config.logLevel }) const configPromise = await (isGit
? getRemoteConfig({ git: configPath, config: configFilename, logLevel: config.logLevel, tmpPath })
: getLocalConfig({ config: configFilename, logLevel: config.logLevel })) : getLocalConfig({ config: configFilename, logLevel: config.logLevel }))
// resolve the config // resolve the config
@@ -123,7 +126,7 @@ export async function getLocalConfig(config: ConfigLoadConfig & Partial<LogConfi
export async function getRemoteConfig( export async function getRemoteConfig(
config: RemoteConfigLoadConfig & Partial<LogConfig>, config: RemoteConfigLoadConfig & Partial<LogConfig>,
): Promise<ScaffoldConfigFile> { ): Promise<ScaffoldConfigFile> {
const { config: configFile, git, ...logConfig } = config as Required<typeof config> const { config: configFile, git, tmpPath, ...logConfig } = config as Required<typeof config>
log(logConfig, LogLevel.info, `Loading config from remote ${git}, file ${configFile}`) 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}`) throw new Error(`Unsupported protocol ${url.protocol}`)
} }
return getGitConfig(url, configFile, logConfig) return getGitConfig(url, configFile, tmpPath, logConfig)
} }

View File

@@ -1,5 +1,4 @@
import path from "node:path" import path from "node:path"
import os from "node:os"
import fs from "node:fs/promises" import fs from "node:fs/promises"
import { log } from "./logger" import { log } from "./logger"
import { AsyncResolver, LogConfig, LogLevel, ScaffoldCmdConfig, ScaffoldConfigMap } from "./types" import { AsyncResolver, LogConfig, LogLevel, ScaffoldCmdConfig, ScaffoldConfigMap } from "./types"
@@ -9,15 +8,15 @@ import { resolve, wrapNoopResolver } from "./utils"
export async function getGitConfig( export async function getGitConfig(
url: URL, url: URL,
file: string, file: string,
tmpPath: string,
logConfig: LogConfig, logConfig: LogConfig,
): Promise<AsyncResolver<ScaffoldCmdConfig, ScaffoldConfigMap>> { ): Promise<AsyncResolver<ScaffoldCmdConfig, ScaffoldConfigMap>> {
const repoUrl = `${url.protocol}//${url.host}${url.pathname}` const repoUrl = `${url.protocol}//${url.host}${url.pathname}`
log(logConfig, LogLevel.info, `Cloning git repo ${repoUrl}`) log(logConfig, LogLevel.info, `Cloning git repo ${repoUrl}`)
const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`)
return new Promise((res, reject) => { 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]) const clone = spawn("git", ["clone", "--recurse-submodules", "--depth", "1", repoUrl, tmpPath])
clone.on("error", reject) clone.on("error", reject)
@@ -47,6 +46,7 @@ export async function loadGitConfig({
log(logConfig, LogLevel.info, `Loading config from git repo: ${repoUrl}`) log(logConfig, LogLevel.info, `Loading config from git repo: ${repoUrl}`)
const filename = file || (await findConfigFile(tmpPath)) const filename = file || (await findConfigFile(tmpPath))
const absolutePath = path.resolve(tmpPath, filename) 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) const loadedConfig = await resolve(async () => (await import(absolutePath)).default as ScaffoldConfigMap, logConfig)
log(logConfig, LogLevel.info, `Loaded config from git`) 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)), templates: v.templates.map((t) => path.resolve(tmpPath, t)),
} }
} }
await fs.rm(tmpPath, { recursive: true })
return wrapNoopResolver(fixedConfig) return wrapNoopResolver(fixedConfig)
} }

View File

@@ -5,6 +5,8 @@
* See [readme](README.md) * See [readme](README.md)
*/ */
import path from "node:path" import path from "node:path"
import os from "node:os"
import { handleErr, resolve } from "./utils" import { handleErr, resolve } from "./utils"
import { import {
isDir, isDir,
@@ -128,8 +130,9 @@ Scaffold.fromConfig = async function (
config: pathOrUrl, config: pathOrUrl,
...config, ...config,
} }
const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`)
const _overrides = resolve(overrides, _cmdConfig) const _overrides = resolve(overrides, _cmdConfig)
const _config = await parseConfigFile(_cmdConfig) const _config = await parseConfigFile(_cmdConfig, tmpPath)
return Scaffold({ ..._config, ..._overrides }) return Scaffold({ ..._config, ..._overrides })
} }

View File

@@ -393,7 +393,7 @@ export type LogConfig = Pick<ScaffoldConfig, "logLevel">
export type ConfigLoadConfig = LogConfig & Pick<ScaffoldCmdConfig, "config"> export type ConfigLoadConfig = LogConfig & Pick<ScaffoldCmdConfig, "config">
/** @internal */ /** @internal */
export type RemoteConfigLoadConfig = LogConfig & Pick<ScaffoldCmdConfig, "config" | "git"> export type RemoteConfigLoadConfig = LogConfig & Pick<ScaffoldCmdConfig, "config" | "git"> & { tmpPath: string }
/** @internal */ /** @internal */
export type MinimalConfig = Pick<ScaffoldCmdConfig, "name" | "key"> export type MinimalConfig = Pick<ScaffoldCmdConfig, "name" | "key">