feat: support providing name in config

This commit is contained in:
2024-02-13 01:10:41 +02:00
committed by Chen Asraf
parent e48b832e0b
commit 4e7ac34db9
4 changed files with 26 additions and 6 deletions

View File

@@ -45,6 +45,12 @@ module.exports = (config) => {
}
```
If you want to provide templates that need no name (such as common config files which are easily
portable between projects), you may provide the `name` property in the config object.
You will always be able to override it using `--name NewName`, but it will be given a value by
default and therefore it will no longer be required in the CLI arguments.
## Using a config file
Once your config is created, you can use it by providing the file name to the `--config` (or `-c`

View File

@@ -1,4 +1,5 @@
/** @type {import('simple-scaffold').ScaffoldConfigFile} */
// @ts-check
/** @type {import('./dist').ScaffoldConfigFile} */
module.exports = (conf) => {
console.log("Config:", conf)
return {
@@ -12,5 +13,10 @@ module.exports = (conf) => {
output: "examples/test-output/component",
data: { property: "myProp", value: "10" },
},
configs: {
templates: ["examples/test-input/**/.*"],
output: "examples/test-output/configs",
name: "---",
},
}
}

View File

@@ -33,6 +33,9 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
log(config, LogLevel.debug, "Parsing config file...", config)
const parsed = await parseConfigFile(config, tmpPath)
await Scaffold(parsed)
} catch (e) {
const message = "message" in (e as any) ? (e as any).message : e?.toString()
log(config, LogLevel.error, message)
} finally {
log(config, LogLevel.debug, "Cleaning up temporary files...", tmpPath)
await fs.rm(tmpPath, { recursive: true, force: true })
@@ -45,7 +48,7 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
"Name to be passed to the generated files. `{{name}}` and other data parameters inside " +
"contents and file names will be replaced accordingly. You may omit the `--name` or `-n` for this specific option.",
isDefault: true,
required: !isVersionFlag,
required: !isConfigProvided,
})
.option({
name: "config",

View File

@@ -81,6 +81,7 @@ export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string
// If the config is a function or promise, return the output
if (typeof configImport.default === "function" || configImport.default instanceof Promise) {
log(config, LogLevel.debug, "Config is a function or promise, resolving...")
configImport = await resolve(configImport.default, config)
}
@@ -88,19 +89,23 @@ export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string
throw new Error(`Template "${key}" not found in ${configFilename}`)
}
const importedKey = configImport[key]
const imported = configImport[key]
log(config, LogLevel.debug, "Imported result", imported)
output = {
...config,
...importedKey,
...imported,
data: {
...(importedKey as any).data,
...(imported as any).data,
...config.data,
},
}
}
output.data = { ...output.data, ...config.appendData }
delete config.appendData
if (!output.name) {
throw new Error("simple-scaffold: Missing required option: name")
}
log(output, LogLevel.debug, "Parsed config", output)
return output
}