Skip to content

Commit f961c13

Browse files
committed
feat: add github remote templates
1 parent 05487f4 commit f961c13

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/cmd.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { parseAppendData, parseConfig } from "./utils"
99

1010
export async function parseCliArgs(args = process.argv.slice(2)) {
1111
const pkg = JSON.parse((await fs.readFile(path.join(__dirname, "package.json"))).toString())
12-
const isConfig = args.includes("--config") || args.includes("-c")
12+
const isConfig = args.includes("--config") || args.includes("-c") || args.includes("--github") || args.includes("-gh")
1313

1414
return (
1515
massarg<ScaffoldCmdConfig>()
@@ -29,7 +29,13 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
2929
name: "config",
3030
aliases: ["c"],
3131
description:
32-
"Filename to load config from instead of passing arguments to CLI or using a Node.js script. You may pass a JSON or JS file, with a relative or absolute path, a URL to a repository, or a GitHub path (e.g. username/package). You may also optionally add a key (same as passing --key) to load from inside the config.",
32+
"Filename or https git URL to load config from instead of passing arguments to CLI or using a Node.js script. You may pass a JSON or JS file with a relative or absolute path",
33+
})
34+
.option({
35+
name: "github",
36+
aliases: ["gh"],
37+
description:
38+
"GitHub path to load config from instead of passing arguments to CLI or using a Node.js script. You may pass a JSON or JS file, with a relative or absolute path, a URL to a repository, or a GitHub path (e.g. username/package#scaffold.config.js). You may also optionally add a key (same as passing --key) to load from inside the config.",
3339
})
3440
.option({
3541
name: "key",

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ export interface ScaffoldCmdConfig {
338338
dryRun: boolean
339339
config?: string
340340
key?: string
341+
github?: string
341342
}
342343

343344
export type ScaffoldConfigFile = Record<string, ScaffoldConfig>

src/utils.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ function isWrappedWithQuotes(string: string): boolean {
409409
/** @internal */
410410
export async function parseConfig(config: ScaffoldCmdConfig & OptionsBase): Promise<ScaffoldConfig> {
411411
let c: ScaffoldConfig = config
412+
if (config.github) {
413+
log(config, LogLevel.Info, `Loading config from github ${config.github}`)
414+
const gitUrl = new URL(`https://github.com/${config.github}`)
415+
if (!gitUrl.pathname.endsWith(".git")) {
416+
gitUrl.pathname += ".git"
417+
}
418+
config.config = gitUrl.toString()
419+
}
412420

413421
if (config.config) {
414422
const isUrl = config.config.includes("://")
@@ -419,6 +427,7 @@ export async function parseConfig(config: ScaffoldCmdConfig & OptionsBase): Prom
419427
? [config.config.substring(0, colonIndex), config.config.substring(colonIndex + 1)]
420428
: [config.config, undefined]
421429
const key = (config.key ?? templateKey) || "default"
430+
log(config, LogLevel.Info, `Loading config from ${configFile} with key ${key}`)
422431
const configImport = await getConfig({ config: configFile, quiet: config.quiet, verbose: config.verbose })
423432
if (!configImport[key]) {
424433
throw new Error(`Template "${key}" not found in ${configFile}`)
@@ -461,15 +470,16 @@ export async function getConfig(
461470
const tmpPath = path.resolve(os.tmpdir(), `scaffold-config-${Date.now()}`)
462471

463472
return new Promise((resolve, reject) => {
464-
const clone = spawn("git", ["clone", repoUrl, tmpPath])
473+
const clone = spawn("git", ["clone", "--depth", "1", repoUrl, tmpPath])
465474

466475
clone.on("error", reject)
467476
clone.on("close", async (code) => {
468477
if (code === 0) {
469-
log(logConfig, LogLevel.Info, `Loading config from git repo: ${configFile}`)
470-
const absolutePath = path.resolve(tmpPath, url.hash.replace("#", ""))
478+
log(logConfig, LogLevel.Info, `Loading config from git repo: ${repoUrl}`)
479+
const hashPath = url.hash?.replace("#", "") || "scaffold.config.js"
480+
const absolutePath = path.resolve(tmpPath, hashPath)
471481
const loadedConfig = (await import(absolutePath)).default as ScaffoldConfigFile
472-
log(logConfig, LogLevel.Info, `Loaded config from git repo`)
482+
log(logConfig, LogLevel.Info, `Loaded config from git`)
473483
log(logConfig, LogLevel.Debug, `Raw config:`, loadedConfig)
474484
const fixedConfig: ScaffoldConfigFile = Object.fromEntries(
475485
Object.entries(loadedConfig).map(([k, v]) => [

0 commit comments

Comments
 (0)