This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·94 lines (82 loc) · 2.22 KB
/
index.js
File metadata and controls
executable file
·94 lines (82 loc) · 2.22 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
const program = require("commander");
const marked = require("marked");
const TerminalRenderer = require("marked-terminal");
const npmRoot = require("npm-root");
const glob = require("glob");
const fs = require("fs");
marked.setOptions({
renderer: new TerminalRenderer()
});
program
.version("1.0.0")
.usage("[options] <module_name>")
.option("-g, --global", "Search global cache")
.arguments("<module>")
.action((module, cmd) => {
checkLocalMachine(module, !!cmd.global)
.then((modulePath) => {
if (modulePath) {
return readFileFromDisk(modulePath);
} else {
throw new Error("README not installed on local machine");
}
})
.then((markdown) => {
renderMarkdown(markdown);
})
.catch((err) => {
console.log(err);
});
})
.parse(process.argv);
function checkLocalMachine(module, searchGlobal) {
const pathsPromiseArray = [];
pathsPromiseArray.push(new Promise((resolve, reject) => {
glob(`node_modules/**/${module}/[rR][eE][aA][dD][mM][eE].[mM][dD]`, (globErr, files) => {
if (!globErr && files && files.length > 0) {
resolve(files[0]);
} else {
resolve(null);
}
})
}));
if (searchGlobal) {
// glob search the npm globule modules directory
pathsPromiseArray.push(new Promise((resolve, reject) => {
npmRoot({global: true}, (rootErr, globalPath) => {
if (!rootErr && globalPath) {
glob(`${globalPath}/**/${module}/[rR][eE][aA][dD][mM][eE].[mM][dD]`, (globErr, files) => {
if (!globErr && files && files.length > 0) {
resolve(files[0]);
} else {
resolve(null);
}
});
}
});
}));
}
return Promise.all(pathsPromiseArray).then((pathsResults) => {
return pathsResults.find((possiblePath) => !!possiblePath);
}).catch((err) => {
console.log(err);
});
}
function readFileFromDisk(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, "utf8", (err, data) => {
if (err || !data) {
reject(err || "Tried to read local file, but no data was read");
} else {
resolve(data);
}
});
});
}
// TODO if README not found locally, try to find it online
function retrieveFromRemoteRepository(module) {
}
function renderMarkdown(markdown) {
console.log(marked(markdown));
}