-
Notifications
You must be signed in to change notification settings - Fork 3
Using Scaffe
The simplest way to generate a project using scaffe is to use scaffe.generate
with src
and target
directory. This will be equivalent to just copying and
pasting templateDir
folder to outDir
.
await scaffe.generate(templateDir, outDir)
The template directory could have two types of files.
- That needs to be evaluated as EJS files (filename prefixed by
_
, e.g._package.json
will be copied topackage.json
with ejs-evaluated content) - That just needs to be copied exactly (normal file which will be copied as it is to output directory)
_package.json
{
"name": "<%= name %>"
}
will be copied as
package.json
{
"name": "app"
}
where name = "app"
scaffe.generate
function takes 3 arguments. 3rd argument is the config
, which is an object
where we can pass our variables
which will be used by EJS and overwrite
boolean
flag (default: false) to tell whether we want to overwrite current directory or not.
const config = {
variables: { name: "app" },
overwrite: true,
}
await scaffe.generate(templateDir, outDir, config)
By default, scaffe will ignore dot files & folders. You'll have to pass { dot: true }
to make
scaffe take dotfiles into consideration
generator("template", "app", {
variables: {
version: "0.1.0",
},
dot: true
})
We can explicitly add files to the output project even if they aren't a part of the current template folder.
- Source path also supports glob pattern, scaffe uses fast-glob behind the scene to track files
- Target path should end with "/" if trying to infer a directory when using
add
orignore
-
add
function will not preserve the folder hierarchy
const config = {
variables: { name: "app" },
overwrite: true,
}
await scaffe.generate(templateDir, outDir, config)
.add("../common/assets/**/*", "assets/");
// OR
const s = scaffe.generate(templateDir, outDir, config)
if(addCommonAssets){
// In this case, we appended target path with "/" to show that we want all the files tracked by
// src glob string in assets folder
// if we choose to not end with "/" then this will dump content of any 1 file to "assets" file
s.add("../common/assets/**/*", "assets/")
}
await s
const config = {
variables: { name: "app" },
overwrite: true,
}
await scaffe.generate(templateDir, outDir, config)
.ignore("docs/**/*")
// OR
const s = scaffe.generate(templateDir, outDir, config)
if(dontAddDocs){
s.ignore("docs/**/*")
}
await s
The ideal way to catch errors in scaffe is to use try-catch clause.
const s = scaffe.generate(templateDir, outDir, config)
s.ignore("docs/**/*")
try { await s } catch(err) { console.log(err) }
- All source paths in
add
andignore
should be relative totemplateDir
- All target paths in
add
andignore
should be relative tooutDir
- Adding a file, using
add
, which already exists in the template or had been previously added byadd
will get overridden
scaffe.generate(templateDir, outDir, { overwrite: true, variables: { name: "app" })
.add("../common/assets/logo.png", "assets/logo.png") // this will get ignored
.add("../common/assets/logo.png", "assets/icon.png")