Skip to content
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

fix: consider unmatched files ok #8

Merged
merged 1 commit into from
Aug 9, 2024
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
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