-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperkins
executable file
·201 lines (175 loc) · 5.36 KB
/
perkins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env node
/**
* Perkins v3.0.0 CLI by Carlos Elias
*/
var exec = require('child_process').exec,
os = require('os'),
fs = require('fs'),
ospath = require('path'),
colors = require('colors'),
perkins = require('commander'),
mkdir = require('mkdirp'),
bower = require('bower'),
express = require('express'),
favicon = require('static-favicon'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
lessMiddleware = require('less-middleware'),
pkg = require('./package.json'),
version = pkg.version;
// setting bower assets folder
bower.config.directory = "vendor/";
perkins
.version('Perkins ' +version.cyan)
.usage('[command] --options <dir>'.bold)
.option('-p, --port <port>', 'specify the port [8000]', Number, 8000)
perkins
.command('init <path>')
.description('setup a initial structure in the specified directory.')
.action(function(dir){
if (fs.existsSync(dir)) {
perkins.confirm('destination is not empty, continue? ', function(ok){
if (ok) {
process.stdin.destroy();
createApplicationAt(dir);
} else {
abort('aborting');
}
});
} else {
createApplicationAt(dir);
}
});
perkins
.command('server [path]')
.description('start a HTTP Server in the current directory')
.action(function(dir){
if (fs.existsSync(dir)) {
// express server
app = express();
dir = ospath.resolve(dir)
console.log('Server started at '.cyan+'http://localhost:'+perkins.port+' Sir.'.cyan)
app.set('views', dir + '/');
app.use(favicon());
app.use(morgan('dev'));
app.use(bodyParser());
app.use(methodOverride());
var lessOptions = {
dest: dir,
preprocess: {
path: function(pathname, req) {
return pathname.replace('/stylesheets', '');
}
},
force: true
};
var parserOptions = {
optimization: 2
};
var compilerOptions = {
compress: true
};
app.use(lessMiddleware(dir + '/vendor/perkins', lessOptions, parserOptions, compilerOptions));
app.use(express.static(dir + '/'));
app.get('/', function(req, res){
dir = ospath.resolve(dir)
res.sendfile(dir + '/index.html');
});
app.listen(perkins.port);
} else {
console.log('Sorry, Sir but '+dir+' does not exists...');
}
});
/*
perkins
.command('build <path>')
.description('build out your project as static files into public/')
.action(function(dir) {
if (fs.existsSync(dir)) {
console.log('Buildout ' +dir+ 'results are in public/')
}
else {
console.log('Sorry, Sir but '+ dir +' does not exists');
}
});
*/
perkins
.command('search [dep]')
.description('search for dependencies (using bower)')
.action(function() {
command = process.argv.slice(2);
doBower(command);
});
perkins
.command('install [dep]')
.description('install dependencies (usign bower)')
.action(function() {
mkdir.sync('vendor/', 0755);
command = process.argv.slice(2);
doBower(command);
});
var eol = 'win32' == os.platform() ? '\r\n' : '\n'
var plainIndex = [
'<!DOCTYPE html>',
'<html lang="en">',
' <head>',
' <meta charset="utf-8">',
' <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">',
' <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">',
' <meta name="apple-mobile-web-app-capable" content="yes">',
' <title>Perkins v'+version+'</title>',
' <link rel=\'stylesheet\' href=\'stylesheets/app.css\' />',
' </head>',
' <body>',
' <h1>Perkins</h1>',
' <p>Now you can start building the future, Sir</p>',
' </body>',
'</html>',
].join(eol);
function doBower(command) {
bower.commands[command[0]].line(['node', __dirname].concat(command))
.on('data', function (data) { data && console.log(data); })
.on('end', function (data) { data && console.log(data); })
.on('error', function (err) { throw err });
}
function write(path, str) {
fs.writeFile(path, str);
console.log('Creating: '.cyan);
console.log(path.bold);
}
function createApplicationAt(dir) {
console.log();
process.on('exit', function(){
console.log('Perkins ' +version+' init @ ' +dir);
console.log();
console.log(' If you want a more complex app install dependencies:');
console.log(' $ cd %s && npm install', dir);
console.log();
console.log(' Or just run the static HTTP Server:');
console.log(' $ perkins server %s', dir);
console.log();
});
mkdir(dir, function(){
mkdir(dir + '/javascripts');
mkdir(dir + '/images');
mkdir(dir + '/stylesheets');
// package.json
var pkg = {
name: 'application-name',
version: '0.0.1',
private: true,
dependencies: {
express: '*',
'less-middleware': '*'
}
}
bower.config.directory = "/" + dir + "/vendor/";
command = [ 'install', 'perkins' ];
doBower(command);
// console.log(bower.config.directory);
write(dir + '/package.json', JSON.stringify(pkg, null, 2));
write(dir + '/index.html', plainIndex);
});
}
perkins.parse(process.argv);