Skip to content

🎥 Make videos programmatically with React

[studio-server] Refactor duplicated codemod formatting logic

Cursor Agent00d847862527
diff --git a/packages/studio-server/src/codemods/delete-jsx-node.ts b/packages/studio-server/src/codemods/delete-jsx-node.ts
index 575c626896..93403a8ecc 100644
--- a/packages/studio-server/src/codemods/delete-jsx-node.ts
+++ b/packages/studio-server/src/codemods/delete-jsx-node.ts
@@ -26,6 +26,7 @@ import type {
} from '@babel/types';
import type {SequenceNodePath} from '@remotion/studio-shared';
import * as recast from 'recast';
+import {formatFileContent} from './format-file-content';
import {parseAst, serializeAst} from './parse-ast';
const {builders: b, namedTypes} = recast.types;
@@ -441,61 +442,14 @@ export const deleteJsxNode = async ({
deleteJsxElementAtPath(jsxPath);
const finalFile = serializeAst(ast);
-
- // eslint-disable-next-line @typescript-eslint/consistent-type-imports
- type PrettierType = typeof import('prettier');
- let prettier: PrettierType | null = null;
-
- try {
- prettier = await import('prettier');
- } catch {
- return {
- output: finalFile,
- formatted: false,
- nodeLabel,
- logLine,
- };
- }
-
- const {format, resolveConfig, resolveConfigFile} = prettier as PrettierType;
-
- let prettierConfig: Record<string, unknown> | null;
-
- if (prettierConfigOverride !== undefined) {
- prettierConfig = prettierConfigOverride;
- } else {
- const configFilePath = await resolveConfigFile();
- if (!configFilePath) {
- return {
- output: finalFile,
- formatted: false,
- nodeLabel,
- logLine,
- };
- }
-
- prettierConfig = await resolveConfig(configFilePath);
- }
-
- if (!prettierConfig) {
- return {
- output: finalFile,
- formatted: false,
- nodeLabel,
- logLine,
- };
- }
-
- const prettified = await format(finalFile, {
- ...prettierConfig,
- filepath: 'test.tsx',
- plugins: [],
- endOfLine: 'lf',
+ const {output, formatted} = await formatFileContent({
+ input: finalFile,
+ prettierConfigOverride,
});
return {
- output: prettified,
- formatted: true,
+ output,
+ formatted,
nodeLabel,
logLine,
};
diff --git a/packages/studio-server/src/codemods/format-file-content.ts b/packages/studio-server/src/codemods/format-file-content.ts
new file mode 100644
index 0000000000..2df36247bc
--- /dev/null
+++ b/packages/studio-server/src/codemods/format-file-content.ts
@@ -0,0 +1,58 @@
+// eslint-disable-next-line @typescript-eslint/consistent-type-imports
+type PrettierType = typeof import('prettier');
+
+export const formatFileContent = async ({
+ input,
+ prettierConfigOverride,
+}: {
+ input: string;
+ prettierConfigOverride?: Record<string, unknown> | null;
+}): Promise<{output: string; formatted: boolean}> => {
+ let prettier: PrettierType | null = null;
+
+ try {
+ prettier = await import('prettier');
+ } catch {
+ return {
+ output: input,
+ formatted: false,
+ };
+ }
+
+ const {format, resolveConfig, resolveConfigFile} = prettier as PrettierType;
+
+ let prettierConfig: Record<string, unknown> | null;
+
+ if (prettierConfigOverride !== undefined) {
+ prettierConfig = prettierConfigOverride;
+ } else {
+ const configFilePath = await resolveConfigFile();
+ if (!configFilePath) {
+ return {
+ output: input,
+ formatted: false,
+ };
+ }
+
+ prettierConfig = await resolveConfig(configFilePath);
+ }
+
+ if (!prettierConfig) {
+ return {
+ output: input,
+ formatted: false,
+ };
+ }
+
+ const prettified = await format(input, {
+ ...prettierConfig,
+ filepath: 'test.tsx',
+ plugins: [],
+ endOfLine: 'lf',
+ });
+
+ return {
+ output: prettified,
+ formatted: true,
+ };
+};
diff --git a/packages/studio-server/src/codemods/update-sequence-props.ts b/packages/studio-server/src/codemods/update-sequence-props.ts
index 5c6698cc50..b0c3c97b1c 100644
--- a/packages/studio-server/src/codemods/update-sequence-props.ts
+++ b/packages/studio-server/src/codemods/update-sequence-props.ts
@@ -9,6 +9,7 @@ import type {
import type {SequenceNodePath} from '@remotion/studio-shared';
import * as recast from 'recast';
import {findJsxElementAtNodePath} from '../preview-server/routes/can-update-sequence-props';
+import {formatFileContent} from './format-file-content';
import {parseAst, serializeAst} from './parse-ast';
import {parseValueExpression, updateNestedProp} from './update-nested-prop';
@@ -126,62 +127,15 @@ export const updateSequenceProps = async ({
}
const finalFile = serializeAst(ast);
-
- // eslint-disable-next-line @typescript-eslint/consistent-type-imports
- type PrettierType = typeof import('prettier');
- let prettier: PrettierType | null = null;
-
- try {
- prettier = await import('prettier');
- } catch {
- return {
- output: finalFile,
- oldValueString,
- formatted: false,
- logLine,
- };
- }
-
- const {format, resolveConfig, resolveConfigFile} = prettier as PrettierType;
-
- let prettierConfig: Record<string, unknown> | null;
-
- if (prettierConfigOverride !== undefined) {
- prettierConfig = prettierConfigOverride;
- } else {
- const configFilePath = await resolveConfigFile();
- if (!configFilePath) {
- return {
- output: finalFile,
- oldValueString,
- formatted: false,
- logLine,
- };
- }
-
- prettierConfig = await resolveConfig(configFilePath);
- }
-
- if (!prettierConfig) {
- return {
- output: finalFile,
- oldValueString,
- formatted: false,
- logLine,
- };
- }
-
- const prettified = await format(finalFile, {
- ...prettierConfig,
- filepath: 'test.tsx',
- plugins: [],
- endOfLine: 'lf',
+ const {output, formatted} = await formatFileContent({
+ input: finalFile,
+ prettierConfigOverride,
});
return {
- output: prettified,
+ output,
oldValueString,
- formatted: true,
+ formatted,
logLine,
};
};