-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·166 lines (128 loc) · 4.19 KB
/
cli.js
File metadata and controls
executable file
·166 lines (128 loc) · 4.19 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const readline = require("readline");
// Import Local Config Files
const baseConfig = require("./index");
const nodeConfig = require("./node");
const nextConfig = require("./next");
// Parse Arguments
const args = process.argv.slice(2);
const preset = args[0] || "base";
const root = process.cwd();
const allowedPresets = ["base", "node", "next"];
if (!allowedPresets.includes(preset)) {
console.log("❌ Invalid preset. Use: base | node | next");
process.exit(1);
}
// Package Manager Detection
function detectPackageManager() {
if (fs.existsSync(path.join(root, "pnpm-lock.yaml"))) return "pnpm";
if (fs.existsSync(path.join(root, "yarn.lock"))) return "yarn";
return "npm";
}
const packageManager = detectPackageManager();
console.log(`📦 Installing Prettier and plugins using ${packageManager}...`);
try {
const installCmd =
packageManager === "yarn"
? "yarn add -D prettier @ianvs/prettier-plugin-sort-imports prettier-plugin-tailwindcss"
: packageManager === "pnpm"
? "pnpm add -D prettier @ianvs/prettier-plugin-sort-imports prettier-plugin-tailwindcss"
: "npm install -D prettier @ianvs/prettier-plugin-sort-imports prettier-plugin-tailwindcss";
// shows real-time output of installation process
execSync(installCmd, { stdio: "inherit" });
} catch {
console.error("❌ Failed to install dependencies.");
process.exit(1);
}
// Select Config
function getConfig(preset) {
if (preset === "next") return nextConfig;
if (preset === "node") return nodeConfig;
return baseConfig;
}
// Create prettier.config.js
const configPath = path.join(root, "prettier.config.js");
if (fs.existsSync(configPath)) {
console.log("⚠ prettier.config.js already exists. Skipping creation.");
} else {
console.log("📝 Creating prettier.config.js...");
const selectedConfig = getConfig(preset);
// Write the config object to prettier.config.js
fs.writeFileSync(
configPath,
"module.exports = " + JSON.stringify(selectedConfig, null, 2) + ";\n",
);
}
// Create .prettierignore
const ignorePath = path.join(root, ".prettierignore");
if (fs.existsSync(ignorePath)) {
console.log("⚠ .prettierignore already exists. Skipping creation.");
} else {
console.log("📝 Creating .prettierignore...");
const ignoreContent =
`
node_modules
dist
build
coverage
.next
out
`.trim() + "\n";
fs.writeFileSync(ignorePath, ignoreContent);
}
// Inject Format Scripts
const pkgPath = path.join(root, "package.json");
if (fs.existsSync(pkgPath)) {
const pkgRaw = fs.readFileSync(pkgPath, "utf8");
const pkg = JSON.parse(pkgRaw);
pkg.scripts = pkg.scripts || {};
let modified = false;
if (!pkg.scripts.format) {
pkg.scripts.format = "prettier --write .";
console.log("✨ Added 'format' script");
modified = true;
}
if (!pkg.scripts["format:check"]) {
pkg.scripts["format:check"] = "prettier --check .";
console.log("✨ Added 'format:check' script");
modified = true;
}
// Only write back to package.json if we made changes
if (modified) {
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
}
} else {
console.log("⚠ No package.json found. Skipping script setup.");
}
console.log("✅ Setup complete!");
console.log(`👉 Using preset: ${preset}`);
// Ask to Format Immediately
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"\n🚀 Do you want to format the entire project now? (y/n): ",
(answer) => {
if (answer.toLowerCase() === "y") {
try {
execSync("npx prettier --write .", { stdio: "inherit" });
console.log("✅ Project formatted successfully!");
} catch {
console.log("❌ Formatting failed.");
}
} else {
console.log("\n📌 You can format manually anytime:");
console.log("👉 Format entire project:");
console.log(" npm run format");
console.log("\n👉 Check formatting:");
console.log(" npm run format:check");
console.log("\n👉 Format single file:");
console.log(" npx prettier --write <file-path>");
}
rl.close();
},
);