|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import fs from 'node:fs' |
|
import path from 'node:path' |
|
import {fileURLToPath} from 'node:url' |
|
import {codes} from './errors.js' |
|
|
|
const hasOwnProperty = {}.hasOwnProperty |
|
|
|
const {ERR_INVALID_PACKAGE_CONFIG} = codes |
|
|
|
|
|
const cache = new Map() |
|
|
|
|
|
|
|
|
|
|
|
|
|
export function read(jsonPath, {base, specifier}) { |
|
const existing = cache.get(jsonPath) |
|
|
|
if (existing) { |
|
return existing |
|
} |
|
|
|
|
|
let string |
|
|
|
try { |
|
string = fs.readFileSync(path.toNamespacedPath(jsonPath), 'utf8') |
|
} catch (error) { |
|
const exception = (error) |
|
|
|
if (exception.code !== 'ENOENT') { |
|
throw exception |
|
} |
|
} |
|
|
|
|
|
const result = { |
|
exists: false, |
|
pjsonPath: jsonPath, |
|
main: undefined, |
|
name: undefined, |
|
type: 'none', |
|
exports: undefined, |
|
imports: undefined |
|
} |
|
|
|
if (string !== undefined) { |
|
|
|
let parsed |
|
|
|
try { |
|
parsed = JSON.parse(string) |
|
} catch (error_) { |
|
const cause = (error_) |
|
const error = new ERR_INVALID_PACKAGE_CONFIG( |
|
jsonPath, |
|
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier), |
|
cause.message |
|
) |
|
error.cause = cause |
|
throw error |
|
} |
|
|
|
result.exists = true |
|
|
|
if ( |
|
hasOwnProperty.call(parsed, 'name') && |
|
typeof parsed.name === 'string' |
|
) { |
|
result.name = parsed.name |
|
} |
|
|
|
if ( |
|
hasOwnProperty.call(parsed, 'main') && |
|
typeof parsed.main === 'string' |
|
) { |
|
result.main = parsed.main |
|
} |
|
|
|
if (hasOwnProperty.call(parsed, 'exports')) { |
|
|
|
result.exports = parsed.exports |
|
} |
|
|
|
if (hasOwnProperty.call(parsed, 'imports')) { |
|
|
|
result.imports = parsed.imports |
|
} |
|
|
|
|
|
if ( |
|
hasOwnProperty.call(parsed, 'type') && |
|
(parsed.type === 'commonjs' || parsed.type === 'module') |
|
) { |
|
result.type = parsed.type |
|
} |
|
} |
|
|
|
cache.set(jsonPath, result) |
|
|
|
return result |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function getPackageScopeConfig(resolved) { |
|
|
|
let packageJSONUrl = new URL('package.json', resolved) |
|
|
|
while (true) { |
|
const packageJSONPath = packageJSONUrl.pathname |
|
if (packageJSONPath.endsWith('node_modules/package.json')) { |
|
break |
|
} |
|
|
|
const packageConfig = read(fileURLToPath(packageJSONUrl), { |
|
specifier: resolved |
|
}) |
|
|
|
if (packageConfig.exists) { |
|
return packageConfig |
|
} |
|
|
|
const lastPackageJSONUrl = packageJSONUrl |
|
packageJSONUrl = new URL('../package.json', packageJSONUrl) |
|
|
|
|
|
|
|
if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) { |
|
break |
|
} |
|
} |
|
|
|
const packageJSONPath = fileURLToPath(packageJSONUrl) |
|
|
|
|
|
return { |
|
pjsonPath: packageJSONPath, |
|
exists: false, |
|
type: 'none' |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getPackageType(url) { |
|
|
|
return getPackageScopeConfig(url).type |
|
} |
|
|