1
1
import { exec } from 'node:child_process'
2
- import { createWriteStream , promises as fs } from 'node:fs'
2
+ import { createWriteStream , existsSync , promises as fsp } from 'node:fs'
3
3
import http from 'node:http'
4
4
import https from 'node:https'
5
5
import path from 'node:path'
6
+ import process from 'node:process'
6
7
import { fileURLToPath } from 'node:url'
7
8
import * as tar from 'tar'
8
9
/**
@@ -24,9 +25,15 @@ export async function fetchAndExtractPackage(options: { name: string, dist?: str
24
25
try {
25
26
await requestAuth ( path . join ( url , '..' ) )
26
27
28
+ // 为了兼容低版本 npm,需要 package.json, 这里把外层的 package.json copy 一份到当前位置
29
+ // 判断当前位置是否有 package.json, 如果无从外层 copy 进来
30
+ const commonIntellisensePackageJsonPath = path . join ( url , '..' , 'package.json' )
31
+ const distPackageJsonPath = process . env . VITEST ? path . join ( tempDir , '../../' , 'package.json' ) : path . join ( tempDir , 'package.json' )
32
+ if ( ! existsSync ( distPackageJsonPath ) ) {
33
+ await fsp . copyFile ( commonIntellisensePackageJsonPath , distPackageJsonPath )
34
+ }
27
35
// Create temporary directory
28
-
29
- await fs . mkdir ( tempDir , { recursive : true } )
36
+ await fsp . mkdir ( tempDir , { recursive : true } )
30
37
31
38
// Get the package tarball URL
32
39
const tgzPath = await Promise . any ( [
@@ -44,7 +51,7 @@ export async function fetchAndExtractPackage(options: { name: string, dist?: str
44
51
45
52
// Read package.json to get the main field
46
53
const packageJsonPath = path . join ( tempDir , 'package' , 'package.json' )
47
- const packageJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf-8' ) )
54
+ const packageJson = JSON . parse ( await fsp . readFile ( packageJsonPath , 'utf-8' ) )
48
55
let mainFile = packageJson . main || 'index.js'
49
56
if ( dist && ! mainFile . includes ( dist ) && packageJson . exports ) {
50
57
for ( const key in packageJson . exports ) {
@@ -61,15 +68,15 @@ export async function fetchAndExtractPackage(options: { name: string, dist?: str
61
68
}
62
69
// Read the main file content
63
70
const mainFilePath = path . join ( tempDir , 'package' , mainFile )
64
- const mainFileContent = await fs . readFile ( mainFilePath , 'utf-8' )
71
+ const mainFileContent = await fsp . readFile ( mainFilePath , 'utf-8' )
65
72
// Clean up: remove the temporary directory and tarball
66
- await fs . rm ( tempDir , { recursive : true , force : true } )
73
+ await fsp . rm ( tempDir , { recursive : true , force : true } )
67
74
68
75
return mainFileContent
69
76
}
70
77
catch ( error ) {
71
78
// Clean up in case of error
72
- await fs . rm ( tempDir , { recursive : true , force : true } )
79
+ await fsp . rm ( tempDir , { recursive : true , force : true } )
73
80
throw error
74
81
}
75
82
}
@@ -103,7 +110,7 @@ async function downloadWitchPack(name: string, tempDir: string, retry: number) {
103
110
} )
104
111
} , retry )
105
112
const tarballPattern = `${ name . replace ( '@' , '' ) . replace ( '/' , '-' ) } -.*.tgz`
106
- const [ tarballPath ] = await fs . readdir ( tempDir ) . then ( files => files . filter ( file => file . match ( tarballPattern ) ) )
113
+ const [ tarballPath ] = await fsp . readdir ( tempDir ) . then ( files => files . filter ( file => file . match ( tarballPattern ) ) )
107
114
return path . join ( tempDir , tarballPath )
108
115
}
109
116
@@ -136,7 +143,7 @@ async function downloadWithHttp(name: string, tempDir: string, tempFile: string,
136
143
resolve ( )
137
144
} )
138
145
} ) . on ( 'error' , ( error ) => {
139
- fs . unlink ( tgzPath ) . catch ( ( error ) => {
146
+ fsp . unlink ( tgzPath ) . catch ( ( error ) => {
140
147
reject ( error )
141
148
} )
142
149
reject ( error )
@@ -147,5 +154,5 @@ async function downloadWithHttp(name: string, tempDir: string, tempFile: string,
147
154
}
148
155
149
156
function requestAuth ( tempDir : string ) {
150
- return fs . chmod ( tempDir , 0o777 )
157
+ return fsp . chmod ( tempDir , 0o777 )
151
158
}
0 commit comments