-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.js
115 lines (114 loc) · 2.68 KB
/
simulator.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
111
112
113
114
115
var fs = require('fs'),
path = require('path'),
$ = require('jquery'),
File = Object.freeze({
FILE: 0,
DIRECTORY: 1
})
$(function(){
var get = $.get
$.get = function(url, data, callback, type){
if(/^file:\/\/|^fs:|^[a-z~/]/i.test(url)) {
if (callback = data instanceof Function ? data : callback) {
var path = /^fs:/i.test(url) ? url.slice(3) : /^file:\/\//i.test(url) ? url.slice(7) : url
return fs.access(path, function(err) {
if (err) {
console.log(err)
} else {
fs.stat(path, function(err, stats) {
if (stats.isDirectory()) {
fs.readdir(path, callback)
} else {
fs.readFile(path, callback)
}
})
}
})
}
} else {
return get.apply(this, arguments)
}
}
var post = $.post
$.post = function(url, data, callback, type){
if(/^file:\/\/|^fs:/i.test(url)) {
return fs.access(path, fs.R_OK, function(err) {
if (err) {
console.log(err)
} else {
fs.writeFile(
/^fs:/i.test(url) ? url.slice(3) :
/^file:\/\//i.test(url) ? url.slice(7) :
url,
data || '', data instanceof Function ? data : callback || $.noop
)
}
})
} else {
return post.apply(this, arguments)
}
}
time = new Date();
$.get('fs:/home/somebody/node/Desktop.js', function(err, data){
if (data instanceof Array) {
console.log(data)
console.log((new Date() - time)/1000);
} else {
console.log(data.toString())
}
})
//TODO: expand paths
})
$.fn.extend({
findUnescaped: function(string, char) {
var indices = []
for (var i = 0; i < string.length; i++)
if (i === '\\')
escaped = true
else if (i === char) {
indices.push(i)
escaped = false
} else
escaped = false
return indices
},
splitUnescaped: function(string, char) {
var indices = findUnescaped(string, char),
oldIndex = 0
words = []
for (var index in indices) {
words.push(string.slice(oldIndex, index))
oldIndex = ++index
}
},
parseCmd: function(cmd) {
var command = {},
words = $.findUnescaped(cmd, ' '),
currentOption = ''
for(var word in words)
//TODO: parse args
if (arg[0] === arg[1] === '-') {
var argument = word.slice(2)
currentOption = argument
returns[argument] = []
} else if (arg[0] === '-') {
var options = arg.slice(1)
if (options.length == 1) {
var argument = options[0].toString()
currentOption = argument
returns[argument] = []
} else for(var option in options)
returns[option] = true
} else
returns[currentOption].push(arg)
},
jsh: function(cmd) {
var command = parseCmd(cmd)
},
install: function(cmd) {
var command = parseCmd(cmd)
},
remove: function(cmd) {
var command = parseCmd(cmd)
}
})