kopy

Gracefully copy a directory and render templates.
Why is this useful?
This could be used to build a scaffolding tool like yeoman or vue-cli.
Install
$ npm install --save kopy
Usage
const copy = require('kopy')
copy('./template', './dest', {
data: {
foo: 'bar'
}
}).then(({files}) => {
console.log(files)
}).catch(err => {
console.log(err.stack)
})
Template Syntax
Templates could use lodash.template syntax or any template engine supported by jstransformer
API
copy(src, dest, options)
Returns a Promise which resolve {files, data, answers, merged}
, merged
is the result of answers of answers
merged with data
.
src
Type: string
Required: true
Source directory. Could be a relative or absolute path.
dest
Type: string
Required: true
Destination directory.
options
template
Type: object
Default: require('jstransformer-ejs')
You can use a custom template engine, like [handlebars]:
copy(src, dest, {
template: require('jstransformer-handlebars')
})
templateOptions
Type: object
The template engine options.
clean
Type: boolean
Default: false
Whether to clean destination directory before writing to it.
cwd
Type: string
Default: process.cwd()
Current working directory.
data
Type: object
Default: undefined
The data to render templates in source directory.
prompts
Type: Array<InquirerPrompt>
Default: undefined
inquirer prompts, the answers of prompts will be assigned to data
skipInterpolation
Type: string | Array<string> | function
Default: undefined
(we skip all binary files by default)
Patterns(minimatch) used to skip interpolation, eg: ./foo*/bar-*.js
It could also be a function, whose first arg is file path and second arg is file content, eg. we want to exclude all .js
files:
copy(src, dest, {
skipInterpolation(file, content) {
return /\.js$/.test(file)
}
})
disableInterpolation
Type: boolean
Default: false
Similar to skipInterpolation
, but disableInterpolation
disables all template interpolation, template markup will remain the way it is.
filters
Type: object
Default: undefined
An object containing file filter rules, the key of each entry is a minimatch pattern, and its value is a JavaScript expression evaluated in the context of (prompt answers) data:
copy(src, dest, {
filters: {
'**/*.js': 'useJavaScript',
'**/*.ts': '!useJavaScript'
}
})
skipExisting
Type: function
boolean
Default: undefined
Whether to skip existing file, it could be function that takes the path to existing file as argument.
copy(src, dest, {
skipExisting(file) {
console.log(`${file} exists, skipped!`)
}
})
kopy © EGOIST, Released under the MIT License.
Authored and maintained by EGOIST with help from contributors (list).
egoistian.com · GitHub @egoist · Twitter @rem_rin_rin