forked from chromium/axiom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwash.js
executable file
·110 lines (90 loc) · 3.28 KB
/
wash.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env nodejs
if (!process.env.NODE_PATH) {
console.log('Please set NODE_PATH to an absolute /path/to/axiom/tmp/cjs/lib');
process.exit(-1);
}
global.Promise = require('es6-promise').Promise;
require('source-map-support').install();
var AxiomError = require('axiom/core/error').default;
var Path = require('axiom/fs/path').default;
var FileSystemManager = require('axiom/fs/base/file_system_manager').default;
var StdioSource = require('axiom/fs/stdio_source').default;
var JsFileSystem = require('axiom/fs/js/file_system').default;
var NodeFileSystem = require('axiom/fs/node/file_system').default;
var TTYState = require('axiom/fs/tty_state').default;
var washExecutables = require('wash/exe_modules').dir;
if ('setRawMode' in process.stdin) {
// Stdin seems to be missing setRawMode under grunt.
process.stdin.setRawMode(true);
}
function onResize(stdioSource) {
var tty = new TTYState();
tty.isatty = process.stdout.isTTY;
tty.rows = process.stdout.rows;
tty.columns = process.stdout.columns;
stdioSource.signal.write({name: 'tty-change', value: tty});
}
function startWash(fsm) {
// TODO(rpaquay)
var stdioSource = new StdioSource();
var stdio = stdioSource.stdio;
return fsm.createExecuteContext(new Path('jsfs:exe/wash'), stdio, {}).then(
function(cx) {
stdioSource.stdout.onData.addListener(function(value) {
process.stdout.write(value);
});
stdioSource.stderr.onData.addListener(function(value) {
process.stderr.write(value);
});
cx.onReady.addListener(function() {
// Resume all streams (except stdin as we want to buffer input until a
// consumer is ready to process it).
stdioSource.stdout.resume();
stdioSource.stderr.resume();
stdioSource.stdio.signal.resume();
}.bind(this));
cx.onClose.addListener(function(reason, value) {
console.log('wash closed: ' + reason + ', ' + value);
});
process.stdin.on('data', function(buffer) {
if (buffer == '\x03')
cx.closeError(new AxiomError.Interrupt());
stdioSource.stdin.write(buffer.toString());
});
onResize(stdioSource);
process.stdout.on('resize', onResize.bind(null, stdioSource));
var home = new Path('nodefs:').combine(process.env.HOME);
cx.setEnv('$HOME', home.spec);
cx.setEnv('$HISTFILE', home.combine('.wash_history').spec);
if (process.env.PWD) {
cx.setEnv('$PWD', new Path('nodefs:').combine(process.env.PWD).spec);
}
cx.setEnv('@PATH', [new Path('jsfs:exe').spec]);
return cx.execute();
});
}
function main() {
var jsfs = new JsFileSystem();
var fsm = jsfs.fileSystemManager;
return jsfs.rootDirectory.mkdir('exe').then(function(jsdir) {
jsdir.install(washExecutables);
mountNodefs(fsm);
return startWash(fsm);
});
}
function mountNodefs(fsm) {
var fs = require('fs');
NodeFileSystem.mount(fsm, 'nodefs', fs);
}
module.exports = { main: main };
if (/wash.js$/.test(process.argv[1])) {
// Keep node from exiting due to lack of events.
var aliveInterval = setInterval(function() {}, 60 * 1000);
main().then(function(value) {
console.log('exit:', value);
process.exit();
}).catch(function(err) {
console.log('Uncaught exception:', err, err.stack);
process.exit();
});
}