a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(lex-cli): use Node.js module resolution to resolve lexicon imports

Mary 3ef08ad5 7cd0bd45

+39 -22
+5
.changeset/fluffy-turtles-sip.md
··· 1 + --- 2 + '@atcute/lex-cli': patch 3 + --- 4 + 5 + use Node.js module resolution to resolve lexicon imports
+34 -22
packages/lexicons/lex-cli/src/commands/generate.ts
··· 1 1 import * as fs from 'node:fs/promises'; 2 + import * as module from 'node:module'; 2 3 import * as path from 'node:path'; 3 4 4 5 import { merge, object } from '@optique/core/constructs'; ··· 22 23 configDirname: string, 23 24 ): Promise<ImportMapping[]> => { 24 25 const mappings: ImportMapping[] = []; 26 + const require = module.createRequire(path.join(configDirname, '__lex_cli__.js')); 25 27 26 28 for (const packageName of imports) { 27 - // walk up from config directory to find package in node_modules 28 29 let packageJson: unknown; 29 - let currentDir = configDirname; 30 - let found = false; 30 + 31 + try { 32 + const entryPath = require.resolve(packageName); 31 33 32 - while (currentDir !== path.dirname(currentDir)) { 33 - const candidatePath = path.join(currentDir, 'node_modules', packageName, 'package.json'); 34 - try { 35 - const content = await fs.readFile(candidatePath, 'utf8'); 36 - packageJson = JSON.parse(content); 37 - found = true; 38 - break; 39 - } catch (err: any) { 40 - // only continue to parent if file not found 41 - if (err.code !== 'ENOENT') { 42 - console.error(pc.bold(pc.red(`failed to read package.json for "${packageName}":`))); 43 - console.error(err); 44 - process.exit(1); 34 + let currentDir = path.dirname(entryPath); 35 + while (true) { 36 + const candidatePath = path.join(currentDir, 'package.json'); 37 + try { 38 + const content = await fs.readFile(candidatePath, 'utf8'); 39 + packageJson = JSON.parse(content); 40 + break; 41 + } catch (err: any) { 42 + if (err.code !== 'ENOENT') { 43 + console.error(pc.bold(pc.red(`failed to read package.json for "${packageName}":`))); 44 + console.error(err); 45 + process.exit(1); 46 + } 45 47 } 46 48 47 - // not found, try parent directory 48 - currentDir = path.dirname(currentDir); 49 + if (currentDir === configDirname) { 50 + break; 51 + } 52 + 53 + const parentDir = path.dirname(currentDir); 54 + if (parentDir === currentDir) { 55 + break; 56 + } 57 + 58 + currentDir = parentDir; 49 59 } 60 + } catch (err) { 61 + console.error(pc.bold(pc.red(`failed to resolve package "${packageName}"`))); 62 + console.error(err); 63 + process.exit(1); 50 64 } 51 65 52 - if (!found) { 53 - console.error(pc.bold(pc.red(`failed to resolve package "${packageName}"`))); 54 - console.error(`Could not find package in node_modules starting from ${configDirname}`); 66 + if (!packageJson) { 67 + console.error(pc.bold(pc.red(`failed to locate package.json for "${packageName}"`))); 55 68 process.exit(1); 56 69 } 57 70 58 - // validate package.json 59 71 const result = packageJsonSchema.try(packageJson, { mode: 'passthrough' }); 60 72 if (!result.ok) { 61 73 console.error(pc.bold(pc.red(`invalid atcute:lexicons in "${packageName}":`)));