forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_http_index.ts
More file actions
executable file
·72 lines (62 loc) · 2.03 KB
/
make_http_index.ts
File metadata and controls
executable file
·72 lines (62 loc) · 2.03 KB
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
#! /usr/bin/env node
import * as fs from 'fs';
import * as path from 'path';
const parser = require('gitignore-parser');
const symLinks: {[dev: number]: {[ino: number]: boolean}} = {};
const ignoreFiles = ['.git'];
type FileTree = {[name: string]: FileTree | null};
let vscodeignores: {[path: string]: { denies: (p: string) => boolean } | null} = {}
function rdSync(dpath: string, tree: FileTree): FileTree {
const files = fs.readdirSync(dpath);
if (files.indexOf('.vscodeignore') > -1) {
vscodeignores[dpath] = parser.compile(fs.readFileSync(path.join(dpath, '.vscodeignore'), 'utf8'));
}
const vscodeignorePath = Object.keys(vscodeignores).find(f => dpath.indexOf(f) === 0);
const vscodeignore = vscodeignorePath ? vscodeignores[vscodeignorePath] : undefined;
files.forEach((file) => {
// ignore non-essential directories / files
if (ignoreFiles.indexOf(file) !== -1 || file[0] === '.') {
return;
}
const fpath = `${dpath}/${file}`;
if (vscodeignore && vscodeignore.denies(fpath.replace(vscodeignorePath!, ''))) {
return;
}
try {
// Avoid infinite loops.
const lstat = fs.lstatSync(fpath)
if (lstat.isSymbolicLink()) {
if (!symLinks[lstat.dev]) {
symLinks[lstat.dev] = {};
}
// Ignore if we've seen it before
if (symLinks[lstat.dev][lstat.ino]) {
return;
}
symLinks[lstat.dev][lstat.ino] = true;
}
const fstat = fs.statSync(fpath);
if (fstat.isDirectory()) {
const child = tree[file] = {}
rdSync(fpath, child)
} else {
tree[file] = null
}
} catch (e) {
// Ignore and move on.
}
});
return tree
}
const fsListing = JSON.stringify(rdSync(process.cwd(), {}));
if (process.argv.length === 3) {
const fname = process.argv[2];
let parent = path.dirname(fname);
while (!fs.existsSync(parent)) {
fs.mkdirSync(parent);
parent = path.dirname(parent);
}
fs.writeFileSync(fname, fsListing, { encoding: 'utf8' });
} else {
console.log(fsListing);
}