|
1 |
| -const fs = require('fs').promises; // needs Node.JS v10+ |
| 1 | +const fs = require('fs').promises; // Needs Node.JS v10+ |
2 | 2 |
|
3 |
| -const { Elm } = require('../build/elm.js'); // build using Makefile... no Webpack around here! |
4 |
| -const { registerPort } = require('./utils.js'); |
| 3 | +const {Elm} = require('../build/elm.js'); // Build using Makefile... no Webpack around here! |
| 4 | +const {registerPort} = require('./utils.js'); |
5 | 5 |
|
6 | 6 | // Async/await is nice! (needs Node.JS v7.6+)
|
7 | 7 | (async function () {
|
| 8 | + // Process command line arguments |
| 9 | + const yargs = require('yargs'); |
| 10 | + const argv = yargs |
| 11 | + .option('main', { |
| 12 | + alias: 'm', |
| 13 | + description: 'The main Elm file', |
| 14 | + type: 'string', |
| 15 | + demandOption: true |
| 16 | + }) |
| 17 | + .option('output', { |
| 18 | + alias: 'o', |
| 19 | + description: 'The format to emit: JavaScript or JSON', |
| 20 | + type: 'string', |
| 21 | + default: 'JavaScript' |
| 22 | + }) |
| 23 | + .help() |
| 24 | + .alias('help', 'h') |
| 25 | + .argv; |
8 | 26 |
|
9 |
| - // process command line arguments |
10 |
| - const yargs = require('yargs') |
11 |
| - const argv = yargs |
12 |
| - .option('main', { |
13 |
| - alias: 'm', |
14 |
| - description: 'The main Elm file', |
15 |
| - type: 'string', |
16 |
| - demandOption: true |
17 |
| - }) |
18 |
| - .option('output', { |
19 |
| - alias: 'o', |
20 |
| - description: 'The format to emit: JavaScript or JSON', |
21 |
| - type: 'string', |
22 |
| - default: 'JavaScript' |
23 |
| - }) |
24 |
| - .help() |
25 |
| - .alias('help', 'h') |
26 |
| - .argv; |
| 27 | + console.log('---------------------------'); |
| 28 | + console.log('-- STARTING THE COMPILER --'); |
| 29 | + console.log('---------------------------'); |
27 | 30 |
|
28 |
| - console.log('---------------------------'); |
29 |
| - console.log('-- STARTING THE COMPILER --'); |
30 |
| - console.log('---------------------------'); |
| 31 | + const mainFileContents = await fs.readFile(argv.main, {encoding: 'utf8'}); |
| 32 | + const elmJson = await fs.readFile('./elm.json', {encoding: 'utf8'}); |
31 | 33 |
|
32 |
| - const mainFileContents = await fs.readFile(argv.main, { encoding: 'utf8' }); |
33 |
| - const elmJson = await fs.readFile('./elm.json', { encoding: 'utf8' }); |
34 |
| - |
35 |
| - const app = Elm.Main.init({ |
36 |
| - flags: { |
37 |
| - mainFilePath: argv.main, |
38 |
| - mainFileContents, |
39 |
| - elmJson, |
40 |
| - outputFormat: argv.output |
41 |
| - } |
42 |
| - }); |
43 |
| - |
44 |
| - registerPort(app, 'stdout', string => process.stdout.write(string)); |
45 |
| - registerPort(app, 'stderr', string => { |
46 |
| - process.stderr.write('\n---------------------------'); |
47 |
| - process.stderr.write('\n-- COMPILER ERROR ---------'); |
48 |
| - process.stderr.write('\n---------------------------'); |
49 |
| - process.stderr.write(`\n${string}`); |
50 |
| - }); |
51 |
| - registerPort(app, 'read', async function ({moduleName, filePath}) { |
52 |
| - try { |
53 |
| - const fileContents = await fs.readFile(filePath, { encoding: 'utf8' }); |
54 |
| - app.ports.readSubscription.send({ |
55 |
| - filePath, |
56 |
| - fileContents, |
57 |
| - }); |
58 |
| - } catch (e) { |
59 |
| - if (e.code != null) { |
60 |
| - app.ports.readErrorSubscription.send({ |
61 |
| - moduleName, |
62 |
| - filePath, |
63 |
| - errorCode: e.code, |
64 |
| - }); |
65 |
| - } |
66 |
| - } |
67 |
| - }); |
68 |
| - registerPort(app, 'writeToFile', async function ({ filePath, fileContents }) { |
69 |
| - setTimeout(() => { |
70 |
| - process.stdout.write('---------------------------\n'); |
71 |
| - process.stdout.write('-- WRITING TO FS ----------\n'); |
72 |
| - process.stdout.write('---------------------------\n'); |
73 |
| - process.stdout.write(`${fileContents}\n`); |
74 |
| - }, 0); |
75 |
| - await fs.writeFile(filePath, fileContents); |
76 |
| - }); |
77 |
| - registerPort(app, 'setExitCode', code => { |
78 |
| - process.exitCode = code; |
79 |
| - }); |
| 34 | + const app = Elm.Main.init({ |
| 35 | + flags: { |
| 36 | + mainFilePath: argv.main, |
| 37 | + mainFileContents, |
| 38 | + elmJson, |
| 39 | + outputFormat: argv.output |
| 40 | + } |
| 41 | + }); |
80 | 42 |
|
| 43 | + registerPort(app, 'stdout', string => process.stdout.write(string)); |
| 44 | + registerPort(app, 'stderr', string => { |
| 45 | + process.stderr.write('\n---------------------------'); |
| 46 | + process.stderr.write('\n-- COMPILER ERROR ---------'); |
| 47 | + process.stderr.write('\n---------------------------'); |
| 48 | + process.stderr.write(`\n${string}`); |
| 49 | + }); |
| 50 | + registerPort(app, 'read', async ({moduleName, filePath}) => { |
| 51 | + try { |
| 52 | + const fileContents = await fs.readFile(filePath, {encoding: 'utf8'}); |
| 53 | + app.ports.readSubscription.send({ |
| 54 | + filePath, |
| 55 | + fileContents |
| 56 | + }); |
| 57 | + } catch (error) { |
| 58 | + if (error.code !== null && error.code !== undefined) { |
| 59 | + app.ports.readErrorSubscription.send({ |
| 60 | + moduleName, |
| 61 | + filePath, |
| 62 | + errorCode: error.code |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | + registerPort(app, 'writeToFile', async ({filePath, fileContents}) => { |
| 68 | + setTimeout(() => { |
| 69 | + process.stdout.write('---------------------------\n'); |
| 70 | + process.stdout.write('-- WRITING TO FS ----------\n'); |
| 71 | + process.stdout.write('---------------------------\n'); |
| 72 | + process.stdout.write(`${fileContents}\n`); |
| 73 | + }, 0); |
| 74 | + await fs.writeFile(filePath, fileContents); |
| 75 | + }); |
| 76 | + registerPort(app, 'setExitCode', code => { |
| 77 | + process.exitCode = code; |
| 78 | + }); |
81 | 79 | })();
|
0 commit comments