#!/usr/bin/env node "use strict"; var mosaic = require('../lib/mosaic') var program = require('commander') var fs = require('fs') var path = require('path') var mkpath = require('mkpath') // Make all directories in a path, like mkdir -p program.parse(process.argv) var cwd = process.cwd() var mopath = program.args[0] var parts, family, moname, relativePath if (!mopath) { /** * If user doesn't pass any params to mo init * set the namespace and component name * using the last 2 directoy names */ parts = cwd.split(path.sep) family = parts[parts.length - 2] moname = parts[parts.length - 1] mopath = cwd relativePath = family + '/' + moname confirmArgs(function() { checkDuplication(generateFiles) }) } else { /** * Accepted user inputs: * mo init qin/featured * mo init featured * if split with '/' && got a second part * the first part acts as the namespace * else * the current directory name will be set to the namespace */ parts = mopath.split('/') family = parts[1] ? parts[0] : path.basename(cwd) moname = parts[1] ? parts[1] : parts[0] mopath = path.join(cwd, mopath) relativePath = family + '/' + moname // if(!parts[1]) { confirmArgs(function() { checkDuplication(generateFiles) }) // } // else checkDuplication(generateFiles) } function checkDuplication(callback) { var js = path.join(mopath, 'index.js') var pkg = path.join(mopath, 'package.json') var readme = path.join(mopath, 'README.md') var isFileExists = fs.existsSync(js) || fs.existsSync(pkg) || fs.existsSync(readme) if (isFileExists) { var question = "CAUTION!! File exists, overwrite? (y/n): " var handler = function(ok) { if (ok) { callback && callback() } else { process.exit(1) } } program.confirm(question, handler) } else { callback && callback() } } function checkFamilyPattern() { var FAMILY_PATTERN = /^\w+\.\w+(?:\.\w+)*$/ if (!family.match(FAMILY_PATTERN) && family !== 'mosaics' && family !== 'mo') { mosaic.error('Family does\'t match pattern: mosaics | part1.part2') family = '' return false } if (family === 'mo') { mosaic.warn('family', 'mo is a special family for testing purpose only. It won\'t be published to CDN.') } return true } function confirmArgs(callback) { if(!parts[1] || !family) { // user types: mo init [moname] // or family should be renamed program.prompt('Specify family (' + family + '): ', function(val) { if(val) { family = val mopath = path.join(cwd, family, moname) relativePath = family + '/' + moname } if(checkFamilyPattern()) callback && callback() else confirmArgs(callback) }) } else if(mopath === cwd) { // user types: mo init program.prompt('Specify family/name (' + family + '/' + moname + '): ', function(val) { if(val) { var split = val.split('/') family = split[0] || family moname = split[1] || moname mopath = path.join(cwd, family, moname) relativePath = family + '/' + moname } if(checkFamilyPattern()) callback && callback() else confirmArgs(callback) }) } else { // user types: mo init [family]/[moname] if(checkFamilyPattern()) callback && callback() else confirmArgs(callback) } } function generateFiles() { var pathJs = path.join(mopath, 'index.js') var pathPackage = path.join(mopath, 'package.json') var pathReadme var contentsOfJs = [ 'KISSY.add(\'' + relativePath + '/index\', function(S, Brick) {', '', ' return Brick', '', '}, {', ' requires: [\'brix/base\']', '})' ].join('\n') var contentsOfPkg = [ '{', ' "name": "' + relativePath + '",', ' "version": "0.0.1"', '}' ].join('\n') var contentsOfReadme if(!fs.existsSync(mopath)) mkpath.sync(mopath) fs.writeFileSync(pathJs, contentsOfJs) mosaic.log("create", pathJs) fs.writeFileSync(pathPackage, contentsOfPkg) mosaic.log("create", pathPackage) if (family === 'mosaics') { pathReadme = path.join(mopath, 'readme.md') contentsOfReadme = [ '# ' + moname, '', '## 用法', '', '```html', '
', '
', '```' ].join('\n') fs.writeFileSync(pathReadme, contentsOfReadme) mosaic.log("create", pathReadme) } mosaic.log("inited", mopath) process.exit(0) }