-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·106 lines (90 loc) · 2.81 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
#!/usr/bin/env node
const glob = require('glob');
const fs = require('fs');
const colors = require('colors');
const yargs = require('yargs');
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 configuration = require(`${options.config}`);
const CommonUtils = require('./src/utils/common');
const ConfigUtils = require('./src/utils/configValidation');
const FileUtils = require('./src/utils/writeInFile');
const MDParseLine = require('./src/utils/mdParseLine');
const ParsePropTypes = require('./src/utils/parsePropTypesToTable');
var reactDocs = require('react-docgen');
const filterFileWithTags = require('./src/filterFileWithTags');
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 componentInfo = reactDocs.parse(fileData, null, null, options);
const componentName = componentInfo.displayName;
const mdFilePathWithComponantName = CommonUtils.createMDFilePath(
componentName
);
const fileSeparatedByTags = filterFileWithTags(fileData);
if (fileSeparatedByTags.IGNORE || componentName === '') {
return;
}
arrayOfComponentsToIndex.push(componentName);
const propTypesTable = ParsePropTypes.parseProptypeToMDTable(
componentInfo.props
);
if (fileSeparatedByTags.MDLINES.length > 0) {
FileUtils.writeMDFile(
componentName,
mdFilePathWithComponantName,
fileSeparatedByTags.MDLINES,
propTypesTable
);
} else if (fileSeparatedByTags.NOTAGSRESULT.length > 0) {
FileUtils.writeMDFile(
componentName,
mdFilePathWithComponantName,
fileSeparatedByTags.NOTAGSRESULT,
propTypesTable
);
}
};
/** MAIN */
global.globalConfiguration = ConfigUtils.treatConfigurationProps(configuration);
if (ConfigUtils.isValideDirectoriesProps()) {
getFilesFromInputDirectories((err, res) => treatFiles(err, res));
}