Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 1.15 KB

10-scaffold-out-files-and-projects-from-templates-in-a-node-js-cli.md

File metadata and controls

45 lines (31 loc) · 1.15 KB

10. Scaffold out Files and Projects from Templates in a Node.js CLI

Notes

The best library for this is copy-template-dir.

Given a source folder and a destination folder, copy from one to the other.

const copy = require('copy-template-dir')
const path = require('path')

const vars = { foo: 'bar' }
const inDir = path.join(process.cwd(), 'templates')
const outDir = path.join(process.cwd(), 'dist')

copy(inDir, outDir, vars, (err, createdFiles) => {
  if (err) throw err
  createdFiles.forEach(filePath => console.log(`Created ${filePath}`))
  console.log('done!')
})

// promise based alternative
const {promisify} = require('utils')
promisify(copy(inDir, outDir, vars))
  .then(() => console.log('done'))
  .catch(err => throw err)

Personal Take

  • Scaffolding is nice to have for your CLI.

Additional Resources

Other Express templating libraries:

As well as specific template flavors: