Skip to content

Commit 9625154

Browse files
Merge pull request #245 from hypermod-io/hm-app-runner
Ability to run codemods from the Hypermod App
2 parents f7f2ec4 + 55bc230 commit 9625154

File tree

11 files changed

+372
-214
lines changed

11 files changed

+372
-214
lines changed

.changeset/pink-bananas-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hypermod/cli': minor
3+
---
4+
5+
Adds the ability to fetch codemods from the hypermod app API. Also includes a large refactor of the source code to accomidate

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@types/inquirer": "^8.2.1",
4949
"@types/jest": "^29.0.0",
5050
"@types/jscodeshift": "^0.11.6",
51+
"@types/node": "^16.11.0",
5152
"@types/prettier": "^2.0.0",
5253
"@types/tar": "^4.0.4",
5354
"@typescript-eslint/eslint-plugin": "^5.0.0",

packages/cli/src/fetchers/app.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import fs from 'fs-extra';
2+
import ora from 'ora';
3+
import chalk from 'chalk';
4+
import path from 'path';
5+
6+
interface MarketPlaceEntry {
7+
id: string;
8+
slug: string;
9+
description: string;
10+
status: string;
11+
tags: string[];
12+
type: 'OPTIMIZATION' | 'MIGRATION';
13+
transformId: string;
14+
transform: Transform;
15+
}
16+
17+
interface Transform {
18+
author: { image: string; name: string };
19+
id: string;
20+
name: string;
21+
sources: Source[];
22+
}
23+
24+
interface Source {
25+
id: string;
26+
name: string;
27+
code: string;
28+
}
29+
30+
function writeSourceFiles(slug: string, transform: Transform, dir: string) {
31+
transform.sources.forEach(source => {
32+
const filePath = path.join(dir, slug, source.name);
33+
fs.outputFileSync(filePath, source.code);
34+
});
35+
}
36+
37+
export async function fetchHmPkg(slug: string, dir: string) {
38+
const spinner = ora(
39+
`${chalk.green('Attempting to download Hypermod transform:')} ${slug}`,
40+
).start();
41+
42+
let marketplaceEntry: MarketPlaceEntry;
43+
44+
try {
45+
// @ts-expect-error
46+
marketplaceEntry = await fetch(
47+
`https://www.hypermod.io/api/cli/transforms/${slug.replace('hm-', '')}`,
48+
).then((res: any) => {
49+
if (res.status === 404) {
50+
throw new Error(`Transform not found: ${slug}`);
51+
}
52+
53+
if (!res.ok) {
54+
throw new Error(`Error fetching transform: ${res.statusText}`);
55+
}
56+
57+
return res.json();
58+
});
59+
60+
spinner.succeed(
61+
`${chalk.green(
62+
'Found Hypermod transform:',
63+
)} https://www.hypermod.io/explore/${slug}`,
64+
);
65+
} catch (error) {
66+
spinner.fail(`${chalk.red('Unable to fetch Hypermod transform:')} ${slug}`);
67+
throw new Error(`Unable to locate Hypermod transform: ${slug}\n${error}`);
68+
}
69+
70+
writeSourceFiles(slug, marketplaceEntry.transform, dir);
71+
72+
return marketplaceEntry;
73+
}

packages/cli/src/utils/fetch-package.ts renamed to packages/cli/src/fetchers/npm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
} from '@hypermod/fetcher';
1010
import { isValidConfig } from '@hypermod/validator';
1111

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

14-
export async function fetchPackages(
14+
export async function fetchNpmPkg(
1515
packageName: string,
1616
packageManager: ModuleLoader,
1717
) {

packages/cli/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ Examples:
180180
}
181181

182182
console.error(chalk.red(error));
183-
console.log(error);
184183
process.exit(1);
185184
}
186185
})();

packages/cli/src/list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from 'chalk';
22
import { PluginManager } from 'live-plugin-manager';
33

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

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

1111
for (const packageName of packages) {
1212
try {
13-
const { community, remote } = await fetchPackages(
13+
const { community, remote } = await fetchNpmPkg(
1414
packageName,
1515
packageManager,
1616
);

0 commit comments

Comments
 (0)