Skip to content

Commit 5b2aed3

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

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

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

Lines changed: 12 additions & 1 deletion
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, '..')
@@ -82,6 +84,11 @@ describe('cache reuse offline', () => {
8284
stdio: 'inherit'
8385
})
8486

87+
const prefer =
88+
(spawn('pnpm', ['--version'], {stdio: 'ignore'}).status || 1) === 0
89+
? 'pnpm'
90+
: 'npm'
91+
8592
let r = spawnSync(
8693
process.execPath,
8794
[
@@ -91,7 +98,11 @@ describe('cache reuse offline', () => {
9198
'--browser=chrome',
9299
'--silent=true'
93100
],
94-
{cwd: work, env: {...process.env, EXTENSION_DLX: 'npm'}, stdio: 'inherit'}
101+
{
102+
cwd: work,
103+
env: {...process.env, EXTENSION_DLX: prefer},
104+
stdio: 'inherit'
105+
}
95106
)
96107
expect(r.status).toBe(0)
97108

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)