-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (68 loc) · 2.25 KB
/
index.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
const fs = require('fs');
const path = require('path');
const appsDir = path.join(__dirname, 'apps');
const packagesDir = path.join(__dirname, 'packages');
const baseAppDir = path.join(packagesDir, 'base-app');
// Excluded files
const filesToExclude = [
'.next',
'.turbo',
'.env',
'.env.dev',
'.env.tst',
'.env.prd',
'.gitignore',
'node_modules',
];
// Helper function to copy directory recursively
const copyDirectory = (src, dest) => {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirectory(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
};
// Custom implementation of the copy function using fs-extra
const copy = (src, dest) => {
if (fs.lstatSync(src).isDirectory()) {
copyDirectory(src, dest);
} else {
fs.copyFileSync(src, dest);
}
};
// Iterate over each app
fs.readdirSync(appsDir).forEach((appName) => {
const appPath = path.join(appsDir, appName);
// Delete all files and folders within the app directory, except for the specified files to keep
fs.readdirSync(appPath).forEach((file) => {
const filePath = path.join(appPath, file);
if (!filesToExclude.includes(file)) {
fs.rmSync(filePath, { recursive: true, force: true });
}
});
// Copy all files from the base app directory to the app directory, except for specified files to exclude
fs.readdirSync(baseAppDir).forEach((file) => {
if (!filesToExclude.includes(file)) {
const filePath = path.join(baseAppDir, file);
copy(filePath, path.join(appPath, file));
}
});
// Read the package.json file for the current app
const appPackageFilePath = path.join(appPath, 'package.json');
const appPackageJson = JSON.parse(
fs.readFileSync(appPackageFilePath, 'utf8')
);
// Update the app name
appPackageJson.name = appName;
// Write the updated package.json back to the app directory
fs.writeFileSync(appPackageFilePath, JSON.stringify(appPackageJson, null, 2));
});
console.log('Package.json update and directory copy complete.');