feat: node.js function for remote configs

This commit is contained in:
Chen Asraf
2023-05-04 21:50:08 +03:00
parent d0c217adbe
commit 7e9022f433
2 changed files with 59 additions and 1 deletions

View File

@@ -132,3 +132,24 @@ This example is equivalent to the above, just shorter to write:
```shell ```shell
simple-scaffold -c chenasraf/simple-scaffold#examples/test-input/scaffold.config.js:component 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
},
)
```

39
src/scaffold.ts Normal file → Executable file
View File

@@ -23,8 +23,10 @@ import {
getTemplateFileInfo, getTemplateFileInfo,
logInitStep, logInitStep,
logInputFile, logInputFile,
parseConfig,
} from "./utils" } from "./utils"
import { LogLevel, ScaffoldConfig } from "./types" import { LogLevel, ScaffoldCmdConfig, ScaffoldConfig } from "./types"
import { OptionsBase } from "massarg/types"
/** /**
* Create a scaffold using given `options`. * Create a scaffold using given `options`.
@@ -50,6 +52,7 @@ import { LogLevel, ScaffoldConfig } from "./types"
* For available default values, see {@link DefaultHelpers}. * For available default values, see {@link DefaultHelpers}.
* *
* @param {ScaffoldConfig} config The main configuration object * @param {ScaffoldConfig} config The main configuration object
* @return {Promise<void>} A promise that resolves when the scaffold is complete
* *
* @see {@link DefaultHelpers} * @see {@link DefaultHelpers}
* @see {@link CaseHelpers} * @see {@link CaseHelpers}
@@ -101,6 +104,40 @@ export async function Scaffold(config: ScaffoldConfig): Promise<void> {
throw e 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<string, string>} config Information needed before loading the config
* @param {Partial<Omit<ScaffoldConfig, 'name'>>} overrides Any overrides to the loaded config
*
* @see {@link Scaffold}
* @category Main
* @return {Promise<void>} A promise that resolves when the scaffold is complete
*/
Scaffold.fromConfig = async function (
pathOrUrl: string,
config: Pick<ScaffoldCmdConfig, "name" | "key">,
overrides?: Partial<Omit<ScaffoldConfig, "name">>,
): Promise<void> {
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( async function handleTemplateFile(
config: ScaffoldConfig, config: ScaffoldConfig,
{ templatePath, basePath }: { templatePath: string; basePath: string }, { templatePath, basePath }: { templatePath: string; basePath: string },