-
Notifications
You must be signed in to change notification settings - Fork 0
/
budget-to-formula.ts
executable file
·87 lines (63 loc) · 2.31 KB
/
budget-to-formula.ts
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
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
#!/usr/bin/awk BEGIN{system("deno run --unstable --allow-read --allow-run "ARGV[1]" "ARGV[2]" "ARGV[3]" "ARGV[4]" "ARGV[5])}
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import { exec } from "https://deno.land/x/[email protected]/mod.ts";
import util from "https://deno.land/[email protected]/node/util.ts";
util.inspect.defaultOptions.maxArrayLength = 10000;
// import makeloc from 'https://deno.land/x/[email protected]/mod.ts'
//TODO awk loop concat
const decoder = new TextDecoder('utf-8');
function readFileSync(filePath: string): string {
// return fs.readFileSync(filePath, 'utf8')
return decoder.decode(Deno.readFileSync(filePath));
}
let scriptPath = path.fromFileUrl(import.meta.url);
let scriptFile = path.basename(scriptPath);
if (Deno.args.length < 1) {
console.log(
`Usage:
${scriptFile} [BUDGET_FILE_NAME]`
);
Deno.exit(1);
}
let budgetFilePath = Deno.args[0]
console.log(budgetFilePath);
let budgetFileContents = readFileSync(budgetFilePath)
let budgetLinesRaw: string[] = budgetFileContents.match(/[^\r\n]+/g) || [];
let budgetLines: string[] = budgetLinesRaw.filter((line: string) => {
return !line.match(/^(\#|\/|\()/)
})
let summandsRaw: Array<string|undefined> = budgetLines.map((line: string) => {
let match = line.match('[0-9\+\-]+[\s\?]*$')
if (match) {
return match[0];
}
})
let summandsFiltered: string[] = summandsRaw.filter(
(x) => x != undefined
) as string[];
let summands: string[] = summandsFiltered.map((summand: string) => {
summand = summand.replace(/[\s\?]*$/, '');
let match = summand.match(/^[\+\-]/)
return match ? summand : '+' + summand;
})
// map((line?: string) => {
// return line ;
// })
// const { __dirname, __filename } = makeloc(import.meta)
let formula = summands.join('')
formula = formula.replace(/^\+/, '')
let formulaWithPrefix = '= ' + formula
// let command = `sh -c 'echo "${formula}" | bc -l'`
// let command = `sh -c 'echo "1+1" | bc -l'`
// let command = `sh -c "echo '1+1' | bc -l"`
// let command = ['sh', '-c', 'echo 1']
let command = ['sh', '-c', `echo "${formula}" | bc -l`]
// console.log(budgetLines);
console.log(summandsRaw);
console.log(util.inspect(summandsRaw))
// console.log(summands);
console.log(formulaWithPrefix);
// console.log(command)
console.log()
console.log(await exec(command))
// console.log(__filename);