Skip to content

Commit 9e6d596

Browse files
committed
fix: check peer dependencies on install
1 parent c624883 commit 9e6d596

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
},
5555
"scripts": {
5656
"prepack": "nuxt-module-build build && node scripts/post-build.js",
57-
"postinstall": "node -e \"try { require('fs').chmodSync('./dist/cli.mjs', '755') } catch(e) {}\"",
57+
"postinstall": "node -e \"try { require('fs').chmodSync('./dist/cli.mjs', '755') } catch(e) {}\" && node scripts/check-peer-dependencies.js",
5858
"predev": "node -v | grep -q 'v22' || (echo 'Please use Node.js v22 for development' && exit 1)",
5959
"dev": "yarn dev:prepare && nuxi dev playground",
6060
"dev:build": "nuxi build playground",

scripts/check-peer-dependecies.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env node
2+
3+
import { readFileSync, existsSync } from 'node:fs'
4+
import { join } from 'node:path'
5+
6+
const REQUIRED_PEERS = ['bcrypt', 'nodemailer', '@nuxt/kit']
7+
const OPTIONAL_PEERS = ['nuxt-api-shield']
8+
9+
function checkPackageJson() {
10+
const missing = []
11+
const optional = []
12+
13+
try {
14+
// Look for package.json in the consumer app (current working directory)
15+
const pkgPath = join(process.cwd(), 'package.json')
16+
17+
if (!existsSync(pkgPath)) {
18+
console.log('⚠️ Could not find package.json in current directory')
19+
return { missing: REQUIRED_PEERS, optional: OPTIONAL_PEERS }
20+
}
21+
22+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'))
23+
24+
// Check all possible dependency locations
25+
const allDeps = {
26+
...pkg.dependencies || {},
27+
...pkg.devDependencies || {},
28+
...pkg.peerDependencies || {}
29+
}
30+
31+
// Check required dependencies
32+
for (const dep of REQUIRED_PEERS) {
33+
if (!allDeps[dep]) {
34+
missing.push(dep)
35+
}
36+
}
37+
38+
// Check optional dependencies
39+
for (const dep of OPTIONAL_PEERS) {
40+
if (!allDeps[dep]) {
41+
optional.push(dep)
42+
}
43+
}
44+
45+
return { missing, optional, packageName: pkg.name || 'your project' }
46+
}
47+
catch (error) {
48+
console.log('⚠️ Error reading package.json:', error.message)
49+
// If we can't read package.json, assume all deps are missing
50+
return { missing: REQUIRED_PEERS, optional: OPTIONAL_PEERS, packageName: 'your project' }
51+
}
52+
}
53+
54+
function getPackageManagerCommand() {
55+
// Detect package manager from lock files or environment
56+
const cwd = process.cwd()
57+
58+
if (existsSync(join(cwd, 'pnpm-lock.yaml'))) {
59+
return 'pnpm add'
60+
}
61+
else if (existsSync(join(cwd, 'yarn.lock'))) {
62+
return 'yarn add'
63+
}
64+
else if (existsSync(join(cwd, 'package-lock.json'))) {
65+
return 'npm install'
66+
}
67+
else if (process.env.npm_config_user_agent?.includes('pnpm')) {
68+
return 'pnpm add'
69+
}
70+
else if (process.env.npm_config_user_agent?.includes('yarn')) {
71+
return 'yarn add'
72+
}
73+
74+
// Default fallback
75+
return 'npm install'
76+
}
77+
78+
function displayResults({ missing, optional, packageName }) {
79+
const primaryCmd = getPackageManagerCommand()
80+
81+
if (missing.length > 0) {
82+
console.log('\n🚨 nuxt-users: Missing required peer dependencies!')
83+
console.log(`Project: ${packageName}`)
84+
console.log('These dependencies are required for nuxt-users to work properly.\n')
85+
86+
console.log('💾 Missing dependencies:')
87+
missing.forEach(dep => console.log(` - ${dep}`))
88+
console.log('')
89+
90+
console.log('🔧 Install command (detected):')
91+
console.log(` ${primaryCmd} ${missing.join(' ')}\n`)
92+
93+
console.log('🔧 Alternative install commands:')
94+
console.log(` npm install ${missing.join(' ')}`)
95+
console.log(` pnpm add ${missing.join(' ')}`)
96+
console.log(` yarn add ${missing.join(' ')}\n`)
97+
98+
console.log('📚 Documentation: https://nuxt-users.webmania.cc/installation')
99+
console.log('🐛 Issues: https://github.com/rrd108/nuxt-users/issues\n')
100+
101+
// Exit with error code to make installation fail
102+
process.exit(1)
103+
}
104+
105+
if (optional.length > 0 && missing.length === 0) {
106+
console.log('\n💡 nuxt-users: Optional dependencies available!')
107+
console.log(`Project: ${packageName}`)
108+
console.log('These provide additional security and features:\n')
109+
110+
console.log('🔒 Optional dependencies:')
111+
optional.forEach(dep => console.log(` - ${dep}`))
112+
console.log('')
113+
114+
console.log('🔧 Install command (detected):')
115+
console.log(` ${primaryCmd} ${optional.join(' ')}\n`)
116+
117+
console.log('🔧 Alternative install commands:')
118+
console.log(` npm install ${optional.join(' ')}`)
119+
console.log(` pnpm add ${optional.join(' ')}`)
120+
console.log(` yarn add ${optional.join(' ')}\n`)
121+
}
122+
123+
if (missing.length === 0 && optional.length === 0) {
124+
console.log(`✅ nuxt-users: All dependencies found in ${packageName}!`)
125+
}
126+
else if (missing.length === 0) {
127+
console.log(`✅ nuxt-users: All required dependencies found in ${packageName}!`)
128+
}
129+
}
130+
131+
async function checkPeerDependencies() {
132+
const result = checkPackageJson()
133+
displayResults(result)
134+
}
135+
136+
// Handle both direct execution and module import
137+
if (import.meta.url === `file://${process.argv[1]}`) {
138+
checkPeerDependencies().catch(console.error)
139+
}
140+
141+
export { checkPeerDependencies }

0 commit comments

Comments
 (0)