Skip to content

Commit

Permalink
feat: synchronous API (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed May 12, 2024
1 parent a60c13c commit 4dc1118
Show file tree
Hide file tree
Showing 9 changed files with 1,299 additions and 1,223 deletions.
48 changes: 35 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _fs from 'fs';
import path from 'path';
import type { PackageJson } from 'type-fest';
import { getAllFiles } from './utils/get-all-files.js';
import { getAllFiles, getAllFilesSync } from './utils/get-all-files.js';
import { createPathMatcher, pathMatches, type PathMatcher } from './utils/path-matcher.js';
import { STAR } from './utils/constants.js';

Expand Down Expand Up @@ -197,18 +197,10 @@ const legacyCondition = (
filePath: string,
) => [[['default'], filePath] as ConditionToPath];

export const getPackageEntryPoints = async (
packagePath: string,
fs = _fs.promises,
): Promise<PackageEntryPoints> => {
const packageJsonString = await fs.readFile(path.join(packagePath, 'package.json'), 'utf8');
const packageJson = JSON.parse(packageJsonString) as PackageJson;
const packageFiles = await getAllFiles(fs, packagePath);

if (packageJson.exports !== undefined) {
return analyzeExportsWithFiles(packageJson.exports, packageFiles);
}

const analyzeLegacyExports = (
packageJson: PackageJson,
packageFiles: string[],
): PackageEntryPoints => {
const jsExtension = /\.(?:json|[cm]?js|d\.ts)$/;
const legacyExports = Object.fromEntries(
packageFiles
Expand All @@ -235,3 +227,33 @@ export const getPackageEntryPoints = async (

return legacyExports;
};

export const getPackageEntryPoints = async (
packagePath: string,
fs = _fs.promises,
): Promise<PackageEntryPoints> => {
const packageJsonString = await fs.readFile(path.join(packagePath, 'package.json'), 'utf8');
const packageJson = JSON.parse(packageJsonString) as PackageJson;
const packageFiles = await getAllFiles(fs, packagePath);

if (packageJson.exports !== undefined) {
return analyzeExportsWithFiles(packageJson.exports, packageFiles);
}

return analyzeLegacyExports(packageJson, packageFiles);
};

export const getPackageEntryPointsSync = (
packagePath: string,
fs = _fs,
): PackageEntryPoints => {
const packageJsonString = fs.readFileSync(path.join(packagePath, 'package.json'), 'utf8');
const packageJson = JSON.parse(packageJsonString) as PackageJson;
const packageFiles = getAllFilesSync(fs, packagePath);

if (packageJson.exports !== undefined) {
return analyzeExportsWithFiles(packageJson.exports, packageFiles);
}

return analyzeLegacyExports(packageJson, packageFiles);
};
29 changes: 29 additions & 0 deletions src/utils/get-all-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,32 @@ export const getAllFiles = async (

return fileTree.flat();
};

export const getAllFilesSync = (
fs: Pick<typeof _fs, 'readdirSync' | 'statSync'>,
directoryPath: string,
dontShortenPath?: boolean,
): string[] => {
const directoryFiles = fs.readdirSync(directoryPath);
const fileTree = directoryFiles.map((fileName) => {
const filePath = path.join(directoryPath, fileName);
const stat = fs.statSync(filePath);

if (stat.isDirectory()) {
const files = getAllFilesSync(fs, filePath, true);
return (
dontShortenPath
? files
: files.map(file => `./${path.relative(directoryPath, file)}`)
);
}

return (
dontShortenPath
? filePath
: `./${path.relative(directoryPath, filePath)}`
);
});

return fileTree.flat();
};
Loading

0 comments on commit 4dc1118

Please sign in to comment.