UNPKG

3.37 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path')
4 , fs = require('fs')
5 , cardinal = require('cardinal')
6 , state = require('./state')
7 , utl = require('./utl')
8 , log = require('./log')
9 , rewrite = require('./rewrite')
10 , wire = require('./wire')
11 , requireLike = require('require-like')
12 , talk = require('./talk')
13 , config = require('../config/current')
14 , findexquire = require('./findexquire')
15 ;
16
17module.exports = function feedEdits(stdin, stdout) {
18 var repl = state.repl
19 , rli = repl.rli
20 , feed = config.feed
21 , exportsKey = config.feed.exports || '$';
22
23 state.format = feed.format || {
24 indent : { style: ' ', base: 0 }
25 , quotes : 'single'
26 , json : false
27 , renumber : false
28 , hexadecimal : false
29 , escapeless : false
30 , compact : true
31 , parentheses : false
32 , semicolons : false
33 };
34
35 function feedEdit(file) {
36
37 if (state.fileFeedSuspended) return;
38
39 function adaptGlobals() {
40 global.require = findexquire(file.fullPath, true);
41 global.__filename = file.fullPath;
42 global.__dirname = path.dirname(file.fullPath);
43 global.exports = global.module.exports;
44 }
45
46 function restoreGlobals() {
47 global.require = findexquire(path.join(process.cwd(), 'repl.js'), true);
48 }
49
50 function emitHighlightedCode(src, format) {
51 if (config.highlight) {
52 // force 'compact' since there is no point in sourcing entire code if we printed it highlighted already
53 format.compact = true;
54 try {
55 stdout.write(cardinal.highlight(src, { linenos: true }) + '\n');
56 } catch(e) { }
57 }
58 }
59
60 function rewriteCode(src, format) {
61 try {
62 return rewrite(src, format);
63 } catch (e) {
64 stdout.write('\n');
65 log.error('Unable to parse source from: ' + file.path + '\n' + e);
66 return null;
67 }
68 }
69
70 function emitCode(rewritten) {
71 // ensure emitted lines don't become part of the history
72 var currentHist = rli.history.slice(0);
73
74 wire.emit('emit-code.start');
75
76 try {
77 // source last in order to have results show last
78 stdin.emit('data', rewritten);
79 } catch(e) { }
80
81 wire.emit('emit-code.finish');
82 rli.history = currentHist;
83 }
84
85 function talkCode(code) {
86 talk(code, file);
87 }
88
89 fs.readFile(file.fullPath, 'utf-8', function (err, src) {
90 var format, rewritten;
91
92 if (err) return log.error(err);
93
94 // Avoid code being appended to garbage
95 rli.clearLine();
96
97 format = utl.shallowClone(state.format);
98
99 state.feedingFile = true;
100 emitHighlightedCode(src, format);
101
102 rewritten = rewriteCode(src, format);
103
104 if (!rewritten) return repl.displayPrompt();
105
106 try {
107 adaptGlobals();
108
109 emitCode(rewritten);
110 log.displayPrompt();
111
112 // talking after emitting causes error inside difflet in cases so for now we talk first
113 talkCode(src);
114 log.displayPrompt();
115
116 state.lastFedFile = file;
117 global[exportsKey] = global.module.exports;
118
119 } finally {
120 restoreGlobals();
121 repl.displayPrompt();
122 state.feedingFile = false;
123 }
124 });
125 }
126
127 return feedEdit;
128};