@docubook/create 1.8.5 → 1.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/create.js +61 -21
- package/package.json +1 -1
package/create.js
CHANGED
|
@@ -11,7 +11,7 @@ import figlet from "figlet";
|
|
|
11
11
|
import cliProgress from "cli-progress";
|
|
12
12
|
import { execSync } from "child_process";
|
|
13
13
|
|
|
14
|
-
// Logging
|
|
14
|
+
// ========== Logging ========== //
|
|
15
15
|
const log = {
|
|
16
16
|
info: (msg) => console.log(chalk.cyan("ℹ️ " + msg)),
|
|
17
17
|
success: (msg) => console.log(chalk.green("✔ " + msg)),
|
|
@@ -19,8 +19,8 @@ const log = {
|
|
|
19
19
|
error: (msg) => console.log(chalk.red("✖ " + msg)),
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
// Cek
|
|
23
|
-
function
|
|
22
|
+
// ========== Cek apakah package manager terinstal ========== //
|
|
23
|
+
function isInstalled(command) {
|
|
24
24
|
try {
|
|
25
25
|
execSync(`${command} --version`, { stdio: "ignore" });
|
|
26
26
|
return true;
|
|
@@ -29,7 +29,16 @@ function checkInstalled(command) {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
//
|
|
32
|
+
// ========== Ambil versi package manager ========== //
|
|
33
|
+
function getPackageManagerVersion(pm) {
|
|
34
|
+
try {
|
|
35
|
+
return execSync(`${pm} --version`).toString().trim();
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ========== Rename postcss.config.js ke .cjs jika menggunakan bun ========== //
|
|
33
42
|
function updatePostcssConfig(projectPath) {
|
|
34
43
|
const oldPath = path.join(projectPath, "postcss.config.js");
|
|
35
44
|
const newPath = path.join(projectPath, "postcss.config.cjs");
|
|
@@ -39,7 +48,7 @@ function updatePostcssConfig(projectPath) {
|
|
|
39
48
|
}
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
//
|
|
51
|
+
// ========== ASCII welcome art ========== //
|
|
43
52
|
function displayAsciiArt() {
|
|
44
53
|
return new Promise((resolve, reject) => {
|
|
45
54
|
figlet.text("DocuBook", { horizontalLayout: "full" }, (err, data) => {
|
|
@@ -54,7 +63,7 @@ function displayWelcomeMessage() {
|
|
|
54
63
|
console.log(chalk.white("DocuBook Installer"));
|
|
55
64
|
}
|
|
56
65
|
|
|
57
|
-
// Simulasi
|
|
66
|
+
// ========== Simulasi animasi setup ========== //
|
|
58
67
|
async function simulateInstallation() {
|
|
59
68
|
const bar = new cliProgress.SingleBar(
|
|
60
69
|
{
|
|
@@ -73,6 +82,7 @@ async function simulateInstallation() {
|
|
|
73
82
|
bar.stop();
|
|
74
83
|
}
|
|
75
84
|
|
|
85
|
+
// ========== Jika user batal install, tunjukkan langkah manual ========== //
|
|
76
86
|
function manualSteps(projectDirectory, packageManager) {
|
|
77
87
|
const manualInstructions = `
|
|
78
88
|
You chose not to continue with the installation.
|
|
@@ -85,21 +95,32 @@ function manualSteps(projectDirectory, packageManager) {
|
|
|
85
95
|
|
|
86
96
|
console.log(
|
|
87
97
|
boxen(manualInstructions, {
|
|
88
|
-
padding:
|
|
98
|
+
padding: 0.5,
|
|
89
99
|
borderStyle: "round",
|
|
90
100
|
borderColor: "cyan",
|
|
91
101
|
})
|
|
92
102
|
);
|
|
93
103
|
}
|
|
94
104
|
|
|
105
|
+
// Deteksi default package manager dari eksekusi CLI
|
|
106
|
+
function detectDefaultPackageManager() {
|
|
107
|
+
const userAgent = process.env.npm_config_user_agent || "";
|
|
108
|
+
if (userAgent.includes("pnpm")) return "pnpm";
|
|
109
|
+
if (userAgent.includes("yarn")) return "yarn";
|
|
110
|
+
if (userAgent.includes("bun")) return "bun";
|
|
111
|
+
return "npm";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ========== Entry CLI ========== //
|
|
95
115
|
program
|
|
96
|
-
.version("1.8.
|
|
116
|
+
.version("1.8.7")
|
|
97
117
|
.description("CLI to create a new Docubook project")
|
|
98
118
|
.argument("[project-directory]", "Directory to create the new Docubook project")
|
|
99
119
|
.action(async (projectDirectory) => {
|
|
100
120
|
await displayAsciiArt();
|
|
101
121
|
displayWelcomeMessage();
|
|
102
122
|
|
|
123
|
+
// Tanya nama direktori jika belum diisi
|
|
103
124
|
if (!projectDirectory) {
|
|
104
125
|
const { directoryName } = await inquirer.prompt([
|
|
105
126
|
{
|
|
@@ -112,21 +133,25 @@ program
|
|
|
112
133
|
projectDirectory = directoryName;
|
|
113
134
|
}
|
|
114
135
|
|
|
115
|
-
|
|
136
|
+
// Pilihan package manager
|
|
137
|
+
const defaultPM = detectDefaultPackageManager();
|
|
138
|
+
const { packageManager } = await inquirer.prompt([
|
|
116
139
|
{
|
|
117
140
|
type: "list",
|
|
118
141
|
name: "packageManager",
|
|
119
142
|
message: "Choose a package manager:",
|
|
120
143
|
choices: ["npm", "pnpm", "yarn", "bun"],
|
|
121
|
-
default:
|
|
144
|
+
default: defaultPM,
|
|
122
145
|
},
|
|
123
146
|
]);
|
|
124
147
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
148
|
+
const version = getPackageManagerVersion(packageManager);
|
|
149
|
+
if (!version) {
|
|
150
|
+
log.error(`${packageManager} is not installed on your system.`);
|
|
151
|
+
process.exit(1);
|
|
128
152
|
}
|
|
129
153
|
|
|
154
|
+
// Clone repositori starter
|
|
130
155
|
const repo = "https://gitlab.com/mywildancloud/docubook.git";
|
|
131
156
|
const branch = "starter";
|
|
132
157
|
const projectPath = path.resolve(process.cwd(), projectDirectory);
|
|
@@ -137,13 +162,26 @@ program
|
|
|
137
162
|
execSync(cloneCommand);
|
|
138
163
|
spinner.succeed(`Project created in ${projectDirectory}/`);
|
|
139
164
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
165
|
+
// Rename config jika menggunakan bun
|
|
166
|
+
if (packageManager === "bun") {
|
|
167
|
+
updatePostcssConfig(projectPath);
|
|
168
|
+
}
|
|
143
169
|
|
|
170
|
+
// Validasi packageManager dari project yang di-clone
|
|
144
171
|
const pkgPath = path.join(projectPath, "package.json");
|
|
145
172
|
if (fs.existsSync(pkgPath)) {
|
|
146
173
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
174
|
+
const existingPM = pkg.packageManager?.split("@")[0];
|
|
175
|
+
|
|
176
|
+
// Jika berbeda, tampilkan peringatan & timpa packageManager
|
|
177
|
+
if (existingPM && existingPM !== packageManager) {
|
|
178
|
+
log.warn(`This project recommends ${existingPM}, but you chose ${packageManager}. Overriding...`);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
pkg.packageManager = `${packageManager}@${version}`;
|
|
182
|
+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
|
|
183
|
+
log.info(`Updated packageManager to "${pkg.packageManager}" in package.json`);
|
|
184
|
+
|
|
147
185
|
console.log(
|
|
148
186
|
chalk.green(
|
|
149
187
|
`=================\nDocuBook v${pkg.version}\n=================`
|
|
@@ -151,7 +189,7 @@ program
|
|
|
151
189
|
);
|
|
152
190
|
}
|
|
153
191
|
|
|
154
|
-
// Tanya apakah
|
|
192
|
+
// Tanya apakah lanjut install dependencies
|
|
155
193
|
const { continueInstallation } = await inquirer.prompt([
|
|
156
194
|
{
|
|
157
195
|
type: "confirm",
|
|
@@ -168,19 +206,21 @@ program
|
|
|
168
206
|
|
|
169
207
|
log.info("Installing dependencies...");
|
|
170
208
|
console.log(chalk.yellow("This is a joke for you:"));
|
|
171
|
-
console.log(chalk.white("When you install the package manager, the installation speed depends on the
|
|
209
|
+
console.log(chalk.white("When you install the package manager, the installation speed depends on the hardware you have."));
|
|
172
210
|
const installSpinner = ora(`Using ${packageManager}...`).start();
|
|
173
211
|
|
|
174
212
|
try {
|
|
175
213
|
execSync(`${packageManager} install`, { cwd: projectPath, stdio: "ignore" });
|
|
176
214
|
installSpinner.succeed("Dependencies installed.");
|
|
177
|
-
|
|
215
|
+
} catch {
|
|
178
216
|
installSpinner.fail("Failed to install dependencies.");
|
|
217
|
+
manualSteps(projectDirectory, packageManager); // Jika gagal install otomatis
|
|
179
218
|
process.exit(1);
|
|
180
|
-
|
|
219
|
+
}
|
|
181
220
|
|
|
182
221
|
await simulateInstallation();
|
|
183
222
|
|
|
223
|
+
// Saran langkah selanjutnya
|
|
184
224
|
console.log(
|
|
185
225
|
boxen(
|
|
186
226
|
`Next Steps:\n\n` +
|
|
@@ -188,7 +228,7 @@ program
|
|
|
188
228
|
`2. ${chalk.cyan(`${packageManager} install (if not automatically installed)`)}\n` +
|
|
189
229
|
`3. ${chalk.cyan(`${packageManager} run dev`)}`,
|
|
190
230
|
{
|
|
191
|
-
padding:
|
|
231
|
+
padding: 0.5,
|
|
192
232
|
borderStyle: "round",
|
|
193
233
|
borderColor: "cyan",
|
|
194
234
|
}
|