-
Notifications
You must be signed in to change notification settings - Fork 0
/
large.js
executable file
·359 lines (295 loc) · 8.45 KB
/
large.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
events = require('events'),
promise = require('node-promise').Promise,
namp = require('namp'),
prettyPrint = require('pretty-print'),
program = require('commander');
program
.version('0.0.1')
.option('-i --init', 'intialize large')
.option('-c --config', 'add user configurations')
.option('-n --new', 'create new article')
.option('-p --publish', 'publish article')
.option('--me', 'show author details')
.option('--flush', 'flush out large instance *Only for Dev*')
.option('--test', 'test command binding');
var template = {
authorConfig : {
name : "",
email : "",
signature : ""
}
};
var Large = {
author : {},
config : {
path: [process.cwd(),'.large'].join('/'),
author: [process.cwd(),'.large','author.json'].join('/'),
post: [process.cwd(), '.post'].join('/')
},
folder : {
markdown : [process.cwd(), 'post'].join('/'),
html : ''
},
printerConfig : {
leftPadding: 2,
rightPadding: 3
},
_printer : function(obj) {
prettyPrint(obj, this.printerConfig);
},
_readMarkdown : function(filename) {
var that = this,
path = [this.folder.markdown, filename].join('/');
fs.readFile(path, "utf-8", function(err, data) {
if(err) { throw "ERROR: Article not found"; }
that.channel.emit('onReadMarkdown', that, data);
});
},
_renderHTML : function(that, md) {
console.log(that._convertMarkdown(md));
},
_convertMarkdown : function(md) {
return namp(md, {highlight : true});
},
publish : function(filename) {
this._readMarkdown(filename);
},
init : function() {
var that = this;
fs.exists(this.config.path, function(exists) {
if(exists) {
console.log('\n Already Initialized!\n');
}else {
fs.mkdir(that.config.path, function() {
that._authorConfigIO('w',
function() { console.log('\n Medium is Medium, But this is Large!\n'); },
{ data: template.authorConfig });
});
}
fs.mkdir(that.config.post, function(err) {});
fs.exists(that.folder.markdown, function(exists) {
if(!exists) { fs.mkdir(that.folder.markdown); }
})
});
},
_authorConfigErrorCheck : function(err) {
if(err) {
throw "ERROR : author conf i/o failed!";
}
},
_authorConfigIO : function(mode, cb, options) {
var that = this;
switch(mode) {
case 'r':
fs.readFile(this.config.author, function(err, data) {
cb.call(that, err, (data) ? JSON.parse(data) : undefined, options)
});
break;
case 'w':
fs.writeFile(this.config.author, JSON.stringify(options.data), function(err) {
cb.call(that, err)
});
break;
default:
throw 'ERROR: unkown operation!';
}
},
_configurationHelp : function() {
var printObj = {
name : 'author name',
email : 'author email',
signature : 'author signature'
};
console.log('\n Usage large --config [param] [value]\n');
this._printer(printObj);
},
_parseConfig : function(data, args) {
var argsLength = args.length,
key = null,
value = null;
for(var i = 0; i < argsLength;) {
if(args[i] === 'help') {
this._configurationHelp();
i++;
}else {
key = args[i];
value = args[i+1];
if(value) {
if(data.hasOwnProperty(key)) {
data[key] = value;
}
}
i += 2;
}
}
return data;
},
_printUserProfile : function(err, data) {
try{
this._authorConfigErrorCheck(err);
}catch(msg) {
console.log(msg);
}
this._printer(data);
},
me : function() {
this._authorConfigIO('r', this._printUserProfile);
},
configuration : function(args) {
var that = this;
this._authorConfigIO('r', function(err, data) {
try { this._authorConfigErrorCheck(err) }
catch(msg) { console.log(msg); exit; }
data = this._parseConfig(data, args);
this._authorConfigIO('w', this._authorConfigErrorCheck, { data : data });
});
},
_filenameBuilder : function(args) {
var filename = '';
if(Array.isArray(args)) {
args = args.join('_');
}else if(args instanceof Object) {
return null;
}
filename = args.toString().replace(/\s/g, '_');
filename += '.md';
filename = filename.toLowerCase();
return filename;
},
_loadAuthorConfig : function() {
var that = this;
this._authorConfigIO('r', function(err, data) {
try { this._authorConfigErrorCheck() }
catch(msg) { console.log(msg); exit; }
that.author = data;
that.channel.emit('onLoadAuthorConfig', that);
});
},
_getAuthor : function(prop) {
if(this.author.hasOwnProperty(prop)) {
return this.author[prop];
}else {
return null;
}
},
_getArticleMetaData : function(filename) {
var article = '',
metadata = {
filename : '',
article : '',
author : '',
email : '',
created_date : '',
modified_date : '',
publish_date : ''
};
article = path.basename(filename, path.extname(filename)).replace(/_/g,' ');
metadata.filename = filename;
metadata.article = article;
metadata.author = this._getAuthor('name');
metadata.email = this._getAuthor('email');
metadata.created_date = Date.now();
return metadata;
},
_prepareNewArticle : function(that) {
var metadata = {},
filePath = '';
if(that._getAuthor('name').length <= 0 ||
that._getAuthor('email').length <= 0) {
throw 'ERROR: author name is empty';
}
metadata = that._getArticleMetaData(that.tmp);
filePath = path.join(that.config.post, metadata.filename.replace('.md', '.json'));
fs.writeFile(filePath, JSON.stringify(metadata), function(err) {
if(err) { throw 'ERROR: post i/o failed!'; }
fs.writeFile(path.join(that.folder.markdown, metadata.filename), function() {
console.log('Article is ready');
});
})
},
articleOperation : function(args) {
if(!args || !args.length) {
throw "ERROR: article name empty!"
}
this.tmp = this._filenameBuilder(args);
this._loadAuthorConfig();
},
_functionSwitch : function(that) {
try {
switch(that.option.toLowerCase()) {
case 'new':
that._prepareNewArticle(that);
break;
case 'publish':
that._publishArticle(that);
break;
default:
console.log('unknown option :// ');
}
}catch(msg) {
console.log(msg);
}
},
_publishArticle : function(that) {
var filename = that.tmp.replace('.md', '.json');
filename = path.join(this.config.post, filename);
that._getArticleConfiguration(filename)
.then(function(data) {
that._updateArticleConfiguration(data, filename)
.then(function() {
console.log('end');
})
});
},
_getArticleConfiguration : function(filename) {
var p = new promise();
fs.readFile(filename, function(err, data) {
if(err) {
throw 'ERROR: article configuration file not found!'
}
p.resolve(JSON.parse(data));
});
return p;
},
_updateArticleConfiguration : function(data, filename) {
var p = new promise();
data.publish_date = Date.now();
fs.writeFile(filename, JSON.stringify(data), function(err) {
if(err) {
throw "ERROR: publish date updation failed!"
}
p.resolve();
});
return p;
},
boot : function() {
this.channel = new events.EventEmitter();
this.channel.on('onReadMarkdown', this._renderHTML);
this.channel.on('onLoadAuthorConfig', this._functionSwitch);
}
};
Large.boot();
program.parse(process.argv);
if(program.init) {
Large.init();
}else if(program.config) {
Large.configuration(program.args);
}else if(program.me) {
Large.me();
}else if(program.flush) {
require('child_process').exec('rm -rf .large/', function() {
require('child_process').exec('rm -rf .post/', function() {
console.log('Large is removed :( !');
});
});
}else if(program.new || program.publish) {
Large.option = (program.new) ? 'new' : 'publish';
Large.articleOperation(program.args);
}
if(program.test) {
console.log(Large._readMarkdown('this_is_a_test.md'));
}
exports.Large = Large;