From 4e7ac34db9bf67d012bbd1c06c1a26bc5ac93559 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Tue, 13 Feb 2024 01:10:41 +0200 Subject: [PATCH] feat: support providing name in config --- docs/docs/usage/02-configuration_files.md | 6 ++++++ scaffold.config.js | 8 +++++++- src/cmd.ts | 5 ++++- src/config.ts | 13 +++++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/docs/docs/usage/02-configuration_files.md b/docs/docs/usage/02-configuration_files.md index 227d005..04ed7f0 100644 --- a/docs/docs/usage/02-configuration_files.md +++ b/docs/docs/usage/02-configuration_files.md @@ -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` diff --git a/scaffold.config.js b/scaffold.config.js index a0c2040..5d34cc1 100644 --- a/scaffold.config.js +++ b/scaffold.config.js @@ -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: "---", + }, } } diff --git a/src/cmd.ts b/src/cmd.ts index 9710d19..926afa5 100644 --- a/src/cmd.ts +++ b/src/cmd.ts @@ -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", diff --git a/src/config.ts b/src/config.ts index b9217dc..4cbd0e3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 }