{"version":3,"file":"filesystem.cjs","names":["Path","pc","FS"],"sources":["../src/filesystem/flatten-package-export-paths.ts","../src/filesystem/verify-package-export-paths.ts"],"sourcesContent":["import type { PackageJson } from 'type-fest';\n\n/**\n * Aggregate all export paths from a given package.json file.\n * Useful for building a list of files to check for invalid export paths.\n * @param packageJson\n */\nexport function flattenPackageExportPaths(packageJson: PackageJson): ResolvedExportPaths {\n    const result: ResolvedExportPaths = {\n        main: null,\n        types: null,\n        exports: null,\n        all: [],\n        warnings: [],\n    }\n    \n    if (packageJson.exports) {\n        result.exports = unwrapExports(packageJson.exports);\n        result.all.push(...result.exports);\n    }\n    \n    if (packageJson.types) {\n        result.types = packageJson.types;\n        result.all.push(result.types);\n    }\n    \n    if (packageJson.main) {\n        result.main = packageJson.main;\n        result.all.push(result.main);\n    }\n    \n    result.exports?.forEach(path => {\n        if (!path.startsWith('./')) {\n            result.warnings.push(`Invalid export path: ${path}`);\n        }\n    })\n    \n    return result;\n}\n\nfunction unwrapExports(field: PackageJson['exports']): string[] {\n    if (typeof field === 'string') {\n        return [field];\n    }\n    if (Array.isArray(field)) {\n        return field.map(unwrapExports).flat();\n    }\n    if (!field) {\n        return [];\n    }\n    return Object.values(field).map(unwrapExports).flat();\n}\n\ntype ResolvedExportPaths = {\n    types: string | null;\n    main: string | null;\n    exports: string[] | null;\n    all: string[];\n    warnings: string[];\n}","import FS from 'node:fs';\nimport Path from 'node:path';\nimport pc from 'picocolors';\nimport type { PackageJson } from 'type-fest';\nimport { flattenPackageExportPaths } from './flatten-package-export-paths.ts';\n\n/**\n * Verify that the exports in the provided package.json file exist and are valid.\n */\nexport async function verifyPackageExportPaths(config: VerifyOptions) {\n    const packageJson: PackageJson = await import(config.packageJsonPath, { with: { type: 'json' } }).then((result) => result.default ?? result);\n    const packageDir = Path.dirname(config.packageJsonPath);\n    \n    const exports = flattenPackageExportPaths(packageJson);\n    \n    if (exports.all.length === 0) {\n        throw new Error(`No export paths found`);\n    }\n    \n    for (const warning of exports.warnings) {\n        console.warn(pc.yellow(warning));\n    }\n    \n    const missingFiles: string[] = [];\n    \n    for (const path of exports.all) {\n        if (pathExists(Path.resolve(packageDir, path))) {\n            continue;\n        }\n        \n        console.warn(pc.yellow(`Export path does not exist: ${pc.underline(path)}`));\n        missingFiles.push(path);\n    }\n    \n    if (missingFiles.length !== 0) {\n        throw new Error(`Missing ${missingFiles.length} exported files`);\n    }\n    \n    if (!packageJson.files && packageJson.fileWhitelist !== false && config.requireFilesWhitelist !== false) {\n        throw new Error(`No files listed in package.json \"files\" field`);\n    }\n    \n    console.info(`✅  All ${pc.underline(exports.all.length)} export paths are accessible`);\n    \n    return exports;\n}\n\nfunction pathExists(path: string) {\n    // Check whether path is a glob pattern\n    const { globExt } = path.match(/\\*(?<globExt>[\\w.]+)$/)?.groups || { globExt: '' };\n    \n    if (!globExt) {\n        return FS.existsSync(path);\n    }\n    \n    const dirname = Path.dirname(path);\n    for (const file of FS.readdirSync(dirname)) {\n        if (file.endsWith(globExt)) {\n            return true;\n        }\n    }\n    \n    return false;\n}\n\ninterface VerifyOptions {\n    packageJsonPath: string;\n    requireFilesWhitelist?: boolean;\n}"],"mappings":";;;;;;;;;;;;;;AAOA,SAAgB,0BAA0B,aAA+C;CACrF,MAAM,SAA8B;EAChC,MAAM;EACN,OAAO;EACP,SAAS;EACT,KAAK,EAAE;EACP,UAAU,EAAE;EACf;AAED,KAAI,YAAY,SAAS;AACrB,SAAO,UAAU,cAAc,YAAY,QAAQ;AACnD,SAAO,IAAI,KAAK,GAAG,OAAO,QAAQ;;AAGtC,KAAI,YAAY,OAAO;AACnB,SAAO,QAAQ,YAAY;AAC3B,SAAO,IAAI,KAAK,OAAO,MAAM;;AAGjC,KAAI,YAAY,MAAM;AAClB,SAAO,OAAO,YAAY;AAC1B,SAAO,IAAI,KAAK,OAAO,KAAK;;AAGhC,QAAO,SAAS,SAAQ,SAAQ;AAC5B,MAAI,CAAC,KAAK,WAAW,KAAK,CACtB,QAAO,SAAS,KA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