Skip to content

Commit c4dafa6

Browse files
authored
feat: filter migration list to package dependencies (#48)
1 parent 90f3322 commit c4dafa6

File tree

5 files changed

+54
-24
lines changed

5 files changed

+54
-24
lines changed

src/analyze/replacements.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as replacements from 'module-replacements';
2-
import {PackageJsonLike, ReportPluginResult} from '../types.js';
2+
import {ReportPluginResult} from '../types.js';
33
import type {FileSystem} from '../file-system.js';
4+
import {getPackageJson} from '../file-system-utils.js';
45

56
/**
67
* Generates a standard URL to the docs of a given rule
@@ -27,25 +28,9 @@ export async function runReplacements(
2728
messages: []
2829
};
2930

30-
let packageJsonText: string;
31+
const packageJson = await getPackageJson(fileSystem);
3132

32-
try {
33-
packageJsonText = await fileSystem.readFile('/package.json');
34-
} catch {
35-
// No package.json found
36-
return result;
37-
}
38-
39-
let packageJson: PackageJsonLike;
40-
41-
try {
42-
packageJson = JSON.parse(packageJsonText);
43-
} catch {
44-
// Not parseable
45-
return result;
46-
}
47-
48-
if (!packageJson.dependencies) {
33+
if (!packageJson || !packageJson.dependencies) {
4934
// No dependencies
5035
return result;
5136
}

src/commands/migrate.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,37 @@ import {glob} from 'tinyglobby';
66
import {readFile, writeFile} from 'node:fs/promises';
77
import {fixableReplacements} from './fixable-replacements.js';
88
import type {Replacement} from '../types.js';
9-
10-
const fixableReplacementsTargets = new Set(
11-
fixableReplacements.map((rep) => rep.from)
12-
);
9+
import {LocalFileSystem} from '../local-file-system.js';
10+
import {getPackageJson} from '../file-system-utils.js';
1311

1412
export async function run(ctx: CommandContext<typeof meta.args>) {
1513
const [_commandName, ...targetModules] = ctx.positionals;
1614
const dryRun = ctx.values['dry-run'] === true;
1715
const interactive = ctx.values.interactive === true;
1816
const include = ctx.values.include;
17+
const fileSystem = new LocalFileSystem(process.cwd());
18+
const packageJson = await getPackageJson(fileSystem);
1919

2020
prompts.intro(`Migrating packages...`);
2121

22+
if (!packageJson) {
23+
prompts.cancel(
24+
'Error: No package.json found in the current directory. Please run this command in a project directory.'
25+
);
26+
return;
27+
}
28+
29+
const dependencies = {
30+
...packageJson.dependencies,
31+
...packageJson.devDependencies
32+
};
33+
34+
const fixableReplacementsTargets = new Set(
35+
fixableReplacements
36+
.filter((rep) => Object.hasOwn(dependencies, rep.from))
37+
.map((rep) => rep.from)
38+
);
39+
2240
if (interactive) {
2341
const additionalTargets = await prompts.autocompleteMultiselect({
2442
message: 'Select packages to migrate',

src/file-system-utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type {FileSystem} from './file-system.js';
2+
import type {PackageJsonLike} from './types.js';
3+
4+
export async function getPackageJson(
5+
fileSystem: FileSystem
6+
): Promise<PackageJsonLike | null> {
7+
let packageJsonText: string;
8+
9+
try {
10+
packageJsonText = await fileSystem.readFile('/package.json');
11+
} catch {
12+
// No package.json found
13+
return null;
14+
}
15+
16+
try {
17+
return JSON.parse(packageJsonText);
18+
} catch {
19+
// Not parseable
20+
return null;
21+
}
22+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"name": "foo",
3+
"type": "module",
34
"private": true,
4-
"version": "0.0.1"
5+
"version": "0.0.1",
6+
"main": "lib/main.js",
7+
"dependencies": {
8+
"chalk": "^4.0.0"
9+
}
510
}

0 commit comments

Comments
 (0)