API
Besides being used as a CLI tool, scaffdog provides a high-level API for Node.js.
See this page on using scaffdog programmatically.
Note: API documentation is currently not detailed. Check the source code for more information.
createScaffdog(options)
#
Factory function of a Scaffdog object. Usually initialised using the result of reading using the @scaffdog/config package.
Parameters:
options.filepath
:string
options.config
:ResolvedConfig
options.cwd?
:string
Return:
import { loadConfig } from '@scaffdog/config';import { createScaffdog } from 'scaffdog';const descriptor = loadConfig(project);const scaffdog = createScaffdog(descriptor);
loadScaffdog(path, options)
#
Finds the scaffdog project in the specified directory and returns a Scaffdog object.
If no configuration file is found and the scaffdog project is deemed invalid, a rejected Promise is returned.
Parameters:
path
:string
options.cwd?
:string
Return:
import { loadScaffdog } from 'scaffdog';const scaffdog = await loadScaffdog();
Scaffdog
#
An object that holds information about the scaffdog project and provides the central API for the scaffdog.
scaffdog.list()
#
Returns a list of documents corresponding to files in the configuration file.
Return:
import { ScaffdogAggregateError } from 'scaffdog';try {const documents = await scaffdog.list();const document = documents.find((d) => d.name === 'component');} catch (e) {if (e instanceof ScaffdogAggregateError) {console.error(e.message, e.errors);} else {console.error(e);}}
scaffdog.generate(document, output, options)
#
Specify the document object and output directory and compile each template. No file generation is actually performed.
Parameters:
document
:Document
output
:string
options.inputs?
:VariableRecord
|GenerateInputsResolver
Return:
import fs from 'node:fs/promises';const [document] = await scaffdog.list();// Using object inputsconst files = await scaffdog.generate(document, '/path/to/dist', {inputs: {name: 'FooComponent',},});for (const file of files) {if (file.skip) {continue;}await fs.writeFile(file.path, file.content);}// Using inputs factory// const files = await scaffdog.generate(document, '/path/to/dist', {// inputs: async (context) => {// return {// name: 'FooComponent',// };// },// });
Last edited on