mirror of
https://github.com/chenasraf/simple-scaffold.git
synced 2026-05-17 17:28:09 +00:00
fix: rm tmp dir too early
This commit is contained in:
11
src/cmd.ts
11
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",
|
||||
|
||||
@@ -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<ScaffoldConfig> {
|
||||
export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string): Promise<ScaffoldConfig> {
|
||||
let output: ScaffoldConfig = config
|
||||
|
||||
if (config.quiet) {
|
||||
@@ -69,8 +71,9 @@ export async function parseConfigFile(config: ScaffoldCmdConfig): Promise<Scaffo
|
||||
const configPath = isGit ? config.git : configFilename
|
||||
|
||||
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 }))
|
||||
|
||||
// resolve the config
|
||||
@@ -123,7 +126,7 @@ export async function getLocalConfig(config: ConfigLoadConfig & Partial<LogConfi
|
||||
export async function getRemoteConfig(
|
||||
config: RemoteConfigLoadConfig & Partial<LogConfig>,
|
||||
): 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}`)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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<AsyncResolver<ScaffoldCmdConfig, ScaffoldConfigMap>> {
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
|
||||
@@ -393,7 +393,7 @@ export type LogConfig = Pick<ScaffoldConfig, "logLevel">
|
||||
export type ConfigLoadConfig = LogConfig & Pick<ScaffoldCmdConfig, "config">
|
||||
|
||||
/** @internal */
|
||||
export type RemoteConfigLoadConfig = LogConfig & Pick<ScaffoldCmdConfig, "config" | "git">
|
||||
export type RemoteConfigLoadConfig = LogConfig & Pick<ScaffoldCmdConfig, "config" | "git"> & { tmpPath: string }
|
||||
|
||||
/** @internal */
|
||||
export type MinimalConfig = Pick<ScaffoldCmdConfig, "name" | "key">
|
||||
|
||||
Reference in New Issue
Block a user