New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@atcute/lex-cli

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@atcute/lex-cli - npm Package Compare versions

Comparing version
2.2.2
to
2.3.0
+37
dist/lexicon-metadata.d.ts
import * as v from 'valibot';
/**
* Schema for a single lexicon mapping entry
*/
declare const lexiconMappingEntry: v.ObjectSchema<{
readonly type: v.PicklistSchema<["namespace", "named"], undefined>;
readonly path: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, "path must be \".\" or start with \"./\"">]>;
}, undefined>;
/**
* Schema for the atcute:lexicons field in package.json
*/
declare const atcuteLexiconsField: v.ObjectSchema<{
readonly mappings: v.OptionalSchema<v.RecordSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, "invalid NSID pattern (must be valid NSID or end with .*)">]>, v.ObjectSchema<{
readonly type: v.PicklistSchema<["namespace", "named"], undefined>;
readonly path: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, "path must be \".\" or start with \"./\"">]>;
}, undefined>, undefined>, undefined>;
}, undefined>;
/**
* Schema for package.json with atcute:lexicons field
*/
export declare const packageJsonSchema: v.LooseObjectSchema<{
readonly 'atcute:lexicons': v.OptionalSchema<v.ObjectSchema<{
readonly mappings: v.OptionalSchema<v.RecordSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, "invalid NSID pattern (must be valid NSID or end with .*)">]>, v.ObjectSchema<{
readonly type: v.PicklistSchema<["namespace", "named"], undefined>;
readonly path: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.RegexAction<string, "path must be \".\" or start with \"./\"">]>;
}, undefined>, undefined>, undefined>;
}, undefined>, undefined>;
}, undefined>;
export type LexiconMappingEntry = v.InferOutput<typeof lexiconMappingEntry>;
export type AtcuteLexiconsField = v.InferOutput<typeof atcuteLexiconsField>;
export type PackageJsonWithLexicons = v.InferOutput<typeof packageJsonSchema>;
/**
* Validates a package.json object against the schema
*/
export declare const validatePackageJson: (data: unknown) => v.SafeParseResult<typeof packageJsonSchema>;
export {};
//# sourceMappingURL=lexicon-metadata.d.ts.map
{"version":3,"file":"lexicon-metadata.d.ts","sourceRoot":"","sources":["../src/lexicon-metadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAmB7B;;GAEG;AACH,QAAA,MAAM,mBAAmB;;;aAGvB,CAAC;AAEH;;GAEG;AACH,QAAA,MAAM,mBAAmB;;;;;aAUvB,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,iBAAiB;;;;;;;aAE5B,CAAC;AAEH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAC5E,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAC5E,MAAM,MAAM,uBAAuB,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAE9E;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,MAAM,OAAO,KAAG,CAAC,CAAC,eAAe,CAAC,OAAO,iBAAiB,CAE7F,CAAC"}
import * as v from 'valibot';
import { isNsid } from '@atcute/lexicons/syntax';
/**
* Validates if a string is a valid NSID pattern (exact or wildcard)
* - Exact: "com.atproto.repo.getRecord"
* - Wildcard: "com.atproto.*"
*/
const isValidLexiconPattern = (pattern) => {
if (pattern.endsWith('.*')) {
// For wildcards, remove the .* and validate the prefix as an NSID segment
const prefix = pattern.slice(0, -2);
// Add a dummy segment to make it a valid NSID for validation
return isNsid(prefix + '.x');
}
return isNsid(pattern);
};
/**
* Schema for a single lexicon mapping entry
*/
const lexiconMappingEntry = v.object({
type: v.picklist(['namespace', 'named']),
path: v.pipe(v.string(), v.regex(/^\.$|^\.\//, `path must be "." or start with "./"`)),
});
/**
* Schema for the atcute:lexicons field in package.json
*/
const atcuteLexiconsField = v.object({
mappings: v.optional(v.record(v.pipe(v.string(), v.check(isValidLexiconPattern, `invalid NSID pattern (must be valid NSID or end with .*)`)), lexiconMappingEntry)),
});
/**
* Schema for package.json with atcute:lexicons field
*/
export const packageJsonSchema = v.looseObject({
'atcute:lexicons': v.optional(atcuteLexiconsField),
});
/**
* Validates a package.json object against the schema
*/
export const validatePackageJson = (data) => {
return v.safeParse(packageJsonSchema, data);
};
//# sourceMappingURL=lexicon-metadata.js.map
{"version":3,"file":"lexicon-metadata.js","sourceRoot":"","sources":["../src/lexicon-metadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEjD;;;;GAIG;AACH,MAAM,qBAAqB,GAAG,CAAC,OAAe,EAAW,EAAE;IAC1D,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5B,0EAA0E;QAC1E,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACpC,6DAA6D;QAC7D,OAAO,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC9B,CAAC;IACD,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC;AACxB,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;IACxC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,qCAAqC,CAAC,CAAC;CACtF,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,QAAQ,EAAE,CAAC,CAAC,QAAQ,CACnB,CAAC,CAAC,MAAM,CACP,CAAC,CAAC,IAAI,CACL,CAAC,CAAC,MAAM,EAAE,EACV,CAAC,CAAC,KAAK,CAAC,qBAAqB,EAAE,0DAA0D,CAAC,CAC1F,EACD,mBAAmB,CACnB,CACD;CACD,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC,WAAW,CAAC;IAC9C,iBAAiB,EAAE,CAAC,CAAC,QAAQ,CAAC,mBAAmB,CAAC;CAClD,CAAC,CAAC;AAMH;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAAa,EAA+C,EAAE;IACjG,OAAO,CAAC,CAAC,SAAS,CAAC,iBAAiB,EAAE,IAAI,CAAC,CAAC;AAC7C,CAAC,CAAC"}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://unpkg.com/@atcute/lex-cli/schema/lexicon-package.schema.json",
"title": "package.json with atcute:lexicons",
"description": "JSON Schema for package.json with atcute:lexicons field for lexicon import mappings",
"type": "object",
"properties": {
"atcute:lexicons": {
"type": "object",
"properties": {
"mappings": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"type": {
"enum": [
"namespace",
"named"
]
},
"path": {
"type": "string",
"pattern": "^\\.$|^\\.\\/"
}
},
"required": [
"type",
"path"
]
}
}
},
"required": []
}
},
"required": []
}
import * as v from 'valibot';
import { isNsid } from '@atcute/lexicons/syntax';
/**
* Validates if a string is a valid NSID pattern (exact or wildcard)
* - Exact: "com.atproto.repo.getRecord"
* - Wildcard: "com.atproto.*"
*/
const isValidLexiconPattern = (pattern: string): boolean => {
if (pattern.endsWith('.*')) {
// For wildcards, remove the .* and validate the prefix as an NSID segment
const prefix = pattern.slice(0, -2);
// Add a dummy segment to make it a valid NSID for validation
return isNsid(prefix + '.x');
}
return isNsid(pattern);
};
/**
* Schema for a single lexicon mapping entry
*/
const lexiconMappingEntry = v.object({
type: v.picklist(['namespace', 'named']),
path: v.pipe(v.string(), v.regex(/^\.$|^\.\//, `path must be "." or start with "./"`)),
});
/**
* Schema for the atcute:lexicons field in package.json
*/
const atcuteLexiconsField = v.object({
mappings: v.optional(
v.record(
v.pipe(
v.string(),
v.check(isValidLexiconPattern, `invalid NSID pattern (must be valid NSID or end with .*)`),
),
lexiconMappingEntry,
),
),
});
/**
* Schema for package.json with atcute:lexicons field
*/
export const packageJsonSchema = v.looseObject({
'atcute:lexicons': v.optional(atcuteLexiconsField),
});
export type LexiconMappingEntry = v.InferOutput<typeof lexiconMappingEntry>;
export type AtcuteLexiconsField = v.InferOutput<typeof atcuteLexiconsField>;
export type PackageJsonWithLexicons = v.InferOutput<typeof packageJsonSchema>;
/**
* Validates a package.json object against the schema
*/
export const validatePackageJson = (data: unknown): v.SafeParseResult<typeof packageJsonSchema> => {
return v.safeParse(packageJsonSchema, data);
};
+152
-70
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as url from 'node:url';
import { Builtins, Command, Option, Program } from '@externdefs/collider';
import { object } from '@optique/core/constructs';
import { command, constant, option } from '@optique/core/primitives';
import { run } from '@optique/run';
import { path as pathParser } from '@optique/run/valueparser';
import pc from 'picocolors';
import { lexiconDoc } from '@atcute/lexicon-doc';
import { generateLexiconApi } from './codegen.js';
const program = new Program({ binaryName: 'lex-cli' });
program.register(Builtins.HelpCommand);
program.register(class GenerateCommand extends Command {
static paths = [['generate']];
static usage = Command.Usage({
description: `Generates TypeScript schema`,
});
config = Option.String(['-c', '--config'], {
required: true,
description: `Config file`,
});
async execute() {
const configFilename = path.resolve(this.config);
const configDirname = path.dirname(configFilename);
let config;
try {
const configURL = url.pathToFileURL(configFilename);
const configMod = (await import(configURL.href));
config = configMod.default;
}
catch (err) {
console.error(pc.bold(pc.red(`failed to import config:`)));
console.error(err);
return 1;
}
const documents = [];
for await (const filename of fs.glob(config.files, { cwd: configDirname })) {
let source;
import { validatePackageJson } from './lexicon-metadata.js';
/**
* Resolves package imports to ImportMapping[]
*/
const resolveImportsToMappings = async (imports, configDirname) => {
const mappings = [];
for (const packageName of imports) {
// Walk up from config directory to find package in node_modules
let packageJson;
let currentDir = configDirname;
let found = false;
while (currentDir !== path.dirname(currentDir)) {
const candidatePath = path.join(currentDir, 'node_modules', packageName, 'package.json');
try {
source = await fs.readFile(path.join(configDirname, filename), 'utf8');
const content = await fs.readFile(candidatePath, 'utf8');
packageJson = JSON.parse(content);
found = true;
break;
}
catch (err) {
console.error(pc.bold(pc.red(`file read error with "${filename}"`)));
console.error(err);
return 1;
}
let json;
try {
json = JSON.parse(source);
}
catch (err) {
console.error(pc.bold(pc.red(`json parse error in "${filename}"`)));
console.error(err);
return 1;
}
const result = lexiconDoc.try(json, { mode: 'strip' });
if (!result.ok) {
console.error(pc.bold(pc.red(`schema validation failed for "${filename}"`)));
console.error(result.message);
for (const issue of result.issues) {
console.log(`- ${issue.code} at .${issue.path.join('.')}`);
// Only continue to parent if file not found
if (err.code !== 'ENOENT') {
console.error(pc.bold(pc.red(`failed to read package.json for "${packageName}":`)));
console.error(err);
process.exit(1);
}
return 1;
// Not found, try parent directory
currentDir = path.dirname(currentDir);
}
documents.push(result.value);
}
const result = await generateLexiconApi({
documents: documents,
mappings: config.mappings ?? [],
modules: {
importSuffix: config.modules?.importSuffix ?? '.js',
},
prettier: {
cwd: process.cwd(),
},
});
const outdir = path.join(configDirname, config.outdir);
for (const file of result.files) {
const filename = path.join(outdir, file.filename);
const dirname = path.dirname(filename);
await fs.mkdir(dirname, { recursive: true });
await fs.writeFile(filename, file.code);
if (!found) {
console.error(pc.bold(pc.red(`failed to resolve package "${packageName}"`)));
console.error(`Could not find package in node_modules starting from ${configDirname}`);
process.exit(1);
}
// Validate package.json
const result = validatePackageJson(packageJson);
if (!result.success) {
console.error(pc.bold(pc.red(`invalid atcute:lexicons in "${packageName}":`)));
console.error(result.issues);
process.exit(1);
}
const lexicons = result.output['atcute:lexicons'];
if (!lexicons?.mappings) {
continue;
}
// Convert mapping to ImportMapping[]
for (const [pattern, entry] of Object.entries(lexicons.mappings)) {
const isWildcard = pattern.endsWith('.*');
mappings.push({
nsid: [pattern],
imports: (nsid) => {
// Check if pattern matches
if (isWildcard) {
if (!nsid.startsWith(pattern.slice(0, -1))) {
throw new Error(`NSID ${nsid} does not match pattern ${pattern}`);
}
}
else {
if (nsid !== pattern) {
throw new Error(`NSID ${nsid} does not match pattern ${pattern}`);
}
}
const nsidPrefix = isWildcard ? pattern.slice(0, -2) : pattern;
const nsidRemainder = isWildcard ? nsid.slice(nsidPrefix.length + 1) : '';
let expandedPath = entry.path
.replaceAll('{{nsid}}', nsid.replaceAll('.', '/'))
.replaceAll('{{nsid_remainder}}', nsidRemainder.replaceAll('.', '/'))
.replaceAll('{{nsid_prefix}}', nsidPrefix.replaceAll('.', '/'));
if (expandedPath === '.') {
expandedPath = packageName;
}
else if (expandedPath.startsWith('./')) {
expandedPath = `${packageName}/${expandedPath.slice(2)}`;
}
return {
type: entry.type,
from: expandedPath,
};
},
});
}
}
});
const exitCode = await program.run(process.argv.slice(2));
process.exitCode = exitCode;
return mappings;
};
const parser = command('generate', object({
type: constant('generate'),
config: option('-c', '--config', pathParser({ metavar: 'CONFIG' })),
}));
const result = run(parser, { programName: 'lex-cli' });
if (result.type === 'generate') {
const configFilename = path.resolve(result.config);
const configDirname = path.dirname(configFilename);
let config;
try {
const configURL = url.pathToFileURL(configFilename);
const configMod = (await import(configURL.href));
config = configMod.default;
}
catch (err) {
console.error(pc.bold(pc.red(`failed to import config:`)));
console.error(err);
process.exit(1);
}
// Resolve imports to mappings
const importMappings = config.imports ? await resolveImportsToMappings(config.imports, configDirname) : [];
const allMappings = [...importMappings, ...(config.mappings ?? [])];
const documents = [];
for await (const filename of fs.glob(config.files, { cwd: configDirname })) {
let source;
try {
source = await fs.readFile(path.join(configDirname, filename), 'utf8');
}
catch (err) {
console.error(pc.bold(pc.red(`file read error with "${filename}"`)));
console.error(err);
process.exit(1);
}
let json;
try {
json = JSON.parse(source);
}
catch (err) {
console.error(pc.bold(pc.red(`json parse error in "${filename}"`)));
console.error(err);
process.exit(1);
}
const result = lexiconDoc.try(json, { mode: 'strip' });
if (!result.ok) {
console.error(pc.bold(pc.red(`schema validation failed for "${filename}"`)));
console.error(result.message);
for (const issue of result.issues) {
console.log(`- ${issue.code} at .${issue.path.join('.')}`);
}
process.exit(1);
}
documents.push(result.value);
}
const generationResult = await generateLexiconApi({
documents: documents,
mappings: allMappings,
modules: {
importSuffix: config.modules?.importSuffix ?? '.js',
},
prettier: {
cwd: process.cwd(),
},
});
const outdir = path.join(configDirname, config.outdir);
for (const file of generationResult.files) {
const filename = path.join(outdir, file.filename);
const dirname = path.dirname(filename);
await fs.mkdir(dirname, { recursive: true });
await fs.writeFile(filename, file.code);
}
}
//# sourceMappingURL=cli.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC1E,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,OAAO,EAAE,UAAU,EAAmB,MAAM,qBAAqB,CAAC;AAElE,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGlD,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC,CAAC;AAEvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAEvC,OAAO,CAAC,QAAQ,CACf,MAAM,eAAgB,SAAQ,OAAO;IACpC,MAAM,CAAU,KAAK,GAAG,CAA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AAC,CAAC;IAEvD,KAAK,MAAM,IAAI,IAAI,gBAAgB,CAAC,KAAK,EAAE,CAAC;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAEvC,MAAM,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC7C,MAAM,EAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;AACF,CAAC"}

@@ -5,2 +5,3 @@ import type { ImportMapping } from './codegen.js';

files: string[];
imports?: string[];
mappings?: ImportMapping[];

@@ -7,0 +8,0 @@ modules?: {

@@ -1,1 +0,1 @@

{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC;IAC3B,OAAO,CAAC,EAAE;QACT,YAAY,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;CACF;AAED,eAAO,MAAM,mBAAmB,GAAI,QAAQ,aAAa,KAAG,aAE3D,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAC;IAC3B,OAAO,CAAC,EAAE;QACT,YAAY,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;CACF;AAED,eAAO,MAAM,mBAAmB,GAAI,QAAQ,aAAa,KAAG,aAE3D,CAAC"}

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,MAAqB,EAAiB,EAAE;IAC3E,OAAO,MAAM,CAAC;AACf,CAAC,CAAC"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAYA,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,MAAqB,EAAiB,EAAE;IAC3E,OAAO,MAAM,CAAC;AACf,CAAC,CAAC"}
{
"type": "module",
"name": "@atcute/lex-cli",
"version": "2.2.2",
"version": "2.3.0",
"description": "cli tool to generate type definitions for atcute",

@@ -16,3 +16,4 @@ "license": "0BSD",

"!src/**/*.test.ts",
"cli.mjs"
"cli.mjs",
"schema/"
],

@@ -25,15 +26,19 @@ "bin": "./cli.mjs",

"@badrap/valita": "^0.4.6",
"@externdefs/collider": "^0.3.0",
"@optique/core": "^0.6.1",
"@optique/run": "^0.6.1",
"picocolors": "^1.1.1",
"prettier": "^3.6.2",
"@atcute/lexicon-doc": "^1.1.2"
"valibot": "^1.0.0",
"@atcute/lexicon-doc": "^1.1.3"
},
"devDependencies": {
"@types/node": "^22.18.0",
"@valibot/to-json-schema": "^1.3.0",
"@atcute/lexicons": "^1.2.2"
},
"scripts": {
"build": "tsc",
"build": "pnpm run generate:schema && tsc",
"generate:schema": "node scripts/generate-schema.ts",
"prepublish": "rm -rf dist; pnpm run build"
}
}

@@ -26,8 +26,34 @@ # @atcute/lex-cli

highly recommend packaging the generated schemas as a publishable library for others to use.
## publishing your schemas
if you're packaging your generated schemas as a publishable library, add the `atcute:lexicons`
field to your package.json. this allows other projects to automatically discover and import your
schemas without manual configuration.
```json
{
"name": "@example/my-schemas",
"atcute:lexicons": {
"mappings": {
"com.example.*": {
"type": "namespace",
"path": "./types/{{nsid_remainder}}"
}
}
}
}
```
the `path` field supports several template expansions:
- `.` or `./` at the start is replaced with the package name (e.g., `./types/foo` becomes
`@example/my-schemas/types/foo`, or `.` becomes `@example/my-schemas`)
- `{{nsid}}` - the full NSID with dots replaced by slashes (e.g., `com/example/foo/bar`)
- `{{nsid_prefix}}` - the part before the wildcard (e.g., `com/example`)
- `{{nsid_remainder}}` - the part after the prefix (e.g., `foo/bar`)
## external references
when your lexicons reference types from namespaces outside your configured files, you'll need to
configure mappings to resolve these references.
configure how these references are resolved.

@@ -58,3 +84,4 @@ for example, if your lexicon references a type from another namespace:

define mappings in your configuration to specify how external namespaces should be imported:
the simplest way to resolve external references is using the `imports` array with packages that
provide the `atcute:lexicons` metadata:

@@ -68,2 +95,19 @@ ```ts

outdir: 'src/lexicons/',
imports: ['@atcute/atproto', '@atcute/bluesky'],
});
```
the CLI will automatically discover the namespace mappings from each package's `atcute:lexicons`
field in their package.json.
for packages without metadata, or when you need more fine-grained control over import resolution,
use the `mappings` configuration instead:
```ts
// file: lex.config.js
import { defineLexiconConfig } from '@atcute/lex-cli';
export default defineLexiconConfig({
files: ['lexicons/**/*.json'],
outdir: 'src/lexicons/',
mappings: [

@@ -87,4 +131,1 @@ {

```
with this configuration, any reference to a lexicon in the `com.atproto.*` or `app.bsky.*` namespace
will be imported from `@atcute/atproto` or `@atcute/bluesky`, respectively.
+170
-76

@@ -5,3 +5,6 @@ import * as fs from 'node:fs/promises';

import { Builtins, Command, Option, Program } from '@externdefs/collider';
import { object } from '@optique/core/constructs';
import { command, constant, option } from '@optique/core/primitives';
import { run } from '@optique/run';
import { path as pathParser } from '@optique/run/valueparser';
import pc from 'picocolors';

@@ -11,101 +14,192 @@

import { generateLexiconApi } from './codegen.js';
import { generateLexiconApi, type ImportMapping } from './codegen.js';
import type { LexiconConfig } from './index.js';
import { validatePackageJson } from './lexicon-metadata.js';
const program = new Program({ binaryName: 'lex-cli' });
/**
* Resolves package imports to ImportMapping[]
*/
const resolveImportsToMappings = async (
imports: string[],
configDirname: string,
): Promise<ImportMapping[]> => {
const mappings: ImportMapping[] = [];
program.register(Builtins.HelpCommand);
for (const packageName of imports) {
// Walk up from config directory to find package in node_modules
let packageJson: unknown;
let currentDir = configDirname;
let found = false;
program.register(
class GenerateCommand extends Command {
static override paths = [['generate']];
while (currentDir !== path.dirname(currentDir)) {
const candidatePath = path.join(currentDir, 'node_modules', packageName, 'package.json');
try {
const content = await fs.readFile(candidatePath, 'utf8');
packageJson = JSON.parse(content);
found = true;
break;
} catch (err: any) {
// Only continue to parent if file not found
if (err.code !== 'ENOENT') {
console.error(pc.bold(pc.red(`failed to read package.json for "${packageName}":`)));
console.error(err);
process.exit(1);
}
static override usage = Command.Usage({
description: `Generates TypeScript schema`,
});
// Not found, try parent directory
currentDir = path.dirname(currentDir);
}
}
config = Option.String(['-c', '--config'], {
required: true,
description: `Config file`,
});
if (!found) {
console.error(pc.bold(pc.red(`failed to resolve package "${packageName}"`)));
console.error(`Could not find package in node_modules starting from ${configDirname}`);
process.exit(1);
}
async execute(): Promise<number | void> {
const configFilename = path.resolve(this.config);
const configDirname = path.dirname(configFilename);
// Validate package.json
const result = validatePackageJson(packageJson);
if (!result.success) {
console.error(pc.bold(pc.red(`invalid atcute:lexicons in "${packageName}":`)));
console.error(result.issues);
process.exit(1);
}
let config: LexiconConfig;
try {
const configURL = url.pathToFileURL(configFilename);
const configMod = (await import(configURL.href)) as { default: LexiconConfig };
config = configMod.default;
} catch (err) {
console.error(pc.bold(pc.red(`failed to import config:`)));
console.error(err);
const lexicons = result.output['atcute:lexicons'];
if (!lexicons?.mappings) {
continue;
}
return 1;
}
// Convert mapping to ImportMapping[]
for (const [pattern, entry] of Object.entries(lexicons.mappings)) {
const isWildcard = pattern.endsWith('.*');
const documents: LexiconDoc[] = [];
mappings.push({
nsid: [pattern],
imports: (nsid: string) => {
// Check if pattern matches
if (isWildcard) {
if (!nsid.startsWith(pattern.slice(0, -1))) {
throw new Error(`NSID ${nsid} does not match pattern ${pattern}`);
}
} else {
if (nsid !== pattern) {
throw new Error(`NSID ${nsid} does not match pattern ${pattern}`);
}
}
for await (const filename of fs.glob(config.files, { cwd: configDirname })) {
let source: string;
try {
source = await fs.readFile(path.join(configDirname, filename), 'utf8');
} catch (err) {
console.error(pc.bold(pc.red(`file read error with "${filename}"`)));
console.error(err);
const nsidPrefix = isWildcard ? pattern.slice(0, -2) : pattern;
const nsidRemainder = isWildcard ? nsid.slice(nsidPrefix.length + 1) : '';
return 1;
}
let expandedPath = entry.path
.replaceAll('{{nsid}}', nsid.replaceAll('.', '/'))
.replaceAll('{{nsid_remainder}}', nsidRemainder.replaceAll('.', '/'))
.replaceAll('{{nsid_prefix}}', nsidPrefix.replaceAll('.', '/'));
let json: unknown;
try {
json = JSON.parse(source);
} catch (err) {
console.error(pc.bold(pc.red(`json parse error in "${filename}"`)));
console.error(err);
if (expandedPath === '.') {
expandedPath = packageName;
} else if (expandedPath.startsWith('./')) {
expandedPath = `${packageName}/${expandedPath.slice(2)}`;
}
return 1;
}
return {
type: entry.type,
from: expandedPath,
};
},
});
}
}
const result = lexiconDoc.try(json, { mode: 'strip' });
if (!result.ok) {
console.error(pc.bold(pc.red(`schema validation failed for "${filename}"`)));
console.error(result.message);
return mappings;
};
for (const issue of result.issues) {
console.log(`- ${issue.code} at .${issue.path.join('.')}`);
}
const parser = command(
'generate',
object({
type: constant('generate'),
config: option('-c', '--config', pathParser({ metavar: 'CONFIG' })),
}),
);
return 1;
}
const result = run(parser, { programName: 'lex-cli' });
documents.push(result.value);
}
if (result.type === 'generate') {
const configFilename = path.resolve(result.config);
const configDirname = path.dirname(configFilename);
const result = await generateLexiconApi({
documents: documents,
mappings: config.mappings ?? [],
modules: {
importSuffix: config.modules?.importSuffix ?? '.js',
},
prettier: {
cwd: process.cwd(),
},
});
let config: LexiconConfig;
try {
const configURL = url.pathToFileURL(configFilename);
const configMod = (await import(configURL.href)) as { default: LexiconConfig };
config = configMod.default;
} catch (err) {
console.error(pc.bold(pc.red(`failed to import config:`)));
console.error(err);
const outdir = path.join(configDirname, config.outdir);
process.exit(1);
}
for (const file of result.files) {
const filename = path.join(outdir, file.filename);
const dirname = path.dirname(filename);
// Resolve imports to mappings
const importMappings = config.imports ? await resolveImportsToMappings(config.imports, configDirname) : [];
const allMappings = [...importMappings, ...(config.mappings ?? [])];
await fs.mkdir(dirname, { recursive: true });
await fs.writeFile(filename, file.code);
const documents: LexiconDoc[] = [];
for await (const filename of fs.glob(config.files, { cwd: configDirname })) {
let source: string;
try {
source = await fs.readFile(path.join(configDirname, filename), 'utf8');
} catch (err) {
console.error(pc.bold(pc.red(`file read error with "${filename}"`)));
console.error(err);
process.exit(1);
}
let json: unknown;
try {
json = JSON.parse(source);
} catch (err) {
console.error(pc.bold(pc.red(`json parse error in "${filename}"`)));
console.error(err);
process.exit(1);
}
const result = lexiconDoc.try(json, { mode: 'strip' });
if (!result.ok) {
console.error(pc.bold(pc.red(`schema validation failed for "${filename}"`)));
console.error(result.message);
for (const issue of result.issues) {
console.log(`- ${issue.code} at .${issue.path.join('.')}`);
}
process.exit(1);
}
},
);
const exitCode = await program.run(process.argv.slice(2));
process.exitCode = exitCode;
documents.push(result.value);
}
const generationResult = await generateLexiconApi({
documents: documents,
mappings: allMappings,
modules: {
importSuffix: config.modules?.importSuffix ?? '.js',
},
prettier: {
cwd: process.cwd(),
},
});
const outdir = path.join(configDirname, config.outdir);
for (const file of generationResult.files) {
const filename = path.join(outdir, file.filename);
const dirname = path.dirname(filename);
await fs.mkdir(dirname, { recursive: true });
await fs.writeFile(filename, file.code);
}
}

@@ -6,2 +6,3 @@ import type { ImportMapping } from './codegen.js';

files: string[];
imports?: string[];
mappings?: ImportMapping[];

@@ -8,0 +9,0 @@ modules?: {