forked from plotly/dash-pivottable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-meta.js
93 lines (78 loc) · 2.22 KB
/
extract-meta.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const reactDocs = require('react-docgen');
const componentPaths = process.argv.slice(2);
if (!componentPaths.length) {
help();
process.exit(1);
}
const metadata = Object.create(null);
componentPaths.forEach(componentPath =>
collectMetadataRecursively(componentPath)
);
writeOut(metadata);
function help() {
console.error('usage: ');
console.error(
'extract-meta path/to/component(s) ' +
' [path/to/more/component(s), ...] > metadata.json'
);
}
function writeError(msg, filePath) {
if (filePath) {
process.stderr.write(`Error with path ${filePath}`);
}
process.stderr.write(msg + '\n');
if (msg instanceof Error) {
process.stderr.write(msg.stack + '\n');
}
}
function checkWarn(name, value) {
if (value.length < 1) {
process.stderr.write(`\nDescription for ${name} is missing!\n`)
}
}
function docstringWarning(doc) {
checkWarn(doc.displayName, doc.description);
Object.entries(doc.props).forEach(
([name, p]) => checkWarn(`${doc.displayName}.${name}`, p.description)
);
}
function parseFile(filepath) {
const urlpath = filepath.split(path.sep).join('/');
let src;
if (!['.jsx', '.js'].includes(path.extname(filepath))) {
return;
}
try {
src = fs.readFileSync(filepath);
const doc = metadata[urlpath] = reactDocs.parse(src);
docstringWarning(doc);
} catch (error) {
writeError(error, filepath);
}
}
function collectMetadataRecursively(componentPath) {
if (fs.lstatSync(componentPath).isDirectory()) {
let dirs;
try {
dirs = fs.readdirSync(componentPath);
} catch (error) {
writeError(error, componentPath);
}
dirs.forEach(filename => {
const filepath = path.join(componentPath, filename);
if (fs.lstatSync(filepath).isDirectory()) {
collectMetadataRecursively(filepath);
} else {
parseFile(filepath);
}
});
} else {
parseFile(componentPath);
}
}
function writeOut(result) {
console.log(JSON.stringify(result, '\t', 2));
}