Skip to content

Commit 61635b4

Browse files
committed
fix: preinstall hook runs after installing
1 parent 4301e06 commit 61635b4

File tree

5 files changed

+40
-47
lines changed

5 files changed

+40
-47
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"build": "vite build",
4343
"serve": "vite preview",
4444
"release": "esno ./scripts/release.mts",
45-
"preinstall": "node -e \"if (process.env.INIT_CWD === process.cwd()) { process.exit(1) }\" || esno ./scripts/preinstall.mts",
45+
"pnpm:devPreinstall": "esno ./scripts/devPreinstall.mts",
46+
"preinstall": "node -e \"if (process.env.INIT_CWD === process.cwd()) { process.exit(1) }\" || npx only-allow pnpm",
4647
"postinstall": "node -e \"if (process.env.INIT_CWD === process.cwd()) { process.exit(1) }\" || npx simple-git-hooks"
4748
},
4849
"peerDependencies": {
@@ -80,6 +81,7 @@
8081
"lint-staged": "latest",
8182
"lodash-es": "latest",
8283
"magicast": "latest",
84+
"only-allow": "latest",
8385
"open": "latest",
8486
"prompts": "latest",
8587
"semver": "latest",

scripts/dev.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// pnpm add esno prompts cross-spawn kolorist magicast -D -w
1+
// pnpm i only-allow esno prompts cross-spawn kolorist magicast -D -w
22

33
import fs from 'node:fs'
44
import { execSync } from 'node:child_process'
@@ -181,7 +181,7 @@ async function dev() {
181181
console.log(cyan('Installing node lts...'))
182182
spawn.sync('pnpm', ['env', 'use', '-g', 'lts'], { stdio: 'inherit' })
183183
console.log(cyan('Installing global packages...'))
184-
spawn('pnpm', ['add', 'cnpm', '@antfu/ni', 'only-allow', '-g'], { stdio: 'inherit' })
184+
spawn('pnpm', ['add', 'cnpm', '@antfu/ni', '-g'], { stdio: 'inherit' })
185185
} catch (e) {
186186

187187
}

scripts/devPreinstall.mts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from 'node:fs'
2+
3+
function convertVersion(dependencies) {
4+
let isConverted = false
5+
for (const k in dependencies) {
6+
if (dependencies[k].startsWith('^') || dependencies[k] === '*') {
7+
dependencies[k] = 'latest'
8+
isConverted = true
9+
}
10+
}
11+
return isConverted
12+
}
13+
14+
function devPreinstall() {
15+
// 安装依赖前,将依赖的版本号替换为 latest
16+
// 只会替换 ^* 和 *,如果需要指定某些依赖的版本,可以使用波浪号,或直接锁死
17+
// 一方面是保持 git diff 的干净清爽,另一方面是保持依赖不过时
18+
// 版本号的变更记录用 lock 文件来追踪
19+
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
20+
const { dependencies, devDependencies } = pkg
21+
22+
const isDependenciesConverted = convertVersion(dependencies)
23+
const isDevDependenciesConverted = convertVersion(devDependencies)
24+
25+
if (isDependenciesConverted || isDevDependenciesConverted) {
26+
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2))
27+
}
28+
}
29+
30+
try {
31+
devPreinstall()
32+
} catch (e) {
33+
console.error(e)
34+
}

scripts/preinstall.mts

Lines changed: 0 additions & 43 deletions
This file was deleted.

scripts/release.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// pnpm add esno prompts semver cross-spawn kolorist del open -D -w
1+
// pnpm i esno prompts semver cross-spawn kolorist del open -D -w
22

33
import fs from 'node:fs'
44
import prompts from 'prompts'

0 commit comments

Comments
 (0)