Browse Source

Reorganize bin.js a little

master
Dylan Baker 5 years ago
parent
commit
9ae2911337
3 changed files with 51 additions and 23 deletions
  1. 30
    23
      bin/oslo.js
  2. 17
    0
      repl.js
  3. 4
    0
      src/evaluator.js

+ 30
- 23
bin/oslo.js View File

@@ -9,7 +9,10 @@ const oslo = require('../src/index')
9 9
 class OsloCLI {
10 10
   constructor(opts) {
11 11
     this.context = {}
12
+    this.eval(opts)
13
+  }
12 14
 
15
+  eval(opts) {
13 16
     if (opts.l) {
14 17
       this.context = JSON.parse(fs.readFileSync(this.absolutePath(opts.l)))
15 18
     }
@@ -41,29 +44,7 @@ class OsloCLI {
41 44
     }
42 45
 
43 46
     if (opts.o) {
44
-      const absoluteOutputPath = this.absolutePath(opts.o)
45
-      if (opts.f || opts.e) {
46
-        fs.writeFile(absoluteOutputPath, output, function(err) {
47
-          if (err) throw err
48
-        })
49
-      } else if (opts.d) {
50
-        const absoluteInputPath = this.absolutePath(opts.d)
51
-        Object.keys(output).forEach(function(file) {
52
-          const absoluteOutputFilePath = file
53
-            .replace(absoluteInputPath, absoluteOutputPath)
54
-            .replace(/\.oslo$/, '.html')
55
-          const parentDirectory = path.dirname(absoluteOutputFilePath)
56
-          if (!fs.existsSync(parentDirectory)) {
57
-            fs.mkdirSync(parentDirectory)
58
-          }
59
-          fs.writeFile(absoluteOutputFilePath, output[file], function(err) {
60
-            if (err) throw err
61
-            console.log(
62
-              `Writing to ${absoluteOutputFilePath.replace(process.cwd(), '')}`,
63
-            )
64
-          })
65
-        })
66
-      }
47
+      this.output(opts, output)
67 48
     } else {
68 49
       process.stdout.write(output)
69 50
       process.stdout.write('\n')
@@ -100,6 +81,32 @@ class OsloCLI {
100 81
     return path.join(root, file)
101 82
   }
102 83
 
84
+  output(opts, output) {
85
+    const absoluteOutputPath = this.absolutePath(opts.o)
86
+    if (opts.f || opts.e) {
87
+      fs.writeFile(absoluteOutputPath, output, function(err) {
88
+        if (err) throw err
89
+      })
90
+    } else if (opts.d) {
91
+      const absoluteInputPath = this.absolutePath(opts.d)
92
+      Object.keys(output).forEach(function(file) {
93
+        const absoluteOutputFilePath = file
94
+          .replace(absoluteInputPath, absoluteOutputPath)
95
+          .replace(/\.oslo$/, '.html')
96
+        const parentDirectory = path.dirname(absoluteOutputFilePath)
97
+        if (!fs.existsSync(parentDirectory)) {
98
+          fs.mkdirSync(parentDirectory)
99
+        }
100
+        fs.writeFile(absoluteOutputFilePath, output[file], function(err) {
101
+          if (err) throw err
102
+          console.log(
103
+            `Writing to ${absoluteOutputFilePath.replace(process.cwd(), '')}`,
104
+          )
105
+        })
106
+      })
107
+    }
108
+  }
109
+
103 110
   printUsage() {
104 111
     console.log(
105 112
       `USAGE

+ 17
- 0
repl.js View File

@@ -0,0 +1,17 @@
1
+const readline = require('readline')
2
+
3
+async function repl() {
4
+  let output
5
+
6
+  const rl = readline.createInterface({
7
+    input: process.stdin,
8
+    output: process.stdout,
9
+  })
10
+
11
+  rl.question('hello? ', line => {
12
+    console.log(`you said ${line}`)
13
+    rl.close()
14
+  })
15
+}
16
+
17
+repl()

+ 4
- 0
src/evaluator.js View File

@@ -15,6 +15,10 @@ module.exports = class Evaluator {
15 15
       }
16 16
     })
17 17
 
18
+    console.log(
19
+      require('util').inspect(evaluatedTree, { depth: null })
20
+    )
21
+
18 22
     return evaluatedTree
19 23
   }
20 24
 

Loading…
Cancel
Save