Skip to content

Commit

Permalink
rename CodeshiftConfig type to Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Del Core committed Feb 7, 2024
1 parent 7510eec commit ea20de1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .changeset/stupid-mice-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@hypermod/validator': patch
'@hypermod/fetcher': patch
'@hypermod/types': patch
'@hypermod/cli': patch
---

Renames type `CodeshiftConfig` to `Config` (with backwards compatible alias)
4 changes: 2 additions & 2 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PluginManager, PluginManagerOptions } from 'live-plugin-manager';
import { installPackage } from '@antfu/install-pkg';

import * as core from '@hypermod/core';
import { CodeshiftConfig } from '@hypermod/types';
import { Config } from '@hypermod/types';
import { fetchConfigAtPath, fetchConfigs } from '@hypermod/fetcher';

import { InvalidUserInputError } from './errors';
Expand Down Expand Up @@ -78,7 +78,7 @@ export default async function main(

if (rootPackageJson && rootPackageJson.workspaces) {
const configs = await (rootPackageJson.workspaces as string[]).reduce<
Promise<{ filePath: string; config: CodeshiftConfig }[]>
Promise<{ filePath: string; config: Config }[]>
>(async (accum, filePath) => {
const configs = await fetchConfigs(filePath);
if (!configs.length) return accum;
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import inquirer from 'inquirer';

import { CodeshiftConfig } from '@hypermod/types';
import { Config } from '@hypermod/types';

export const getConfigPrompt = (config: CodeshiftConfig) => {
export const getConfigPrompt = (config: Config) => {
const transforms = Object.keys(config.transforms || {});
const presets = Object.keys(config.presets || {});

Expand All @@ -22,7 +22,7 @@ export const getConfigPrompt = (config: CodeshiftConfig) => {
};

export const getMultiConfigPrompt = (
configs: { filePath: string; config: CodeshiftConfig }[],
configs: { filePath: string; config: Config }[],
) => {
const choices = configs.reduce<any[]>((accum, { filePath, config }) => {
function mapToConfig(codemods: Record<string, string> = {}) {
Expand Down
10 changes: 4 additions & 6 deletions packages/fetcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import path from 'path';
import globby from 'globby';
import { PluginManager } from 'live-plugin-manager';

import { CodeshiftConfig } from '@hypermod/types';
import { Config } from '@hypermod/types';

export interface ConfigMeta {
filePath: string;
config: CodeshiftConfig;
config: Config;
}

function resolveConfigExport(pkg: any): CodeshiftConfig {
function resolveConfigExport(pkg: any): Config {
return pkg.default ? pkg.default : pkg;
}

Expand Down Expand Up @@ -62,9 +62,7 @@ export async function fetchConfigs(filePath: string): Promise<ConfigMeta[]> {
return configs;
}

export async function fetchConfigAtPath(
filePath: string,
): Promise<CodeshiftConfig> {
export async function fetchConfigAtPath(filePath: string): Promise<Config> {
const resolvedFilePath = path.resolve(filePath);
const exists = fs.existsSync(resolvedFilePath);

Expand Down
4 changes: 3 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export interface CodeshiftConfig {
export interface Config {
targets?: string[];
maintainers?: string[];
description?: string;
transforms?: Record<string, string>;
presets?: Record<string, string>;
}

export type CodeshiftConfig = Config;
16 changes: 8 additions & 8 deletions packages/validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import semver from 'semver';

import { CodeshiftConfig } from '@hypermod/types';
import { Config } from '@hypermod/types';
import { fetchConfig } from '@hypermod/fetcher';

function hasValidTransforms(config: CodeshiftConfig) {
function hasValidTransforms(config: Config) {
if (!config.transforms) return true;

return Object.entries(config.transforms).every(([key]) => semver.valid(key));
}

function hasValidPresets(config: CodeshiftConfig): boolean {
function hasValidPresets(config: Config): boolean {
if (!config.presets) return true;

return Object.entries(config.presets).every(([key]) =>
key.match(/^[0-9a-zA-Z\-]+$/),
);
}

function getInvalidProperties(config: CodeshiftConfig) {
function getInvalidProperties(config: Config) {
const validProperties = [
'maintainers',
'description',
Expand All @@ -33,7 +33,7 @@ export function isValidPackageName(dir: string): boolean {
return !!dir.match(/^(@[a-z0-9-~][a-z0-9-._~]*__)?[a-z0-9-~][a-z0-9-._~]*$/);
}

export function isValidConfig(config: CodeshiftConfig) {
export function isValidConfig(config: Config) {
return hasValidTransforms(config) && hasValidPresets(config);
}

Expand All @@ -44,10 +44,10 @@ export async function isValidConfigAtPath(filePath: string) {
throw new Error(`Unable to locate config file at path: ${filePath}`);
}

const invalidProperites = getInvalidProperties(configMeta.config);
if (invalidProperites.length) {
const invalidProperties = getInvalidProperties(configMeta.config);
if (invalidProperties.length) {
throw new Error(
`Invalid transform ids found: ${invalidProperites.join(', ')}`,
`Invalid transform ids found: ${invalidProperties.join(', ')}`,
);
}

Expand Down

0 comments on commit ea20de1

Please sign in to comment.