markdown-maker 1.10.2 → 1.10.3
Sign up to get free protection for your applications and to get access to all the features.
- package/.github/workflows/node.js.yml +21 -26
- package/.vscode/extensions.json +3 -0
- package/.vscode/settings.json +8 -3
- package/.vscode/snippets.code-snippets +6 -8
- package/jest.config.js +7 -0
- package/package.json +41 -40
- package/src/cltool.ts +113 -71
- package/src/commands.ts +237 -243
- package/src/errors.ts +26 -0
- package/src/main.ts +110 -109
- package/src/parser.ts +378 -350
- package/src/templates.ts +5 -1
- package/src/types.ts +33 -0
- package/tests/_test-util.ts +44 -0
- package/tests/advanced.spec.ts +92 -0
- package/tests/basic.spec.ts +68 -0
- package/tests/clargs.spec.ts +50 -0
- package/tests/errors.spec.ts +64 -0
- package/tests/html.spec.ts +23 -0
- package/tests/line.spec.ts +21 -0
- package/tests/marked.spec.ts +40 -0
- package/tests/target.spec.ts +41 -0
- package/tests/vars.spec.ts +45 -0
- package/tsconfig.json +66 -64
- package/prettierrc.yaml +0 -4
- package/test/advanced.test.js +0 -34
- package/test/basic.test.js +0 -67
- package/test/clargs.test.js +0 -51
- package/test/errors.test.js +0 -47
- package/test/hooks.js +0 -9
- package/test/hooks.test.js +0 -114
- package/test/html.test.js +0 -37
- package/test/line.test.js +0 -21
- package/test/marked.test.js +0 -43
- package/test/target.test.js +0 -43
- package/test/tester.test.js +0 -41
- package/test/vars.test.js +0 -49
package/src/main.ts
CHANGED
@@ -1,122 +1,123 @@
|
|
1
1
|
import path from "path";
|
2
2
|
import { WebSocketServer } from "ws";
|
3
|
-
import Parser from "./parser";
|
4
3
|
import * as fs from "fs";
|
5
|
-
const choki = require("chokidar");
|
6
4
|
|
7
|
-
import
|
5
|
+
import choki from "chokidar";
|
6
|
+
import Parser from "./parser";
|
7
|
+
|
8
|
+
/* for adding colours to strings */
|
9
|
+
import { enable as ColorsEnable } from "colors.ts";
|
10
|
+
ColorsEnable();
|
11
|
+
|
12
|
+
import { argParser, CommandLineArgs, ParserOptions } from "./cltool";
|
8
13
|
const configFileName = ".mdmconfig.json";
|
9
14
|
|
10
15
|
function main() {
|
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
|
-
} catch (e) {
|
112
|
-
console.log(e.message);
|
113
|
-
}
|
114
|
-
}
|
16
|
+
let clargs: CommandLineArgs;
|
17
|
+
let server: WebSocketServer | undefined;
|
18
|
+
|
19
|
+
/* Read config file or parse args from cmd-line */
|
20
|
+
if (fs.existsSync(configFileName)) {
|
21
|
+
let data: CommandLineArgs = JSON.parse(
|
22
|
+
fs.readFileSync(configFileName).toString()
|
23
|
+
).opts;
|
24
|
+
|
25
|
+
let args: (string | number)[] = [];
|
26
|
+
Object.entries(data).forEach(([key, value]) => {
|
27
|
+
if (key != "src" && value !== false) {
|
28
|
+
args.push("--" + key);
|
29
|
+
}
|
30
|
+
if (typeof value != "boolean") {
|
31
|
+
args.push(value);
|
32
|
+
}
|
33
|
+
});
|
34
|
+
|
35
|
+
/* We skip [0] and [1], as it is the binary and source file, even when compiled*/
|
36
|
+
for (let i = 2; i < process.argv.length; i++)
|
37
|
+
args.push(process.argv[i]);
|
38
|
+
|
39
|
+
clargs = argParser.parse_args(args.map((x) => x.toString()));
|
40
|
+
} else {
|
41
|
+
clargs = argParser.parse_args();
|
42
|
+
}
|
43
|
+
|
44
|
+
/* if src is init, create config file and exit */
|
45
|
+
if (clargs.src == "init") {
|
46
|
+
const template = fs.readFileSync(
|
47
|
+
path.join(
|
48
|
+
__dirname,
|
49
|
+
"..",
|
50
|
+
"src",
|
51
|
+
"templates",
|
52
|
+
"configTemplate.json"
|
53
|
+
)
|
54
|
+
);
|
55
|
+
fs.writeFileSync(configFileName, template);
|
56
|
+
fs.writeFileSync("main.md", "# Main\n");
|
57
|
+
return;
|
58
|
+
}
|
59
|
+
|
60
|
+
/* helper method for calling parser */
|
61
|
+
const compile = (source, output, cb?) => {
|
62
|
+
/* load data from file, if it exists,
|
63
|
+
* otherwise, interpret as string */
|
64
|
+
|
65
|
+
const parser = new Parser(source, clargs);
|
66
|
+
parser.to(output, (file) => {
|
67
|
+
console.log(`Compiled ${file}`.green);
|
68
|
+
if (cb) cb();
|
69
|
+
});
|
70
|
+
return parser;
|
71
|
+
};
|
72
|
+
|
73
|
+
const internalCooldown = 1000;
|
74
|
+
function watcher(_, path: string) {
|
75
|
+
const now = Date.now();
|
76
|
+
|
77
|
+
if (!this.time) this.time = now;
|
78
|
+
if (now - this.time < internalCooldown) return;
|
79
|
+
console.log(`Detected change in ${path}...`);
|
80
|
+
try {
|
81
|
+
compile(clargs.src, clargs.output, () => {
|
82
|
+
/* after compile, send refresh command to clients */
|
83
|
+
server.clients.forEach((client) => {
|
84
|
+
if (client.OPEN) client.send("refresh");
|
85
|
+
});
|
86
|
+
});
|
87
|
+
} catch (e) {
|
88
|
+
console.log(e.message);
|
89
|
+
}
|
90
|
+
|
91
|
+
this.time = now;
|
92
|
+
}
|
93
|
+
|
94
|
+
/* in case source is a directory, look for entry in directory */
|
95
|
+
if (fs.existsSync(clargs.src) && fs.lstatSync(clargs.src).isDirectory()) {
|
96
|
+
clargs.src = path.join(clargs.src, clargs.entry);
|
97
|
+
}
|
98
|
+
|
99
|
+
if (clargs.debug) console.dir(clargs);
|
100
|
+
|
101
|
+
/* compile once if not watching
|
102
|
+
otherwise watch the folder and recompile on change */
|
103
|
+
if (!clargs.watch) compile(clargs.src, clargs.output);
|
104
|
+
else {
|
105
|
+
const srcDirName = path.dirname(clargs.src);
|
106
|
+
console.log(`Watching ${srcDirName} for changes...`.yellow);
|
107
|
+
server = new WebSocketServer({ port: 7788 });
|
108
|
+
|
109
|
+
const _watcher = choki.watch(srcDirName).on("all", watcher);
|
110
|
+
try {
|
111
|
+
compile(clargs.src, clargs.output);
|
112
|
+
} catch (e) {
|
113
|
+
console.log(e.message);
|
114
|
+
}
|
115
|
+
}
|
115
116
|
}
|
116
117
|
export default {
|
117
|
-
|
118
|
+
Parser,
|
118
119
|
};
|
119
120
|
/* main entrypoint */
|
120
121
|
if (require.main === module) {
|
121
|
-
|
122
|
+
main();
|
122
123
|
}
|