forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-ts-tests.js
49 lines (43 loc) · 1.14 KB
/
generate-ts-tests.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
const glob = require('glob');
const { readFileSync, writeFileSync, mkdirSync } = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const SOURCE_FILES_PATTERN = './src/**/*.js';
const DESTINATION_DIR = './__ts-tests__';
const EXAMPLE_REGEX = /(## Usage\n \* ```js)([\S\s]*?)(```)/g;
const JS_EXT = '.js';
const TS_TEST_EXT = '.test.tsx';
const transformContent = content =>
content
.replace("'react-native-paper'", "'..'")
.split('\n')
.map(e => e.slice(3))
.join('\n');
const getFiles = () =>
glob
.sync(SOURCE_FILES_PATTERN)
.map(filePath => {
const content = readFileSync(filePath, 'utf-8');
const match = EXAMPLE_REGEX.exec(content);
return match
? {
path: filePath,
content: transformContent(match[2]),
}
: null;
})
.filter(Boolean);
const writeFiles = files => {
mkdirSync(DESTINATION_DIR);
files.forEach(f =>
writeFileSync(
path.join(
DESTINATION_DIR,
`${path.basename(f.path, JS_EXT)}${TS_TEST_EXT}`
),
f.content
)
);
};
rimraf.sync(DESTINATION_DIR);
writeFiles(getFiles());