Skip to content

Commit

Permalink
style: format by prettier
Browse files Browse the repository at this point in the history
```sh
cmdx fmt
```
  • Loading branch information
suzuki-shunsuke committed Feb 17, 2024
1 parent 246a10d commit d061624
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 117 deletions.
74 changes: 42 additions & 32 deletions list-module-callers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,54 @@ import * as core from "@actions/core";
import * as fs from "fs";
import * as path from "path";
import * as child_process from "child_process";
import {buildModuleToCallers, resolveRelativeCallTree} from "./lib";
import { buildModuleToCallers, resolveRelativeCallTree } from "./lib";

try {
const configFiles = fs
.readFileSync(core.getInput("config_files"), "utf8")
.split("\n");
const moduleFiles = fs
.readFileSync(core.getInput("module_files"), "utf8")
.split("\n");
const configFiles = fs
.readFileSync(core.getInput("config_files"), "utf8")
.split("\n");
const moduleFiles = fs
.readFileSync(core.getInput("module_files"), "utf8")
.split("\n");

const rawModuleCalls: Record<string, Array<string>> = {};
const rawModuleCalls: Record<string, Array<string>> = {};

const allTerraformFiles = Array.from([...configFiles, ...moduleFiles]);
allTerraformFiles.forEach(tfFile => {
if (tfFile == "") {
return;
}
const allTerraformFiles = Array.from([...configFiles, ...moduleFiles]);
allTerraformFiles.forEach((tfFile) => {
if (tfFile == "") {
return;
}

const tfDir = path.dirname(tfFile);
const inspection = JSON.parse(child_process.execSync(`/home/runner/go/bin/terraform-config-inspect --json ${tfDir}`).toString("utf-8"));
const tfDir = path.dirname(tfFile);
const inspection = JSON.parse(
child_process
.execSync(
`/home/runner/go/bin/terraform-config-inspect --json ${tfDir}`,
)
.toString("utf-8"),
);

// List keys of Local Path modules (source starts with ./ or ../) in module_calls
rawModuleCalls[tfDir] = Object.values(inspection["module_calls"]).flatMap((module: any) => {
const source = module.source;
if (source.startsWith("./") || source.startsWith("../")) {
return [source]
} else {
return []
}
});
});
// List keys of Local Path modules (source starts with ./ or ../) in module_calls
rawModuleCalls[tfDir] = Object.values(inspection["module_calls"]).flatMap(
(module: any) => {
const source = module.source;
if (source.startsWith("./") || source.startsWith("../")) {
return [source];
} else {
return [];
}
},
);
});

const moduleCallers = buildModuleToCallers(resolveRelativeCallTree(rawModuleCalls));
const json = JSON.stringify(moduleCallers);
core.info(`file: ${json}`);
core.setOutput("file", json);
const moduleCallers = buildModuleToCallers(
resolveRelativeCallTree(rawModuleCalls),
);
const json = JSON.stringify(moduleCallers);
core.info(`file: ${json}`);
core.setOutput("file", json);
} catch (error) {
core.setFailed(
error instanceof Error ? error.message : JSON.stringify(error),
);
core.setFailed(
error instanceof Error ? error.message : JSON.stringify(error),
);
}
12 changes: 8 additions & 4 deletions list-module-callers/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import path from "node:path";

type ModuleCalls = Record<string, string[]>
type ModuleCalls = Record<string, string[]>;

type ModuleToCallers = Record<string, string[]>
type ModuleToCallers = Record<string, string[]>;

export function resolveRelativeCallTree(rawModuleCalls: ModuleCalls): ModuleCalls {
export function resolveRelativeCallTree(
rawModuleCalls: ModuleCalls,
): ModuleCalls {
const moduleCalls: ModuleCalls = {};
for (const [module, thisChildren] of Object.entries(rawModuleCalls)) {
const absModulePath = path.resolve("/", module);
Expand All @@ -19,7 +21,9 @@ export function resolveRelativeCallTree(rawModuleCalls: ModuleCalls): ModuleCall
return moduleCalls;
}

export function buildModuleToCallers(modulesCalls: ModuleCalls): ModuleToCallers {
export function buildModuleToCallers(
modulesCalls: ModuleCalls,
): ModuleToCallers {
function findCallers(module: string): string[] {
const callers = [];
for (const [directCaller, modules] of Object.entries(modulesCalls)) {
Expand Down
124 changes: 45 additions & 79 deletions list-module-callers/test/lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,53 @@
import {expect, describe, it} from 'vitest'
import {resolveRelativeCallTree, buildModuleToCallers} from '../src/lib'
import { expect, describe, it } from "vitest";
import { resolveRelativeCallTree, buildModuleToCallers } from "../src/lib";

describe("resolveRelativeCallTree", () => {
it("resolves relative paths in terraform-config-inspect", () => {
const actual = resolveRelativeCallTree({
"modules/a": [
"../b/v2",
"../c",
"../../d",
],
"modules/c": [
"../e",
],
"d": [
"../modules/g"
]
});
expect(actual).toEqual({
// caller : [caller, ...]
"modules/a": [
"modules/b/v2",
"modules/c",
"d",
],
"modules/c": [
"modules/e",
],
"d": [
"modules/g",
],
});
it("resolves relative paths in terraform-config-inspect", () => {
const actual = resolveRelativeCallTree({
"modules/a": ["../b/v2", "../c", "../../d"],
"modules/c": ["../e"],
d: ["../modules/g"],
});
expect(actual).toEqual({
// caller : [caller, ...]
"modules/a": ["modules/b/v2", "modules/c", "d"],
"modules/c": ["modules/e"],
d: ["modules/g"],
});
});
});

describe("buildCallerToCallers", () => {
it("creates a map from callee to its direct callers and transitive callers", () => {
const actual = buildModuleToCallers(resolveRelativeCallTree({
"modules/a": [
"../b/v2",
"../c",
"../../d",
],
"modules/c": [
"../e",
],
"modules/e": [
"../f",
],
"d": [
"../modules/g"
],
"modules/x": [
"../b/v2",
"../e",
],
}));
expect(actual).toEqual({
// callee : [caller, ...caller]
"modules/b/v2": [
"modules/a",
"modules/x",
],
"modules/c": [
"modules/a",
],
"d": [
"modules/a",
],
"modules/e": [
"modules/c",
"modules/a", // e -> c -> a
"modules/x", // x -> e
],
"modules/f": [
"modules/e",
"modules/c",
"modules/a", // f -> e -> c -> a
"modules/x", // f -> e -> x
],
"modules/g": [
"d",
"modules/a", // g -> d -> a
],
});
it("creates a map from callee to its direct callers and transitive callers", () => {
const actual = buildModuleToCallers(
resolveRelativeCallTree({
"modules/a": ["../b/v2", "../c", "../../d"],
"modules/c": ["../e"],
"modules/e": ["../f"],
d: ["../modules/g"],
"modules/x": ["../b/v2", "../e"],
}),
);
expect(actual).toEqual({
// callee : [caller, ...caller]
"modules/b/v2": ["modules/a", "modules/x"],
"modules/c": ["modules/a"],
d: ["modules/a"],
"modules/e": [
"modules/c",
"modules/a", // e -> c -> a
"modules/x", // x -> e
],
"modules/f": [
"modules/e",
"modules/c",
"modules/a", // f -> e -> c -> a
"modules/x", // f -> e -> x
],
"modules/g": [
"d",
"modules/a", // g -> d -> a
],
});
});
});
4 changes: 2 additions & 2 deletions list-module-callers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true
"esModuleInterop": true,
},
"exclude": ["node_modules", "**/*.test.ts"]
"exclude": ["node_modules", "**/*.test.ts"],
}

0 comments on commit d061624

Please sign in to comment.