Skip to content
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
15 changes: 15 additions & 0 deletions packages/migrator/docs/CLI_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ npx @coinbase/cds-migrator ./src -t button-variant -t input-size

**Transforms are standalone files** in `src/transforms/` that run independently.

#### `-ps, --package-scope <scope>`

Limit scope-aware transforms (for example `button-variant-values`, `migrate-use-merge-refs`) to imports under a single npm scope. Omit this flag to rewrite every scope (for example both `@coinbase/cds-web` and `@acme/cds-web`).

```bash
# Only rewrite @coinbase/cds-* imports
npx @coinbase/cds-migrator ./src -t button-variant-values --package-scope coinbase

# Shorthand
npx @coinbase/cds-migrator ./src -t button-variant-values -ps coinbase

# Equivalent
npx @coinbase/cds-migrator ./src -t button-variant-values --package-scope @coinbase
```

#### `--clear-history`

Clear migration history for a path.
Expand Down
5 changes: 5 additions & 0 deletions packages/migrator/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
displayName: 'migrator',
preset: '../../jest.preset.js',
testEnvironment: 'node',
};
6 changes: 6 additions & 0 deletions packages/migrator/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"build": {
"command": "rm -rf cjs && babel ./src --out-dir cjs --extensions .ts,.tsx,.js,.jsx --copy-files --no-copy-ignored"
},
"test": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "{projectRoot}/jest.config.mjs"
}
},
"lint": {
"executor": "@nx/eslint:lint"
},
Expand Down
7 changes: 7 additions & 0 deletions packages/migrator/src/cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type CliArgs = {
clearHistory?: boolean;
partial?: boolean;
transform?: string[];
/** When set, only rewrite imports from this npm scope (e.g. coinbase or @coinbase). */
packageScope?: string;
};

export function parseCliArgs(): CliArgs {
Expand All @@ -30,6 +32,10 @@ export function parseCliArgs(): CliArgs {
.option(
'-t, --transform <transforms...>',
'Migrate specific transforms by name (preset auto-detected)',
)
.option(
'-ps, --package-scope <scope>',
'Only rewrite imports from this npm scope (e.g. coinbase or @coinbase). Omit to include every scope',
);

program.parse();
Expand All @@ -45,5 +51,6 @@ export function parseCliArgs(): CliArgs {
clearHistory: options.clearHistory,
partial: options.partial,
transform: options.transform,
packageScope: options.packageScope,
};
}
8 changes: 8 additions & 0 deletions packages/migrator/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,17 @@ async function setupMigration(args: CliArgs) {
console.log('\n📋 Migration Summary:');
console.log(` Path: ${targetPath}`);
console.log(` Mode: ${args.dryRun ? 'Dry Run' : 'Apply Changes'}`);
if (args.packageScope) {
console.log(` Package scope: ${args.packageScope}`);
}
console.log(` Transforms: ${filtered.length}\n`);

return {
preset: 'direct-transform',
path: targetPath,
dryRun: args.dryRun || false,
transformsToRun: filtered,
packageScope: args.packageScope,
};
}

Expand Down Expand Up @@ -294,13 +298,17 @@ async function setupMigration(args: CliArgs) {
console.log(` Preset: ${preset}`);
console.log(` Path: ${targetPath}`);
console.log(` Mode: ${args.dryRun ? 'Dry Run' : 'Apply Changes'}`);
if (args.packageScope) {
console.log(` Package scope: ${args.packageScope}`);
}
console.log(` Transforms: ${filtered.length}\n`);

return {
preset,
path: targetPath,
dryRun: args.dryRun || false,
transformsToRun: filtered,
packageScope: args.packageScope,
};
}

Expand Down
22 changes: 6 additions & 16 deletions packages/migrator/src/presets/v8-to-v9/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@
"preset": "v8-to-v9",
"description": "Migration from CDS v8 to v9",
"transforms": [
{
"name": "input-size-values",
"description": "Update Input size prop values from sm/md/lg to small/medium/large (@coinbase/cds-web)",
"file": "example-transform"
},
{
"name": "use-theme-return-type",
"description": "Update useTheme hook destructured return values (@coinbase/cds-common)",
"file": "example-transform"
},
{
"name": "format-currency-options",
"description": "Update formatCurrency options parameter (@coinbase/cds-utils)",
"file": "example-transform"
},
{
"name": "button-variant-values",
"description": "Remap Button/IconButton variant values: tertiary -> inverse, foregroundMuted -> secondary",
"file": "button-variant-values"
"file": "v9/button-variant-values"
},
{
"name": "migrate-use-merge-refs-import",
"description": "Migrate useMergeRefs: @coinbase/cds-common/hooks/useMergeRefs → utils/mergeRefs, rename binding to mergeRefs, merge duplicate imports",
"file": "v9/migrate-use-merge-refs"
}
]
}
16 changes: 15 additions & 1 deletion packages/migrator/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fs from 'fs';
import path from 'path';

import { createLogger, recordTransformRun } from './utils/index';
import { normalizePackageScope } from './utils/package-scope';
import type { Transform } from './types';

// In CommonJS, __dirname is automatically available
Expand All @@ -19,10 +20,19 @@ type RunMigrationOptions = {
path: string;
dryRun: boolean;
transformsToRun: Transform[];
/** Forwarded to jscodeshift as `--packageScope` for scope-targeted transforms. */
packageScope?: string;
};

export async function runMigration(options: RunMigrationOptions): Promise<void> {
const { preset, path: targetPath, dryRun, transformsToRun } = options;
const {
preset,
path: targetPath,
dryRun,
transformsToRun,
packageScope: packageScopeRaw,
} = options;
const packageScope = packageScopeRaw ? normalizePackageScope(packageScopeRaw) : undefined;

console.log(`\n🔄 Running migration...\n`);

Expand All @@ -31,6 +41,9 @@ export async function runMigration(options: RunMigrationOptions): Promise<void>
logger.info(`Starting migration${preset ? ` (${preset})` : ''}`);
logger.info(`Target path: ${targetPath}`);
logger.info(`Mode: ${dryRun ? 'Dry Run' : 'Apply Changes'}`);
if (packageScope) {
logger.info(`Package scope: ${packageScope} (only matching imports are rewritten)`);
}

if (transformsToRun.length === 0) {
console.log('ℹ️ No transforms to run.');
Expand Down Expand Up @@ -87,6 +100,7 @@ export async function runMigration(options: RunMigrationOptions): Promise<void>
'--ignore-pattern=**/dist/**',
'--ignore-pattern=**/build/**',
`--transform=${fullTransformPath}`,
...(packageScope ? [`--packageScope=${packageScope}`] : []),
targetPath,
];

Expand Down
21 changes: 21 additions & 0 deletions packages/migrator/src/test-utils/readTransformFixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs';
import path from 'path';

/**
* Read a fixture file from `…/__testfixtures__/<suite>/<relativePath>` relative to a transform’s
* `__tests__` directory (sibling of `__testfixtures__`).
*
* @param testFileDir - `__dirname` from the test file (the `__tests__` folder).
* @param suite - Subfolder under `__testfixtures__` (e.g. `migrate-use-merge-refs`).
* @param relativePath - File name within that folder, including extension (e.g. `basic.input.tsx`).
*/
export function readTransformFixture(
testFileDir: string,
suite: string,
relativePath: string,
): string {
return fs.readFileSync(
path.join(testFileDir, '..', '__testfixtures__', suite, relativePath),
'utf8',
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useMergeRefs } from '@example/cds-common/hooks/useMergeRefs';

export const X = () => {
const ref = useMergeRefs(a, b);
return ref;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { mergeRefs } from "@example/cds-common/utils/mergeRefs";

export const X = () => {
const ref = mergeRefs(a, b);
return ref;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useMergeRefs } from '@coinbase/cds-common/hooks/useMergeRefs';

export const X = () => {
const ref = useMergeRefs(a, b);
return ref;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { mergeRefs } from "@coinbase/cds-common/utils/mergeRefs";

export const X = () => {
const ref = mergeRefs(a, b);
return ref;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { useMergeRefs as combineRefs } from '@coinbase/cds-common/hooks/useMergeRefs';
combineRefs(r1, r2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { mergeRefs as combineRefs } from "@coinbase/cds-common/utils/mergeRefs";
combineRefs(r1, r2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
jest.mock('@coinbase/cds-common/hooks/useMergeRefs');
import { useMergeRefs } from '@coinbase/cds-common/hooks/useMergeRefs';
useMergeRefs(x);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
jest.mock("@coinbase/cds-common/utils/mergeRefs");
import { mergeRefs } from "@coinbase/cds-common/utils/mergeRefs";
mergeRefs(x);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { mergeRefs } from '@coinbase/cds-common/utils/mergeRefs';
import { useMergeRefs } from '@coinbase/cds-common/hooks/useMergeRefs';

const cb = mergeRefs(useMergeRefs(a));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { mergeRefs } from '@coinbase/cds-common/utils/mergeRefs';

const cb = mergeRefs(mergeRefs(a));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import React from 'react';
export const x = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useMergeRefs } from '@coinbase/cds-common/hooks/useMergeRefs';
const o = { useMergeRefs: 1 };
useMergeRefs(r);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { mergeRefs } from "@coinbase/cds-common/utils/mergeRefs";
const o = { useMergeRefs: 1 };
mergeRefs(r);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useMergeRefs } from '@coinbase/cds-common/hooks/useMergeRefs';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mergeRefs } from "@coinbase/cds-common/utils/mergeRefs";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useMergeRefs } from 'some-other-library';

export function f() {
return useMergeRefs(a, b);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import fs from 'fs';
import path from 'path';
import { applyTransform } from 'jscodeshift/src/testUtils';

const { applyTransform } = require('jscodeshift/src/testUtils');
import { readTransformFixture } from '../../../test-utils/readTransformFixture';
import transform from '../button-variant-values';

const transform = require('../button-variant-values');
const FIXTURE_SUITE = 'button-variant-values';

const PARSER_OPTIONS = { parser: 'tsx' };

function applyButtonVariantTransform(source: string) {
return applyTransform(transform, {}, { source }, PARSER_OPTIONS);
function applyButtonVariantTransform(
source: string,
jscodeshiftOptions: Record<string, unknown> = {},
) {
return applyTransform(transform, jscodeshiftOptions, { source }, { parser: 'tsx' });
}

function readFixture(name: string) {
return fs.readFileSync(
path.join(__dirname, '..', '__testfixtures__', 'button-variant-values', `${name}.tsx`),
'utf8',
);
return readTransformFixture(__dirname, FIXTURE_SUITE, `${name}.tsx`);
}

describe('button-variant-values', () => {
Expand All @@ -40,6 +38,24 @@ const App = () => <Button variant="foregroundMuted">Click</Button>;
expect(output).not.toContain('variant="foregroundMuted"');
});

it('rewrites variant="tertiary" to variant="inverse" on Button from a non-@coinbase scope', () => {
const input = `
import { Button } from '@example/cds-web';
const App = () => <Button variant="tertiary">Click</Button>;
`;
const output = applyButtonVariantTransform(input);
expect(output).toContain('variant="inverse"');
expect(output).not.toContain('variant="tertiary"');
});

it('skips non-matching scope when --package-scope is set', () => {
const input = `
import { Button } from '@example/cds-web';
const App = () => <Button variant="tertiary">Click</Button>;
`;
expect(applyButtonVariantTransform(input, { packageScope: '@coinbase' })).toBe('');
});

it('rewrites variant="tertiary" to variant="inverse" on IconButton from @coinbase/cds-mobile', () => {
const input = `
import { IconButton } from '@coinbase/cds-mobile';
Expand Down
Loading
Loading