@pnpm/resolving.jsr-specifier-parser
Version:
Parser of jsr specifiers
53 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseJsrSpecifier = parseJsrSpecifier;
const error_1 = require("@pnpm/error");
function parseJsrSpecifier(rawSpecifier, alias) {
if (!rawSpecifier.startsWith('jsr:'))
return null;
rawSpecifier = rawSpecifier.substring('jsr:'.length);
// syntax: jsr:@<scope>/<name>[@<version_selector>]
if (rawSpecifier.startsWith('@')) {
const index = rawSpecifier.lastIndexOf('@');
// syntax: jsr:@<scope>/<name>
if (index === 0) {
return {
jsrPkgName: rawSpecifier,
npmPkgName: jsrToNpmPackageName(rawSpecifier),
};
}
// syntax: jsr:@<scope>/<name>@<version_selector>
const jsrPkgName = rawSpecifier.substring(0, index);
return {
jsrPkgName,
npmPkgName: jsrToNpmPackageName(jsrPkgName),
versionSelector: rawSpecifier.substring(index + '@'.length),
};
}
// syntax: jsr:<name>@<version_selector> (invalid)
if (rawSpecifier.includes('@')) {
throw new error_1.PnpmError('MISSING_JSR_PACKAGE_SCOPE', 'Package names from JSR must have a scope');
}
if (!alias) {
throw new error_1.PnpmError('INVALID_JSR_SPECIFIER', `JSR specifier '${rawSpecifier}' is missing a package name`);
}
// syntax: jsr:<spec>
return {
versionSelector: rawSpecifier,
jsrPkgName: alias,
npmPkgName: jsrToNpmPackageName(alias),
};
}
function jsrToNpmPackageName(jsrPkgName) {
if (!jsrPkgName.startsWith('@')) {
throw new error_1.PnpmError('MISSING_JSR_PACKAGE_SCOPE', 'Package names from JSR must have a scope');
}
const sepIndex = jsrPkgName.indexOf('/');
if (sepIndex === -1) {
throw new error_1.PnpmError('INVALID_JSR_PACKAGE_NAME', `The package name '${jsrPkgName}' is invalid`);
}
const scope = jsrPkgName.substring(0, sepIndex);
const name = jsrPkgName.substring(sepIndex + '/'.length);
return `@jsr/${scope.substring(1)}__${name}`;
}
//# sourceMappingURL=index.js.map