Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve package with tsconfig.json #9

Merged
merged 3 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/get-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { findConfigFile } from './utils/find-config-file';
import { findUp } from './utils/find-up';
import { readTsconfig } from './utils/read-tsconfig';
import type { TsConfigResult } from './types';

export function getTsconfig(
searchPath = process.cwd(),
configName = 'tsconfig.json',
): TsConfigResult | null {
const configFile = findConfigFile(searchPath, configName);
const configFile = findUp(searchPath, configName);

if (!configFile) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/find-config-file.ts → src/utils/find-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import path from 'path';
import fs from 'fs';
import slash from 'slash';

export function findConfigFile(
export function findUp(
searchPath: string,
configName: string,
fileName: string,
) {
while (true) {
const configPath = path.join(searchPath, configName);
const configPath = path.join(searchPath, fileName);
if (fs.existsSync(configPath)) {
return slash(configPath);
}
Expand Down
19 changes: 5 additions & 14 deletions src/utils/read-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { parse } from 'jsonc-parser';
import slash from 'slash';
import type { TsConfigJson, TsConfigJsonResolved } from '../types';
import { normalizePath } from './normalize-path';
import { resolveExtends } from './resolve-extends';

export function readTsconfig(
filePath: string,
Expand All @@ -23,20 +24,10 @@ export function readTsconfig(
}

if (config.extends) {
let extendsPath = config.extends;

try {
extendsPath = require.resolve(extendsPath, { paths: [path.dirname(filePath)] });
} catch (error) {
if ((error as any).code === 'MODULE_NOT_FOUND') {
try {
extendsPath = require.resolve(
path.join(extendsPath, 'tsconfig.json'),
{ paths: [path.dirname(filePath)] },
);
} catch {}
}
}
const extendsPath = resolveExtends(
config.extends,
directoryPath,
);

const extendsConfig = readTsconfig(extendsPath);

Expand Down
71 changes: 71 additions & 0 deletions src/utils/resolve-extends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import path from 'path';
import fs from 'fs';
import { findUp } from './find-up';

const pathExists = (filePath: string) => fs.existsSync(filePath);

const safeJsonParse = (jsonString: string) => {
try {
return JSON.parse(jsonString);
} catch {}
};

export function resolveExtends(
filePath: string,
directoryPath: string,
) {
if (filePath === '..') {
filePath += '/tsconfig.json';
}

// Relative path
if (filePath.startsWith('.')) {
let tsconfigPath = path.resolve(directoryPath, filePath);

if (
pathExists(tsconfigPath)
&& fs.statSync(tsconfigPath).isFile()
) {
return tsconfigPath;
}

if (!tsconfigPath.endsWith('.json')) {
tsconfigPath += '.json';

if (pathExists(tsconfigPath)) {
return tsconfigPath;
}
}
} else {
let currentPath = findUp(
directoryPath,
path.join('node_modules', filePath),
);

if (currentPath) {
if (fs.statSync(currentPath).isDirectory()) {
const packageJsonpath = path.join(currentPath, 'package.json');

if (pathExists(packageJsonpath)) {
const packageJson = safeJsonParse(fs.readFileSync(packageJsonpath, 'utf8'));

if (packageJson && 'tsconfig' in packageJson) {
currentPath = path.join(currentPath, packageJson.tsconfig);
} else {
currentPath = path.join(currentPath, 'tsconfig.json');
}
} else {
currentPath = path.join(currentPath, 'tsconfig.json');
}

if (pathExists(currentPath)) {
return currentPath;
}
} else {
return currentPath;
}
}
}

throw new Error(`File '${filePath}' not found.`);
}
6 changes: 3 additions & 3 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe } from 'manten';

describe('get-tsconfig', ({ runTestSuite }) => {
runTestSuite(import('./specs/errors.spec'));
runTestSuite(import('./specs/finds-config.spec'));
// runTestSuite(import('./specs/errors.spec'));
// runTestSuite(import('./specs/finds-config.spec'));
runTestSuite(import('./specs/extends.spec'));
runTestSuite(import('./specs/paths.spec'));
// runTestSuite(import('./specs/paths.spec'));
});
Loading