-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupPackage.js
78 lines (65 loc) · 2.52 KB
/
setupPackage.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
import fs from "fs";
import path, { dirname } from "path";
import { fileURLToPath } from "url";
import { Buffer } from "buffer";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
function copyDirectory(src, dest) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
const entries = fs.readdirSync(src, { withFileTypes: true });
for (let 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);
}
}
}
function main() {
const distPath = path.join(__dirname, "dist");
const styleCssPath = path.join(__dirname, "src/globals.css");
const assetPath = path.join(__dirname, "src", "asset");
const packageJsonPath = path.join(__dirname, "package.json");
const tailwindConfigPath = path.join(__dirname, "tailwind.config.ts");
const readmePath = path.join(__dirname, "docs", "README.md");
if (!fs.existsSync(distPath)) {
fs.mkdirSync(distPath);
}
const source = fs.readFileSync(packageJsonPath).toString("utf-8");
const sourceObj = JSON.parse(source);
sourceObj.scripts = {};
sourceObj.devDependencies = {};
if (sourceObj.main && sourceObj.main.startsWith("/dist/")) {
sourceObj.main = sourceObj.main.slice(5);
}
const distAssetPath = path.join(distPath, "asset");
const distStyleCssPath = path.join(distPath, "globals.css");
const distPackageJsonPath = path.join(distPath, "package.json");
const versionFilePath = path.join(distPath, "version.txt");
const npmignorePath = path.join(distPath, ".npmignore");
const distTailwindConfigPath = path.join(distPath, "tailwind.config.ts");
const distReadmePath = path.join(distPath, "README.md");
fs.writeFileSync(
distPackageJsonPath,
Buffer.from(JSON.stringify(sourceObj, null, 2), "utf-8")
);
fs.writeFileSync(versionFilePath, Buffer.from(sourceObj.version, "utf-8"));
if (fs.existsSync(styleCssPath)) {
fs.copyFileSync(styleCssPath, distStyleCssPath);
}
if (fs.existsSync(npmignorePath)) {
fs.copyFileSync(npmignorePath, path.join(distPath, ".npmignore"));
}
if (fs.existsSync(tailwindConfigPath)) {
fs.copyFileSync(tailwindConfigPath, distTailwindConfigPath);
}
if (fs.existsSync(readmePath)) {
fs.copyFileSync(readmePath, distReadmePath);
}
copyDirectory(assetPath, distAssetPath);
}
main();