Skip to content

Commit 2cb6656

Browse files
committed
chore: Compatible with lower versions of npm
1 parent c6e1dee commit 2cb6656

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/index.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
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'
33
import http from 'node:http'
44
import https from 'node:https'
55
import path from 'node:path'
6+
import process from 'node:process'
67
import { fileURLToPath } from 'node:url'
78
import * as tar from 'tar'
89
/**
@@ -24,9 +25,15 @@ export async function fetchAndExtractPackage(options: { name: string, dist?: str
2425
try {
2526
await requestAuth(path.join(url, '..'))
2627

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+
}
2735
// Create temporary directory
28-
29-
await fs.mkdir(tempDir, { recursive: true })
36+
await fsp.mkdir(tempDir, { recursive: true })
3037

3138
// Get the package tarball URL
3239
const tgzPath = await Promise.any([
@@ -44,7 +51,7 @@ export async function fetchAndExtractPackage(options: { name: string, dist?: str
4451

4552
// Read package.json to get the main field
4653
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'))
4855
let mainFile = packageJson.main || 'index.js'
4956
if (dist && !mainFile.includes(dist) && packageJson.exports) {
5057
for (const key in packageJson.exports) {
@@ -61,15 +68,15 @@ export async function fetchAndExtractPackage(options: { name: string, dist?: str
6168
}
6269
// Read the main file content
6370
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')
6572
// 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 })
6774

6875
return mainFileContent
6976
}
7077
catch (error) {
7178
// Clean up in case of error
72-
await fs.rm(tempDir, { recursive: true, force: true })
79+
await fsp.rm(tempDir, { recursive: true, force: true })
7380
throw error
7481
}
7582
}
@@ -103,7 +110,7 @@ async function downloadWitchPack(name: string, tempDir: string, retry: number) {
103110
})
104111
}, retry)
105112
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)))
107114
return path.join(tempDir, tarballPath)
108115
}
109116

@@ -136,7 +143,7 @@ async function downloadWithHttp(name: string, tempDir: string, tempFile: string,
136143
resolve()
137144
})
138145
}).on('error', (error) => {
139-
fs.unlink(tgzPath).catch((error) => {
146+
fsp.unlink(tgzPath).catch((error) => {
140147
reject(error)
141148
})
142149
reject(error)
@@ -147,5 +154,5 @@ async function downloadWithHttp(name: string, tempDir: string, tempFile: string,
147154
}
148155

149156
function requestAuth(tempDir: string) {
150-
return fs.chmod(tempDir, 0o777)
157+
return fsp.chmod(tempDir, 0o777)
151158
}

0 commit comments

Comments
 (0)