-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-registries.js
60 lines (49 loc) · 1.93 KB
/
generate-registries.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
#! /usr/bin/env node
/**
* This file automatically generates the index registries for all directories specified in `directories` below. It runs automatically when `yarn preflight` runs or you can run it via `yarn genreg`
*/
const fs = require('fs');
const path = require('path');
const camelCase = require('camelcase');
const directories = ['./components', './helpers'];
const message = `/***
This file is autogenerated, please do not edit it.
To generate an updated version, please run \`yarn genreg\`
If you register your helper with this index, it can be referenced more easily.
e.g. without registering
import fetcher from '@helpers/fetcher
import poster from '@helpers/poster
e.g. with registering
import { fetcher, poster } from '@helpers/index'
*/
`;
const generateLines = (directory) => {
// If the directory only has one file then it's only the index file and we should generate just an empty export
if (fs.readdirSync(directory).length <= 1) {
console.log(`${directory} is empty. Generating empty export`);
return `export {}`;
}
// If it's not empty, get all the files in the directory and generate the lines
return fs
.readdirSync(path.resolve(directory))
.map((file) => {
const include = '.ts';
const exclude = `index|.spec.ts`;
// Only look for requested files and ignore indexes
if (file.match(exclude)) return null;
if (!file.match(include))
return `export { default as ${camelCase(file, {
pascalCase: true
})} } from './${file}'`;
const fileNoExt = file.replace(include, '');
const varName = camelCase(fileNoExt);
return `export { default as ${varName} } from './${fileNoExt}'`;
})
.filter((item) => item)
.toString()
.replace(/,/g, '\n');
};
directories.forEach((directory) => {
const fileContents = `${message}\n\n${generateLines(directory)}`;
fs.writeFileSync(path.resolve(directory, `index.ts`), fileContents);
});