-
Notifications
You must be signed in to change notification settings - Fork 8
/
copyTypes.js
92 lines (81 loc) · 2.17 KB
/
copyTypes.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
const fs = require("fs").promises;
const path = require("path");
const { exec } = require("child_process");
const destinationFolder = path.join("frontend", "types");
// Adjust these paths as per your project structure
const files = [
{
src: path.join(
__dirname,
"backend",
"types",
"generated",
"contentTypes.d.ts"
),
dest: path.join(__dirname, destinationFolder, "contentTypes.d.ts"),
},
{
src: path.join(
__dirname,
"backend",
"types",
"generated",
"components.d.ts"
),
dest: path.join(__dirname, destinationFolder, "components.d.ts"),
},
];
async function generateTypes() {
console.log("🚀 Generating Strapi types...");
return new Promise((resolve, reject) => {
exec(
"cd backend && yarn strapi ts:generate-types",
(error, stdout, stderr) => {
if (error) {
console.error(`❌ Error generating types: ${error}`);
reject(error);
return;
}
console.log("📄 Types generated successfully");
resolve(stdout);
}
);
});
}
async function copyFile({ src, dest }) {
try {
const destinationDir = path.dirname(dest);
await fs.mkdir(destinationDir, { recursive: true }).catch((err) => {
if (err.code !== "EEXIST") throw err; // Ignore if the directory exists
});
const fileExists = await fs
.stat(src)
.then(() => true)
.catch((err) => {
if (err.code === "ENOENT") {
console.log(
`⚠️ Source file does not exist, will be generated: ${src}`
);
}
return false;
});
// Generate types before copying
await generateTypes();
// Copy the file directly
await fs.copyFile(src, dest);
if (fileExists) {
console.log(`🔄 Updated and copied: ${dest}`);
} else {
console.log(`✨ Created and copied: ${dest}`);
}
} catch (err) {
console.error(`❌ Error: Failed to copy ${src} to ${dest}: ${err}`);
process.exit(1);
}
}
async function main() {
for (const file of files) {
await copyFile(file);
}
}
main().catch((err) => console.error(`❌ Error during execution: ${err}`));