Skip to content

Commit

Permalink
fix: consider unmatched files ok
Browse files Browse the repository at this point in the history
Fixes a small bug where unmatched/clean files were still causing the
result to show as "detected replaceable modules".
  • Loading branch information
43081j committed Aug 9, 2024
1 parent 8cceb1a commit 88565c7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"clean:test": "premove coverage",
"build:js": "npm run clean:build && tsc",
"build": "npm run lint && npm run build:js",
"lint": "eslint src",
"lint": "prettier --check src && eslint src",
"format": "prettier --write src",
"test": "npm run build:js && npm run clean:test && c8 node --test",
"prepublishOnly": "npm run test"
Expand Down
4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export async function run(): Promise<void> {

const tasks = await cl.multiselect({
message: 'What would you like to do?',
initialValues: [
'module-replacements'
],
initialValues: ['module-replacements'],
options: [
{
label: 'Scan for module replacements',
Expand Down
26 changes: 17 additions & 9 deletions src/stages/scan-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ async function scanFile(
contents: string,
lines: string[],
replacements: modReplacements.ModuleReplacement[]
): Promise<FileReplacement> {
): Promise<FileReplacement | null> {
const ast = sg.parse(contents);
const root = ast.root();
const result: FileReplacement = {
path: filePath,
contents,
replacements: []
};
const matches: modReplacements.ModuleReplacement[] = [];

for (const replacement of replacements) {
const imports = root.findAll({
Expand All @@ -42,7 +38,7 @@ async function scanFile(
});

if (imports.length > 0) {
result.replacements.push(replacement);
matches.push(replacement);
}

for (const node of imports) {
Expand Down Expand Up @@ -73,7 +69,15 @@ async function scanFile(
}
}

return result;
if (matches.length === 0) {
return null;
}

return {
path: filePath,
contents,
replacements: matches
};
}

export async function scanFiles(
Expand All @@ -90,7 +94,11 @@ export async function scanFiles(

spinner.message(`Scanning ${file}`);

results.push(await scanFile(file, contents, lines, replacements));
const scanResult = await scanFile(file, contents, lines, replacements);

if (scanResult) {
results.push(scanResult);
}
} catch (err) {
cl.log.error(dedent`
Could not read file ${file}:
Expand Down

0 comments on commit 88565c7

Please sign in to comment.