markdown-maker 1.10.2 → 1.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 { argParser, CLArgs as CommmandLineArgs, ParserOptions } from "./cltool";
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
- let clargs: CommmandLineArgs;
12
- let server: WebSocketServer | undefined;
13
-
14
- /* Read config file or parse args from cmd-line */
15
- if (fs.existsSync(configFileName)) {
16
- let data: CommmandLineArgs = JSON.parse(
17
- fs.readFileSync(configFileName).toString()
18
- ).opts;
19
-
20
- let args: (string | number)[] = [];
21
- Object.entries(data).forEach(([key, value]) => {
22
- if (key != "src" && value !== false) {
23
- args.push("--" + key);
24
- }
25
- if (typeof value != "boolean") {
26
- args.push(value);
27
- }
28
- });
29
-
30
- /* We skip [0] and [1], as it is the binary and source file, even when compiled*/
31
- for (let i = 2; i < process.argv.length; i++)
32
- args.push(process.argv[i]);
33
-
34
- clargs = argParser.parse_args(args.map((x) => x.toString()));
35
- } else {
36
- clargs = argParser.parse_args();
37
- }
38
-
39
- /* if src is init, create config file and exit */
40
- if (clargs.src == "init") {
41
- const template = fs.readFileSync(
42
- path.join(
43
- __dirname,
44
- "..",
45
- "src",
46
- "templates",
47
- "configTemplate.json"
48
- )
49
- );
50
- fs.writeFileSync(configFileName, template);
51
- fs.writeFileSync("main.md", "# Main\n");
52
- return;
53
- }
54
-
55
- /* helper method for calling parser */
56
- const compile = (source, output, cb?) => {
57
- /* load data from file, if it exists,
58
- * otherwise, interpret as string */
59
-
60
- const parser = new Parser(source, clargs);
61
- parser.to(output, (file) => {
62
- console.log(`Compiled ${file}`.green);
63
- if (cb) cb();
64
- });
65
- return parser;
66
- };
67
-
68
- const internalCooldown = 1000;
69
- function watcher(event, path) {
70
- const now = Date.now();
71
-
72
- if (!this.time) this.time = now;
73
-
74
- if (now - this.time < internalCooldown) return;
75
-
76
- console.log(path);
77
-
78
- console.log(`Detected change in ${path}...`);
79
-
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 */
102
- if (!clargs.watch) compile(clargs.src, clargs.output);
103
- /* watch the folder and recompile on change */ else {
104
- const srcDirName = path.dirname(clargs.src);
105
- console.log(`Watching ${srcDirName} for changes...`.yellow);
106
- server = new WebSocketServer({ port: 7788 });
107
-
108
- const _watcher = choki.watch(srcDirName).on("all", watcher);
109
- try {
110
- compile(clargs.src, clargs.output);
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
- Parser,
118
+ Parser,
118
119
  };
119
120
  /* main entrypoint */
120
121
  if (require.main === module) {
121
- main();
122
+ main();
122
123
  }