Skip to content

Commit

Permalink
refactor: fix eslint warnings/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
allohamora committed Mar 16, 2024
1 parent 4f377fa commit aa158dc
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 37 deletions.
4 changes: 2 additions & 2 deletions __tests__/categories/js/docker/docker.config.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getConfig } from 'src/categories/js/docker/docker.config';
import { expectJsConfig } from '__tests__/test-utils/js-config';

interface ExpectDockerConfigArgs {
type ExpectDockerConfigArgs = {
getDockerFile: ({ version }: { version: string }) => string;
dockerIgnore: string;
}
};

const expectDockerConfig = ({ getDockerFile, dockerIgnore }: ExpectDockerConfigArgs) => {
const version = '16.14.2';
Expand Down
4 changes: 2 additions & 2 deletions __tests__/test-utils/cjs-module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import vm from 'node:vm';

interface Context {
type Context = {
module: {
exports?: Record<string, unknown>;
};
[key: string]: unknown;
}
};

export const parse = (script: string) => {
const context: Context = { module: { exports: {} } };
Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils/run-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const spawnMock = <EV>(event: 'exit' | 'error' = 'exit', emitValue?: EV, delayMs
const child = new ChildMock();

const exitHandler = () => child.emit(event, emitValue);
delay(delayMs).then(exitHandler);
void delay(delayMs).then(exitHandler);

childProcessMocked.spawn.mockReturnValueOnce(child as ReturnType<typeof childProcess.spawn>);

Expand Down
4 changes: 2 additions & 2 deletions __tests__/utils/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ describe('createConfigState', () => {
const types = ['1', '2', '3'];
const createNewTypeState = () => createTypeState(types);
let state = createNewTypeState();
let [getType, setType] = state;
let [, setType] = state;

const configValues = { default: 'default', '1': '1' };
const createNewConfigState = () => createConfigState(state, configValues);
let [getConfig] = createNewConfigState();

afterEach(() => {
state = createNewTypeState();
[getType, setType] = state;
[, setType] = state;
[getConfig] = createNewConfigState();
});

Expand Down
4 changes: 2 additions & 2 deletions src/categories/js/docker/config/default.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { readableMultilineString } from 'src/utils/string';
import { FILE_NAME, IGNORE_NAME } from '../docker.const';

interface GetDockerFileArgs {
type GetDockerFileArgs = {
version: string;
}
};

export const getDockerFile = ({ version }: GetDockerFileArgs) => readableMultilineString`
FROM node:${version}
Expand Down
8 changes: 4 additions & 4 deletions src/categories/js/eslint/config/config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Mutation } from 'src/utils/mutation';

export interface EslintConfig {
export type EslintConfig = {
parser?: string;
parserOptions?: {
project?: string;
Expand All @@ -19,12 +19,12 @@ export interface EslintConfig {
plugins?: string[];
extends?: string[];
rules?: Record<string, string | unknown[]>;
}
};

export interface Config {
export type Config = {
dependencies: string[];
eslintConfig: EslintConfig;
ignore: string[];
scripts: { name: string; script: string }[];
mutations: Mutation<Config>[];
}
};
12 changes: 6 additions & 6 deletions src/categories/js/eslint/eslint.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import { isPrettierInstalled } from '../prettier/prettier.utils';
import { Config } from './config/config.interface';
import { CONFIG_FILE_NAME, SCRIPT_NAME } from './eslint.const';

export const prettierMutation = async (config: Config) => {
if (await isPrettierInstalled()) {
addPrettierToConfig(config);
}
};

export const jestMutation = async (config: Config) => {
if (await isJestInstalled()) {
config.eslintConfig.env = { ...config.eslintConfig.env, jest: true };
Expand All @@ -27,4 +21,10 @@ const addPrettierToConfig = (config: Config) => {
config.eslintConfig.extends = eslintExtends;
};

export const prettierMutation = async (config: Config) => {
if (await isPrettierInstalled()) {
addPrettierToConfig(config);
}
};

export const isEslintInstalled = isInstalledAndInRootCheck(SCRIPT_NAME, CONFIG_FILE_NAME);
4 changes: 2 additions & 2 deletions src/categories/js/jest/config/config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NpmScript } from 'src/utils/npm';

export interface Config {
export type Config = {
devDependencies: string[];
scripts: NpmScript[];
configFileContent: string;
}
};
4 changes: 2 additions & 2 deletions src/categories/js/lint-staged/config/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Mutation } from 'src/utils/mutation';

export type LintStagedConfig = Record<string, unknown>;

export interface Config {
export type Config = {
config: LintStagedConfig;
mutations: Mutation<LintStagedConfig>[];
}
};
4 changes: 2 additions & 2 deletions src/categories/js/lint-staged/lint-staged.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { CLI_NAME as STYLELINT_CLI_NAME } from '../stylelint/stylelint.const';

type ScriptFileExtension = '*.js' | '*.ts' | '*.css' | '*.{ts,tsx}' | '*.{css,ts,tsx}';

interface OptionMutation {
type OptionMutation = {
check: (config: LintStagedConfig, key: string, value: string) => boolean;
mutate: (config: LintStagedConfig, key: string, value: string) => void;
}
};

const arrayOptionMutation: OptionMutation = {
check: (config, key) => Array.isArray(config[key]),
Expand Down
8 changes: 4 additions & 4 deletions src/categories/js/stylelint/config/config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Mutation } from 'src/utils/mutation';
import { NpmScript } from 'src/utils/npm';

interface StylelintConfig {
type StylelintConfig = {
extends: string[];
overrides?: { files: string[]; customSyntax?: string }[];
}
};

export interface Config {
export type Config = {
devDependencies: string[];
scripts: NpmScript[];
stylelintConfig: StylelintConfig;
stylelintIgnore: string;
mutations: Mutation<Config>[];
}
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export const main = async () => {
};

if (require.main) {
main();
void main();
}
4 changes: 2 additions & 2 deletions src/states/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface Context {
export type Context = {
installing: string[];
}
};

export const context: Context = { installing: [] };

Expand Down
13 changes: 8 additions & 5 deletions src/utils/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { stringify } from './json';
import { PACKAGE_JSON_PATH } from './path';
import { runCommand } from './run-command';

interface PackageJson extends BasePackageJson {
type PackageJson = {
[key: string]: unknown;
}
} & BasePackageJson;

export const getPackageJson = async () => {
const json = await fsp.readFile(PACKAGE_JSON_PATH, { encoding: 'utf-8' });
Expand All @@ -28,18 +28,21 @@ export const addToPackageJson = async <V>(name: keyof PackageJson, value: V) =>
await setPackageJson(packageJson);
};

export interface NpmScript {
export type NpmScript = {
name: string;
script: string;
}
};

export const addScripts = async (...scripts: NpmScript[]) => {
const packageJson = await getPackageJson();

packageJson.scripts ??= {};

scripts.forEach(({ name, script }) => {
packageJson.scripts![name] = script;
// type-guard
if (!packageJson.scripts) throw new Error('packageJson.scripts is undefined');

packageJson.scripts[name] = script;
});

await setPackageJson(packageJson);
Expand Down

0 comments on commit aa158dc

Please sign in to comment.