This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
/
ls.js
303 lines (270 loc) Β· 10.3 KB
/
ls.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
var utils = require('../utils'),
packages = require('../packages'),
logger = require('../logger'),
couchdb = require('../couchdb'),
kansorc = require('../kansorc'),
argParse = require('../args').parse,
path = require('path'),
tree = require('../tree'),
install = require('./install');
exports.summary = 'Builds a project and reports a list of its exports';
exports.usage = '' +
'kanso ls [PATH]\n' +
'\n' +
'Parameters:\n' +
' PATH Path to project directory to inspect (defaults to ".")\n' +
'\n' +
'Options:\n' +
' --name Show package name\n' +
' --version Show package version\n' +
' --description Show package description\n' +
' --preprocessors List preprocessors\n' +
' --postprocessors List postprocessors\n' +
' --attachments List attachment filenames\n' +
' --properties List other properties added the design doc\n' +
' --shows List show functions\n' +
' --lists List list function\n' +
' --updates List update functions\n' +
' --filters List filter functions\n' +
' --views List views\n' +
' --rewrites List rewrites (by "from" pattern)\n' +
' --validate Test whether validate_doc_update exists\n' +
' --size Calculate size of design doc including dependencies\n' +
' --dependencies List dependencies\n' +
'\n' +
'If no options are specified, shows all information';
exports.indent = ' ';
function print_heading(str, count) {
console.log(
logger.bold(str) +
(count === undefined ? '': ' (' + count + ')')
);
}
// prints property names without recursing
function shallow_print(title, obj) {
if (!obj) {
return;
}
var names = Object.keys(obj);
if (!names.length) {
return;
}
print_heading(title, names.length);
names.forEach(function (name) {
console.log(exports.indent + name);
});
console.log('');
}
function deep_print(obj, ignores, path) {
path = path || [];
var str = '';
var sp = exports.indent;
for (var i = 0; i < path.length; i++) {
sp += exports.indent;
}
for (var k in obj) {
var kp = path.concat([k]);
if (ignores.indexOf(kp.join('.')) === -1) {
if (typeof obj[k] === 'object') {
if (obj[k] instanceof Array) {
str += sp + k + ' (' + k.length + ')\n';
}
else {
str += sp + k + '\n';
str += deep_print(obj[k], ignores, path.concat([k]));
}
}
else {
str += sp + k + '\n';
}
}
}
return str;
}
/**
* Prints a hierarchy. Parameter 2 and 3 should not be passed by user code.
* @param obj an object with a structure like: {title: 'root', children: [{title: 'ch1', children: []}, {title: 'ch2', children: []}]}
*/
function hierarchy_print(obj, thislevellog, nextlevellog) {
var joinedprint = function () {
console.log(Array.prototype.slice.call(arguments).join(''));
};
thislevellog = thislevellog || joinedprint;
nextlevellog = nextlevellog || joinedprint;
if (typeof obj.title !== 'undefined') {
if (obj.children.length > 0) {
thislevellog(logger.yellow('β¬β ') + obj.title);
} else {
thislevellog(logger.yellow('ββ ') + obj.title);
}
}
for (var i = 0; i < obj.children.length - 1; i++) {
hierarchy_print(obj.children[i], nextlevellog.bind(null, logger.yellow('ββ')), nextlevellog.bind(null, logger.yellow('β ')));
}
if (obj.children.length > 0) {
hierarchy_print(obj.children[obj.children.length - 1], nextlevellog.bind(null, logger.yellow('ββ')), nextlevellog.bind(null, ' '));
}
};
exports.run = function (settings, args) {
var options = {
name: {match: '--name'},
version: {match: '--version'},
description: {match: '--description'},
preprocessors: {match: '--preprocessors'},
postprocessors: {match: '--postprocessors'},
attachments: {match: '--attachments'},
properties: {match: '--properties'},
shows: {match: '--shows'},
lists: {match: '--lists'},
updates: {match: '--updates'},
filters: {match: '--filters'},
views: {match: '--views'},
rewrites: {match: '--rewrites'},
validate: {match: '--validate'},
size: {match: '--size'},
dependencies: {match: '--dependencies'},
};
var a = argParse(args, options);
var dir = utils.abspath(a.positional[0] || '.');
kansorc.extend(settings, dir + '/.kansorc', function (err, settings) {
if (err) {
return logger.error(err);
}
var custom = false;
for (var k in options) {
custom = custom || a.options[k];
}
// suppress logger output
logger.level = 'error';
exports.loadApp(dir, {}, settings, function (err, doc, cfg) {
if (err) {
return logger.error(err);
}
if (!custom || a.options.name) {
console.log(logger.bold('Name: ') + cfg.name + '\n');
}
if (!custom || a.options.version) {
console.log(logger.bold('Version: ') + cfg.version + '\n');
}
if (!custom || a.options.description) {
console.log(
logger.bold('Description') + '\n' +
cfg.description + '\n'
);
}
if (!custom || a.options.preprocessors) {
shallow_print('Preprocessors', cfg.preprocessors);
}
if (!custom || a.options.postprocessors) {
shallow_print('Postprocessors', cfg.postprocessors);
}
if (!custom || a.options.attachments) {
shallow_print('Attachments', doc._attachments);
}
if (!custom || a.options.properties) {
var str = deep_print(doc, [
// ignore these properties
'_attachments',
'shows',
'lists',
'updates',
'filters',
'rewrites',
'views',
'validate_doc_update'
]);
if (str) {
print_heading('Extra properties');
console.log(str);
}
}
if (!custom || a.options.shows) {
shallow_print('Show functions', doc.shows);
}
if (!custom || a.options.lists) {
shallow_print('List functions', doc.lists);
}
if (!custom || a.options.updates) {
shallow_print('Update functions', doc.updates);
}
if (!custom || a.options.filters) {
shallow_print('Filter functions', doc.filters);
}
if (!custom || a.options.views) {
shallow_print('Views', doc.views);
}
if (!custom || a.options.rewrites) {
// Rewrites
if (doc.rewrites && doc.rewrites.length) {
print_heading('Rewrites', doc.rewrites.length);
doc.rewrites.forEach(function (r) {
console.log(exports.indent + r.from);
});
console.log('');
}
}
if (!custom || a.options.validate) {
if (doc.validate_doc_update || custom) {
console.log(
logger.bold('Validate doc update: ') +
Boolean(doc.validate_doc_update).toString() +
'\n'
);
}
}
if (!custom || a.options.size) {
function roundNum(num, dec) {
return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
}
var str = JSON.stringify(doc);
var bytes = Buffer.byteLength(str, 'utf8');
var num = bytes;
var units = 'Bytes';
if (num > 1024) {
num = num / 1024;
units = 'KB'
}
if (num > 1024) {
num = num / 1024;
units = 'MB'
}
console.log(
logger.bold('Size: ') +
roundNum(num, 2) + ' ' + units +
' (including dependencies)\n'
);
}
if (!custom || a.options.dependencies) {
console.log(logger.bold('Dependencies'));
tree.build({config: cfg, source: 'root'}, [install.dirSource(path.join(dir, 'packages'))], function (err, packages) {
var mypkgs = [];
var attach_deps = function (key, obj, ptree) {
for (var k in ptree) {
if (ptree.hasOwnProperty(k) && ptree[k].ranges && ptree[k].ranges.hasOwnProperty(key)) {
obj.push({
"title" : logger.cyan(k) + (ptree[k].ranges[key] ? logger.cyan('(' + ptree[k].ranges[key] + ')') : '') + '@' + ptree[k].current_version,
"children": []
});
attach_deps(k, obj[obj.length -1 ].children, ptree);
}
}
};
attach_deps(cfg.name, mypkgs, packages);
hierarchy_print({children: mypkgs});
console.log('');
logger.end();
});
} else {
logger.end();
}
});
});
};
exports.loadApp = function (dir, options, settings, callback) {
var paths = settings.package_paths || [];
var parent_dir = path.dirname(dir);
if (path.basename(parent_dir) === 'packages') {
paths.push(parent_dir);
}
packages.load(dir, paths, null, options, callback);
};