UNPKG

3.43 kBPlain TextView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5var heredoc = require('heredoc').strip
6var fs = require('fs')
7var spawn = require('child_process').spawn
8
9var readFile = fs.readFileSync
10var writeFile = fs.writeFileSync
11var mkdir = fs.mkdirSync
12var readdir = fs.readdirSync
13var exists = fs.existsSync
14
15var program = require('commander')
16
17
18program
19 .option('-f --force', 'Force initialzie')
20
21program.on('--help', function() {
22 console.log(' Examples:')
23 console.log('')
24 console.log(' $ oceanify init --force')
25 console.log(' $ oceanify init')
26 console.log('')
27})
28
29program.parse(process.argv)
30
31
32if (readdir(process.cwd()).length > 0 && !program.force) {
33 console.error('Current working directory is not empty.')
34 process.exit()
35}
36
37if (exists('package.json')) {
38 init()
39}
40else {
41 var proc = spawn('npm', ['init'], { stdio: 'inherit' })
42
43 proc.on('exit', function(code) {
44 if (code !== 0) process.exit()
45 init()
46 })
47}
48
49
50function render(template, data, fn) {
51 fn = fn || function(str) {
52 return ('' + str)
53 .replace(/&/g, '&')
54 .replace(/</g, '&lt;')
55 .replace(/>/g, '&gt;')
56 .replace(/"/g, '&quot;')
57 .replace(/'/g, '&#39;')
58 }
59
60 return template.replace(/\{(!?[\w\.]+)\}/g, function(m, key) {
61 return (key.charAt(0) === '!' ? getValue(data, key.slice(1)) : fn(getValue(data, key))) || ''
62 })
63
64 function getValue(data, key){
65 return key.split('.').reduce(function(data, key){
66 return data[key]
67 }, data)
68 }
69}
70
71function init() {
72 var pkg = JSON.parse(readFile('package.json', 'utf-8'))
73
74 writeFile('index.js', heredoc(function() {/*
75 'use strict';
76 */}))
77
78 writeFile('.gitignore', heredoc(function() {/*
79 .DS_Store
80 *~
81 *.log
82 dump.rdb
83 node_modules
84 */}))
85
86 writeFile('.jshintrc', heredoc(function() {/*
87 {
88 "asi": true,
89 "unused": "vars",
90 "quotmark": false,
91 "mocha": true,
92 "browser": true,
93
94 "globals": {
95 "require": false,
96 "exports": false,
97 "module": false
98 }
99 }
100 */}))
101
102 writeFile('.npmignore', heredoc(function() {/*
103 test
104 */}))
105
106 writeFile('Readme.md', render(heredoc(function() {/*
107 # {name}
108
109 ## Usage
110
111 // TODO
112
113 ## Development
114
115 ```js
116 $ npm start
117 $ open http://localhost:5000/tets/runner.html
118 ```
119 */}), pkg))
120
121 if (!exists('test')) mkdir('test')
122
123 writeFile('test/runner.html', render(heredoc(function() {/*
124 <!DOCTYPE html>
125 <html>
126 <head>
127 <meta charset="utf-8">
128 <link type="text/css" rel="stylesheet" href="http://amo.alicdn.com/L1/377/101010/assets/mocha-932de965a07255c38bbb1e9f678cde64.css" />
129 <title>{name} &mdash; Test Runner</title>
130 </head>
131 <body>
132 <div id="mocha"></div>
133 <div id="fixture"></div>
134 <script src="http://amo.alicdn.com/L1/377/101010/assets/mocha-39ab115a28db1aa1b20a33772d1272a1.js"></script>
135 <script src="http://amo.alicdn.com/L1/377/101010/assets/expect-0c4d7b1e6c1f3df2bfaba7fadb0a2732.js"></script>
136 <script src="http://amo.alicdn.com/L1/377/10010/sea.js"></script>
137 <!--[if lte IE 9]><!-->
138 <script src="http://amo.alicdn.com/L1/377/101010/assets/es5-7dfe7fb63d161c704e2f35874957b921.js"></script>
139 <!--<![endif]-->
140 <script src="/config.js"></script>
141 <script>
142 mocha.ui('bdd')
143
144 seajs.use('{name}', function() {
145 mocha.run()
146 })
147 </script>
148 </body>
149 </html>
150 */}), pkg))
151}