Skip to content

Using Scaffe

Sanket Chaudhari edited this page Nov 4, 2021 · 6 revisions

Generate project from a template

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 to package.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"

Passing Variables

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)

Dot files/folders

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
})

Add/Ignore files

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 or ignore
  • 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

Ignore files

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

Catching Errors

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) }

NOTE

  • All source paths in add and ignore should be relative to templateDir
  • All target paths in add and ignore should be relative to outDir
  • Adding a file, using add, which already exists in the template or had been previously added by add 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")