Skip to content
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

Lint using 🐊Putout #111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions packages/cli/bin/codeshift-cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env node
const tryCatch = require('try-catch');
const isNumber = a => typeof a === 'number';
/* eslint-disable */

const fs = require('fs');
Expand All @@ -11,10 +13,10 @@ if (dev && !require.extensions['.ts']) {
require('ts-node').register({ project });
}

try {
require(path.join('..', dev ? 'src/index' : 'dist/codeshift-cli.cjs.js'));
} catch (error) {
if (typeof error === 'number') {
const [error] = tryCatch(require, path.join('..', dev ? 'src/index' : 'dist/codeshift-cli.cjs.js'));

if (error) {
if (isNumber(error)) {
process.exit(error);
}
console.error(error);
Expand Down
51 changes: 25 additions & 26 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tryToCatch from 'try-to-catch';
import chalk from 'chalk';
import { Command, Option, CommanderError } from 'commander';

Expand Down Expand Up @@ -128,34 +129,32 @@ Examples:

program.exitOverride();

(async function() {
try {
await program.parseAsync(process.argv);
} catch (error) {
if (error instanceof CommanderError) {
if (
error.code === 'commander.helpDisplayed' ||
error.code === 'commander.version'
) {
return;
}

console.error(chalk.red(error.message));
process.exit(error.exitCode);
}
const [error] = await tryToCatch(program.parseAsync, process.argv);

if (error instanceof InvalidUserInputError) {
console.warn(program.help());
console.warn(chalk.red(error.message));
process.exit(9);
if (error) {
if (error instanceof CommanderError) {
if (
error.code === 'commander.helpDisplayed' ||
error.code === 'commander.version'
) {
return;
}

if (error instanceof InvalidConfigError) {
console.warn(chalk.red(error.message));
process.exit(7);
}
console.error(chalk.red(error.message));
process.exit(error.exitCode);
}

console.error(chalk.red(error));
process.exit(1);
if (error instanceof InvalidUserInputError) {
console.warn(program.help());
console.warn(chalk.red(error.message));
process.exit(9);
}
})();

if (error instanceof InvalidConfigError) {
console.warn(chalk.red(error.message));
process.exit(7);
}

console.error(chalk.red(error));
process.exit(1);
}
2 changes: 1 addition & 1 deletion packages/cli/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import { initDirectory, initTransform } from '@codeshift/initializer';

export default async function init(
export default function init(
packageName: string,
transform?: string,
preset?: string,
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ${chalk.bold('@foo/bar')}
it('should return error message a package is not found', async () => {
(PluginManager as jest.Mock).mockImplementation(() => ({
install: jest.fn().mockImplementation(() => {
throw new Error('404 not found');
throw Error('404 not found');
}),
}));

Expand All @@ -93,7 +93,7 @@ ${chalk.bold('@foo/bar')}
it('should return error message for multiple packages that are not found', async () => {
(PluginManager as jest.Mock).mockImplementation(() => ({
install: jest.fn().mockImplementation(() => {
throw new Error('404 not found');
throw Error('404 not found');
}),
}));

Expand All @@ -113,12 +113,12 @@ ${chalk.bold('@foo/bar')}
install: jest
.fn()
.mockImplementationOnce(() => {
throw new Error('404 not found');
throw Error('404 not found');
})
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce(undefined)
.mockImplementationOnce(() => {
throw new Error('404 not found');
throw Error('404 not found');
})
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce(undefined),
Expand Down
38 changes: 20 additions & 18 deletions packages/cli/src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function list(packages: string[]) {

try {
await packageManager.install(codemodName);
} catch (error) {
} catch {
console.warn(
chalk.red(
`Unable to find codeshift package: ${chalk.bold(packageName)}.`,
Expand All @@ -24,30 +24,32 @@ export default async function list(packages: string[]) {

await packageManager.install(codemodName);
const pkg = packageManager.require(codemodName);
const config: CodeshiftConfig = pkg.default ? pkg.default : pkg;
const config: CodeshiftConfig = pkg.default || pkg;

console.log(chalk.bold(packageName));

if (config.transforms) {
console.log(`├─ transforms`),
Object.keys(config.transforms).forEach((transform, index, array) => {
if (index + 1 === array.length) {
console.log(`| └─ ${transform}`);
return;
}
console.log(`| ├─ ${transform}`);
});
console.log(`├─ transforms`);

Object.keys(config.transforms).forEach((transform, index, array) => {
if (index + 1 === array.length) {
console.log(`| └─ ${transform}`);
return;
}
console.log(`| ├─ ${transform}`);
});
}

if (config.presets) {
console.log(`└─ presets`),
Object.keys(config.presets).forEach((transform, index, array) => {
if (index + 1 === array.length) {
console.log(` └─ ${transform}`);
return;
}
console.log(`| ├─ ${transform}`);
});
console.log(`└─ presets`);

Object.keys(config.presets).forEach((transform, index, array) => {
if (index + 1 === array.length) {
console.log(` └─ ${transform}`);
return;
}
console.log(`| ├─ ${transform}`);
});
}
}
}
62 changes: 33 additions & 29 deletions packages/cli/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import * as jscodeshift from 'jscodeshift/src/Runner';
import { PluginManager } from 'live-plugin-manager';
import globby from 'globby';

import tryToCatch from 'try-to-catch';

import main from './main';

const mockPath = 'src/pages/home-page/';
Expand All @@ -23,9 +25,11 @@ describe('main', () => {
it('should exit early if file path is not supplied', async () => {
expect.assertions(1);

try {
await main([], { transform: 'path/to/transform.ts' });
} catch (error) {
const [error] = await tryToCatch(main, [], {
transform: 'path/to/transform.ts',
});

if (error) {
// @ts-ignore
expect(error.message).toMatch(
'No path provided, please specify which files your codemod should modify',
Expand All @@ -36,9 +40,9 @@ describe('main', () => {
it('should exit early if nether a package or transform is supplied', async () => {
expect.assertions(1);

try {
await main([mockPath], {});
} catch (error) {
const [error] = await tryToCatch(main, [mockPath], {});

if (error) {
// @ts-ignore
expect(error.message).toMatch(
'No transform provided, please specify a transform with either the --transform or --packages flags',
Expand Down Expand Up @@ -125,7 +129,7 @@ describe('main', () => {
install: jest.fn().mockResolvedValue(undefined),
require: jest.fn().mockImplementation((codemodName: string) => {
if (!codemodName.startsWith('@codeshift')) {
throw new Error('Attempted to fetch codemod from npm');
throw Error('Attempted to fetch codemod from npm');
}

return {
Expand Down Expand Up @@ -279,13 +283,13 @@ describe('main', () => {
it('should throw when package format is invalid', async () => {
expect.assertions(1);

try {
await main([mockPath], {
packages: 'mylib@NOT_SEMVER',
parser: 'babel',
extensions: 'js',
});
} catch (error) {
const [error] = await tryToCatch(main, [mockPath], {
packages: 'mylib@NOT_SEMVER',
parser: 'babel',
extensions: 'js',
});

if (error) {
// @ts-ignore
expect(error.message).toMatch(
'Invalid version provided to the --packages flag. Unable to resolve version "NOT_SEMVER" for package "mylib". Please try: "[scope]/[package]@[version]" for example @mylib/[email protected]',
Expand All @@ -296,13 +300,13 @@ describe('main', () => {
it('should throw when transform is not found', async () => {
expect.assertions(1);

try {
await main([mockPath], {
packages: '[email protected]',
parser: 'babel',
extensions: 'js',
});
} catch (error) {
const [error] = await tryToCatch(main, [mockPath], {
packages: '[email protected]',
parser: 'babel',
extensions: 'js',
});

if (error) {
// @ts-ignore
expect(error.message).toMatch(
'Invalid version provided to the --packages flag. Unable to resolve version "120.0.0" for package "mylib"',
Expand Down Expand Up @@ -386,7 +390,7 @@ describe('main', () => {
install: jest.fn().mockResolvedValue(undefined),
require: jest.fn().mockImplementation((codemodName: string) => {
if (!codemodName.startsWith('@codeshift')) {
throw new Error('Attempted to fetch codemod from npm');
throw Error('Attempted to fetch codemod from npm');
}

return {
Expand Down Expand Up @@ -473,13 +477,13 @@ describe('main', () => {
it('should throw when preset is not found', async () => {
expect.assertions(1);

try {
await main([mockPath], {
packages: 'mylib#does-not-exist',
parser: 'babel',
extensions: 'js',
});
} catch (error) {
const [error] = await tryToCatch(main, [mockPath], {
packages: 'mylib#does-not-exist',
parser: 'babel',
extensions: 'js',
});

if (error) {
// @ts-ignore
expect(error.message).toMatch(
'Invalid preset provided to the --packages flag. Unable to resolve preset "does-not-exist" for package "mylib"',
Expand Down
Loading