-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathcreate-enum-export.mjs
58 lines (46 loc) · 1.91 KB
/
create-enum-export.mjs
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
import fs from 'fs';
import path from 'path';
import PATHS from '../../../config/paths.js';
const ENUMS_DIR = path.resolve(PATHS.packages, 'main', 'src', 'enums');
const INTERNAL_COMPONENTS = ['GrowingMode', 'index'];
const enums = fs
.readdirSync(ENUMS_DIR)
.filter((file) => fs.statSync(`${ENUMS_DIR}/${file}`).isFile())
.map((file) => path.basename(file, '.ts'))
// filter internal components
.filter((file) => !INTERNAL_COMPONENTS.includes(file));
// enums/index.ts
let fileContentEnums = `// This is an autogenerated file, please do not modify this file manually.
// In case you added a new file to the /enums folder, please rerun the scripts/create-enum-export.js script.
`;
fileContentEnums += enums
.map((file) => {
const fileName = path.basename(file, '.js');
return `export { ${fileName} } from './${file}.js';`;
})
.join('\n');
fs.writeFileSync(path.join(PATHS.packages, 'main', 'src', 'enums', 'index.ts'), fileContentEnums);
// index.ts
const TARGET_FILE = path.join(PATHS.packages, 'main', 'src', 'index.ts');
const fileContent = fs.readFileSync(TARGET_FILE, 'utf8');
const [beforeSection] = fileContent.split(
'// ================================\n// Enums\n// ================================\n'
);
let newContent = `// This part is auto-generated, please do not modify it manually.
// In case you added a new file to the /enums folder, please rerun the scripts/create-enum-export.js script.\n\n`;
newContent += enums
.map((file) => {
const fileName = path.basename(file, '.js');
return `export { ${fileName} } from './enums/${file}.js';`;
})
.join('\n');
// Create the new file content with updated section
const updatedFileContent = `
${beforeSection}
// ================================
// Enums
// ================================
${newContent}`;
// Write the updated content back to the file
fs.writeFileSync(TARGET_FILE, updatedFileContent);
console.log('Files updated successfully.');