forked from workshopper/workshopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint-text.js
98 lines (78 loc) · 2.31 KB
/
print-text.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
const fs = require('fs')
, path = require('path')
, colorsTmpl = require('colors-tmpl')
, msee = require('msee')
, mseeOptions = {
paragraphStart: ''
, paragraphEnd: '\n\n'
}
function printText (appName, appDir, filetype, contents) {
var variables = {
appname : appName
, rootdir : appDir
}
contents = colorsTmpl(contents)
contents = contents.replace(/\{([^}]+)\}/gi, function (match, k) {
return variables[k] || ('{' + k + '}')
})
// proper path resolution
contents = contents.replace(/\{rootdir:([^}]+)\}/gi, function (match, subpath) {
return 'file://' + encodeURI(path.join(appDir, subpath))
})
if (filetype == 'md') {
// convert Markdown to ANSI
contents = msee.parse(contents, mseeOptions)
}
console.log(contents)
}
function printFile (appName, appDir, file, callback) {
fs.readFile(file, 'utf8', function (err, contents) {
if (err)
throw err
printText(appName, appDir, path.extname(file).replace(/^\./, ''), contents)
callback && callback();
})
}
function getExistingFile (file, lang) {
if (!file)
return false
file = file.replace(/\{lang\}/g, lang)
if (fs.existsSync(file)) {
var stat = fs.statSync(file)
if (stat && stat.isFile())
return file
}
return null
}
function printLocalisedFile (appName, appDir, file, lang, callback) {
file = getExistingFile(file, lang)
if (file) {
printFile(appName, appDir, file, callback)
return true
}
if (callback)
process.nextTick(callback)
return false
}
function printLocalisedFirstFile (appName, appDir, files, lang, callback) {
var consumed = false
files.filter(function (file) {
// Since the files that will be printed are subject to user manipulation
// a null can happen here, checking for it just in case.
return file !== undefined && file !== null
}).forEach(function (file) {
if (consumed)
return
if (file = getExistingFile(file, lang)) {
consumed = true
printFile(appName, appDir, file, callback)
}
})
if (!consumed && callback)
process.nextTick(callback)
return consumed
}
module.exports.text = printText
module.exports.file = printFile
module.exports.localisedFile = printLocalisedFile
module.exports.localisedFirstFile = printLocalisedFirstFile