@ts-bridge/cli
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -10,2 +10,8 @@ # Changelog | ||
## [0.1.4] | ||
### Fixed | ||
- Check parent paths for `package.json` file when checking if a module is ESM ([#16](https://github.com/ts-bridge/ts-bridge/pull/16)) | ||
## [0.1.3] | ||
@@ -48,3 +54,4 @@ | ||
[Unreleased]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.1.3...HEAD | ||
[Unreleased]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.1.4...HEAD | ||
[0.1.4]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.1.3...@ts-bridge/cli@0.1.4 | ||
[0.1.3]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.1.2...@ts-bridge/cli@0.1.3 | ||
@@ -51,0 +58,0 @@ [0.1.2]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.1.1...@ts-bridge/cli@0.1.2 |
@@ -11,2 +11,14 @@ import type { System } from 'typescript'; | ||
/** | ||
* Get the paths of the parent directories of a package, which can contain a | ||
* `package.json` file. | ||
* | ||
* @param packageName - The name of the package. | ||
* @param baseDirectory - The directory to start resolving from. | ||
* @param paths - The paths of the parent directories. This is used for | ||
* recursion. | ||
* @param basePackageName - The base package name. This is used for recursion. | ||
* @returns The paths of the parent directories of the package. | ||
*/ | ||
export declare function getPackageParentPaths(packageName: string, baseDirectory: string, paths?: string[], basePackageName?: string): string[]; | ||
/** | ||
* Get the `package.json` file for a module. | ||
@@ -13,0 +25,0 @@ * |
import { isBuiltin, createRequire } from 'module'; | ||
import { dirname, join } from 'path'; | ||
import { dirname, join, sep } from 'path'; | ||
import { exports as resolveExports, legacy as resolveLegacy, } from 'resolve.exports'; | ||
@@ -35,2 +35,24 @@ import { readJsonFile } from './file-system.js'; | ||
/** | ||
* Get the paths of the parent directories of a package, which can contain a | ||
* `package.json` file. | ||
* | ||
* @param packageName - The name of the package. | ||
* @param baseDirectory - The directory to start resolving from. | ||
* @param paths - The paths of the parent directories. This is used for | ||
* recursion. | ||
* @param basePackageName - The base package name. This is used for recursion. | ||
* @returns The paths of the parent directories of the package. | ||
*/ | ||
export function getPackageParentPaths(packageName, baseDirectory, paths = [], basePackageName = getPackageName(packageName)) { | ||
const parentPath = packageName.split(sep).slice(0, -1).join(sep); | ||
if (parentPath.length <= basePackageName.length) { | ||
return paths; | ||
} | ||
const path = join(baseDirectory, parentPath); | ||
return getPackageParentPaths(parentPath, baseDirectory, | ||
// Note: The order of paths here is important, since we want to check the | ||
// "deepest" directory first. | ||
[...paths, path], basePackageName); | ||
} | ||
/** | ||
* Get the `package.json` file for a module. | ||
@@ -50,4 +72,8 @@ * | ||
} | ||
for (const path of paths) { | ||
const packagePath = join(path, packageName, 'package.json'); | ||
const pathsWithPackage = paths.flatMap((path) => [ | ||
...getPackageParentPaths(packageName, path), | ||
join(path, packageName), | ||
]); | ||
for (const path of pathsWithPackage) { | ||
const packagePath = join(path, 'package.json'); | ||
if (system.fileExists(packagePath)) { | ||
@@ -54,0 +80,0 @@ return readJsonFile(packagePath, system); |
@@ -6,3 +6,3 @@ import { getMockNodeModule, getMockPackageJson, getVirtualEnvironment, } from '@ts-bridge/test-utils'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { getPackageEntryPoint, getPackageJson, getPackageName, getPackagePath, isESModule, } from './module-resolver.js'; | ||
import { getPackageEntryPoint, getPackageJson, getPackageName, getPackageParentPaths, getPackagePath, isESModule, } from './module-resolver.js'; | ||
const { sys } = typescript; | ||
@@ -27,2 +27,16 @@ const BASE_DIRECTORY = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..', '..'); | ||
}); | ||
describe('getPackageParentPaths', () => { | ||
it('returns the paths of the parent directories of a package', () => { | ||
const paths = getPackageParentPaths('typescript/foo/bar/baz', BASE_DIRECTORY); | ||
expect(paths[0]).toBe(resolve(BASE_DIRECTORY, 'typescript/foo/bar')); | ||
expect(paths[1]).toBe(resolve(BASE_DIRECTORY, 'typescript/foo')); | ||
expect(paths).not.toContain(resolve(BASE_DIRECTORY, 'typescript')); | ||
}); | ||
it('returns the paths of the parent directories of a scoped package', () => { | ||
const paths = getPackageParentPaths('@babel/core/foo/bar/baz', BASE_DIRECTORY); | ||
expect(paths[0]).toBe(resolve(BASE_DIRECTORY, '@babel/core/foo/bar')); | ||
expect(paths[1]).toBe(resolve(BASE_DIRECTORY, '@babel/core/foo')); | ||
expect(paths).not.toContain(resolve(BASE_DIRECTORY, '@babel/core')); | ||
}); | ||
}); | ||
describe('getPackageJson', () => { | ||
@@ -29,0 +43,0 @@ it('returns the package.json file for a given module', () => { |
{ | ||
"name": "@ts-bridge/cli", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Bridge the gap between ES modules and CommonJS modules with an easy-to-use alternative to `tsc`.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
231104
340
5632
2