Skip to content
Merged
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
29 changes: 26 additions & 3 deletions packages/realm-server/scripts/codemod/context-search/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// CLI for the `@context` search codemod. Dry-run by default; pass `--write` to
// apply. Walks the given files/directories for `.gts` card source, migrates the
// usages it can transform mechanically, and reports the ones it can't so they
// can be migrated by hand.
// can be migrated by hand. A file whose template can't be parsed is skipped and
// reported (left untouched) rather than aborting the whole sweep — such a module
// already fails to compile, so it's out of scope here.
//
// node scripts/codemod/context-search/run.ts <path…> [--write]

Expand All @@ -10,7 +12,7 @@ import { join, resolve } from 'path';

import * as prettier from 'prettier';

import { transformContextSearch } from './transform.ts';
import { transformContextSearch, type TransformResult } from './transform.ts';

// Format the migrated source through the repo's prettier config (with
// prettier-plugin-ember-template-tag for `.gts`) so the structural edits land as
Expand Down Expand Up @@ -62,10 +64,21 @@ async function main(): Promise<void> {
let files = collectGtsFiles(paths);
let transformed: string[] = [];
let reported: { file: string; reasons: string[] }[] = [];
let unparseable: { file: string; error: string }[] = [];

for (let file of files) {
let source = readFileSync(file, 'utf8');
let result = transformContextSearch(source, { filename: file });
let result: TransformResult;
try {
result = transformContextSearch(source, { filename: file });
} catch (err) {
// The transformer threw while parsing this module's template, so it can't
// be migrated mechanically (and would not compile as-is either). Record it
// and keep going so a single bad module doesn't mask every file after it.
let message = err instanceof Error ? err.message : String(err);
unparseable.push({ file, error: message.split('\n')[0] });
continue;
}
if (result.status === 'transformed') {
transformed.push(file);
if (write && result.output !== source) {
Expand Down Expand Up @@ -97,6 +110,16 @@ async function main(): Promise<void> {
}
}

if (unparseable.length > 0) {
console.log(
`\nSkipped ${unparseable.length} unparseable file(s) (left untouched):`,
);
for (let { file, error } of unparseable) {
console.log(` ⚠ ${file}`);
console.log(` - ${error}`);
}
}

if (!write && transformed.length > 0) {
console.log('\nRe-run with --write to apply.');
}
Expand Down
Loading