File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 54
54
"test-unit" : " vitest run" ,
55
55
"coverage" : " vitest run --coverage" ,
56
56
"release" : " esno ./scripts/release.mts" ,
57
- "preinstall" : " node -e \" if (process.env.INIT_CWD === process.cwd()) { process.exit(1) }\" || npx only-allow pnpm " ,
57
+ "preinstall" : " node -e \" if (process.env.INIT_CWD === process.cwd()) { process.exit(1) }\" || esno ./scripts/preinstall.mts " ,
58
58
"postinstall" : " node -e \" if (process.env.INIT_CWD === process.cwd()) { process.exit(1) }\" || npx simple-git-hooks"
59
59
},
60
60
"peerDependencies" : {
Original file line number Diff line number Diff line change
1
+ import fs from 'node:fs'
2
+ import spawn from 'cross-spawn'
3
+
4
+ declare const process : NodeJS . Process
5
+
6
+ function convertVersion ( dependencies ) {
7
+ let isConverted = false
8
+ for ( const k in dependencies ) {
9
+ if ( dependencies [ k ] . startsWith ( '^' ) || dependencies [ k ] === '*' ) {
10
+ dependencies [ k ] = 'latest'
11
+ isConverted = true
12
+ }
13
+ }
14
+ return isConverted
15
+ }
16
+
17
+ async function preinstall ( ) {
18
+ // 仅限开发环境
19
+ if ( process . env . INIT_CWD === process . cwd ( ) ) {
20
+ // 配置 git 钩子
21
+ spawn . sync ( 'npx' , [ 'only-allow' , 'pnpm' ] , { stdio : 'inherit' } )
22
+
23
+ // 安装依赖前,将依赖的版本号替换为 latest
24
+ // 只会替换 ^* 和 *,如果需要指定某些依赖的版本,可以使用波浪号,或直接锁死
25
+ // 一方面是保持 git diff 的干净清爽,另一方面是保持依赖不过时
26
+ // 版本号的变更记录用 lock 文件来追踪
27
+ const pkg = JSON . parse ( fs . readFileSync ( './package.json' , 'utf-8' ) )
28
+ const { dependencies, devDependencies } = pkg
29
+
30
+ const isDependenciesConverted = convertVersion ( dependencies )
31
+ const isDevDependenciesConverted = convertVersion ( devDependencies )
32
+
33
+ if ( isDependenciesConverted || isDevDependenciesConverted ) {
34
+ fs . writeFileSync ( './package.json' , JSON . stringify ( pkg , null , 2 ) )
35
+ }
36
+ }
37
+ }
38
+
39
+ try {
40
+ preinstall ( )
41
+ } catch ( e ) {
42
+ console . error ( e )
43
+ }
You can’t perform that action at this time.
0 commit comments