Skip to content

Commit

Permalink
test: rebind command args in DIContainer, when repeat execute.
Browse files Browse the repository at this point in the history
identical instance execute target method repeatedly, when using jest
running.
  • Loading branch information
ytetsuro committed May 16, 2022
1 parent b952b0c commit 84945f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/Sabik/DIContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@ container
.to(HalsteadConverterForTypeScript)
.whenAnyAncestorNamed('TypeScript');
container.bind<ASTGenerator>(Types.astNodeGenerator).to(ASTGeneratorForTypeScript).whenAnyAncestorNamed('TypeScript');
container.bind<string>(Types.rootPath).toConstantValue(__dirname);
container.bind<string | null>(Types.outputPath).toConstantValue(__dirname);
container.bind<RegExp>(Types.fileMatches).toConstantValue(/.*/);
container.bind<RegExp[]>(Types.fileExcludes).toConstantValue([/$^/]);

export { container };
18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ Fo r HTML or CSV, specify the directory, and for JSON, specify the file.`
.arguments('[targetPath]')
.description('This is source code metrics tool.')
.action(async (targetPath?: string, options?) => {
const outputReportPath = options.outputReportPath ?? options.outputFormat === 'JSON' ? null : './sabik_report';
const outputPath = outputReportPath !== null ? resolve(outputReportPath) : null;
const analyzedTarget = resolve(targetPath ?? './');
const outputReportPath = options.outputReportPath ?? (options.outputFormat === 'JSON' ? null : './sabik_report');
const outputPath = outputReportPath !== null ? resolve(outputReportPath) : null;
const analyzedTarget = resolve(targetPath ?? './');
const excludes = (<string>options.excludes).split(',').map((row) => new RegExp(row));
const matches = new RegExp(options.matches);
const matches = new RegExp(options.matches);

if (!fs.existsSync(analyzedTarget)) {
throw new Error(`${analyzedTarget}: No such file or directory.`);
}

const rootPath = fs.statSync(analyzedTarget).isDirectory() ? analyzedTarget : dirname(analyzedTarget);
const rootPath = fs.statSync(analyzedTarget).isDirectory() ? analyzedTarget : dirname(analyzedTarget);
container.rebind<string>(Types.rootPath).toConstantValue(rootPath);
container.rebind<string | null>(Types.outputPath).toConstantValue(outputPath);
container.rebind<RegExp>(Types.fileMatches).toConstantValue(matches);
container.rebind<RegExp[]>(Types.fileExcludes).toConstantValue(excludes);

container.bind<string>(Types.rootPath).toConstantValue(rootPath);
container.bind<string | null>(Types.outputPath).toConstantValue(outputPath);
container.bind<RegExp>(Types.fileMatches).toConstantValue(matches);
container.bind<RegExp[]>(Types.fileExcludes).toConstantValue(excludes);
const main = container.getNamed(Main, options.outputFormat);

await main.exec(analyzedTarget);
Expand Down

0 comments on commit 84945f2

Please sign in to comment.