UNPKG

spoonjs

Version:
97 lines (84 loc) 2.87 kB
/*jshint node:true, es5:true*/ 'use strict'; var path = require('path'); var utils = require('mout'); var fs = require('fs'); module.exports = function (task) { task .id('spoon-module-create') .name('SpoonJS module create') .author('Indigo United') .description('Create module') .option('name', 'The name of the module') .option('force', 'Force the creation of the module, even if it already exists', false) .setup(function (opts, ctx, next) { // Get the location in which the the module will be created var cwd = path.normalize(process.cwd()), location = path.dirname(opts.name); // Extract only the basename opts.name = path.basename(opts.name, '.js'); // Validate name if (/[^a-z0-9_\-\.]/i.test(opts.name)) { return next(new Error('"' + opts.name + '" contains unallowed chars')); } // Generate suitable names opts.name = utils.string.pascalCase(opts.name.replace(/_/g, '-')); opts.underscoredName = utils.string.underscore(opts.name); opts.hyphenatedName = utils.string.hyphenate(opts.name); if (location === '.') { return next(new Error('Please specify a folder for the module (e.g. src/' + opts.name + ')')); } opts.dir = path.join(cwd, location, opts.name); opts.__dirname = __dirname; // Check if module already exists if (!opts.force) { fs.stat(opts.dir, function (err) { if (!err || err.code !== 'ENOENT') { return next(new Error('"' + opts.name + '" already exists')); } return next(); }); } else { next(); } }) .do('cp', { description: 'Copy the structure of the module', options: { files: { '{{__dirname}}/module_structure/**/*' : '{{dir}}' }, glob: { dot: true } } }) .do('scaffolding-file-rename', { description: 'Rename files based on the name of the module', options: { files: '{{dir}}/**/*', data: { name: '{{name}}', underscoredName: '{{underscoredName}}', hyphenatedName: '{{hyphenatedName}}' } } }) .do('scaffolding-replace', { description: 'Set up files', options: { files: '{{dir}}/**/*.+(css|html|js)', data: { name: '{{name}}', underscoredName: '{{underscoredName}}', hyphenatedName: '{{hyphenatedName}}' } } }) .do('rm', { description: 'Cleanup dummy files', options: { files: '{{dir}}/**/.gitkeep' } }); };