forked from TobiasNickel/teditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (136 loc) · 4.87 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const path = require('path');
const express = require('express');
require('tasyncexpress');
const fs = require('fs-extra');
function opt(argv) {
var opt = {}, arg, p; argv = Array.prototype.slice.call(argv || process.argv); for (var i = 2; i < argv.length; i++)if (argv[i].charAt(0) == '-')
((p = (arg = ("" + argv.splice(i--, 1)).replace(/^[\-]+/, '')).indexOf("=")) > 0 ? opt[arg.substring(0, p)] = arg.substring(p + 1) : opt[arg] = true);
return { 'node': argv[0], 'script': argv[1], 'argv': argv.slice(2), 'opt': opt };
}
var options = opt();
const workspacePath = path.resolve(options.opt.dir || options.opt.directory || '.');
const port = options.opt.p || options.opt.port || process.env.PORT || 3000;
const host = options.opt.host || '127.0.0.1';
const editor = { t: 'editor', port: process.env.PORT || 3000, dir: workspacePath };
if(options.opt.h || options.opt.help){
console.log('usage: teditor --dir=. --host=127.0.0.1 --port=3000');
console.log('\tThen open the port shown in the browser');
console.log('options:')
console.log('\t--dir=. \tthe directory to edit in the ide, default is the current directory.')
console.log('\t--host=0.0.0.0 \tset this to 0.0.0.0 to access from anywhere, or to the IP address of a specific interface on the host to serve from there, default is localhost');
console.log('\t--port=3000 \tthe port to bind, default is 3000');
console.log('')
process.exit();
return;
}
console.log(editor)
const app = express();
const indexRoute = async (req,res)=>{
// console.log(req.headers);
var index = (await fs.readFile(__dirname+'/index.html')).toString();
if(req.headers.referer){
index = index.split('localhost:3000').join(new URL(req.headers.referer).host)
}
res.send(index)
}
app.get('/', indexRoute)
app.get('/index.html', indexRoute)
app.get('/index.htm', indexRoute)
app.use(express.static(__dirname+'/public'));
app.use(express.json())
app.get('/fs/.vscode/settings.json', (req, res) => {
res.json({})
});
app.get('/fs/.vscode/tasks.json', (req, res) => {
res.json({})
});
app.get('/fs/.vscode/launch.json', (req, res) => {
res.json({})
});
var count = 0;
app.use((req, res, next) => {
console.log(count++, req.method, req.url);
next();
});
app.get('/readdir/*', async (req, res) => {
try {
console.log('--',req.path,req.path.substr(8))
const dirPath = pathToWorkspace(req.path.substr(8));
console.log('readdir', dirPath)
const dir = await fs.readdir(dirPath);
var vsCodeDir = await Promise.all(dir.filter(e => !['.', '..'].includes(e)).map(async e => {
const stat = await fs.stat(dirPath + '/' + e);
return [
e,
stat.isDirectory() ? 2 : (stat.isFile() ? 1 : 0)
];
}));
res.json(vsCodeDir);
} catch (err) {
console.log(err)
res.status(400).json(err);
}
})
app.get('/fileStat/*', async (req, res) => {
try {
const stat = await fs.stat(pathToWorkspace(req.path.substr(9)))
JSON.stringify(stat)
stat.type = stat.isDirectory() ? 2 : (stat.isFile() ? 1 : 0)
stat.atime = stat.atime.getTime();
stat.mtime = stat.mtime.getTime();
stat.ctime = stat.ctime.getTime();
stat.uri = '/fs' + req.path.substr(12);
res.json(stat)
} catch (err) {
if (err.code === 'ENOENT') {
res.json(null)
return;
}
console.log(err)
res.status(400).json(err)
}
})
app.use('/fs', express.static(workspacePath, {
redirect: false
}));
app.delete('/deletefile', async (req, res) => {
console.log(req.body)
const path = req.body.resource.path
await fs.unlink(pathToWorkspace(path));
res.json(true);
})
app.post('/writefile', async (req, res) => {
const filePath = pathToWorkspace(req.body.resource.path);
const content = req.body.content;
console.log('write:', { filePath, content })
await fs.writeFile(filePath, content);
res.json(true);
});
app.post('/rename', async (req, res) => {
console.log('rename', req.body)
const formPath = pathToWorkspace(req.body.from.path);
const toPath = pathToWorkspace(req.body.to.path);
await fs.move(formPath, toPath, { overwrite: true });
res.json(true);
});
app.post('/mkdir', async (req, res) => {
console.log(req.body);
const newDirPath = pathToWorkspace(req.body.resource.path);
await fs.mkdir(newDirPath);
res.json(true)
})
app.listen(port, host, (err) => {
if (err) {
console.log(err);
}
});
/**
*
* @param {string} inPath
*/
function pathToWorkspace(inPath) {
if (!inPath.startsWith('/fs')) { throw new Error('not in fs'); }
if (inPath.includes('..')) { throw new Error('stay in fs ! !'); }
if (inPath.substr(4).startsWith('/')) { throw new Error('stay in fs ! !'); }
return path.resolve(workspacePath, inPath.substr(4));
}