-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·46 lines (39 loc) · 1.24 KB
/
cli.js
File metadata and controls
executable file
·46 lines (39 loc) · 1.24 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
#!/usr/bin/env node
const fs = require('fs')
const translations = require('./playground_v2/src/lib/mamaTranslations')
const createKeywordsRegex = () => {
const keywords = Object.keys(translations).map((keyword) =>
keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
)
const pattern = `^(?:${keywords.join('|')})$`
return new RegExp(pattern)
}
const translateKeywordToJS = (keyword) => {
return translations[keyword] || keyword
}
const convertToJS = (sourceCode) => {
Object.keys(translations).forEach((keyword) => {
// Use a regex to match whole words only to prevent partial replacements
const regex = new RegExp(`\\b${keyword}\\b`, 'g')
sourceCode = sourceCode.replace(regex, translations[keyword])
})
return sourceCode
}
const runMamaLang = (filename) => {
// Check if the filename is valid
if (!filename || filename.startsWith('--')) {
console.error('Mama file er name tah teh ektu dekh mama! Na likhe thakle kisu toh likh mama!')
return
}
const sourceCode = fs.readFileSync(filename, 'utf8')
const jsCode = convertToJS(sourceCode, translations)
eval(jsCode)
}
const filename = process.argv[2]
runMamaLang(filename)
module.exports = {
createKeywordsRegex,
translateKeywordToJS,
convertToJS,
runMamaLang,
}