@bagelink/workspace 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Bagel Studio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,221 @@
1
+ # @bagelink/workspace
2
+
3
+ Workspace tooling for Bagel projects - works for both single projects and monorepos.
4
+
5
+ ## Features
6
+
7
+ - 🔧 **Simple Configuration**: One config file for all environments
8
+ - 🔀 **Proxy Management**: Vite dev proxy + Netlify production redirects
9
+ - 🏗️ **Build Tooling**: Auto-generate netlify.toml with environment injection
10
+ - 📦 **Monorepo Support**: Cascading config for multi-project setups
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ pnpm add @bagelink/workspace
16
+ ```
17
+
18
+ ## Quick Start
19
+
20
+ ### Option 1: Run `npx bgl init`
21
+
22
+ ```bash
23
+ npx bgl init
24
+
25
+ 🔧 No bgl.config.ts found. Let's create one!
26
+
27
+ ? What is your Bagel project ID? › my-project
28
+ ? Use custom production host? › No / Yes
29
+
30
+ ✅ Created bgl.config.ts
31
+ Production host: https://my-project.bagel.to
32
+ Local dev host: http://localhost:8000
33
+ ```
34
+
35
+ ### Option 2: Automatic on first use
36
+
37
+ The first time you use `@bagelink/workspace`, it will automatically prompt you to create a config file if it doesn't exist.
38
+
39
+ ### Option 3: Create manually
40
+
41
+ Create `bgl.config.ts`:
42
+
43
+ ```typescript
44
+ import { defineWorkspace } from '@bagelink/workspace'
45
+ import type { WorkspaceConfig, WorkspaceEnvironment } from '@bagelink/workspace'
46
+
47
+ const configs: Record<WorkspaceEnvironment, WorkspaceConfig> = {
48
+ local: {
49
+ host: 'http://localhost:8000',
50
+ proxy: '/api',
51
+ openapi_url: 'http://localhost:8000/openapi.json',
52
+ },
53
+ development: {
54
+ host: 'https://project.bagel.to',
55
+ proxy: '/api',
56
+ openapi_url: 'https://project.bagel.to/openapi.json',
57
+ },
58
+ production: {
59
+ host: 'https://project.bagel.to',
60
+ proxy: '/api',
61
+ },
62
+ }
63
+
64
+ export default defineWorkspace(configs)
65
+ ```
66
+
67
+ ## Usage
68
+
69
+ ### 1. Configure Vite (`vite.config.ts`)
70
+
71
+ ```typescript
72
+ import { defineConfig } from 'vite'
73
+ import { createViteProxy } from '@bagelink/workspace'
74
+ import workspace from './bgl.config'
75
+
76
+ export default defineConfig(({ mode }) => {
77
+ const config = workspace(mode as WorkspaceEnvironment)
78
+
79
+ return {
80
+ plugins: [vue()],
81
+ server: {
82
+ proxy: createViteProxy(config)
83
+ }
84
+ }
85
+ })
86
+ ```
87
+
88
+ ### 2. Add scripts to `package.json`
89
+
90
+ ```json
91
+ {
92
+ "scripts": {
93
+ "dev": "vite",
94
+ "dev:local": "vite --mode local",
95
+ "build": "vite build"
96
+ }
97
+ }
98
+ ```
99
+
100
+ ### 3. Generate Netlify Config
101
+
102
+ ```typescript
103
+ import { writeNetlifyConfig } from '@bagelink/workspace'
104
+ import workspace from './bgl.config'
105
+
106
+ const config = workspace('production')
107
+ writeNetlifyConfig(config, './netlify.toml')
108
+ ```
109
+
110
+ ## Monorepo Support
111
+
112
+ ### Root Config (`/bgl.config.ts`)
113
+
114
+ ```typescript
115
+ import { defineWorkspace } from '@bagelink/workspace'
116
+
117
+ export default defineWorkspace({
118
+ local: {
119
+ host: 'http://localhost:8000',
120
+ proxy: '/api',
121
+ },
122
+ development: {
123
+ host: 'https://shared.bagel.to',
124
+ proxy: '/api',
125
+ },
126
+ production: {
127
+ host: 'https://shared.bagel.to',
128
+ proxy: '/api',
129
+ },
130
+ })
131
+ ```
132
+
133
+ ### Project Override (`/projects/admin/bgl.config.ts`)
134
+
135
+ ```typescript
136
+ import { defineWorkspace } from '@bagelink/workspace'
137
+ import rootWorkspace from '../../bgl.config'
138
+
139
+ export default defineWorkspace({
140
+ local: rootWorkspace('local'),
141
+ development: rootWorkspace('development'),
142
+ production: {
143
+ ...rootWorkspace('production'),
144
+ host: 'https://admin.bagel.to', // Override
145
+ },
146
+ })
147
+ ```
148
+
149
+ ## CLI
150
+
151
+ ### `npx bgl init`
152
+
153
+ Interactively generate a `bgl.config.ts` file.
154
+
155
+ ```bash
156
+ npx bgl init
157
+ ```
158
+
159
+ ### `npx bgl --help`
160
+
161
+ Show CLI help.
162
+
163
+ ## API
164
+
165
+ ### `defineWorkspace(configs)`
166
+
167
+ Define workspace configuration for all environments.
168
+
169
+ ```typescript
170
+ const workspace = defineWorkspace({
171
+ local: { host: 'http://localhost:8000', proxy: '/api' },
172
+ development: { host: 'https://project.bagel.to', proxy: '/api' },
173
+ production: { host: 'https://project.bagel.to', proxy: '/api' },
174
+ })
175
+ ```
176
+
177
+ ### `generateWorkspaceConfig(root?, configFile?)`
178
+
179
+ Interactively generate a bgl.config.ts file (called automatically when no config is found).
180
+
181
+ ### `generateWorkspaceConfigSync(projectId, root?, configFile?, customHost?)`
182
+
183
+ Generate a bgl.config.ts file programmatically.
184
+
185
+ ```typescript
186
+ import { generateWorkspaceConfigSync } from '@bagelink/workspace'
187
+
188
+ generateWorkspaceConfigSync('my-project')
189
+ // Creates bgl.config.ts with host: https://my-project.bagel.to
190
+ ```
191
+
192
+ ### `createWorkspace(options?)`
193
+
194
+ Create workspace instance with async config resolution (for advanced monorepo setups).
195
+
196
+ ```typescript
197
+ const workspace = createWorkspace({ root: process.cwd() })
198
+
199
+ const config = await workspace.getConfig('production')
200
+ const proxy = workspace.createProxy(config)
201
+ workspace.generateNetlify(config, './netlify.toml')
202
+ ```
203
+
204
+ ### `createViteProxy(config)`
205
+
206
+ Generate Vite proxy configuration.
207
+
208
+ ### `writeNetlifyConfig(config, outPath?, additionalConfig?)`
209
+
210
+ Generate and write netlify.toml file.
211
+
212
+ ### `setBuildEnvVars(config)`
213
+
214
+ Set environment variables for build process:
215
+ - `BGL_PROXY_PATH` - Proxy path (e.g., `/api`)
216
+ - `BGL_API_HOST` - API host URL
217
+ - `BGL_OPENAPI_URL` - OpenAPI specification URL
218
+
219
+ ## License
220
+
221
+ MIT © Bagel Studio
package/bin/bgl.ts ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ import process from 'node:process'
3
+ import { generateWorkspaceConfig } from '../src/init.js'
4
+
5
+ const [,, command] = process.argv
6
+
7
+ async function main() {
8
+ if (command === 'init') {
9
+ await generateWorkspaceConfig()
10
+ }
11
+ else {
12
+ console.log(`
13
+ Bagel Workspace CLI
14
+
15
+ Usage:
16
+ bgl init Generate bgl.config.ts interactively
17
+
18
+ Options:
19
+ --help, -h Show this help message
20
+ `)
21
+ process.exit(command === '--help' || command === '-h' ? 0 : 1)
22
+ }
23
+ }
24
+
25
+ main().catch((error: unknown) => {
26
+ console.error('Error:', error)
27
+ process.exit(1)
28
+ })
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const process = require('node:process');
5
+ const init = require('../shared/workspace.CmoK7Tc1.cjs');
6
+ require('node:fs');
7
+ require('node:path');
8
+ require('prompts');
9
+
10
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
11
+
12
+ const process__default = /*#__PURE__*/_interopDefaultCompat(process);
13
+
14
+ const [, , command] = process__default.argv;
15
+ async function main() {
16
+ if (command === "init") {
17
+ await init.generateWorkspaceConfig();
18
+ } else {
19
+ console.log(`
20
+ Bagel Workspace CLI
21
+
22
+ Usage:
23
+ bgl init Generate bgl.config.ts interactively
24
+
25
+ Options:
26
+ --help, -h Show this help message
27
+ `);
28
+ process__default.exit(command === "--help" || command === "-h" ? 0 : 1);
29
+ }
30
+ }
31
+ main().catch((error) => {
32
+ console.error("Error:", error);
33
+ process__default.exit(1);
34
+ });
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ import process from 'node:process';
3
+ import { g as generateWorkspaceConfig } from '../shared/workspace.BbV_HGYT.mjs';
4
+ import 'node:fs';
5
+ import 'node:path';
6
+ import 'prompts';
7
+
8
+ const [, , command] = process.argv;
9
+ async function main() {
10
+ if (command === "init") {
11
+ await generateWorkspaceConfig();
12
+ } else {
13
+ console.log(`
14
+ Bagel Workspace CLI
15
+
16
+ Usage:
17
+ bgl init Generate bgl.config.ts interactively
18
+
19
+ Options:
20
+ --help, -h Show this help message
21
+ `);
22
+ process.exit(command === "--help" || command === "-h" ? 0 : 1);
23
+ }
24
+ }
25
+ main().catch((error) => {
26
+ console.error("Error:", error);
27
+ process.exit(1);
28
+ });
package/dist/index.cjs ADDED
@@ -0,0 +1,194 @@
1
+ 'use strict';
2
+
3
+ const node_fs = require('node:fs');
4
+ const node_path = require('node:path');
5
+ const process = require('node:process');
6
+ const init = require('./shared/workspace.CmoK7Tc1.cjs');
7
+ require('prompts');
8
+
9
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
+
11
+ const process__default = /*#__PURE__*/_interopDefaultCompat(process);
12
+
13
+ async function resolveConfig(mode = "development", options = {}) {
14
+ const root = options.root ?? process__default.cwd();
15
+ const configFile = options.configFile ?? "bgl.config.ts";
16
+ const localConfigPath = node_path.resolve(root, configFile);
17
+ const localConfig = await loadConfig(localConfigPath, mode);
18
+ if (localConfig) {
19
+ return localConfig;
20
+ }
21
+ let currentDir = root;
22
+ const rootDir = node_path.resolve("/");
23
+ while (currentDir !== rootDir) {
24
+ const parentDir = node_path.resolve(currentDir, "..");
25
+ const parentConfigPath = node_path.join(parentDir, configFile);
26
+ if (node_fs.existsSync(parentConfigPath)) {
27
+ const config = await loadConfig(parentConfigPath, mode);
28
+ if (config) {
29
+ return config;
30
+ }
31
+ }
32
+ currentDir = parentDir;
33
+ }
34
+ if (options.interactive !== false) {
35
+ await init.generateWorkspaceConfig(root, configFile);
36
+ const newConfig = await loadConfig(localConfigPath, mode);
37
+ if (newConfig) {
38
+ return newConfig;
39
+ }
40
+ }
41
+ throw new Error(`No bgl.config.ts found in ${root} or parent directories`);
42
+ }
43
+ async function loadConfig(configPath, mode) {
44
+ if (!node_fs.existsSync(configPath)) {
45
+ return null;
46
+ }
47
+ try {
48
+ const module = await import(`file://${configPath}`);
49
+ const configMap = module.default ?? module.configs ?? module.config;
50
+ if (typeof configMap === "function") {
51
+ return configMap(mode);
52
+ }
53
+ if (typeof configMap === "object" && configMap !== null) {
54
+ const modeConfig = configMap[mode];
55
+ if (mode in configMap && modeConfig !== void 0 && modeConfig !== null) {
56
+ return modeConfig;
57
+ }
58
+ return configMap;
59
+ }
60
+ return null;
61
+ } catch (error) {
62
+ console.warn(`Failed to load config from ${configPath}:`, error);
63
+ return null;
64
+ }
65
+ }
66
+ function mergeConfigs(base, override) {
67
+ return {
68
+ ...base,
69
+ ...override
70
+ };
71
+ }
72
+
73
+ function generateNetlifyRedirect(config) {
74
+ const redirect = `[[redirects]]
75
+ from = "${config.proxy}/*"
76
+ to = "${config.host}/:splat"
77
+ status = 200
78
+ force = true
79
+ headers = {X-From = "Netlify"}
80
+ `;
81
+ return redirect;
82
+ }
83
+ function generateNetlifyConfig(config, additionalConfig) {
84
+ const redirect = generateNetlifyRedirect(config);
85
+ if (additionalConfig !== void 0 && additionalConfig !== "") {
86
+ return `${redirect}
87
+ ${additionalConfig}`;
88
+ }
89
+ return redirect;
90
+ }
91
+ function writeNetlifyConfig(config, outPath = "./netlify.toml", additionalConfig) {
92
+ const content = generateNetlifyConfig(config, additionalConfig);
93
+ const resolvedPath = node_path.resolve(outPath);
94
+ node_fs.writeFileSync(resolvedPath, content, "utf-8");
95
+ console.log(`\u2713 Generated netlify.toml at ${resolvedPath}`);
96
+ }
97
+ function setBuildEnvVars(config) {
98
+ process__default.env.BGL_PROXY_PATH = config.proxy;
99
+ process__default.env.BGL_API_HOST = config.host;
100
+ if (config.openapi_url !== void 0 && config.openapi_url !== "") {
101
+ process__default.env.BGL_OPENAPI_URL = config.openapi_url;
102
+ }
103
+ }
104
+
105
+ function createViteProxy(config) {
106
+ const proxy = {};
107
+ if (config.proxy && config.host) {
108
+ proxy[config.proxy] = {
109
+ target: config.host,
110
+ changeOrigin: true,
111
+ rewrite: (path) => path.replace(new RegExp(`^${config.proxy}`), ""),
112
+ secure: true
113
+ };
114
+ }
115
+ if (config.host) {
116
+ proxy["/files"] = {
117
+ target: config.host,
118
+ changeOrigin: true,
119
+ secure: true
120
+ };
121
+ }
122
+ return proxy;
123
+ }
124
+ function createCustomProxy(paths, target, options = {}) {
125
+ const proxy = {};
126
+ for (const path of paths) {
127
+ proxy[path] = {
128
+ target,
129
+ changeOrigin: options.changeOrigin ?? true,
130
+ secure: options.secure ?? true,
131
+ ...options.rewrite === true && {
132
+ rewrite: (p) => p.replace(new RegExp(`^${path}`), "")
133
+ }
134
+ };
135
+ }
136
+ return proxy;
137
+ }
138
+
139
+ function defineWorkspace(configs) {
140
+ return (mode = "development") => {
141
+ return configs[mode] || configs.development;
142
+ };
143
+ }
144
+ function createWorkspace(options = {}) {
145
+ let cachedConfig = null;
146
+ return {
147
+ /**
148
+ * Get resolved config for the specified environment
149
+ */
150
+ async getConfig(mode = "development") {
151
+ if (!cachedConfig) {
152
+ cachedConfig = await resolveConfig(mode, options);
153
+ }
154
+ return cachedConfig;
155
+ },
156
+ /**
157
+ * Create Vite proxy configuration
158
+ */
159
+ createProxy(config) {
160
+ return createViteProxy(config);
161
+ },
162
+ /**
163
+ * Generate Netlify configuration file
164
+ */
165
+ generateNetlify(config, outPath = "./netlify.toml", additionalConfig) {
166
+ writeNetlifyConfig(config, outPath, additionalConfig);
167
+ },
168
+ /**
169
+ * Set build environment variables
170
+ */
171
+ setBuildEnv(config) {
172
+ setBuildEnvVars(config);
173
+ },
174
+ /**
175
+ * Clear cached configuration
176
+ */
177
+ clearCache() {
178
+ cachedConfig = null;
179
+ }
180
+ };
181
+ }
182
+
183
+ exports.generateWorkspaceConfig = init.generateWorkspaceConfig;
184
+ exports.generateWorkspaceConfigSync = init.generateWorkspaceConfigSync;
185
+ exports.createCustomProxy = createCustomProxy;
186
+ exports.createViteProxy = createViteProxy;
187
+ exports.createWorkspace = createWorkspace;
188
+ exports.defineWorkspace = defineWorkspace;
189
+ exports.generateNetlifyConfig = generateNetlifyConfig;
190
+ exports.generateNetlifyRedirect = generateNetlifyRedirect;
191
+ exports.mergeConfigs = mergeConfigs;
192
+ exports.resolveConfig = resolveConfig;
193
+ exports.setBuildEnvVars = setBuildEnvVars;
194
+ exports.writeNetlifyConfig = writeNetlifyConfig;
@@ -0,0 +1,127 @@
1
+ type WorkspaceEnvironment = 'local' | 'development' | 'production';
2
+ interface WorkspaceConfig {
3
+ /**
4
+ * The host URL of the backend API server
5
+ * @example 'http://localhost:8000' | 'https://project.bagel.to'
6
+ */
7
+ host: string;
8
+ /**
9
+ * The proxy path to use for API requests
10
+ * @default '/api'
11
+ */
12
+ proxy: string;
13
+ /**
14
+ * Optional OpenAPI specification URL for SDK generation
15
+ */
16
+ openapi_url?: string;
17
+ }
18
+ interface WorkspaceOptions {
19
+ /**
20
+ * Root directory of the workspace
21
+ * @default process.cwd()
22
+ */
23
+ root?: string;
24
+ /**
25
+ * Path to the config file relative to root
26
+ * @default 'bgl.config.ts'
27
+ */
28
+ configFile?: string;
29
+ /**
30
+ * Enable interactive config generation if no config is found
31
+ * @default true
32
+ */
33
+ interactive?: boolean;
34
+ }
35
+ interface ProxyConfig {
36
+ [path: string]: {
37
+ target: string;
38
+ changeOrigin: boolean;
39
+ rewrite?: (path: string) => string;
40
+ secure: boolean;
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Load and resolve bgl.config.ts with cascading support
46
+ * Looks for config files from current directory up to workspace root
47
+ * If no config is found, prompts to create one interactively
48
+ */
49
+ declare function resolveConfig(mode?: WorkspaceEnvironment, options?: WorkspaceOptions): Promise<WorkspaceConfig>;
50
+ /**
51
+ * Merge two configs, with the second one taking precedence
52
+ */
53
+ declare function mergeConfigs(base: WorkspaceConfig, override: Partial<WorkspaceConfig>): WorkspaceConfig;
54
+
55
+ /**
56
+ * Generate bgl.config.ts file interactively
57
+ */
58
+ declare function generateWorkspaceConfig(root?: string, configFile?: string): Promise<void>;
59
+ /**
60
+ * Generate bgl.config.ts non-interactively
61
+ */
62
+ declare function generateWorkspaceConfigSync(projectId: string, root?: string, configFile?: string, customHost?: string): void;
63
+
64
+ /**
65
+ * Generate netlify.toml redirect configuration
66
+ */
67
+ declare function generateNetlifyRedirect(config: WorkspaceConfig): string;
68
+ /**
69
+ * Generate complete netlify.toml file
70
+ */
71
+ declare function generateNetlifyConfig(config: WorkspaceConfig, additionalConfig?: string): string;
72
+ /**
73
+ * Write netlify.toml file to disk
74
+ */
75
+ declare function writeNetlifyConfig(config: WorkspaceConfig, outPath?: string, additionalConfig?: string): void;
76
+ /**
77
+ * Set environment variables for build process
78
+ */
79
+ declare function setBuildEnvVars(config: WorkspaceConfig): void;
80
+
81
+ /**
82
+ * Create Vite proxy configuration from WorkspaceConfig
83
+ */
84
+ declare function createViteProxy(config: WorkspaceConfig): ProxyConfig;
85
+ /**
86
+ * Create custom proxy configuration
87
+ */
88
+ declare function createCustomProxy(paths: string[], target: string, options?: {
89
+ changeOrigin?: boolean;
90
+ rewrite?: boolean;
91
+ secure?: boolean;
92
+ }): ProxyConfig;
93
+
94
+ /**
95
+ * Define workspace configuration
96
+ * Simple helper to get config from a config map
97
+ */
98
+ declare function defineWorkspace(configs: Record<WorkspaceEnvironment, WorkspaceConfig>): (mode?: WorkspaceEnvironment) => WorkspaceConfig;
99
+ /**
100
+ * Create a workspace instance for managing project configuration
101
+ * Supports both single project and monorepo setups
102
+ */
103
+ declare function createWorkspace(options?: WorkspaceOptions): {
104
+ /**
105
+ * Get resolved config for the specified environment
106
+ */
107
+ getConfig(mode?: WorkspaceEnvironment): Promise<WorkspaceConfig>;
108
+ /**
109
+ * Create Vite proxy configuration
110
+ */
111
+ createProxy(config: WorkspaceConfig): ProxyConfig;
112
+ /**
113
+ * Generate Netlify configuration file
114
+ */
115
+ generateNetlify(config: WorkspaceConfig, outPath?: string, additionalConfig?: string): void;
116
+ /**
117
+ * Set build environment variables
118
+ */
119
+ setBuildEnv(config: WorkspaceConfig): void;
120
+ /**
121
+ * Clear cached configuration
122
+ */
123
+ clearCache(): void;
124
+ };
125
+
126
+ export { createCustomProxy, createViteProxy, createWorkspace, defineWorkspace, generateNetlifyConfig, generateNetlifyRedirect, generateWorkspaceConfig, generateWorkspaceConfigSync, mergeConfigs, resolveConfig, setBuildEnvVars, writeNetlifyConfig };
127
+ export type { ProxyConfig, WorkspaceConfig, WorkspaceEnvironment, WorkspaceOptions };