Skip to content

Commit ce5adbe

Browse files
committed
feat: node.js function for remote configs
1 parent 9489f14 commit ce5adbe

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

pages/configuration_files.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,24 @@ This example is equivalent to the above, just shorter to write:
132132
```shell
133133
simple-scaffold -c chenasraf/simple-scaffold#examples/test-input/scaffold.config.js:component
134134
```
135+
136+
## Use In Node.js
137+
138+
You can also start a scaffold from Node.js with a remote file or URL config.
139+
140+
Just use the `Scaffold.fromConfig` function:
141+
142+
```ts
143+
Scaffold.fromConfig(
144+
"scaffold.config.js", // file or HTTPS git URL
145+
{
146+
// name of the generated component
147+
name: "My Component",
148+
// key to load from the config
149+
key: "component",
150+
},
151+
{
152+
// other config overrides
153+
},
154+
)
155+
```

src/scaffold.ts

100644100755
Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import {
2323
getTemplateFileInfo,
2424
logInitStep,
2525
logInputFile,
26+
parseConfig,
2627
} from "./utils"
27-
import { LogLevel, ScaffoldConfig } from "./types"
28+
import { LogLevel, ScaffoldCmdConfig, ScaffoldConfig } from "./types"
29+
import { OptionsBase } from "massarg/types"
2830

2931
/**
3032
* Create a scaffold using given `options`.
@@ -50,6 +52,7 @@ import { LogLevel, ScaffoldConfig } from "./types"
5052
* For available default values, see {@link DefaultHelpers}.
5153
*
5254
* @param {ScaffoldConfig} config The main configuration object
55+
* @return {Promise<void>} A promise that resolves when the scaffold is complete
5356
*
5457
* @see {@link DefaultHelpers}
5558
* @see {@link CaseHelpers}
@@ -101,6 +104,40 @@ export async function Scaffold(config: ScaffoldConfig): Promise<void> {
101104
throw e
102105
}
103106
}
107+
108+
/**
109+
* Create a scaffold based on a config file or URL.
110+
*
111+
* @param {string} pathOrUrl The path or URL to the config file
112+
* @param {Record<string, string>} config Information needed before loading the config
113+
* @param {Partial<Omit<ScaffoldConfig, 'name'>>} overrides Any overrides to the loaded config
114+
*
115+
* @see {@link Scaffold}
116+
* @category Main
117+
* @return {Promise<void>} A promise that resolves when the scaffold is complete
118+
*/
119+
Scaffold.fromConfig = async function (
120+
pathOrUrl: string,
121+
config: Pick<ScaffoldCmdConfig, "name" | "key">,
122+
overrides?: Partial<Omit<ScaffoldConfig, "name">>,
123+
): Promise<void> {
124+
const _cmdConfig: ScaffoldCmdConfig & OptionsBase = {
125+
dryRun: false,
126+
output: process.cwd(),
127+
verbose: LogLevel.Info,
128+
overwrite: false,
129+
templates: [],
130+
createSubFolder: false,
131+
quiet: false,
132+
help: false,
133+
extras: [],
134+
config: pathOrUrl,
135+
...config,
136+
}
137+
const _config = await parseConfig(_cmdConfig)
138+
return Scaffold({ ..._config, ...overrides })
139+
}
140+
104141
async function handleTemplateFile(
105142
config: ScaffoldConfig,
106143
{ templatePath, basePath }: { templatePath: string; basePath: string },

0 commit comments

Comments
 (0)