-
Notifications
You must be signed in to change notification settings - Fork 2
/
copyTypes.js
48 lines (40 loc) · 1.36 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
/**
* Copy strapi types to next.js project. Need for this could be eliminated by using monorepo :)
* @link https://strapi.io/blog/improve-your-frontend-experience-with-strapi-types-and-type-script
*/
const fs = require('fs');
const path = require('path');
const destinationFolder = 'next/src/types';
const files = [
{
src: path.join(__dirname, './strapi/types/generated/contentTypes.d.ts'),
dest: path.join(__dirname, `./${destinationFolder}/contentTypes.d.ts`),
},
{
src: path.join(__dirname, './strapi/types/generated/components.d.ts'),
dest: path.join(__dirname, `./${destinationFolder}/components.d.ts`),
},
];
function copyFile({ src, dest }) {
const destinationDir = path.dirname(dest);
// Check if source file exists
if (!fs.existsSync(src)) {
console.error(`Source file does not exist: ${src}`);
process.exit(1);
}
// Ensure destination directory exists or create it
if (!fs.existsSync(destinationDir)) {
fs.mkdirSync(destinationDir, { recursive: true });
}
// Read the source file, modify its content and write to the destination file
const content = fs.readFileSync(src, 'utf8');
fs.writeFile(dest, content, (err) => {
if (err) {
console.error(`Error writing to destination file: ${err}`);
process.exit(1);
} else {
console.log(`File ${src} copied and modified successfully!`);
}
});
}
files.forEach((file) => copyFile(file));