Filesystem.template()

Read file at source path as a template, render with provided data, then write to destination path.

Uses Lodash template syntax (e.g. `<%= %>`, `<%- %>`, `<% %>`) Also provides access to the Node.js core utility module (as `util`), as well as Lodash itself (as `_`).

Usage

Hover over inputs/exits for more info.

var Filesystem = require('machinepack-fs');


// Read file at source path as a template, render with provided data, then write to destination path.

Filesystem.template({

source: '/Users/mikermcneil/.tmp/foo.tpl',

destination: '/Users/mikermcneil/.tmp/bar.md',

data: '===',

force: false,

}).exec({

// An unexpected error occurred.

error: function (err) {

 

},

// The source template file could not be found.

noTemplate: function () {

 

},

// One or more variables used in the template were not provided in the template data.

missingData: function (result) {

 

},

// Could not render the template due to invalid or unparseable syntax.

couldNotRender: function () {

 

},

// An existing file was found at the specified path (overwrite by enabling the `force` input).

alreadyExists: function () {

 

},

// OK.

success: function () {

 

},

});

Inputs

source required

The path on disk to the template file.

'/Users/mikermcneil/.tmp/foo.tpl'
destination required

The path on disk where the resulting file should be written.

'/Users/mikermcneil/.tmp/bar.md'
data

The data which will be accessible from the template.

'==='
force

Whether or not to overwrite existing file(s).

false

Exits

error

An unexpected error occurred.

noTemplate

The source template file could not be found.

missingData

One or more variables used in the template were not provided in the template data.

{ message: 'Some variables (`me`,`friends`) were used in template "/code/machine/docs/.type-table.tpl", but not provided in the template data dictionary.',
  missingVariables: [ 'me' ] }
couldNotRender

Could not render the template due to invalid or unparseable syntax.

alreadyExists

An existing file was found at the specified path (overwrite by enabling the `force` input).

success

OK.