Socket
Socket
Sign inDemoInstall

factory

Package Overview
Dependencies
8
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    factory

Quick and easy template scaffolding for Node


Version published
Weekly downloads
137
increased by7.87%
Maintainers
1
Install size
7.65 MB
Created
Weekly downloads
 

Readme

Source

Factory

npm version Stability Build Status

Quick and easy template scaffolding for Node

Installation

npm install factory

Example

var factory = require('factory');

var widgetFactory = factory({
	template: 'templates/widget',
	placeholders: [
		{
			name: 'foo',
			type: 'input',
			message: 'Enter a value for foo'
		},
		{
			name: 'bar',
			type: 'input',
			message: 'Enter a value for bar'
		}
	]
});

var options = {
	destination: 'app/widgets',
	overwrite: true,
};
var context = {
	foo: 'baz'
};

// Node-style callback interface
widgetFactory(options, context, function(error, results) {
	if (error) {
		console.error('Widget creation failed: ' + error);
	} else {
		console.info('Widget created successfully');
	}
});

// Promise interface
widgetFactory(options, context)
	.then(function(results) {
		console.info('Widget created successfully');
	}).catch(function(error) {
		console.error('Widget creation failed: ' + error);
	});

Contents of templates/widget/<%= foo %>.js:

module.exports = function <%= foo %>() {
	console.log('<%= bar %>');
};

Output at app/widgets/baz.js:

module.exports = function baz() {
	console.log('[user-prompted value]');
};

How it works

  • Call the factory() function with the following parameters:
    • template: path to the template folder
    • placeholders: (optional) array of inquirer prompts used to gather data for injecting into templates
    • getContext: (optional) function that transforms placeholder values before they are passed to the template
  • You can then call the function that is returned, specifying a destination path and any copy options, and optionally passing in a key/value object containing template placeholder values
  • The user is prompted for the value of any placeholders which were not provided in the placeholder values object
  • The files are copied from the template folder to the destination folder, replacing any placeholders in filenames and file content with the supplied values (using lodash template syntax)

Usage

factory(options)

Create a factory from an existing template

Template filenames/contents can use lodash template syntax to specify placeholder values. These are injected into the template when the factory function is invoked.

Options:
NameTypeRequiredDefaultDescription
templatestringYesN/APath to the template folder
placeholdersArrayNo[]Array of inquirer prompts used to gather data for injecting into templates
getContextfunctionNonullFunction that transforms placeholder values before they are passed to the template
Notes:
  • getContext has the following signature:

    function(context)
    Arguments:
    NameTypeDescription
    contextobjectKey/value object containing placeholder values, gathered from factory context and template placeholders
    Returns:

    object Key/value object containing transformed context placeholder for use in templates

Returns:
  • function(options, [context], [callback])

    Factory function used to create instances of the template

    The user will be prompted for the value of any placeholders which are not specified in the context object.

    Options:
    NameTypeRequiredDefaultDescription
    options.destinationstringYesN/ADestination directory for output files
    options.overwritebooleanNofalseWhether to overwrite existing files
    contextobjectNo{}Preset template placeholder values
    callbackfunctionNonullNode-style callback that is invoked when the operation completes/fails
    Returns:

    Promise<Array> Promise, fulfilled with array of copy results:

    [
    	{
    		"src": "/path/to/src",
    		"dest": "/path/to/dest",
    		"stats": <Stats>
    	},
    	{
    		"src": "/path/to/src/file.txt",
    		"dest": "/path/to/dest/file.txt",
    		"stats": <Stats>
    	},
    	{
    		"src": "/path/to/src/subfolder",
    		"dest": "/path/to/dest/subfolder",
    		"stats": <Stats>
    	},
    	{
    		"src": "/path/to/src/subfolder/nested.txt",
    		"dest": "/path/to/dest/subfolder/nested.txt",
    		"stats": <Stats>
    	}
    ]
    

Events

The value returned by the generated factory function implements the EventEmitter interface, and emits the following events:

EventHandler signature
factory.events.ERRORfunction(error, ErrorInfo)
factory.events.COMPLETEfunction(Array<CopyOperation>)
factory.events.CREATE_DIRECTORY_STARTfunction(CopyOperation)
factory.events.CREATE_DIRECTORY_ERRORfunction(error, CopyOperation)
factory.events.CREATE_DIRECTORY_COMPLETEfunction(CopyOperation)
factory.events.CREATE_SYMLINK_STARTfunction(CopyOperation)
factory.events.CREATE_SYMLINK_ERRORfunction(error, CopyOperation)
factory.events.CREATE_SYMLINK_COMPLETEfunction(CopyOperation)
factory.events.COPY_FILE_STARTfunction(CopyOperation)
factory.events.COPY_FILE_ERRORfunction(error, CopyOperation)
factory.events.COPY_FILE_COMPLETEfunction(CopyOperation)

...where the types referred to in the handler signature are as follows:

ErrorInfo

PropertyTypeDescription
srcstringSource path of the file/folder/symlink that failed to copy
deststringDestination path of the file/folder/symlink that failed to copy

CopyOperation

PropertyTypeDescription
srcstringSource path of the relevant file/folder/symlink
deststringDestination path of the relevant file/folder/symlink
stats fs.StatsStats for the relevant file/folder/symlink

Example: using events

var factory = require('factory');

var widgetFactory = factory({
	template: 'templates/widget'
});

var options = {
	destination: 'app/widgets'
};
var context = {};
widgetFactory(options, context)
	.on(factory.events.COPY_FILE_START, function(copyOperation) {
		console.info('Copying file ' + copyOperation.src + '...');
	})
	.on(factory.events.COPY_FILE_COMPLETE, function(copyOperation) {
		console.info('Copied to ' + copyOperation.dest);
	})
	.on(factory.events.ERROR, function(error, copyOperation) {
		console.error('Unable to copy ' + copyOperation.dest);
	})
	.then(function(results) {
		console.info('Widget created successfully');
	}).catch(function(error) {
		console.error('Widget creation failed: ' + error);
	});

Keywords

FAQs

Last updated on 08 Jan 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc