Skip to content

feat(instability): 各モジュール(ファイル)のカップリング数や不安程度を表示する #175

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

Merged
merged 1 commit into from
May 16, 2024
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"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",
"run:sample:link": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts -d './dummy_project' --mermaid-link",
"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",
"run:instability": "ts-node -O '{\"module\": \"commonjs\"}' ./src/index.ts --measure-instability",
"build": "tsc",
"build:re": "rm -r ./dist && tsc",
"commit": "git-cz",
Expand Down
40 changes: 35 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ program
)
.option('--LR', 'Specify Flowchart orientation Left-to-Right')
.option('--TB', 'Specify Flowchart orientation Top-to-Bottom')
.option(
'--measure-instability',
'Enable the beta feature to measure the instability of the modules',
)
.option(
'--config-file',
'Specify the relative path to the config file (from cwd or specified by -d, --dir). Default is .tsgrc.json.',
Expand All @@ -61,6 +65,27 @@ export async function main(

const { graph: fullGraph, meta } = createGraph(dir);

let couplingData: {
afferentCoupling: number;
efferentCoupling: number;
path: string;
name: string;
isDirectory?: boolean | undefined;
}[] = [];
if (commandOptions.measureInstability) {
console.time('coupling');
couplingData = fullGraph.nodes.map(node => {
const afferentCoupling = fullGraph.relations.filter(
r => r.kind === 'depends_on' && r.to.path === node.path,
).length;
const efferentCoupling = fullGraph.relations.filter(
r => r.kind === 'depends_on' && r.from.path === node.path,
).length;
return { ...node, afferentCoupling, efferentCoupling };
});
console.timeEnd('coupling');
}

const graph = pipe(
fullGraph,
graph =>
Expand All @@ -73,11 +98,16 @@ export async function main(
graph => highlight(commandOptions.highlight, graph),
);

await writeMarkdownFile(commandOptions.md ?? 'typescript-graph', graph, {
...commandOptions,
rootDir: meta.rootDir,
executedScript: commandOptions.executedScript,
});
await writeMarkdownFile(
commandOptions.md ?? 'typescript-graph',
graph,
{
...commandOptions,
rootDir: meta.rootDir,
executedScript: commandOptions.executedScript,
},
couplingData,
);
}

const dir = path.resolve(opt.dir ?? './');
Expand Down
1 change: 1 addition & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type OptionValues = {
LR: boolean;
TB: boolean;
configFile: string;
measureInstability: boolean;
};

type FileName = string;
Expand Down
38 changes: 38 additions & 0 deletions src/writeMarkdownFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export async function writeMarkdownFile(
markdownTitle: string,
graph: Graph,
options: Options,
couplingData: {
afferentCoupling: number;
efferentCoupling: number;
path: string;
name: string;
isDirectory?: boolean | undefined;
}[],
) {
return new Promise((resolve, reject) => {
const filename = markdownTitle.endsWith('.md')
Expand All @@ -31,6 +38,37 @@ export async function writeMarkdownFile(
ws.write('```mermaid\n');
mermaidify(str => ws.write(str), graph, options);
ws.write('```\n');

if (couplingData.length !== 0) {
ws.write('## Instability\n');
ws.write('\n');
ws.write(
'module name | Afferent<br>coupling | Efferent<br>coupling | Instability\n',
);
ws.write('--|--|--|--\n');

couplingData
.filter(node => !node.isDirectory)
// node_modules 配下のモジュールを除外する
.filter(node => !node.path.includes('node_modules'))
// json ファイルを除外する
.filter(node => !node.path.endsWith('.json'))
.map(node => {
const totalCoupling = node.afferentCoupling + node.efferentCoupling;
const instability =
totalCoupling === 0 ? 0 : node.efferentCoupling / totalCoupling;
return { ...node, instability };
})
.toSorted((a, b) => b.instability - a.instability)
.forEach(node => {
const totalCoupling = node.afferentCoupling + node.efferentCoupling;
const instability =
totalCoupling === 0 ? 0 : node.efferentCoupling / totalCoupling;
ws.write(
`${node.name} | ${node.afferentCoupling} | ${node.efferentCoupling} | ${instability.toFixed(2)}\n`,
);
});
}
ws.end();

console.log(filename);
Expand Down