Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Improve UX: Support glob patterns in triple-slash comment #27

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions __tests__/fixtures/directive/dep7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <dependency-tree depends-on="./*.sh" />
Empty file.
Empty file.
1 change: 1 addition & 0 deletions __tests__/fixtures/directive/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

/// <dependency-tree depends-on="./dep4.ejs" />
/// <dependency-tree depends-on="./dir1/**/*" />
10 changes: 9 additions & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ describe('dependencyTree', () => {
]),
resolved: new Map([
[fixture(dir, 'dep1.ts'), new Set()],
[fixture(dir, 'dep7.ts'), new Set([fixture(dir, 'dep3.sh')])],
[
fixture(dir, 'index.ts'),
new Set([
Expand All @@ -468,7 +469,14 @@ describe('dependencyTree', () => {
fixture(dir, 'dep3.sh'),
]),
],
[fixture(dir, 'util.ts'), new Set([fixture(dir, 'dep4.ejs')])],
[
fixture(dir, 'util.ts'),
new Set([
fixture(dir, 'dep4.ejs'),
fixture(dir, 'dir1/dep5.py'),
fixture(dir, 'dir1/dir2/dep6.md'),
]),
],
]),
});
});
Expand Down
46 changes: 45 additions & 1 deletion docs/directive.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ The element can be closed at a new line:

Available attributes:

- **`depends-on="..."`** - contains a relative path to a dependency file.
- **`depends-on="..."`**
- contains a relative path to a dependency file.
- or a relative path with glob pattern.

Note that unavailable attributes will cause a validation error.

Expand All @@ -59,6 +61,14 @@ resolve(__dirname, './dep1.sh');
resolve(__dirname, './dep2.ts'); /// <dependency-tree depends-on="./dep2.js" />
```

directive comment can detect glob patterns:

```typescript
///<dependency-tree depends-on="./dir1/**/*" />
resolve(__dirname, './dir1/dep5.py');
resolve(__dirname, './dir1/dir2/dep6.md');
```

Element's definition can be split into multiple lines:

```typescript
Expand All @@ -81,5 +91,39 @@ The line below will be treated as a comment and not being parsed because of the
// <dependency-tree invalid directive declaration example />
```

#### Example

```bash
root_dir/
|- index.js
|- dep1.sh
|- util.ts
|- dir1/
|- dep5.py
|- dir2/
|- dep6.md
```

```typescript
// root_dir/util.ts

/// <dependency-tree depends-on="./dep1.sh" />
///<dependency-tree depends-on="./dir1/**/*" />
```

```typescript
// index.js
const dependencyTree = new DependencyTree(['root_dir']);
const result = await dependencyTree.gather();
const dependencies = result.resolved;
//{
// "root_dir/util.ts" => Set {
// "root_dir/dep1.sh",
// "root_dir/dir1/dep5.py",
// "root_dir/dir1/dir2/dep6.md",
// },
//}
```

Comment on lines +94 to +127
Copy link
Author

@YiranJing YiranJing Dec 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update the readme with a complete example. (make it easier for new users to understand)

[xml]: https://www.w3schools.com/xml/
[typescript]: https://www.typescriptlang.org/docs/home.html
19 changes: 17 additions & 2 deletions src/processors/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import * as camelcase from 'camelcase';
import escapeRegExp = require('lodash.escaperegexp');
import { OptionsV2, parseStringPromise as xml2js } from 'xml2js';
import { DependencyTree, FileToDeps, Path } from '../';
import { DependencyTree, FileToDeps, Path, ReferenceTransformFn } from '../';
import { FileProcessor } from '../file_processor';
import * as path from 'path';
import * as fg from 'fast-glob';

type Directive = {
dependsOn?: string;
Expand All @@ -19,6 +21,19 @@ type Options = {
fileTypes: string[];
};

/**
* Gets a list of files if the reference is a glob pattern. Otherwise, returns the original reference.
*/
export const transformReference: ReferenceTransformFn = (
ref: string,
source: string,
) => {
if (fg.isDynamicPattern(ref)) {
return fg.sync(ref, { cwd: path.dirname(source), absolute: true });
}
return ref;
};

/**
* An abstract DirectiveProcessor class that implements language-agnostic generic logic of
* directives' parsing. For more info see: tools/dependency-tree/docs/directive.md.
Expand Down Expand Up @@ -165,7 +180,7 @@ abstract class DirectiveProcessor implements FileProcessor {
dependsOn &&
dependencyTree.resolveAndCollect(
file,
dependencyTree.transformReference(dependsOn, file),
transformReference(dependsOn, file),
Comment on lines -168 to +183
Copy link
Author

@YiranJing YiranJing Dec 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will block user provide their own custom transformReference function.

🤔 but I don't think users need an extra transformer for the depend-on=xxx scenario.

importedFiles,
missing,
);
Expand Down