npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

factory

v1.0.5

Published

Quick and easy template scaffolding for Node

Downloads

188

Readme

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:

| Name | Type | Required | Default | Description | | ---- | ---- | -------- | ------- | ----------- | | template | string | Yes | N/A | Path to the template folder | | placeholders | Array | No | [] | Array of inquirer prompts used to gather data for injecting into templates | | getContext | function | No | null | Function that transforms placeholder values before they are passed to the template |

Notes:
  • getContext has the following signature:

    function(context)
    Arguments:

    | Name | Type | Description | | ---- | ---- | ----------- | | context | object | Key/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:

    | Name | Type | Required | Default | Description | | ---- | ---- | -------- | ------- | ----------- | | options.destination | string | Yes | N/A | Destination directory for output files | | options.overwrite | boolean | No | false | Whether to overwrite existing files | | context | object | No | {} | Preset template placeholder values | | callback | function | No | null | Node-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:

| Event | Handler signature | | ----- | ----------------- | | factory.events.ERROR | function(error, ErrorInfo) | | factory.events.COMPLETE | function(Array<CopyOperation>) | | factory.events.CREATE_DIRECTORY_START | function(CopyOperation) | | factory.events.CREATE_DIRECTORY_ERROR | function(error, CopyOperation) | | factory.events.CREATE_DIRECTORY_COMPLETE | function(CopyOperation) | | factory.events.CREATE_SYMLINK_START | function(CopyOperation) | | factory.events.CREATE_SYMLINK_ERROR | function(error, CopyOperation) | | factory.events.CREATE_SYMLINK_COMPLETE | function(CopyOperation) | | factory.events.COPY_FILE_START | function(CopyOperation) | | factory.events.COPY_FILE_ERROR | function(error, CopyOperation) | | factory.events.COPY_FILE_COMPLETE | function(CopyOperation) |

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

ErrorInfo

| Property | Type | Description | | -------- | ---- | ----------- | | src | string | Source path of the file/folder/symlink that failed to copy | | dest | string | Destination path of the file/folder/symlink that failed to copy |

CopyOperation

| Property | Type | Description | | -------- | ---- | ----------- | | src | string | Source path of the relevant file/folder/symlink | | dest | string | Destination path of the relevant file/folder/symlink | | stats | fs.Stats | Stats 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);
	});