Repository navigation
Expand file tree
/
Copy pathmermex-cli.js
More file actions
57 lines (52 loc) · 2.28 KB
/
Copy pathmermex-cli.js
File metadata and controls
57 lines (52 loc) · 2.28 KB
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
import fs from 'node:fs'
import * as readline from 'node:readline/promises';
import path from "node:path"
import { argv, stdin as input, stdout as output } from 'node:process'
import { mermaidToObject } from "./src/origins/mermaid.js"
import { xstateObjToxstateJS } from "./src/targets/xstate.js"
const rl = readline.createInterface({ input, output })
// https://regexr.com/80sgs
const reFilePath = /^(((?:\.{0,2}\/)?(?:\.?\w+\/)*)([\w.+_-]+(\.?\w+)))$/
// https://regex101.com/r/kQpzzt/1
const reFileName = /(\/?((?<filename>[\w._-]*)*))*/
let cliFilePath, outputPath = ''
if (!argv[2] || !reFilePath.test(argv[2])){
cliFilePath = await rl.question('Please input path & file for mermax to process:\n? ')
if (!reFilePath.test(cliFilePath)){
throw new TypeError("Needs well-formed input file path.")
}
outputPath = await rl.question('Please enter output path (empty/malformed = input path):\n? ')
} else {
cliFilePath = argv[2]
}
const inputPathName = cliFilePath.replace(/.mmd$/, '')
const inputFileName = inputPathName +'.mmd'
console.log('reading: ', inputFileName)
const mermaidText = fs.readFileSync(inputFileName,'utf-8')
const { filename } = reFileName.exec(inputPathName).groups
let outputPathName = inputPathName
if (argv[3] && reFilePath.test(argv[3])) {
outputPath = argv[3]
} else if (!outputPath || !reFilePath.test(outputPath)){
outputPath = inputPathName.substring(0, (inputPathName.length - filename.length -1) )
}
outputPathName = outputPath +'/'+ filename
console.log('outputPath',outputPath)
if (!fs.existsSync(outputPath)){
console.log('creating folder:', outputPath)
fs.mkdirSync(outputPath);
}
// // takes raw mermaidJS state diagram & translates into transitory object
// const objectFSM = mermaidToObject(mermaidText)
// const jsonFileName = outputPathName +'.json'
// fs.writeFileSync(jsonFileName, JSON.stringify( objectFSM, null, 2), (err) => {
// if (err) throw err;
// });
// console.log(jsonFileName +' 💾 written to file')
// takes raw mermaidJS state diagram & translates into XState machine configuration
const jsFileName = outputPathName +'.machine.js'
fs.writeFileSync(jsFileName, xstateObjToxstateJS( mermaidToObject(mermaidText)), (err) => {
if (err) throw err;
});
console.log(jsFileName +' 💾 written to file')
process.exit() // hack to fix some weird readline bug in Bun