Skip to content

Commit e0610fd

Browse files
committed
Fix local paths not finding extension-develop
1 parent 02e369f commit e0610fd

File tree

5 files changed

+96
-8
lines changed

5 files changed

+96
-8
lines changed

programs/cli/__spec__/cache-reuse-offline.spec.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import {describe, it, expect} from 'vitest'
12
import {execSync, spawnSync} from 'node:child_process'
23
import fs from 'node:fs'
34
import os from 'node:os'
45
import path from 'node:path'
56
import crypto from 'node:crypto'
7+
import {spawnSync as spawn} from 'node:child_process'
68

79
function cliRoot(): string {
810
return path.resolve(__dirname, '..')
@@ -48,9 +50,16 @@ function writeFixture(root: string) {
4850
fs.writeFileSync(path.join(root, 'background.js'), 'console.log("bg")')
4951
}
5052

53+
function cliMajor(): string {
54+
const pkgJson = JSON.parse(
55+
fs.readFileSync(path.resolve(__dirname, '..', 'package.json'), 'utf8')
56+
)
57+
return String(pkgJson.version || '2.0.0').split('.')[0] || '2'
58+
}
59+
5160
function canReachRegistry(): boolean {
5261
try {
53-
execSync('npm view extension-develop@2 version --json', {
62+
execSync(`npm view extension-develop@${cliMajor()} version --json`, {
5463
timeout: 8000,
5564
stdio: 'ignore'
5665
})
@@ -82,6 +91,11 @@ describe('cache reuse offline', () => {
8291
stdio: 'inherit'
8392
})
8493

94+
const prefer =
95+
(spawn('pnpm', ['--version'], {stdio: 'ignore'}).status || 1) === 0
96+
? 'pnpm'
97+
: 'npm'
98+
8599
let r = spawnSync(
86100
process.execPath,
87101
[
@@ -91,7 +105,11 @@ describe('cache reuse offline', () => {
91105
'--browser=chrome',
92106
'--silent=true'
93107
],
94-
{cwd: work, env: {...process.env, EXTENSION_DLX: 'npm'}, stdio: 'inherit'}
108+
{
109+
cwd: work,
110+
env: {...process.env, EXTENSION_DLX: prefer},
111+
stdio: 'inherit'
112+
}
95113
)
96114
expect(r.status).toBe(0)
97115

programs/cli/__spec__/dynamic-install.spec.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,23 @@ function writeFixture(root: string) {
3939
fs.writeFileSync(path.join(root, 'background.js'), 'console.log("bg")')
4040
}
4141

42+
function cliMajor(): string {
43+
const pkgJson = JSON.parse(
44+
fs.readFileSync(path.resolve(__dirname, '..', 'package.json'), 'utf8')
45+
)
46+
return String(pkgJson.version || '2.0.0').split('.')[0] || '2'
47+
}
48+
4249
function canReachRegistry(): boolean {
4350
try {
44-
execFileSync('npm', ['view', 'extension-develop@2', 'version', '--json'], {
45-
timeout: 8000,
46-
stdio: 'ignore'
47-
})
51+
execFileSync(
52+
'npm',
53+
['view', `extension-develop@${cliMajor()}`, 'version', '--json'],
54+
{
55+
timeout: 8000,
56+
stdio: 'ignore'
57+
}
58+
)
4859
return true
4960
} catch {
5061
return false

programs/cli/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ if (process.argv.length <= 2) {
4444
process.exit(0)
4545
}
4646

47-
extensionJs.parse()
47+
extensionJs.parseAsync().catch((err: unknown) => {
48+
console.error(err)
49+
process.exit(1)
50+
})

programs/cli/utils.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,38 @@ export async function requireOrDlx(
3131
const cacheDir = path.join(os.tmpdir(), 'extensionjs-cache', spec)
3232
const modulePath = path.join(cacheDir, 'node_modules', moduleName)
3333

34+
// Monorepo development fallback: use local sibling package if present
35+
try {
36+
const localDist = path.resolve(
37+
__dirname,
38+
'..', // dist/
39+
'..', // cli/ -> programs/
40+
'develop',
41+
'dist',
42+
'module.js'
43+
)
44+
if (fs.existsSync(localDist)) {
45+
return await import(pathToFileURL(localDist).href)
46+
}
47+
} catch {
48+
// Ignore
49+
}
50+
51+
try {
52+
const cwdDist = path.resolve(
53+
process.cwd(),
54+
'programs',
55+
'develop',
56+
'dist',
57+
'module.js'
58+
)
59+
if (fs.existsSync(cwdDist)) {
60+
return await import(pathToFileURL(cwdDist).href)
61+
}
62+
} catch {
63+
// Ignore
64+
}
65+
3466
let prefer = String(process.env.EXTENSION_DLX || '')
3567
.trim()
3668
.toLowerCase()
@@ -99,6 +131,30 @@ export async function requireOrDlx(
99131
spawnSync(npmCmd, args, {cwd: cacheDir, stdio: 'ignore'}).status || 0
100132
}
101133

134+
// Fallback to alternate package managers if the preferred one failed
135+
if (status !== 0) {
136+
// Try pnpm fallback
137+
try {
138+
fs.writeFileSync(
139+
path.join(cacheDir, 'package.json'),
140+
JSON.stringify({name: 'extensionjs-cache', private: true}, null, 2)
141+
)
142+
} catch {
143+
// Ignore
144+
}
145+
146+
if (prefer !== 'pnpm') {
147+
const args = ['add', spec, '--reporter', 'silent', '--no-frozen-lockfile']
148+
status =
149+
spawnSync(pnpmCmd, args, {cwd: cacheDir, stdio: 'ignore'}).status || 0
150+
}
151+
}
152+
if (status !== 0 && prefer !== 'bun') {
153+
const args = ['add', spec]
154+
status =
155+
spawnSync(bunCmd, args, {cwd: cacheDir, stdio: 'ignore'}).status || 0
156+
}
157+
102158
if (status !== 0) {
103159
throw new Error(`Failed to install ${spec}`)
104160
}

programs/develop/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "https://json.schemastore.org/tsconfig",
33
"extends": "../../tsconfig.json",
44
"compilerOptions": {
5-
"noEmit": false,
5+
"noEmit": true,
66
"types": ["node", "chrome"],
77
"lib": ["ES2021", "DOM"]
88
},

0 commit comments

Comments
 (0)