forked from onejs/one
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-zero.ts
60 lines (50 loc) · 1.69 KB
/
setup-zero.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { promises as fs } from 'node:fs'
import * as path from 'node:path'
console.info(`Linking in zero`)
// Function to check if a file or directory exists
async function pathExists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath)
return true
} catch {
return false
}
}
// Function to create a symlink in the node_modules directory
async function createSymlink(targetPath: string, linkName: string): Promise<void> {
const nodeModulesPath = path.resolve('node_modules', linkName)
try {
await fs.symlink(targetPath, nodeModulesPath, 'junction')
console.info(`Symlink created: ${linkName} -> ${targetPath}`)
} catch (err) {
console.error(`Failed to create symlink for ${linkName}:`, err)
}
}
// Main function to process the zeroDeps in package.json
async function processZeroDeps() {
const packageJsonPath = path.resolve('package.json')
let packageJson
try {
const packageJsonData = await fs.readFile(packageJsonPath, 'utf-8')
packageJson = JSON.parse(packageJsonData)
} catch (err) {
console.error('Error reading package.json:', err)
return
}
const zeroDeps = packageJson.zeroDeps
if (!zeroDeps || typeof zeroDeps !== 'object') {
console.error('No zeroDeps found in package.json')
return
}
for (const [depName, depPath] of Object.entries(zeroDeps)) {
const resolvedDepPath = path.resolve(depPath as string)
const exists = await pathExists(resolvedDepPath)
if (exists) {
await createSymlink(resolvedDepPath, depName)
} else {
console.warn(`Path does not exist: ${resolvedDepPath}`)
}
}
}
// Run the script
processZeroDeps().catch((err) => console.error('Error processing zeroDeps:', err))