Skip to content

Ability to run codemods from the Hypermod App #245

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

Merged
merged 2 commits into from
Apr 17, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/pink-bananas-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hypermod/cli': minor
---

Adds the ability to fetch codemods from the hypermod app API. Also includes a large refactor of the source code to accomidate
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@types/inquirer": "^8.2.1",
"@types/jest": "^29.0.0",
"@types/jscodeshift": "^0.11.6",
"@types/node": "^16.11.0",
"@types/prettier": "^2.0.0",
"@types/tar": "^4.0.4",
"@typescript-eslint/eslint-plugin": "^5.0.0",
Expand Down
73 changes: 73 additions & 0 deletions packages/cli/src/fetchers/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fs from 'fs-extra';
import ora from 'ora';
import chalk from 'chalk';
import path from 'path';

interface MarketPlaceEntry {
id: string;
slug: string;
description: string;
status: string;
tags: string[];
type: 'OPTIMIZATION' | 'MIGRATION';
transformId: string;
transform: Transform;
}

interface Transform {
author: { image: string; name: string };
id: string;
name: string;
sources: Source[];
}

interface Source {
id: string;
name: string;
code: string;
}

function writeSourceFiles(slug: string, transform: Transform, dir: string) {
transform.sources.forEach(source => {
const filePath = path.join(dir, slug, source.name);
fs.outputFileSync(filePath, source.code);
});
}

export async function fetchHmPkg(slug: string, dir: string) {
const spinner = ora(
`${chalk.green('Attempting to download Hypermod transform:')} ${slug}`,
).start();

let marketplaceEntry: MarketPlaceEntry;

try {
// @ts-expect-error
marketplaceEntry = await fetch(
`https://www.hypermod.io/api/cli/transforms/${slug.replace('hm-', '')}`,
).then((res: any) => {
if (res.status === 404) {
throw new Error(`Transform not found: ${slug}`);
}

if (!res.ok) {
throw new Error(`Error fetching transform: ${res.statusText}`);
}

return res.json();
});

spinner.succeed(
`${chalk.green(
'Found Hypermod transform:',
)} https://www.hypermod.io/explore/${slug}`,
);
} catch (error) {
spinner.fail(`${chalk.red('Unable to fetch Hypermod transform:')} ${slug}`);
throw new Error(`Unable to locate Hypermod transform: ${slug}\n${error}`);
}

writeSourceFiles(slug, marketplaceEntry.transform, dir);

return marketplaceEntry;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
} from '@hypermod/fetcher';
import { isValidConfig } from '@hypermod/validator';

import { getHypermodPackageName } from './package-names';
import { getHypermodPackageName } from '../utils/package-names';

export async function fetchPackages(
export async function fetchNpmPkg(
packageName: string,
packageManager: ModuleLoader,
) {
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ Examples:
}

console.error(chalk.red(error));
console.log(error);
process.exit(1);
}
})();
4 changes: 2 additions & 2 deletions packages/cli/src/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import { PluginManager } from 'live-plugin-manager';

import { fetchPackages } from './utils/fetch-package';
import { fetchNpmPkg } from './fetchers/npm';
import { getHypermodPackageName } from './utils/package-names';

export default async function list(packages: string[]) {
Expand All @@ -10,7 +10,7 @@ export default async function list(packages: string[]) {

for (const packageName of packages) {
try {
const { community, remote } = await fetchPackages(
const { community, remote } = await fetchNpmPkg(
packageName,
packageManager,
);
Expand Down
Loading
Loading