Skip to content

Commit 46ec7f0

Browse files
authored
Merge pull request #26 from objectstack-ai/copilot/add-example-folder-for-cli-project
2 parents db2a323 + b0ce092 commit 46ec7f0

File tree

16 files changed

+1658
-0
lines changed

16 files changed

+1658
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ This repository is a monorepo managed by pnpm workspaces:
179179
- **[@objectdocs/cli](./packages/cli)**: The command-line interface for building and developing sites.
180180
- **[@objectdocs/site](./packages/site)**: The core Next.js application template.
181181

182+
## 📚 Examples
183+
184+
- **[example](./example)**: A complete standalone project demonstrating CLI-created project structure. Use this to test Vercel deployment and validate that published npm packages work correctly outside the monorepo.
185+
182186
## 🤝 Contributing
183187

184188
Contributions are welcome! Please read our [Contributing Guide](./CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests.

example/.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Dependencies
2+
node_modules/
3+
.pnp
4+
.pnp.js
5+
6+
# Testing
7+
coverage/
8+
9+
# Next.js
10+
.next/
11+
out/
12+
13+
# Production
14+
build/
15+
dist/
16+
17+
# Misc
18+
.DS_Store
19+
*.pem
20+
21+
# Debug
22+
npm-debug.log*
23+
yarn-debug.log*
24+
yarn-error.log*
25+
pnpm-debug.log*
26+
27+
# Local env files
28+
.env
29+
.env*.local
30+
31+
# Vercel
32+
.vercel
33+
34+
# TypeScript
35+
*.tsbuildinfo
36+
next-env.d.ts

example/ARCHITECTURE.md

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
# Architecture Notes for @objectdocs/site Reference
2+
3+
This document explains how the ObjectDocs CLI references and uses `@objectdocs/site`, and potential alternative approaches for different deployment scenarios.
4+
5+
## Current Approach
6+
7+
### How It Works
8+
9+
The CLI (`@objectdocs/cli`) depends on `@objectdocs/site` as a regular npm dependency:
10+
11+
```json
12+
// packages/cli/package.json
13+
{
14+
"dependencies": {
15+
"@objectdocs/site": "workspace:*" // Converted to "0.2.11" when published
16+
}
17+
}
18+
```
19+
20+
When commands run (dev, build, start), they resolve the site package location:
21+
22+
```javascript
23+
// packages/cli/src/commands/dev.mjs
24+
let nextAppDir;
25+
try {
26+
nextAppDir = path.dirname(require.resolve('@objectdocs/site/package.json'));
27+
} catch (e) {
28+
// Fallback for local development
29+
nextAppDir = path.resolve(__dirname, '../../../site');
30+
}
31+
```
32+
33+
This approach:
34+
- ✅ Works in monorepo (falls back to relative path)
35+
- ✅ Works when installed from npm (resolves from node_modules)
36+
- ✅ Keeps the site package decoupled from user projects
37+
- ✅ Allows easy updates via npm
38+
39+
### Published Package Structure
40+
41+
When `@objectdocs/site` is published, it includes:
42+
43+
```
44+
@objectdocs/site/
45+
├── package.json
46+
├── next.config.mjs
47+
├── app/
48+
│ ├── layout.tsx
49+
│ ├── page.tsx
50+
│ └── [lang]/docs/[[...slug]]/page.tsx
51+
├── lib/
52+
├── middleware.ts
53+
├── source.config.ts
54+
└── ...
55+
```
56+
57+
The CLI runs Next.js commands (dev, build, start) directly in this package directory.
58+
59+
## Why This Approach Works
60+
61+
1. **Clean Separation**: User projects only need content, not Next.js config
62+
2. **Easy Updates**: Update CLI → automatically updates site engine
63+
3. **Consistent Behavior**: All users get the same site functionality
64+
4. **Simple Setup**: No need to scaffold Next.js files
65+
66+
## Potential Issues and Solutions
67+
68+
### Issue 1: Vercel Cannot Find @objectdocs/site
69+
70+
**Symptom**: Build fails with "Cannot find module '@objectdocs/site'"
71+
72+
**Cause**:
73+
- CLI package.json has incorrect dependency reference
74+
- npm/pnpm didn't resolve workspace reference correctly during publish
75+
76+
**Solution**:
77+
- Verify published CLI has correct dependency: `npm view @objectdocs/cli dependencies`
78+
- Should show `"@objectdocs/site": "0.2.11"` not `"workspace:*"`
79+
- If incorrect, republish with proper workspace protocol resolution
80+
81+
### Issue 2: Next.js Config Not Found
82+
83+
**Symptom**: "Could not find next.config.js"
84+
85+
**Cause**:
86+
- Package files filter excludes necessary files
87+
- .gitignore accidentally ignores published files
88+
89+
**Solution**:
90+
- Add `files` field to site package.json:
91+
```json
92+
{
93+
"files": [
94+
"app",
95+
"lib",
96+
"public",
97+
"*.mjs",
98+
"*.ts",
99+
"*.tsx",
100+
"*.json"
101+
]
102+
}
103+
```
104+
105+
### Issue 3: Build Output Not Found
106+
107+
**Symptom**: Build succeeds but .next directory is empty
108+
109+
**Cause**: CLI copies build output, but paths are incorrect
110+
111+
**Solution**: Already handled in build.mjs with proper path resolution
112+
113+
## Alternative Approaches Considered
114+
115+
### Approach 1: Copy Site Files to User Project (Rejected)
116+
117+
**Idea**: CLI scaffolds entire Next.js app in user's project
118+
119+
**Pros**:
120+
- More transparent (users see all files)
121+
- No node_modules dependency resolution needed
122+
123+
**Cons**:
124+
- ❌ Much harder to update (users have modified files)
125+
- ❌ Breaks "configuration as code" philosophy
126+
- ❌ Huge initial scaffolding
127+
- ❌ Users need to understand Next.js
128+
129+
**Verdict**: Rejected - goes against core philosophy
130+
131+
### Approach 2: Bundle Site in CLI (Rejected)
132+
133+
**Idea**: Bundle @objectdocs/site directly into CLI package
134+
135+
**Pros**:
136+
- Single package to install
137+
- No dependency resolution issues
138+
139+
**Cons**:
140+
- ❌ Huge package size
141+
- ❌ Can't update site without updating CLI
142+
- ❌ Makes development harder
143+
144+
**Verdict**: Rejected - poor developer experience
145+
146+
### Approach 3: Peer Dependency (Rejected)
147+
148+
**Idea**: Make @objectdocs/site a peer dependency
149+
150+
**Pros**:
151+
- User explicitly installs site package
152+
- Version control flexibility
153+
154+
**Cons**:
155+
- ❌ More complex setup for users
156+
- ❌ Can lead to version mismatches
157+
- ❌ Defeats purpose of simple CLI
158+
159+
**Verdict**: Rejected - too complex for users
160+
161+
### Approach 4: Dynamic Import (Possible Future)
162+
163+
**Idea**: Download site package on first run if not present
164+
165+
**Pros**:
166+
- Smaller initial CLI package
167+
- Can cache multiple versions
168+
169+
**Cons**:
170+
- Complex implementation
171+
- Slower first run
172+
- Network dependency
173+
174+
**Verdict**: Consider for future if other issues arise
175+
176+
## Current Solution: Hybrid Approach ✅
177+
178+
The current approach (dependency + fallback) is optimal:
179+
180+
```javascript
181+
let nextAppDir;
182+
try {
183+
// Production: resolve from node_modules
184+
nextAppDir = path.dirname(require.resolve('@objectdocs/site/package.json'));
185+
} catch (e) {
186+
// Development: fallback to monorepo
187+
nextAppDir = path.resolve(__dirname, '../../../site');
188+
}
189+
```
190+
191+
**Why it's best**:
192+
- ✅ Works in both development and production
193+
- ✅ Simple for users (just install CLI)
194+
- ✅ Easy to update
195+
- ✅ Clean separation of concerns
196+
- ✅ Standard npm dependency resolution
197+
198+
## Vercel-Specific Considerations
199+
200+
Vercel deploys work because:
201+
202+
1. **Dependency Resolution**: Vercel runs `pnpm install` which installs `@objectdocs/cli` and its dependencies (including `@objectdocs/site`)
203+
204+
2. **Build Command**: `pnpm build``objectdocs build` resolves site from node_modules
205+
206+
3. **Node Modules**: All packages are available in Vercel's build environment
207+
208+
4. **Output**: Build copies .next to project root, which Vercel deploys
209+
210+
**No special configuration needed!**
211+
212+
## Testing Standalone Installation
213+
214+
To verify the approach works outside the monorepo:
215+
216+
```bash
217+
# Create test directory
218+
mkdir /tmp/test-objectdocs
219+
cd /tmp/test-objectdocs
220+
221+
# Install CLI from npm
222+
pnpm init
223+
pnpm add -D @objectdocs/cli
224+
225+
# Verify site is installed
226+
ls -la node_modules/@objectdocs/site
227+
228+
# Create content
229+
mkdir -p content/docs
230+
echo '{"pages":["index"]}' > content/docs/meta.json
231+
echo '---\ntitle: Test\n---\n# Test' > content/docs/index.mdx
232+
233+
# Test
234+
pnpm exec objectdocs build
235+
```
236+
237+
## Recommendations
238+
239+
1. **For maintainers**: Keep the current approach, ensure proper publishing
240+
241+
2. **For users**: Just install `@objectdocs/cli`, everything else is automatic
242+
243+
3. **For Vercel**: No special configuration needed, standard Next.js deployment works
244+
245+
4. **For debugging**: Check that `@objectdocs/site` is in `node_modules` and has all app files
246+
247+
## Conclusion
248+
249+
The current architecture is sound. Any deployment issues are likely due to:
250+
- Incorrect package publishing (workspace:* not resolved)
251+
- User project configuration errors
252+
- Network/registry issues
253+
254+
The `example` folder demonstrates the correct setup and can be used to validate deployments.

0 commit comments

Comments
 (0)