diff --git a/pages/configuration_files.md b/pages/configuration_files.md index fde6918..c22950b 100644 --- a/pages/configuration_files.md +++ b/pages/configuration_files.md @@ -132,3 +132,24 @@ This example is equivalent to the above, just shorter to write: ```shell simple-scaffold -c chenasraf/simple-scaffold#examples/test-input/scaffold.config.js:component ``` + +## Use In Node.js + +You can also start a scaffold from Node.js with a remote file or URL config. + +Just use the `Scaffold.fromConfig` function: + +```ts +Scaffold.fromConfig( + "scaffold.config.js", // file or HTTPS git URL + { + // name of the generated component + name: "My Component", + // key to load from the config + key: "component", + }, + { + // other config overrides + }, +) +``` diff --git a/src/scaffold.ts b/src/scaffold.ts old mode 100644 new mode 100755 index 7385c04..12b1c2e --- a/src/scaffold.ts +++ b/src/scaffold.ts @@ -23,8 +23,10 @@ import { getTemplateFileInfo, logInitStep, logInputFile, + parseConfig, } from "./utils" -import { LogLevel, ScaffoldConfig } from "./types" +import { LogLevel, ScaffoldCmdConfig, ScaffoldConfig } from "./types" +import { OptionsBase } from "massarg/types" /** * Create a scaffold using given `options`. @@ -50,6 +52,7 @@ import { LogLevel, ScaffoldConfig } from "./types" * For available default values, see {@link DefaultHelpers}. * * @param {ScaffoldConfig} config The main configuration object + * @return {Promise} A promise that resolves when the scaffold is complete * * @see {@link DefaultHelpers} * @see {@link CaseHelpers} @@ -101,6 +104,40 @@ export async function Scaffold(config: ScaffoldConfig): Promise { throw e } } + +/** + * Create a scaffold based on a config file or URL. + * + * @param {string} pathOrUrl The path or URL to the config file + * @param {Record} config Information needed before loading the config + * @param {Partial>} overrides Any overrides to the loaded config + * + * @see {@link Scaffold} + * @category Main + * @return {Promise} A promise that resolves when the scaffold is complete + */ +Scaffold.fromConfig = async function ( + pathOrUrl: string, + config: Pick, + overrides?: Partial>, +): Promise { + const _cmdConfig: ScaffoldCmdConfig & OptionsBase = { + dryRun: false, + output: process.cwd(), + verbose: LogLevel.Info, + overwrite: false, + templates: [], + createSubFolder: false, + quiet: false, + help: false, + extras: [], + config: pathOrUrl, + ...config, + } + const _config = await parseConfig(_cmdConfig) + return Scaffold({ ..._config, ...overrides }) +} + async function handleTemplateFile( config: ScaffoldConfig, { templatePath, basePath }: { templatePath: string; basePath: string },