Skip to content

Commit 4e7ac34

Browse files
committed
feat: support providing name in config
1 parent e48b832 commit 4e7ac34

4 files changed

Lines changed: 26 additions & 6 deletions

File tree

docs/docs/usage/02-configuration_files.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ module.exports = (config) => {
4545
}
4646
```
4747

48+
If you want to provide templates that need no name (such as common config files which are easily
49+
portable between projects), you may provide the `name` property in the config object.
50+
51+
You will always be able to override it using `--name NewName`, but it will be given a value by
52+
default and therefore it will no longer be required in the CLI arguments.
53+
4854
## Using a config file
4955

5056
Once your config is created, you can use it by providing the file name to the `--config` (or `-c`

scaffold.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/** @type {import('simple-scaffold').ScaffoldConfigFile} */
1+
// @ts-check
2+
/** @type {import('./dist').ScaffoldConfigFile} */
23
module.exports = (conf) => {
34
console.log("Config:", conf)
45
return {
@@ -12,5 +13,10 @@ module.exports = (conf) => {
1213
output: "examples/test-output/component",
1314
data: { property: "myProp", value: "10" },
1415
},
16+
configs: {
17+
templates: ["examples/test-input/**/.*"],
18+
output: "examples/test-output/configs",
19+
name: "---",
20+
},
1521
}
1622
}

src/cmd.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
3333
log(config, LogLevel.debug, "Parsing config file...", config)
3434
const parsed = await parseConfigFile(config, tmpPath)
3535
await Scaffold(parsed)
36+
} catch (e) {
37+
const message = "message" in (e as any) ? (e as any).message : e?.toString()
38+
log(config, LogLevel.error, message)
3639
} finally {
3740
log(config, LogLevel.debug, "Cleaning up temporary files...", tmpPath)
3841
await fs.rm(tmpPath, { recursive: true, force: true })
@@ -45,7 +48,7 @@ export async function parseCliArgs(args = process.argv.slice(2)) {
4548
"Name to be passed to the generated files. `{{name}}` and other data parameters inside " +
4649
"contents and file names will be replaced accordingly. You may omit the `--name` or `-n` for this specific option.",
4750
isDefault: true,
48-
required: !isVersionFlag,
51+
required: !isConfigProvided,
4952
})
5053
.option({
5154
name: "config",

src/config.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,31 @@ export async function parseConfigFile(config: ScaffoldCmdConfig, tmpPath: string
8181

8282
// If the config is a function or promise, return the output
8383
if (typeof configImport.default === "function" || configImport.default instanceof Promise) {
84+
log(config, LogLevel.debug, "Config is a function or promise, resolving...")
8485
configImport = await resolve(configImport.default, config)
8586
}
8687

8788
if (!configImport[key]) {
8889
throw new Error(`Template "${key}" not found in ${configFilename}`)
8990
}
9091

91-
const importedKey = configImport[key]
92+
const imported = configImport[key]
93+
log(config, LogLevel.debug, "Imported result", imported)
9294
output = {
9395
...config,
94-
...importedKey,
96+
...imported,
9597
data: {
96-
...(importedKey as any).data,
98+
...(imported as any).data,
9799
...config.data,
98100
},
99101
}
100102
}
101103

102104
output.data = { ...output.data, ...config.appendData }
103-
delete config.appendData
105+
if (!output.name) {
106+
throw new Error("simple-scaffold: Missing required option: name")
107+
}
108+
log(output, LogLevel.debug, "Parsed config", output)
104109
return output
105110
}
106111

0 commit comments

Comments
 (0)