-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
203 lines (166 loc) · 5.88 KB
/
index.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env node
const chalk = require('chalk')
const Enquirer = require('enquirer')
const fs = require('fs')
const os = require('os')
const path = require('path')
const { execAsyncSpawn } = require('./lib/exec-async-command')
const pathExists = require('./lib/path-exists')
const pkgPath = path.join(process.cwd(), 'package.json')
const { Select } = Enquirer;
void async function main(){
if (!await pathExists(pkgPath)) {
console.error('package.json does exist!')
return
}
/**
* @type {{ dependencies: Record<string, string> }}
*/
const pkg = JSON.parse(await fs.promises.readFile(pkgPath, 'utf-8'))
if (!pkg.dependencies || typeof pkg.dependencies !== 'object') {
console.error('dependencies field in package.json does not exists or is not of type Object!')
return
}
if (!await pathExists(path.join(process.cwd(), 'node_modules'))) {
console.log('Dependencies are not installed!')
const confirmPrompt = new Select({
message: 'Do you want to install dependencies using npm?',
choices: [
{
message: 'Yes',
name: 'yes'
},
{
message: 'I\'ll install them myself',
name: 'no'
}
]
})
let result
try {
result = await confirmPrompt.run()
} catch (e) {
console.log('You could\'ve selected second option...')
return
}
if (result === 'no') {
console.log('OK!')
return
}
await execAsyncSpawn('npm i', {
pipeInput: true,
pipeOutput: true
})
}
const symlinkedDependencies = Object.entries(pkg.dependencies).filter(([key, value]) => value.startsWith('file:')).map(([key, value]) => {
const splittedValue = value.split(':')
const dependencyPath = splittedValue.pop();
return {
name: key,
value,
path: dependencyPath
}
})
const missingDependencies = (
await Promise.all(
symlinkedDependencies.map(
async (d) => {
const dependencyExists = await pathExists(path.join(process.cwd(), d.path))
if (!dependencyExists) {
return d
}
return true
}
)
)
).filter(v => typeof v !== 'boolean')
if (missingDependencies.length > 0) {
missingDependencies.forEach(d => {
console.error(`Dependency does not exist: ${d.name} -> ${d.value}`)
})
return
}
const missingInstalledDependencies = (
await Promise.all(
symlinkedDependencies.map(
async (d) => {
const dependencyExists = await pathExists(path.join(process.cwd(), 'node_modules', d.name))
if (!dependencyExists) {
return d
}
return true
}
)
)
).filter(v => typeof v !== 'boolean')
if (missingInstalledDependencies.length > 0) {
missingInstalledDependencies.forEach(d => {
console.error(`Dependency is not installed: ${d.name}`)
})
return
}
const realPaths = await Promise.all(symlinkedDependencies.map(async (d) => {
const dependencyPath = path.join(process.cwd(), 'node_modules', d.name)
const dependencyExists = await pathExists(dependencyPath)
const globalPath = path.join(os.homedir(), '.config', 'yarn', 'link', d.name)
const globalPathExists = await pathExists(globalPath)
if (dependencyExists) {
const realPath = await fs.promises.realpath(dependencyPath)
return {
...d,
dependencyPath,
installedPath: path.join(process.cwd(), d.path),
globalPath,
globalPathExists,
realPath,
realProject: realPath.replace(d.path.startsWith('/') ? d.path : `/${d.path}`, '')
}
}
return false
}))
const problematicDependencies = realPaths
.filter(d => typeof d !== 'boolean')
.filter((d) => d.installedPath !== d.realPath)
if (problematicDependencies.length > 0) {
console.log(`You have ${problematicDependencies.length} ${problematicDependencies.length > 1 ? 'dependencies' : 'dependency'} with incorrect path!`)
for (const d of problematicDependencies) {
console.log(d.name);
console.log(`your path: ${chalk.yellow(d.installedPath)}
while, real path: ${chalk.green(d.realPath)}`);
}
const confirmPrompt = new Select({
message: 'Do you want to fix them?',
choices: [
{
message: 'Sure',
name: 'fix dependencies'
},
{
message: 'I\'ll with this myself',
name: 'no'
}
]
})
let result
try {
result = await confirmPrompt.run()
} catch (e) {
return
}
if (result !== 'no') {
await Promise.all(problematicDependencies.map(async (d) => {
if (d.globalPathExists) {
await fs.promises.unlink(d.globalPath)
}
await fs.promises.unlink(d.dependencyPath)
await fs.promises.symlink(d.installedPath, d.globalPath)
await fs.promises.symlink(d.globalPath, d.dependencyPath)
}))
console.log('All done!');
} else {
console.log('OK! Good luck!')
}
} else {
console.log('no issues found! you are good to go!')
}
}()