-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild-lib.js
More file actions
109 lines (98 loc) · 3.69 KB
/
build-lib.js
File metadata and controls
109 lines (98 loc) · 3.69 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const fs = require("fs");
const path = require("path");
// Paths
const distPath = path.join(__dirname, "dist");
const jsFilePath = path.join(distPath, "index.js");
const dtsFilePath = path.join(distPath, "index.d.ts");
const modulePath = path.join(distPath, "tutorial-module", "index.js");
const srcPath = path.join(__dirname, "src");
// File contents
const jsContent = 'export * from "./tutorial-module/index";';
const dtsContent = 'export * from "./tutorial-module/index";';
function validateDistFolder() {
if (!fs.existsSync(distPath)) {
fs.mkdirSync(distPath);
console.log("'dist' directory created.");
} else {
console.log("'dist' directory already exists.");
}
}
// Generate the files
function generateIndexJsWithTypes() {
// Check if ./tutorial-module/index.js exists
if (fs.existsSync(modulePath)) {
console.log("The './tutorial-module/index.js' file exists. Generating files...");
// Write index.js
fs.writeFileSync(jsFilePath, jsContent);
// Write index.d.ts
fs.writeFileSync(dtsFilePath, dtsContent);
} else {
console.log("The './tutorial-module/index.js' file does not exist. Files will not be generated.");
}
}
function updatePackageJson() {
const packageJsonPath = path.join(__dirname, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
const packageJson2 = { ...packageJson, name: packageJson.name.replace(/-app$/, "-component") };
delete packageJson2["manifest.webapp"];
moveToPeerDependencies(packageJson2);
const destinationPath = path.join(distPath, "package.json");
fs.writeFileSync(destinationPath, JSON.stringify(packageJson2, null, 2));
console.log("package.json copied");
}
function moveToPeerDependencies(packageJson) {
const depsToMove = [
["react-router", "react-router-dom", "react", "react-dom"],
["@material-ui/core", "@material-ui/icons", "@material-ui/lab", "@material-ui/styles"],
[
"@dhis2/app-runtime",
"@dhis2/d2-i18n",
"@dhis2/d2-i18n-extract",
"@dhis2/d2-i18n-generate",
"@dhis2/d2-ui-core",
"@dhis2/d2-ui-forms",
"@dhis2/ui",
],
["@eyeseetea/d2-ui-components"],
["purify-ts"],
["d2", "d2-manifest"],
].flat();
depsToMove.forEach(dep => {
if (packageJson.dependencies[dep]) {
const depVersion = packageJson.dependencies[dep];
packageJson.peerDependencies[dep] = depVersion.startsWith("^") ? depVersion : `^${depVersion}`;
delete packageJson.dependencies[dep];
} else {
console.warn(`Dependency ${dep} not found in dependencies`);
}
});
return packageJson;
}
function copyCssFiles() {
const copyRecursive = (src, dest) => {
if (fs.statSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
fs.readdirSync(src).forEach(item => {
copyRecursive(path.join(src, item), path.join(dest, item));
});
} else if (src.endsWith(".css")) {
fs.copyFileSync(src, dest);
console.log(`Copied CSS: ${src} -> ${dest}`);
}
};
const tutorialModuleSrc = path.join(srcPath, "tutorial-module");
const tutorialModuleDest = path.join(distPath, "tutorial-module");
if (fs.existsSync(tutorialModuleSrc)) {
copyRecursive(tutorialModuleSrc, tutorialModuleDest);
console.log("CSS files copied successfully");
}
}
function start() {
updatePackageJson();
validateDistFolder();
generateIndexJsWithTypes();
copyCssFiles();
}
start();