markdown-code-example-inserter
Version:
Syncs code examples with markdown documentation.
38 lines (37 loc) • 1.39 kB
JavaScript
import { join } from 'node:path';
import { readPackageDetails } from './parse-package-json.js';
import { getTsDirs } from './parse-tsconfig.js';
export async function guessPackageIndex(packageDir,
/** For testing purposes. */
overrideTsConfig,
/** For testing purposes. */
overridePackageJson) {
const tsConfigDirs = getTsDirs(packageDir, overrideTsConfig);
const packageDetails = await readPackageDetails(packageDir, overridePackageJson);
let indexPath = packageDetails.mainPath
? packageDetails.mainPath.replace(packageDir, '')
: undefined;
if (tsConfigDirs) {
if (tsConfigDirs.output && indexPath != undefined) {
indexPath = indexPath.replace(tsConfigDirs.output.replace(packageDir, ''), '');
}
if (tsConfigDirs.source) {
if (indexPath == undefined) {
indexPath = tsConfigDirs.source;
}
else {
indexPath = join(tsConfigDirs.source, indexPath.replace(tsConfigDirs.source.replace(packageDir, ''), ''));
}
}
if (indexPath) {
indexPath = indexPath.replace(/\.js$/, '.ts');
}
}
if (indexPath && !indexPath.includes(packageDir)) {
indexPath = join(packageDir, indexPath);
}
return {
replaceName: packageDetails.packageName,
indexPath: indexPath || packageDir,
};
}