Skip to content

Commit f1c3bc4

Browse files
committed
Add a makeIndex utility
1 parent 903f1ef commit f1c3bc4

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/__tests__/CacheFS.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import CacheFS from "../CacheFS";
2+
3+
const fs = new CacheFS();
4+
5+
const treeText = require('./__fixtures__/tree.txt.js');
6+
7+
describe("CacheFS module", () => {
8+
it("print ∘ parse == id", () => {
9+
let parsed = fs.parse(treeText)
10+
let text = fs.print(parsed)
11+
expect(text).toEqual(treeText)
12+
});
13+
});

src/__tests__/__fixtures__/tree.txt renamed to src/__tests__/__fixtures__/tree.txt.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.git 777
1+
module.exports = `.git 777
22
hooks 777
33
info 777
44
objects 777
@@ -1765,4 +1765,4 @@ package.json 666 3996 1545535032598
17651765
renovate.json 666 244 1545535032598
17661766
rollup.config.js 666 804 1545535032598
17671767
tsconfig.json 666 581 1545535032598
1768-
webpack.config.js 666 1730 1545535032599
1768+
webpack.config.js 666 1730 1545535032599`

src/makeIndex.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
var symLinks = {}
4+
5+
module.exports = (dirpath) => {
6+
let str = "";
7+
const printTree = (root, indent) => {
8+
let files = fs.readdirSync(root)
9+
for (let file of files) {
10+
let fpath = `${root}/${file}`
11+
let lstat = fs.lstatSync(fpath)
12+
// Avoid infinite loops.
13+
if (lstat.isSymbolicLink()) {
14+
if (!symLinks[lstat.dev]) {
15+
symLinks[lstat.dev] = {}
16+
}
17+
// Skip this entry if we've seen it before
18+
if (symLinks[lstat.dev][lstat.ino]) {
19+
continue
20+
}
21+
symLinks[lstat.dev][lstat.ino] = true
22+
}
23+
let mode = lstat.mode.toString(8);
24+
str += `\n${"\t".repeat(indent)}`
25+
if (lstat.isDirectory()) {
26+
str += `${file}\t${mode}`;
27+
printTree(fpath, indent + 1);
28+
} else {
29+
str += `${file}\t${mode}\t${lstat.size}\t${lstat.mtimeMs}`;
30+
}
31+
}
32+
};
33+
printTree(dirpath, 0);
34+
return str.trimStart();
35+
}

0 commit comments

Comments
 (0)