Skip to content

Commit

Permalink
Child directory support (#16)
Browse files Browse the repository at this point in the history
* Adding base-url and add-path feature support from child directories

* Adding child directory support for cleanup

* Fixing a bug for cleanup

* Updating README
  • Loading branch information
lloydaf authored Aug 23, 2020
1 parent 3ad15ba commit 1da41aa
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 60 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![npm bundle size](https://img.shields.io/bundlephobia/min/unrelate)
![npm](https://img.shields.io/npm/v/unrelate)

This library is used to transform relative paths into absolute paths for [TypeScript](https://www.typescriptlang.org) projects. **You should have a [tsconfig.json](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) file present in the directory where you run this tool.**
This library is used to transform relative paths into absolute paths for [TypeScript](https://www.typescriptlang.org) projects.

## Usage

Expand Down Expand Up @@ -48,7 +48,7 @@ npx unrelate <command>

## Base URL

Before you start using `unrelate`, you have to configure the [baseUrl](https://www.typescriptlang.org/docs/handbook/module-resolution.html#base-url) property in your `tsconfig.json` file. It informs the compiler where to find modules. All absolute import paths you configure using `unrelate`, are always relative to the `baseUrl`. So set the property to the folder that contains, or contains subfolders that contains all the `.ts` files that would use the absolute imports. It's common to set `baseUrl` to the project root folder or the `src` or `lib` folders, depending on where most of your code lies.
Before you start using `unrelate`, you have to configure the [baseUrl](https://www.typescriptlang.org/docs/handbook/module-resolution.html#base-url) property in your [tsconfig.json](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) file. It informs the compiler where to find modules. All absolute import paths you configure using `unrelate`, are always relative to the `baseUrl`. So set the property to the folder that contains, or contains subfolders that contains all the `.ts` files that would use the absolute imports. It's common to set `baseUrl` to the project root folder or the `src` or `lib` folders, depending on where most of your code lies.

### Configuration

Expand Down
192 changes: 169 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"author": "lloydaf",
"license": "MIT",
"dependencies": {
"comment-json": "^3.0.3"
"comment-json": "^3.0.3",
"find-up": "^5.0.0"
},
"devDependencies": {
"@types/comment-json": "^1.1.1",
Expand Down
8 changes: 6 additions & 2 deletions src/lib/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
doesItExist,
fileOrFolder,
getDirectoryItems,
getAbsolutePathRelativeToBaseUrl,
} from '../util/file.util';
import { CommentJSONValue } from 'comment-json';
import { dirname } from 'path';
Expand Down Expand Up @@ -37,17 +38,19 @@ export async function cleanup(paramPath: string): Promise<void> {
const configManager = configFileDataManager();
const configFile: CommentJSONValue = (await configManager.next()).value;
const configuredPathsObj: Record<string, string[]> = configFile?.compilerOptions?.paths;
const baseUrl: string = configFile?.compilerOptions?.baseUrl;
let baseUrl: string = configFile?.compilerOptions?.baseUrl;

if (!configuredPathsObj || !baseUrl) {
throw new Error('You need to configure base-url and paths first');
}

!baseUrl.endsWith('/') && (baseUrl = `${baseUrl}/`);

// the configured paths
const configuredPaths: Record<string, string> = Object.entries(configuredPathsObj).reduce(
(acc: Record<string, string>, [key, val]: [string, string[]]) => ({
...acc,
[getFilePath(baseUrl + val[0])]: removeTrailingCharacter(key, '*'),
[getAbsolutePathRelativeToBaseUrl(baseUrl + val[0])]: removeTrailingCharacter(key, '*'),
}),
{},
);
Expand All @@ -58,6 +61,7 @@ export async function cleanup(paramPath: string): Promise<void> {
const relativePathsInFile: string[] = file.match(RegExp(`(?<=['"]{1})(\\./)*(\\.\\./)+.*`, 'g')) || [];
const absolutePathsInFile =
relativePathsInFile.map((path: string) => getFilePath(`${dirname(paramPath)}/${path}`)) || [];

Object.entries(configuredPaths).forEach(([key, value]) => {
absolutePathsInFile.forEach((path, index) => {
if (path.includes(key)) {
Expand Down
Loading

0 comments on commit 1da41aa

Please sign in to comment.