Skip to content

Commit 21379fc

Browse files
committed
feat(option): tsconfig を相対パスで指定可能とする
1 parent b664992 commit 21379fc

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

integration.spec.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ beforeAll(() => {
1111
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
1212
});
1313

14+
// TODO 現状、--config-file で指定するパスは -d からの相対パスだが、将来的には -d をなくしたい。その移行のため --config-file での指定は、-d を無視したカレントディレクトリからの相対パスで設定ファイルを優先し、なければ -d からの相対パスで探すようにしたい。
15+
1416
test('run:sample', async () => {
15-
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --md ${filepath}`;
17+
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --md ${filepath}`;
1618

1719
const file = fs.readFileSync(filepath, { encoding: 'utf-8' });
1820
expect(file).toMatchInlineSnapshot(`
1921
"# TypeScript Graph
2022
2123
\`\`\`bash
22-
tsg -d ./dummy_project --md __tmp__/test.md
24+
tsg --tsconfig ./dummy_project/tsconfig.json --md __tmp__/test.md
2325
\`\`\`
2426
2527
\`\`\`mermaid
@@ -115,14 +117,14 @@ test('run:sample', async () => {
115117
});
116118

117119
test('run:sample:argument-include', async () => {
118-
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts includeFiles config -d './dummy_project' --md ${filepath}`;
120+
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts includeFiles config --tsconfig './dummy_project/tsconfig.json' --md ${filepath}`;
119121

120122
const file = fs.readFileSync(filepath, { encoding: 'utf-8' });
121123
expect(file).toMatchInlineSnapshot(`
122124
"# TypeScript Graph
123125
124126
\`\`\`bash
125-
tsg includeFiles config -d ./dummy_project --md __tmp__/test.md
127+
tsg includeFiles config --tsconfig ./dummy_project/tsconfig.json --md __tmp__/test.md
126128
\`\`\`
127129
128130
\`\`\`mermaid
@@ -203,14 +205,14 @@ test('run:sample:argument-include', async () => {
203205
});
204206

205207
test('run:sample:include', async () => {
206-
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --include includeFiles config --md ${filepath}`;
208+
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --include includeFiles config --md ${filepath}`;
207209

208210
const file = fs.readFileSync(filepath, { encoding: 'utf-8' });
209211
expect(file).toMatchInlineSnapshot(`
210212
"# TypeScript Graph
211213
212214
\`\`\`bash
213-
tsg -d ./dummy_project --include includeFiles config --md __tmp__/test.md
215+
tsg --tsconfig ./dummy_project/tsconfig.json --include includeFiles config --md __tmp__/test.md
214216
\`\`\`
215217
216218
\`\`\`mermaid
@@ -291,14 +293,14 @@ test('run:sample:include', async () => {
291293
});
292294

293295
test('run:sample:exclude', async () => {
294-
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --include includeFiles config --exclude excludeFiles utils --md ${filepath}`;
296+
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --include includeFiles config --exclude excludeFiles utils --md ${filepath}`;
295297

296298
const file = fs.readFileSync(filepath, { encoding: 'utf-8' });
297299
expect(file).toMatchInlineSnapshot(`
298300
"# TypeScript Graph
299301
300302
\`\`\`bash
301-
tsg -d ./dummy_project --include includeFiles config --exclude excludeFiles utils --md __tmp__/test.md
303+
tsg --tsconfig ./dummy_project/tsconfig.json --include includeFiles config --exclude excludeFiles utils --md __tmp__/test.md
302304
\`\`\`
303305
304306
\`\`\`mermaid
@@ -351,14 +353,14 @@ test('run:sample:exclude', async () => {
351353
});
352354

353355
test('run:sample:abstraction', async () => {
354-
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --md ${filepath}`;
356+
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --md ${filepath}`;
355357

356358
const file = fs.readFileSync(filepath, { encoding: 'utf-8' });
357359
expect(file).toMatchInlineSnapshot(`
358360
"# TypeScript Graph
359361
360362
\`\`\`bash
361-
tsg -d ./dummy_project --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --md __tmp__/test.md
363+
tsg --tsconfig ./dummy_project/tsconfig.json --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --md __tmp__/test.md
362364
\`\`\`
363365
364366
\`\`\`mermaid
@@ -402,14 +404,14 @@ test('run:sample:abstraction', async () => {
402404
});
403405

404406
test('run:sample:highlight', async () => {
405-
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --highlight config.ts b.ts --md ${filepath}`;
407+
await $`ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --highlight config.ts b.ts --md ${filepath}`;
406408

407409
const file = fs.readFileSync(filepath, { encoding: 'utf-8' });
408410
expect(file).toMatchInlineSnapshot(`
409411
"# TypeScript Graph
410412
411413
\`\`\`bash
412-
tsg -d ./dummy_project --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --highlight config.ts b.ts --md __tmp__/test.md
414+
tsg --tsconfig ./dummy_project/tsconfig.json --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --highlight config.ts b.ts --md __tmp__/test.md
413415
\`\`\`
414416
415417
\`\`\`mermaid

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
"scripts": {
2727
"run": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts",
2828
"run:help": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -h",
29-
"run:sample": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --LR",
30-
"run:sample:include": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --include includeFiles config --LR",
31-
"run:sample:exclude": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --include includeFiles config --exclude excludeFiles utils --LR",
32-
"run:sample:abstraction": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --LR",
33-
"run:sample:highlight": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --LR --highlight config.ts b.ts",
34-
"run:sample:link": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --mermaid-link",
29+
"run:sample": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --LR",
30+
"run:sample:include": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --include includeFiles config --LR",
31+
"run:sample:exclude": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --include includeFiles config --exclude excludeFiles utils --LR",
32+
"run:sample:abstraction": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --LR",
33+
"run:sample:highlight": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --include includeFiles config --exclude excludeFiles utils --abstraction abstractions --LR --highlight config.ts b.ts",
34+
"run:sample:link": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --mermaid-link",
3535
"run:sample:README": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d /Users/horiyousuke/Documents/dev/numberplace --include src/components/atoms/ConfigMenu --exclude test stories node_modules",
36+
"run:sample:dir": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --tsconfig './dummy_project/tsconfig.json' --LR",
3637
"run:instability": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --measure-instability",
3738
"build": "tsc",
3839
"build:re": "rm -r ./dist && tsc",

src/graph/createGraph.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export function createGraph(
1212
**/
1313
opt: Pick<OptionValues, 'exclude' | 'dir' | 'tsconfig'>,
1414
): { graph: Graph; meta: Meta } {
15-
const configPath = ts.findConfigFile(opt.dir!, ts.sys.fileExists);
15+
const configPath = opt.tsconfig
16+
? path.resolve(opt.tsconfig)
17+
: ts.findConfigFile(path.resolve(opt.dir ?? './'), ts.sys.fileExists);
1618
if (!configPath) {
1719
throw new Error('Could not find a valid "tsconfig.json".');
1820
}

src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ program
3535
'-d, --dir <char>',
3636
'Specifies the root directory of the TypeScript project to analyze. It reads and uses the tsconfig.json file found in this directory.',
3737
)
38+
.option(
39+
'--tsconfig <char>',
40+
'Specifies the path to the tsconfig file to use for analysis. If this option is provided, -d, --dir will be ignored.',
41+
)
3842
.option(
3943
'--include <char...>',
4044
'Specify file paths or parts of file paths to include in the graph (relative to the tsconfig directory, without `./`).',
@@ -69,7 +73,10 @@ export async function main(
6973
commandOptions: OptionValues & { executedScript: string },
7074
) {
7175
setupConfig(
72-
path.join(commandOptions.dir, commandOptions.configFile ?? '.tsgrc.json'),
76+
path.join(
77+
path.resolve(commandOptions.dir ?? './'),
78+
commandOptions.configFile ?? '.tsgrc.json',
79+
),
7380
);
7481

7582
const { graph: fullGraph, meta } = createGraph(commandOptions);
@@ -105,4 +112,4 @@ export async function main(
105112
);
106113
}
107114
const executedScript = `tsg ${process.argv.slice(2).join(' ')}`;
108-
main({ ...opt, dir: path.resolve(opt.dir ?? './'), executedScript });
115+
main({ ...opt, executedScript });

src/models.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export type OptionValues = {
22
md?: string;
33
mermaidLink?: boolean;
4-
dir: string;
4+
dir?: string;
5+
tsconfig?: string;
56
include?: string[];
67
exclude?: string[];
78
abstraction?: string[];

0 commit comments

Comments
 (0)