@dev-to/react-plugin 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/dev-to.js +79 -4
- package/package.json +1 -1
package/bin/dev-to.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { spawnSync } from 'node:child_process'
|
|
3
|
+
import fs from 'node:fs'
|
|
3
4
|
import { createRequire } from 'node:module'
|
|
4
5
|
import path from 'node:path'
|
|
5
6
|
import process from 'node:process'
|
|
@@ -70,9 +71,30 @@ function resolveViteBin() {
|
|
|
70
71
|
const require = createRequire(import.meta.url)
|
|
71
72
|
const searchPaths = [process.cwd(), path.join(process.cwd(), 'node_modules')]
|
|
72
73
|
|
|
74
|
+
const resolved = resolvePath(require, 'vite/bin/vite.js', searchPaths)
|
|
75
|
+
if (resolved) {
|
|
76
|
+
return resolved
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const viteEntry = resolvePath(require, 'vite', searchPaths)
|
|
80
|
+
if (viteEntry) {
|
|
81
|
+
const viteRoot = findPackageRoot(viteEntry)
|
|
82
|
+
if (viteRoot) {
|
|
83
|
+
const binPath = resolveBinFromPackage(viteRoot)
|
|
84
|
+
if (binPath) {
|
|
85
|
+
return binPath
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
console.error('Vite is not installed or could not be resolved. Please add it to devDependencies.')
|
|
91
|
+
process.exit(1)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function resolvePath(require, specifier, searchPaths) {
|
|
73
95
|
for (const base of searchPaths) {
|
|
74
96
|
try {
|
|
75
|
-
return require.resolve(
|
|
97
|
+
return require.resolve(specifier, { paths: [base] })
|
|
76
98
|
}
|
|
77
99
|
catch {
|
|
78
100
|
// Try next path.
|
|
@@ -80,14 +102,67 @@ function resolveViteBin() {
|
|
|
80
102
|
}
|
|
81
103
|
|
|
82
104
|
try {
|
|
83
|
-
return require.resolve(
|
|
105
|
+
return require.resolve(specifier)
|
|
84
106
|
}
|
|
85
107
|
catch {
|
|
86
|
-
|
|
87
|
-
process.exit(1)
|
|
108
|
+
return null
|
|
88
109
|
}
|
|
89
110
|
}
|
|
90
111
|
|
|
112
|
+
function findPackageRoot(startPath) {
|
|
113
|
+
let current = path.dirname(startPath)
|
|
114
|
+
while (true) {
|
|
115
|
+
const pkgPath = path.join(current, 'package.json')
|
|
116
|
+
if (fs.existsSync(pkgPath)) {
|
|
117
|
+
try {
|
|
118
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
|
|
119
|
+
if (pkg && pkg.name === 'vite') {
|
|
120
|
+
return current
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// Keep walking up.
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const parent = path.dirname(current)
|
|
129
|
+
if (parent === current) {
|
|
130
|
+
break
|
|
131
|
+
}
|
|
132
|
+
current = parent
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function resolveBinFromPackage(pkgRoot) {
|
|
139
|
+
const pkgPath = path.join(pkgRoot, 'package.json')
|
|
140
|
+
if (!fs.existsSync(pkgPath)) {
|
|
141
|
+
return null
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
|
|
146
|
+
if (pkg && pkg.bin) {
|
|
147
|
+
const binEntry = typeof pkg.bin === 'string'
|
|
148
|
+
? pkg.bin
|
|
149
|
+
: pkg.bin.vite || pkg.bin['vite']
|
|
150
|
+
if (binEntry) {
|
|
151
|
+
const binPath = path.resolve(pkgRoot, binEntry)
|
|
152
|
+
if (fs.existsSync(binPath)) {
|
|
153
|
+
return binPath
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
// Fall back to default path.
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const fallback = path.resolve(pkgRoot, 'bin', 'vite.js')
|
|
163
|
+
return fs.existsSync(fallback) ? fallback : null
|
|
164
|
+
}
|
|
165
|
+
|
|
91
166
|
function printHelp() {
|
|
92
167
|
console.log(
|
|
93
168
|
[
|