-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·123 lines (100 loc) · 3.17 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
#!/usr/bin/env node
const glob = require("glob");
const fs = require("fs");
const colors = require("colors");
const yargs = require("yargs");
const path = require("path");
const options = yargs.usage("Usage: --config <ConfigFilePath>").option("c", {
alias: "config",
describe: "Path to cfg file js-to-markdown.config.js",
type: "string",
demandOption: true,
}).argv;
/** LOCAL DEPENDENCIES */
const CommonUtils = require("./src/utils/common");
const ConfigUtils = require("./src/utils/configValidation");
const FileUtils = require("./src/utils/writeInFile");
const ParsePropTypes = require("./src/propTypesToTable");
var reactDocs = require("react-docgen");
const filterFileWithTags = require("./src/filterFileWithTags");
let configuration = "";
if (ConfigUtils.isValidPath(options.config)) {
const pathResolved = path.resolve(options.config.toString());
configuration = require(`${pathResolved}`);
} else {
CommonUtils.logError("Invalid configuration file path.");
process.exit(10);
}
const arrayOfComponentsToIndex = [];
const getFilesFromInputDirectories = function (callback) {
glob(
globalConfiguration.input +
(globalConfiguration.subfolders ? "/**" : "") +
"/*.js",
callback
);
};
const treatFiles = (err, res) => {
if (err) {
CommonUtils.logError(err);
} else {
res.forEach((filePath) => {
processInputFile(filePath);
});
if (globalConfiguration.outputJsMarkdown) {
FileUtils.writeIndexFile(arrayOfComponentsToIndex);
}
CommonUtils.logSuccess(
`${arrayOfComponentsToIndex.length} markdowns generated at ${globalConfiguration.output}`
);
}
};
const processInputFile = (filePath) => {
const fileData = fs.readFileSync(filePath, "UTF-8");
if (fileData.length <= 0) {
return;
}
const options = {
filename: filePath,
parserOptions: {
plugins: ["jsx", "classProperties"],
errorRecovery: true,
},
};
var filename = filePath.replace(/^.*[\\\/]/, "").replace(".js", "");
let regexComponentName;
try {
var componentInfo = reactDocs.parse(fileData, null, null, options);
} catch (err) {
componentInfo = {};
regexComponentName = CommonUtils.getComponentNameWithRegex(fileData);
}
const componentName =
componentInfo.displayName || regexComponentName || filename;
const mdFilePathWithComponantName = CommonUtils.createMDFilePath(
componentName
);
const fileSeparatedByTags = filterFileWithTags(fileData);
if (fileSeparatedByTags.IGNORE || componentName === "") {
return;
}
arrayOfComponentsToIndex.push(componentName);
let propTypesTable = [];
if (globalConfiguration.propTypesToTable) {
propTypesTable = ParsePropTypes.parseProptypeToMDTable(componentInfo.props);
}
if (fileSeparatedByTags.NOTAGSRESULT.length > 0) {
fileSeparatedByTags.MDLINES = fileSeparatedByTags.NOTAGSRESULT;
}
FileUtils.writeMDFile(
componentName,
mdFilePathWithComponantName,
fileSeparatedByTags.MDLINES,
propTypesTable
);
};
/** MAIN */
global.globalConfiguration = ConfigUtils.treatConfigurationProps(configuration);
if (ConfigUtils.isValidDirectoriesProps()) {
getFilesFromInputDirectories((err, res) => treatFiles(err, res));
}