|
| 1 | +var copySkeleton, fs, init, logger, path, register, wrench; |
| 2 | + |
| 3 | +path = require('path'); |
| 4 | + |
| 5 | +fs = require('fs'); |
| 6 | + |
| 7 | +wrench = require('wrench'); |
| 8 | + |
| 9 | +logger = require('logmimosa'); |
| 10 | + |
| 11 | +init = function(name, opts) { |
| 12 | + var moduleDirPath; |
| 13 | + |
| 14 | + if (name == null) { |
| 15 | + return logger.error("Must provide a module name, ex: mimosa mod:init mimosa-moduleX"); |
| 16 | + } |
| 17 | + if (name.indexOf('mimosa-') !== 0) { |
| 18 | + return logger.error("Your Mimosa module name isn't prefixed with 'mimosa-'. To work properly with Mimosa, " + "modules must be prefixed with 'mimosa-', ex: 'mimosa-moduleX'."); |
| 19 | + } |
| 20 | + moduleDirPath = path.resolve(name); |
| 21 | + return fs.exists(moduleDirPath, function(exists) { |
| 22 | + if (exists) { |
| 23 | + logger.warn("Directory/file already exists at [[ " + moduleDirPath + " ]], will not overwrite it."); |
| 24 | + return process.exit(0); |
| 25 | + } else { |
| 26 | + return copySkeleton(name, opts.coffee, moduleDirPath); |
| 27 | + } |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +copySkeleton = function(name, isCoffee, moduleDirPath) { |
| 32 | + var lang, npmignore, packageJson, skeletonPath; |
| 33 | + |
| 34 | + lang = isCoffee ? "coffee" : 'js'; |
| 35 | + skeletonPath = path.join(__dirname, '..', '..', '..', 'skeletons', 'module', lang); |
| 36 | + wrench.copyDirSyncRecursive(skeletonPath, moduleDirPath, { |
| 37 | + excludeHiddenUnix: false |
| 38 | + }); |
| 39 | + npmignore = path.join(moduleDirPath, 'npmignore'); |
| 40 | + if (fs.existsSync(npmignore)) { |
| 41 | + fs.renameSync(npmignore, path.join(moduleDirPath, '.npmignore')); |
| 42 | + } |
| 43 | + packageJson = path.join(moduleDirPath, 'package.json'); |
| 44 | + return fs.readFile(packageJson, 'ascii', function(err, text) { |
| 45 | + text = text.replace('???', name); |
| 46 | + return fs.writeFile(packageJson, text, function(err) { |
| 47 | + var readme; |
| 48 | + |
| 49 | + readme = path.join(moduleDirPath, 'README.md'); |
| 50 | + return fs.readFile(readme, 'ascii', function(err, text) { |
| 51 | + text = text.replace(/\?\?\?/g, name); |
| 52 | + return fs.writeFile(readme, text, function(err) { |
| 53 | + logger.success(("Module skeleton successfully placed in " + name + " directory. The first thing you'll") + (" want to do is go into " + name + path.sep + "package.json and replace the placeholders.")); |
| 54 | + return process.exit(0); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | +}; |
| 60 | + |
| 61 | +register = function(program, callback) { |
| 62 | + var _this = this; |
| 63 | + |
| 64 | + return program.command('mod:init [name]').option("-D, --debug", "run in debug mode").option("-c, --coffee", "get a coffeescript version of the skeleton").description("create a Mimosa module skeleton in the named directory").action(callback).on('--help', function() { |
| 65 | + logger.green(' The mod:init command will create a directory for the name given, and place a starter'); |
| 66 | + logger.green(' module skeleton into the directory. If a directory for the name given already exists'); |
| 67 | + logger.green(' Mimosa will place the module skeleton inside of it.'); |
| 68 | + logger.blue('\n $ mimosa mod:init [nameOfModule]\n'); |
| 69 | + logger.green(' The default skeleton is written in JavaScript. If you want a skeleton delivered'); |
| 70 | + return logger.green(' in CoffeeScript add a \'coffee\' flag.'); |
| 71 | + }); |
| 72 | +}; |
| 73 | + |
| 74 | +module.exports = function(program) { |
| 75 | + return register(program, init); |
| 76 | +}; |
0 commit comments