Skip to content

Commit

Permalink
Extract CLI functions into separate file (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Feb 1, 2022
1 parent 96f93b4 commit 2f28a55
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 135 deletions.
136 changes: 1 addition & 135 deletions bin/check-dependency-version-consistency.ts
Original file line number Diff line number Diff line change
@@ -1,141 +1,7 @@
#!/usr/bin/env node
/* eslint node/shebang:"off" -- shebang needed so compiled code gets interpreted as JS */

import { Command, Argument } from 'commander';
import { readFileSync } from 'node:fs';
import {
calculateVersionsForEachDependency,
calculateMismatchingVersions,
filterOutIgnoredDependencies,
fixMismatchingVersions,
MismatchingDependencyVersions,
} from '../lib/dependency-versions.js';
import { getPackages } from '../lib/workspace.js';
import {
mismatchingVersionsToOutput,
mismatchingVersionsFixedToOutput,
} from '../lib/output.js';
import { join, dirname } from 'node:path';
import type { PackageJson } from 'type-fest';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));

function getCurrentPackageVersion(): string {
const packageJson: PackageJson = JSON.parse(
readFileSync(join(__dirname, '..', '..', 'package.json'), 'utf-8') // Relative to compiled version of this file in dist/bin
);
if (!packageJson.version) {
throw new Error('Could not find package.json `version`');
}
return packageJson.version;
}

// Used for collecting repeated CLI options into an array.
function collect(value: string, previous: string[]) {
return [...previous, value];
}

// Setup CLI.
function run() {
const program = new Command();

program
.version(getCurrentPackageVersion())
.addArgument(new Argument('[path]', 'path to workspace root').default('.'))
.option(
'--fix',
'Whether to autofix inconsistencies (using latest version present)',
false
)
.option(
'--ignore-dep <dependency-name>',
'Dependency to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-dep-pattern <dependency-name-pattern>',
'RegExp of dependency names to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-package <package-name>',
'Workspace package to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-package-pattern <package-name-pattern>',
'RegExp of package names to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-path <path>',
'Workspace-relative path of packages to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-path-pattern <path-pattern>',
'RegExp of workspace-relative path of packages to ignore (option can be repeated)',
collect,
[]
)
.action(function (
path,
options: {
ignoreDep: string[];
ignoreDepPattern: string[];
ignorePackage: string[];
ignorePackagePattern: string[];
ignorePath: string[];
ignorePathPattern: string[];
fix: boolean;
}
) {
// Calculate.
const packages = getPackages(
path,
options.ignorePackage,
options.ignorePackagePattern.map((s) => new RegExp(s)),
options.ignorePath,
options.ignorePathPattern.map((s) => new RegExp(s))
);

const dependencyVersions = calculateVersionsForEachDependency(packages);

let mismatchingVersions = filterOutIgnoredDependencies(
calculateMismatchingVersions(dependencyVersions),
options.ignoreDep,
options.ignoreDepPattern.map((s) => new RegExp(s))
);
let mismatchingVersionsFixed: MismatchingDependencyVersions = [];

if (options.fix) {
const resultsAfterFix = fixMismatchingVersions(
packages,
mismatchingVersions
);
mismatchingVersions = resultsAfterFix.notFixed;
mismatchingVersionsFixed = resultsAfterFix.fixed;
}

// Show output for dependencies we fixed.
if (mismatchingVersionsFixed.length > 0) {
console.log(mismatchingVersionsFixedToOutput(mismatchingVersionsFixed));
}

// Show output for dependencies that still have mismatches.
if (mismatchingVersions.length > 0) {
console.log(mismatchingVersionsToOutput(mismatchingVersions));
process.exitCode = 1;
}
})
.parse(process.argv);
}
import { run } from '../lib/cli.js';

try {
run();
Expand Down
137 changes: 137 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { Command, Argument } from 'commander';
import { readFileSync } from 'node:fs';
import {
calculateVersionsForEachDependency,
calculateMismatchingVersions,
filterOutIgnoredDependencies,
fixMismatchingVersions,
MismatchingDependencyVersions,
} from '../lib/dependency-versions.js';
import { getPackages } from '../lib/workspace.js';
import {
mismatchingVersionsToOutput,
mismatchingVersionsFixedToOutput,
} from '../lib/output.js';
import { join, dirname } from 'node:path';
import type { PackageJson } from 'type-fest';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));

function getCurrentPackageVersion(): string {
const packageJson: PackageJson = JSON.parse(
readFileSync(join(__dirname, '..', '..', 'package.json'), 'utf-8') // Relative to compiled version of this file in the dist folder.
);
if (!packageJson.version) {
throw new Error('Could not find package.json `version`');
}
return packageJson.version;
}

// Used for collecting repeated CLI options into an array.
function collect(value: string, previous: string[]) {
return [...previous, value];
}

// Setup CLI.
export function run() {
const program = new Command();

program
.version(getCurrentPackageVersion())
.addArgument(new Argument('[path]', 'path to workspace root').default('.'))
.option(
'--fix',
'Whether to autofix inconsistencies (using highest version present)',
false
)
.option(
'--ignore-dep <dependency-name>',
'Dependency to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-dep-pattern <dependency-name-pattern>',
'RegExp of dependency names to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-package <package-name>',
'Workspace package to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-package-pattern <package-name-pattern>',
'RegExp of package names to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-path <path>',
'Workspace-relative path of packages to ignore (option can be repeated)',
collect,
[]
)
.option(
'--ignore-path-pattern <path-pattern>',
'RegExp of workspace-relative path of packages to ignore (option can be repeated)',
collect,
[]
)
.action(function (
path,
options: {
ignoreDep: string[];
ignoreDepPattern: string[];
ignorePackage: string[];
ignorePackagePattern: string[];
ignorePath: string[];
ignorePathPattern: string[];
fix: boolean;
}
) {
// Calculate.
const packages = getPackages(
path,
options.ignorePackage,
options.ignorePackagePattern.map((s) => new RegExp(s)),
options.ignorePath,
options.ignorePathPattern.map((s) => new RegExp(s))
);

const dependencyVersions = calculateVersionsForEachDependency(packages);

let mismatchingVersions = filterOutIgnoredDependencies(
calculateMismatchingVersions(dependencyVersions),
options.ignoreDep,
options.ignoreDepPattern.map((s) => new RegExp(s))
);
let mismatchingVersionsFixed: MismatchingDependencyVersions = [];

if (options.fix) {
const resultsAfterFix = fixMismatchingVersions(
packages,
mismatchingVersions
);
mismatchingVersions = resultsAfterFix.notFixed;
mismatchingVersionsFixed = resultsAfterFix.fixed;
}

// Show output for dependencies we fixed.
if (mismatchingVersionsFixed.length > 0) {
console.log(mismatchingVersionsFixedToOutput(mismatchingVersionsFixed));
}

// Show output for dependencies that still have mismatches.
if (mismatchingVersions.length > 0) {
console.log(mismatchingVersionsToOutput(mismatchingVersions));
process.exitCode = 1;
}
})
.parse(process.argv);

return program;
}

0 comments on commit 2f28a55

Please sign in to comment.