forked from workshopper/workshopper-adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint-text.js
107 lines (85 loc) · 2.77 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
99
100
101
102
103
104
105
106
107
const fs = require('fs')
, path = require('path')
, colorsTmpl = require('colors-tmpl')
, through = require('through')
, msee = require('msee')
, mseeOptions = {
paragraphStart: ''
, paragraphEnd: '\n\n'
}
function commandify (s) {
return String(s).toLowerCase().replace(/\s+/g, '-');
}
function getText (appName, appDir, filetype, contents) {
var variables = {
appname : appName
, rootdir : appDir
, COMMAND : commandify(appName)
, ADVENTURE_COMMAND : commandify(appName)
, ADVENTURE_NAME : appName
}
if (typeof contents === 'object')
contents = contents.toString()
if (typeof contents === 'function')
contents = contents()
if (typeof contents !== 'string')
contents = ''
contents = colorsTmpl(contents)
contents = contents.replace(/\{([^}]+)\}/gi, function (match, k) {
return variables[k] || ('{' + k + '}')
})
contents = contents.replace(/\$([A-Z_]+)/g, function (match, k) {
return variables[k] || ('$' + k)
})
if (appDir) {
// proper path resolution
contents = contents.replace(/\{rootdir:([^}]+)\}/gi, function (match, subpath) {
return 'file://' + path.join(appDir, subpath)
})
}
if (filetype === 'md') {
// convert Markdown to ANSI
contents = msee.parse(contents, mseeOptions)
}
return contents
}
function printText (appName, appDir, filetype, contents) {
console.log(getText(appName, appDir, filetype, contents))
}
function createFileStream (appName, appDir, file) {
var filetype = path.extname(file).replace(/^\./, '')
return fs.createReadStream(file, {encoding: 'utf8'})
.pipe(through(function (data) {
this.emit('data', getText(appName, appDir, filetype, data))
}))
}
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 localisedFileStream (appName, appDir, file, lang) {
file = getExistingFile(file, lang)
return file ? createFileStream(appName, appDir, file) : null
}
function localisedFirstFileStream (appName, appDir, files, lang) {
var file = null
files.forEach(function (rawFile) {
// Since the files that will be printed are subject to user manipulation
// a null can happen here, checking for it just in case.
if (rawFile === undefined || rawFile === null)
return
if (!file)
file = getExistingFile(rawFile, lang)
})
return file ? createFileStream(appName, appDir, file) : null
}
module.exports.text = printText
module.exports.localisedFileStream = localisedFileStream
module.exports.localisedFirstFileStream = localisedFirstFileStream