bunup 0.8.42 → 0.8.44

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.
@@ -0,0 +1,355 @@
1
+ // @bun
2
+ import {
3
+ loadPackageJson
4
+ } from "./chunk-gh7z7s46.js";
5
+ import {
6
+ displayBunupGradientArt
7
+ } from "./chunk-a7j07gk7.js";
8
+ import {
9
+ formatListWithAnd
10
+ } from "./chunk-c1eyecm3.js";
11
+
12
+ // src/cli/init.ts
13
+ import fs from "fs";
14
+ import path from "path";
15
+ import {
16
+ confirm,
17
+ intro,
18
+ log,
19
+ multiselect,
20
+ outro,
21
+ select,
22
+ tasks,
23
+ text
24
+ } from "@clack/prompts";
25
+ import pc from "picocolors";
26
+ import { exec } from "tinyexec";
27
+ async function init() {
28
+ displayBunupGradientArt();
29
+ intro(pc.bgCyan(pc.black(" Initialize bunup in an existing project ")));
30
+ const { path: packageJsonPath } = await loadPackageJson();
31
+ if (!packageJsonPath) {
32
+ log.error("package.json not found");
33
+ process.exit(1);
34
+ }
35
+ const shouldSetupWorkspace = await promptForWorkspace();
36
+ if (shouldSetupWorkspace) {
37
+ await initializeWorkspace(packageJsonPath);
38
+ } else {
39
+ await initializeSinglePackage(packageJsonPath);
40
+ }
41
+ await tasks([
42
+ {
43
+ title: "Installing bunup",
44
+ task: async () => {
45
+ await installBunup();
46
+ return "Bunup installed";
47
+ }
48
+ }
49
+ ]);
50
+ showSuccessOutro(shouldSetupWorkspace);
51
+ }
52
+ async function promptForWorkspace() {
53
+ return await confirm({
54
+ message: "Do you want to setup a Bunup workspace? (for building multiple packages with one command)",
55
+ initialValue: false
56
+ });
57
+ }
58
+ async function initializeWorkspace(packageJsonPath) {
59
+ const workspacePackages = await collectWorkspacePackages();
60
+ const configMethod = await selectWorkspaceConfigurationMethod();
61
+ await generateWorkspaceConfiguration(configMethod, workspacePackages);
62
+ await handleWorkspaceBuildScripts(packageJsonPath);
63
+ }
64
+ async function initializeSinglePackage(packageJsonPath) {
65
+ const entryFiles = await collectEntryFiles();
66
+ const outputFormats = await selectOutputFormats();
67
+ const shouldGenerateDts = await promptForTypeScriptDeclarations(entryFiles);
68
+ const configMethod = await selectConfigurationMethod();
69
+ await generateConfiguration(configMethod, entryFiles, outputFormats, shouldGenerateDts, packageJsonPath);
70
+ await handleBuildScripts(packageJsonPath, entryFiles, outputFormats, shouldGenerateDts, configMethod);
71
+ }
72
+ async function collectWorkspacePackages() {
73
+ const packages = [];
74
+ while (true) {
75
+ const packageName = await text({
76
+ message: packages.length > 0 ? "Enter the next package name:" : "Enter the first package name:",
77
+ placeholder: "core",
78
+ validate: (value) => {
79
+ if (!value)
80
+ return "Package name is required";
81
+ if (packages.some((pkg) => pkg.name === value))
82
+ return "Package name already exists";
83
+ }
84
+ });
85
+ const packageRoot = await text({
86
+ message: `Enter the root directory for "${packageName}":`,
87
+ placeholder: `packages/${packageName}`,
88
+ defaultValue: `packages/${packageName}`,
89
+ validate: (value) => {
90
+ if (!value)
91
+ return "Package root is required";
92
+ if (!fs.existsSync(value))
93
+ return "Package root directory does not exist";
94
+ if (!fs.statSync(value).isDirectory())
95
+ return "Package root must be a directory";
96
+ }
97
+ });
98
+ const entryFiles = await collectEntryFilesForPackage(packageRoot, packageName);
99
+ const outputFormats = await selectOutputFormats();
100
+ const shouldGenerateDts = await promptForTypeScriptDeclarations(entryFiles);
101
+ packages.push({
102
+ name: packageName,
103
+ root: packageRoot,
104
+ entryFiles,
105
+ outputFormats,
106
+ shouldGenerateDts
107
+ });
108
+ const shouldAddMore = await confirm({
109
+ message: "Do you want to add another package?",
110
+ initialValue: true
111
+ });
112
+ if (!shouldAddMore)
113
+ break;
114
+ }
115
+ return packages;
116
+ }
117
+ async function collectEntryFilesForPackage(packageRoot, packageName) {
118
+ const entryFiles = [];
119
+ while (true) {
120
+ const entryFile = await text({
121
+ message: entryFiles.length > 0 ? `Where is the next entry file for "${packageName}"? (relative to ${packageRoot})` : `Where is the entry file for "${packageName}"? (relative to ${packageRoot})`,
122
+ placeholder: "src/index.ts",
123
+ defaultValue: "src/index.ts",
124
+ validate: (value) => {
125
+ if (!value)
126
+ return "Entry file is required";
127
+ const fullPath = path.join(packageRoot, value);
128
+ if (!fs.existsSync(fullPath))
129
+ return `Entry file does not exist at ${fullPath}`;
130
+ if (!fs.statSync(fullPath).isFile())
131
+ return "Entry file must be a file";
132
+ if (entryFiles.includes(value))
133
+ return "You have already added this entry file";
134
+ }
135
+ });
136
+ entryFiles.push(entryFile);
137
+ const shouldAddMore = await confirm({
138
+ message: "Do you want to add another entry file for this package?",
139
+ initialValue: false
140
+ });
141
+ if (!shouldAddMore)
142
+ break;
143
+ }
144
+ return entryFiles;
145
+ }
146
+ async function collectEntryFiles() {
147
+ const entryFiles = [];
148
+ while (true) {
149
+ const entryFile = await text({
150
+ message: entryFiles.length > 0 ? "Where is your next entry file?" : "Where is your entry file?",
151
+ placeholder: "src/index.ts",
152
+ defaultValue: "src/index.ts",
153
+ validate: (value) => {
154
+ if (!value)
155
+ return "Entry file is required";
156
+ if (!fs.existsSync(value))
157
+ return "Entry file does not exist";
158
+ if (!fs.statSync(value).isFile())
159
+ return "Entry file must be a file";
160
+ if (entryFiles.includes(value))
161
+ return "You have already added this entry file";
162
+ }
163
+ });
164
+ entryFiles.push(entryFile);
165
+ const shouldAddMore = await confirm({
166
+ message: "Do you want to add another entry file?",
167
+ initialValue: false
168
+ });
169
+ if (!shouldAddMore)
170
+ break;
171
+ }
172
+ return entryFiles;
173
+ }
174
+ async function selectOutputFormats() {
175
+ return await multiselect({
176
+ message: "Select the output formats",
177
+ options: [
178
+ { value: "esm", label: "ESM (.mjs)" },
179
+ { value: "cjs", label: "CommonJS (.cjs)" },
180
+ { value: "iife", label: "IIFE (.global.js)" }
181
+ ],
182
+ initialValues: ["esm", "cjs"]
183
+ });
184
+ }
185
+ async function promptForTypeScriptDeclarations(entryFiles) {
186
+ const hasTypeScriptFiles = entryFiles.some((file) => file.endsWith(".ts") || file.endsWith(".tsx"));
187
+ if (!hasTypeScriptFiles)
188
+ return false;
189
+ return await confirm({
190
+ message: "Generate TypeScript declarations?",
191
+ initialValue: true
192
+ });
193
+ }
194
+ async function selectWorkspaceConfigurationMethod() {
195
+ return await select({
196
+ message: "How would you like to configure your workspace?",
197
+ options: [
198
+ { value: "ts", label: "bunup.config.ts", hint: "Recommended" },
199
+ { value: "js", label: "bunup.config.js" }
200
+ ],
201
+ initialValue: "ts"
202
+ });
203
+ }
204
+ async function selectConfigurationMethod() {
205
+ return await select({
206
+ message: "How would you like to configure Bunup?",
207
+ options: [
208
+ { value: "ts", label: "bunup.config.ts", hint: "Recommended" },
209
+ { value: "js", label: "bunup.config.js" },
210
+ { value: "json", label: 'package.json "bunup" property' },
211
+ {
212
+ value: "none",
213
+ label: "No config file",
214
+ hint: "Configure via CLI only"
215
+ }
216
+ ],
217
+ initialValue: "ts"
218
+ });
219
+ }
220
+ async function generateWorkspaceConfiguration(configMethod, workspacePackages) {
221
+ const configContent = createWorkspaceConfigFileContent(workspacePackages);
222
+ await Bun.write(`bunup.config.${configMethod}`, configContent);
223
+ }
224
+ async function generateConfiguration(configMethod, entryFiles, outputFormats, shouldGenerateDts, packageJsonPath) {
225
+ if (configMethod === "none") {
226
+ log.info("If you need more control (such as adding plugins or customizing output), you can always create a config file later.");
227
+ return;
228
+ }
229
+ if (configMethod === "ts" || configMethod === "js") {
230
+ await Bun.write(`bunup.config.${configMethod}`, createConfigFileContent(entryFiles, outputFormats, shouldGenerateDts));
231
+ } else if (configMethod === "json") {
232
+ const { data: packageJsonConfig } = await loadPackageJson();
233
+ const updatedConfig = {
234
+ ...packageJsonConfig,
235
+ bunup: createPackageJsonConfig(entryFiles, outputFormats, shouldGenerateDts)
236
+ };
237
+ await Bun.write(packageJsonPath, JSON.stringify(updatedConfig, null, 2));
238
+ }
239
+ }
240
+ async function handleWorkspaceBuildScripts(packageJsonPath) {
241
+ const { data: packageJsonConfig } = await loadPackageJson();
242
+ const existingScripts = packageJsonConfig?.scripts ?? {};
243
+ const newScripts = createWorkspaceBuildScripts();
244
+ const conflictingScripts = Object.keys(newScripts).filter((script) => existingScripts[script]);
245
+ if (conflictingScripts.length > 0) {
246
+ const shouldOverride = await confirm({
247
+ message: `The ${formatListWithAnd(conflictingScripts)} ${conflictingScripts.length > 1 ? "scripts already exist" : "script already exists"} in package.json. Override ${conflictingScripts.length > 1 ? "them" : "it"}?`,
248
+ initialValue: true
249
+ });
250
+ if (!shouldOverride) {
251
+ log.info("Skipped adding build scripts to avoid conflicts.");
252
+ return;
253
+ }
254
+ }
255
+ const updatedConfig = {
256
+ ...packageJsonConfig,
257
+ scripts: { ...existingScripts, ...newScripts }
258
+ };
259
+ await Bun.write(packageJsonPath, JSON.stringify(updatedConfig, null, 2));
260
+ }
261
+ async function handleBuildScripts(packageJsonPath, entryFiles, outputFormats, shouldGenerateDts, configMethod) {
262
+ const { data: packageJsonConfig } = await loadPackageJson();
263
+ const existingScripts = packageJsonConfig?.scripts ?? {};
264
+ const newScripts = createBuildScripts(entryFiles, outputFormats, shouldGenerateDts, configMethod);
265
+ const conflictingScripts = Object.keys(newScripts).filter((script) => existingScripts[script]);
266
+ if (conflictingScripts.length > 0) {
267
+ const shouldOverride = await confirm({
268
+ message: `The ${formatListWithAnd(conflictingScripts)} ${conflictingScripts.length > 1 ? "scripts already exist" : "script already exists"} in package.json. Override ${conflictingScripts.length > 1 ? "them" : "it"}?`,
269
+ initialValue: true
270
+ });
271
+ if (!shouldOverride) {
272
+ log.info("Skipped adding build scripts to avoid conflicts.");
273
+ return;
274
+ }
275
+ }
276
+ const updatedConfig = {
277
+ ...packageJsonConfig,
278
+ scripts: { ...existingScripts, ...newScripts }
279
+ };
280
+ await Bun.write(packageJsonPath, JSON.stringify(updatedConfig, null, 2));
281
+ }
282
+ function createWorkspaceConfigFileContent(workspacePackages) {
283
+ const packagesConfig = workspacePackages.map((pkg) => {
284
+ return ` {
285
+ name: '${pkg.name}',
286
+ root: '${pkg.root}',
287
+ config: {
288
+ entry: [${pkg.entryFiles.map((file) => `'${file}'`).join(", ")}],
289
+ format: [${pkg.outputFormats.map((format) => `'${format}'`).join(", ")}],${pkg.shouldGenerateDts ? `
290
+ dts: true,` : ""}
291
+ },
292
+ }`;
293
+ }).join(`,
294
+ `);
295
+ return `import { defineWorkspace } from 'bunup'
296
+
297
+ export default defineWorkspace([
298
+ ${packagesConfig}
299
+ ])
300
+ `;
301
+ }
302
+ function createConfigFileContent(entryFiles, outputFormats, shouldGenerateDts) {
303
+ return `import { defineConfig } from 'bunup'
304
+
305
+ export default defineConfig({
306
+ entry: [${entryFiles.map((file) => `'${file}'`).join(", ")}],
307
+ format: [${outputFormats.map((format) => `'${format}'`).join(", ")}],${shouldGenerateDts ? `
308
+ dts: true,` : ""}
309
+ })
310
+ `;
311
+ }
312
+ function createPackageJsonConfig(entryFiles, outputFormats, shouldGenerateDts) {
313
+ return {
314
+ entry: entryFiles,
315
+ format: outputFormats,
316
+ ...shouldGenerateDts && { dts: true }
317
+ };
318
+ }
319
+ function createWorkspaceBuildScripts() {
320
+ return {
321
+ build: "bunup",
322
+ dev: "bunup --watch"
323
+ };
324
+ }
325
+ function createBuildScripts(entryFiles, outputFormats, shouldGenerateDts, configMethod) {
326
+ const cliOptions = configMethod === "none" ? ` ${entryFiles.join(" ")} --format ${outputFormats.join(",")}${shouldGenerateDts ? " --dts" : ""}` : "";
327
+ return {
328
+ build: `bunup${cliOptions}`,
329
+ dev: `bunup${cliOptions} --watch`
330
+ };
331
+ }
332
+ function showSuccessOutro(isWorkspace) {
333
+ const buildCommand = isWorkspace ? `${pc.cyan("bun run build")} - Build all packages in your workspace` : `${pc.cyan("bun run build")} - Build your library`;
334
+ const devCommand = isWorkspace ? `${pc.cyan("bun run dev")} - Start development mode (watches all packages)` : `${pc.cyan("bun run dev")} - Start development mode`;
335
+ const filterCommand = isWorkspace ? `${pc.cyan("bunup --filter core,utils")} - Build specific packages` : "";
336
+ outro(`
337
+ ${pc.green("\u2728 Bunup initialized successfully! \u2728")}
338
+
339
+ ${buildCommand}
340
+ ${devCommand}${isWorkspace ? `
341
+ ${filterCommand}` : ""}
342
+
343
+ ${pc.dim("Learn more:")} ${pc.underline("https://bunup.dev/docs")}
344
+
345
+ ${pc.yellow("Happy building!")} \uD83D\uDE80
346
+ `);
347
+ }
348
+ async function installBunup() {
349
+ await exec("bun add -d bunup", [], {
350
+ nodeOptions: { shell: true, stdio: "pipe" }
351
+ });
352
+ }
353
+ export {
354
+ init
355
+ };
@@ -0,0 +1,45 @@
1
+ import {
2
+ BunupPluginError
3
+ } from "./chunk-c1eyecm3.js";
4
+
5
+ // src/plugins/utils.ts
6
+ import pc from "picocolors";
7
+ function filterBunupBunPlugins(plugins) {
8
+ if (!plugins)
9
+ return [];
10
+ return plugins.filter((p) => p.type === "bun");
11
+ }
12
+ function filterBunupPlugins(plugins) {
13
+ if (!plugins)
14
+ return [];
15
+ return plugins.filter((p) => p.type === "bunup");
16
+ }
17
+ async function runPluginBuildStartHooks(bunupPlugins, options) {
18
+ if (!bunupPlugins)
19
+ return;
20
+ for (const plugin of bunupPlugins) {
21
+ if (plugin.hooks.onBuildStart) {
22
+ await plugin.hooks.onBuildStart(options);
23
+ }
24
+ }
25
+ }
26
+ async function runPluginBuildDoneHooks(bunupPlugins, options, output, meta) {
27
+ if (!bunupPlugins)
28
+ return;
29
+ for (const plugin of bunupPlugins) {
30
+ if (plugin.hooks.onBuildDone) {
31
+ await plugin.hooks.onBuildDone({ options, output, meta });
32
+ }
33
+ }
34
+ }
35
+ async function getPackageForPlugin(name, pluginName) {
36
+ let pkg;
37
+ try {
38
+ pkg = await import(name);
39
+ } catch {
40
+ throw new BunupPluginError(`[${pc.cyan(name)}] is required for the ${pluginName} plugin. Please install it with: ${pc.blue(`bun add ${name} --dev`)}`);
41
+ }
42
+ return pkg;
43
+ }
44
+
45
+ export { filterBunupBunPlugins, filterBunupPlugins, runPluginBuildStartHooks, runPluginBuildDoneHooks, getPackageForPlugin };