|
1
|
|
|
const debug = require('debug')('mdprepare:processFile') |
|
2
|
|
|
const {execSync} = require('child_process') |
|
3
|
|
|
const fs = require('fs') |
|
4
|
|
|
const os = require('os') |
|
5
|
|
|
const path = require('path') |
|
6
|
|
|
const h = require('./helpers.js') |
|
7
|
|
|
|
|
8
|
|
|
export default function (filePath, clear) { |
|
9
|
|
|
// this function accepts a markdown filename and runs any mdprepare code which is embedded in that file |
|
10
|
|
|
debug(filePath) |
|
11
|
|
|
try { |
|
12
|
|
|
var txt = fs.readFileSync(filePath, 'utf8') |
|
13
|
|
|
} catch (err) { |
|
14
|
|
|
throw Error('file not found') |
|
15
|
|
|
} |
|
16
|
|
|
try { |
|
17
|
|
|
let p = processText(txt, clear, path.dirname(path.resolve(process.cwd(), filePath))) |
|
18
|
|
|
fs.writeFileSync(filePath, p) |
|
19
|
|
|
} catch (err) { |
|
20
|
|
|
throw Error('unable to write to file') |
|
21
|
|
|
} |
|
22
|
|
|
return true |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
export function processText (txt, clear, fileDirName) { |
|
26
|
|
|
let posn = 0 |
|
27
|
|
|
let r = '' |
|
28
|
|
|
let x |
|
29
|
|
|
let y |
|
30
|
|
|
let t |
|
31
|
|
|
let frm |
|
32
|
|
|
let cliCommand |
|
33
|
|
|
while (true) { |
|
34
|
|
|
x = h.findMdpCode(txt, posn) |
|
35
|
|
|
y = h.findMdpInsert(txt, posn) |
|
36
|
|
|
t = h.earlierOf(x, y) |
|
37
|
|
|
if (t.start === -1) { |
|
38
|
|
|
r = r + txt.substring(posn) |
|
39
|
|
|
break |
|
40
|
|
|
} else { |
|
41
|
|
|
r = r + txt.substring(posn, t.internalStart) |
|
42
|
|
|
frm = t.internalStart + t.internalLength |
|
43
|
|
|
cliCommand = getCliCommand(t.commandString) |
|
44
|
|
|
if (cliCommand === 'ERROR: mdpInsert command not found') { |
|
45
|
|
|
// the mdpInsert command was not present so we don't insert or remove anything, just leave as is |
|
46
|
|
|
r = r + txt.substr(t.internalStart, t.internalLength) |
|
47
|
|
|
} else { |
|
48
|
|
|
if (clear !== true) { |
|
49
|
|
|
r = r + runCliCmd(cliCommand, fileDirName) |
|
50
|
|
|
} else { |
|
51
|
|
|
// we are clearing any content so remove all lines between the start and end lines |
|
52
|
|
|
if (txt.substr(frm, 1) === '\r') { frm++ } |
|
53
|
|
|
/* istanbul ignore else */ |
|
54
|
|
|
if (txt.substr(frm, 1) === '\n') { frm++ } |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
posn = t.start + t.length |
|
58
|
|
|
r = r + txt.substring(frm, posn) |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
return r |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
function getCliCommand (str) { |
|
65
|
|
|
let x = str.indexOf('mdpInsert ') |
|
66
|
|
|
if (x === -1) { |
|
67
|
|
|
return 'ERROR: mdpInsert command not found' |
|
68
|
|
|
} |
|
69
|
|
|
return str.substr(x + 10) |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
export function runCliCmd (str, wDirName) { |
|
73
|
|
|
// wDirName is either an absolute path or a relative path to the directory node was run from |
|
74
|
|
|
let cmd = crossPlatformCmds(str) |
|
75
|
|
|
try { |
|
76
|
|
|
return execSync(cmd, {cwd: wDirName}) |
|
77
|
|
|
} catch (err) { |
|
78
|
|
|
return 'ERROR: ' + err.message |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
function crossPlatformCmds (cmd) { |
|
83
|
|
|
// does limited cross platform translation of limited commands |
|
84
|
|
|
let r = cmd |
|
85
|
|
|
/* istanbul ignore if */ |
|
86
|
|
|
if (os.platform() === 'win32') { |
|
87
|
|
|
if (cmd.substr(0, 4) === 'cat ') { |
|
88
|
|
|
r = r.replace('cat ', 'type ') |
|
89
|
|
|
r = r.replace(/\//g, '\\') |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
return r |
|
93
|
|
|
} |
|
94
|
|
|
|