Skip to content

Commit

Permalink
feat: ignore entries outside of dist directory
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Aug 3, 2022
1 parent ecec8e9 commit 15459f5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/cli.ts
Expand Up @@ -74,6 +74,7 @@ const argv = cli({
});

const cwd = process.cwd();
const { stringify } = JSON;

/**
* The sourcepath may be a symlink.
Expand All @@ -90,7 +91,18 @@ if (tsconfigTarget) {

(async () => {
const packageJson = await readPackageJson(cwd);
const exportEntries = getExportEntries(packageJson);

let exportEntries = getExportEntries(packageJson);

exportEntries = exportEntries.filter((entry) => {
const validPath = entry.outputPath.startsWith(distPath);

if (!validPath) {
console.warn(`Ignoring entry outside of ${distPath} directory: package.json#${entry.from}=${stringify(entry.outputPath)}`);
}

return validPath;
});

if (exportEntries.length === 0) {
throw new Error('No export entries found in package.json');
Expand Down
6 changes: 1 addition & 5 deletions src/utils/get-source-path.ts
Expand Up @@ -27,10 +27,6 @@ export async function getSourcePath(
source: string,
dist: string,
) {
if (!exportEntry.outputPath.startsWith(dist)) {
throw new Error(`Export path ${stringify(exportEntry.outputPath)} from ${stringify(`package.json#${exportEntry.from}`)} is not in directory ${dist}`);
}

const sourcePath = source + exportEntry.outputPath.slice(dist.length);

for (const extension of (Object.keys(sourceExtensions) as (keyof typeof sourceExtensions)[])) {
Expand All @@ -46,5 +42,5 @@ export async function getSourcePath(
}
}

throw new Error(`Could not find mathing source file for export path ${stringify(exportEntry.outputPath)}`);
throw new Error(`Could not find matching source file for export path ${stringify(exportEntry.outputPath)}`);
}
5 changes: 3 additions & 2 deletions tests/specs/error-cases.ts
Expand Up @@ -82,7 +82,7 @@ export default testSuite(({ describe }, nodePath: string) => {
await fixture.rm();
});

test('absolute path entry in package.json', async () => {
test('ignore and warn on path entry outside of dist directory', async () => {
const fixture = await createFixture('./tests/fixture-package');

await fixture.writeJson('package.json', {
Expand All @@ -99,7 +99,8 @@ export default testSuite(({ describe }, nodePath: string) => {
).catch(error => error);

expect(pkgrollProcess.exitCode).toBe(1);
expect(pkgrollProcess.stderr).toMatch('is not in directory ./dist');
expect(pkgrollProcess.stderr).toMatch('Ignoring entry outside of ./dist/ directory: package.json#main="/dist/main.js"');
expect(pkgrollProcess.stderr).toMatch('No export entries found in package.json');

await fixture.rm();
});
Expand Down

0 comments on commit 15459f5

Please sign in to comment.