UNPKG

4.52 kBPlain TextView Raw
1#!/usr/bin/env node
2
3"use strict";
4
5var mosaic = require('../lib/mosaic')
6var program = require('commander')
7var fs = require('fs')
8var path = require('path')
9var mkpath = require('mkpath') // Make all directories in a path, like mkdir -p
10
11program.parse(process.argv)
12
13var cwd = process.cwd()
14var mopath = program.args[0]
15var parts, family, moname, relativePath
16
17if (!mopath) {
18 /**
19 * If user doesn't pass any params to mo init
20 * set the namespace and component name
21 * using the last 2 directoy names
22 */
23 parts = cwd.split(path.sep)
24 family = parts[parts.length - 2]
25 moname = parts[parts.length - 1]
26
27 mopath = cwd
28 relativePath = family + '/' + moname
29
30 confirmArgs(function() {
31 checkDuplication(generateFiles)
32 })
33} else {
34 /**
35 * Accepted user inputs:
36 * mo init qin/featured
37 * mo init featured
38 * if split with '/' && got a second part
39 * the first part acts as the namespace
40 * else
41 * the current directory name will be set to the namespace
42 */
43 parts = mopath.split('/')
44 family = parts[1] ? parts[0] : path.basename(cwd)
45 moname = parts[1] ? parts[1] : parts[0]
46
47 mopath = path.join(cwd, mopath)
48 relativePath = family + '/' + moname
49
50 // if(!parts[1]) {
51 confirmArgs(function() {
52 checkDuplication(generateFiles)
53 })
54 // }
55 // else checkDuplication(generateFiles)
56}
57
58function checkDuplication(callback) {
59 var js = path.join(mopath, 'index.js')
60 var pkg = path.join(mopath, 'package.json')
61 var readme = path.join(mopath, 'README.md')
62 var isFileExists = fs.existsSync(js) || fs.existsSync(pkg) || fs.existsSync(readme)
63
64 if (isFileExists) {
65 var question = "CAUTION!! File exists, overwrite? (y/n): "
66 var handler = function(ok) {
67 if (ok) {
68 callback && callback()
69 }
70 else {
71 process.exit(1)
72 }
73 }
74
75 program.confirm(question, handler)
76 }
77 else {
78 callback && callback()
79 }
80}
81
82function checkFamilyPattern() {
83 var FAMILY_PATTERN = /^\w+\.\w+(?:\.\w+)*$/
84
85 if (!family.match(FAMILY_PATTERN) &&
86 family !== 'mosaics' &&
87 family !== 'mo') {
88 mosaic.error('Family does\'t match pattern: mosaics | part1.part2')
89 family = ''
90 return false
91 }
92 if (family === 'mo') {
93 mosaic.warn('family', 'mo is a special family for testing purpose only. It won\'t be published to CDN.')
94 }
95 return true
96}
97
98function confirmArgs(callback) {
99 if(!parts[1] || !family) {
100 // user types: mo init [moname]
101 // or family should be renamed
102 program.prompt('Specify family (' + family + '): ', function(val) {
103 if(val) {
104 family = val
105 mopath = path.join(cwd, family, moname)
106 relativePath = family + '/' + moname
107 }
108 if(checkFamilyPattern()) callback && callback()
109 else confirmArgs(callback)
110 })
111 }
112 else if(mopath === cwd) {
113 // user types: mo init
114 program.prompt('Specify family/name (' + family + '/' + moname + '): ', function(val) {
115 if(val) {
116 var split = val.split('/')
117 family = split[0] || family
118 moname = split[1] || moname
119 mopath = path.join(cwd, family, moname)
120 relativePath = family + '/' + moname
121 }
122 if(checkFamilyPattern()) callback && callback()
123 else confirmArgs(callback)
124 })
125 }
126 else {
127 // user types: mo init [family]/[moname]
128 if(checkFamilyPattern()) callback && callback()
129 else confirmArgs(callback)
130 }
131}
132
133function generateFiles() {
134 var pathJs = path.join(mopath, 'index.js')
135 var pathPackage = path.join(mopath, 'package.json')
136 var pathReadme
137
138 var contentsOfJs = [
139 'KISSY.add(\'' + relativePath + '/index\', function(S, Brick) {',
140 '',
141 ' return Brick',
142 '',
143 '}, {',
144 ' requires: [\'brix/base\']',
145 '})'
146 ].join('\n')
147 var contentsOfPkg = [
148 '{',
149 ' "name": "' + relativePath + '",',
150 ' "version": "0.0.1"',
151 '}'
152 ].join('\n')
153 var contentsOfReadme
154
155 if(!fs.existsSync(mopath)) mkpath.sync(mopath)
156
157 fs.writeFileSync(pathJs, contentsOfJs)
158 mosaic.log("create", pathJs)
159
160 fs.writeFileSync(pathPackage, contentsOfPkg)
161 mosaic.log("create", pathPackage)
162
163 if (family === 'mosaics') {
164 pathReadme = path.join(mopath, 'readme.md')
165 contentsOfReadme = [
166 '# ' + moname,
167 '',
168 '## 用法',
169 '',
170 '```html',
171 '<div bx-name="mosaics/' + moname + '">',
172 '</div>',
173 '```'
174 ].join('\n')
175 fs.writeFileSync(pathReadme, contentsOfReadme)
176 mosaic.log("create", pathReadme)
177 }
178
179 mosaic.log("inited", mopath)
180 process.exit(0)
181}
\No newline at end of file