-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
executable file
·74 lines (60 loc) · 2.01 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
#!/usr/bin/env node
const argsReader = require('./src/argsReader')
const getComponentsFilePaths = argsReader.getComponentsFilePaths
const getTestsRoot = argsReader.getTestsRoot
const parseComponent = require('./src/componentReader').parseComponent
const writeTestsFile = require('./src/testsWriter').writeTestsFile
const valuesGenerator = require('./src/valuesGenerator')
const getInvalidTypes = valuesGenerator.getInvalidTypes
const logger = require('./src/logger')
const logProcessingStart = logger.logProcessingStart
const logNoInterfaceItemsFoundError = logger.logNoInterfaceItemsFoundError
const logNotSupportedTypesError = logger.logNotSupportedTypesError
const logNoFilesError = logger.logNoFilesError
const logNoTestsRootError = logger.logNoTestsRootError
const componentFilePaths = getComponentsFilePaths()
const testsRoot = getTestsRoot()
function run() {
logProcessingStart()
processFiles(componentFilePaths)
}
function processFiles(componentFilePaths) {
// Walk the files with the components
componentFilePaths.forEach((componentPath) => {
createTestFileForComponent(componentPath)
})
}
function validateProps(componentPath, props, enums) {
if (!props.length) {
logNoInterfaceItemsFoundError(componentPath)
return false
}
const invalidTypes = getInvalidTypes(props, enums)
if (invalidTypes.length) {
logNotSupportedTypesError(
componentPath,
invalidTypes.map((item) => item.type)
)
return false
}
return true
}
function createTestFileForComponent(componentPath) {
parseComponent(componentPath)
.then(({ name, types, enums }) => {
// console.log(`Interface types with values: ` +
// `\n${typesWithValues.map(obj => JSON.stringify(obj) + '\n')}`);
if (!validateProps(componentPath, types, enums)) {
return
}
writeTestsFile(name, componentPath, types, enums, testsRoot)
})
.catch((error) => {})
}
if (!componentFilePaths.length) {
logNoFilesError()
} else if (!testsRoot) {
logNoTestsRootError()
} else {
run()
}