Skip to content
Open
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
29 changes: 26 additions & 3 deletions packages/gamut-codemods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@ Codemods for moving a repo onto `@skillsoft/gamut` and between its releases.

Each preset is one upgrade. Run them in order if you're more than one behind.

| Preset | What it's for |
| ------------ | ------------------------------------------------------- |
| `scope-swap` | Moving from `@codecademy/gamut*` to `@skillsoft/gamut*` |
| Preset | What it's for |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `scope-swap` | Moving from `@codecademy/gamut*` to `@skillsoft/gamut*` |
| `size-and-alignment` | Size values move to `sm` / `md` / `lg`, Select's `sizeVariant` becomes `size`, and left / right alignment values become `start` / `end` |

`npx @skillsoft/gamut-codemods list` shows every preset and the migrations inside it.

## Running size-and-alignment

Run it on code that already imports `@skillsoft/gamut`. Consumers still on `@codecademy/gamut` run `scope-swap` first.

```sh
# Preview without writing anything
npx @skillsoft/gamut-codemods size-and-alignment . --dry

# Run it
npx @skillsoft/gamut-codemods size-and-alignment .
```

It rewrites JSX attributes on the components listed in `src/presets/size-and-alignment/manifest.ts`, and only when the component is imported from `@skillsoft/gamut`. A local component with the same name, or the same prop on a component that isn't listed (Modal's `size`, for example), is left alone. Aliased imports work.

- **Sizes:** `size` on Button, Input, Toggle, SelectDropdown, ProgressBar, Badge, and DatePicker's `inputSize`, plus `labelSize` on FormGroup and ConnectedFormGroup, and `size` on FormGroupLabel. Old values map to the new scale.
- **Select:** `sizeVariant` is renamed to `size`, with `small` → `sm` and `base` → `md`.
- **Alignment:** `left` / `right` become `start` / `end` for `iconPosition`, `buttonPlacement`, `alignContentContainer`, `openFrom`, `menuAlignment`, Popover `align` and `beak`, Coachmark `beak`, and GridForm `layout`. Tip and PopoverContainer `alignment` values like `top-left` and `left-center` map to their `start` / `end` forms.

Only string literals are converted, as `prop="old"` or `prop={'old'}`. Any other expression for a prop that has values to convert gets a warning, so check it by hand.

Not covered, so check these by hand: values in object literals (such as DataList column `justify`), `textAlign` on `Text`, `position` on GridForm fields, and any prop read from a variable.

## Running scope-swap

Start from a clean working tree. The codemod won't write into uncommitted changes (pass `--force` to override), so `git checkout .` always undoes a run.
Expand Down
2 changes: 1 addition & 1 deletion packages/gamut-codemods/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ describe('main: full run', () => {
expect(out).toMatch(/Video needs two peer dependencies/);
expect(out).toMatch(/Some tsconfig files set "lib"/);
expect(out).toMatch(
/\[moved-exports\] .*src\/index\.ts:1 .*no longer re-exports Video/
/src\/index\.ts\n\s+:1 \[moved-exports\] .*no longer re-exports Video/
);
expect(out).toMatch(
/\[moved-exports\] .*reexport\.mdx .*no longer re-exports Video/
Expand Down
34 changes: 34 additions & 0 deletions packages/gamut-codemods/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from './lib/run-source';
import type { FileMigration, Preset } from './lib/types';
import { getPreset, presets } from './presets';
import type { RecordedWarning } from './transform';

/* No published types for the Runner; it's jscodeshift's programmatic API. */
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -125,6 +126,32 @@ const runFileMigrations = (
return changed;
};

/* Prints the warnings each worker recorded, grouped by file and sorted by line. */
const printWarnings = (dir: string) => {
const files = fs.readdirSync(dir).map((key) => ({
file: decodeURIComponent(key),
warnings: fs
.readFileSync(path.join(dir, key), 'utf8')
.split('\n')
.filter(Boolean)
.map((line) => JSON.parse(line) as RecordedWarning),
}));
const total = files.reduce((sum, f) => sum + f.warnings.length, 0);
heading(`warnings (${total})`);
files
.sort((a, b) => a.file.localeCompare(b.file))
.forEach(({ file, warnings }) => {
console.log(file);
warnings
.sort((a, b) => (a.line ?? 0) - (b.line ?? 0))
.forEach((w) =>
console.log(
` ${w.line ? `:${w.line} ` : ''}[${w.migration}] ${w.message}`
)
);
});
};

const printChecklist = (preset: Preset, notes: Set<string>) => {
heading('next steps');
const items = [
Expand Down Expand Up @@ -181,6 +208,9 @@ export const main = async (argv: string[]) => {
opts.skip = skipped.join(',') || undefined;

const notesDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gamut-codemods-'));
const warningsDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'gamut-codemods-warnings-')
);
try {
heading('source files');
const result = await Runner.run(
Expand All @@ -191,6 +221,7 @@ export const main = async (argv: string[]) => {
only: opts.only,
skip: opts.skip,
notesDir,
warningsDir,
dry: opts.dry,
extensions: 'ts,tsx,js,jsx,mjs,cjs,mts,cts',
/* .d.ts stays in: Emotion theme augmentations import gamut-styles. */
Expand All @@ -205,6 +236,8 @@ export const main = async (argv: string[]) => {
notes.add(decodeURIComponent(key))
);

printWarnings(warningsDir);

heading('package.json and .eslintrc');
const changed = runFileMigrations(preset, opts, (key) => notes.add(key));
console.log(`${changed} file(s) ${opts.dry ? 'would change' : 'changed'}`);
Expand All @@ -231,5 +264,6 @@ export const main = async (argv: string[]) => {
return result.error > 0 ? 1 : 0;
} finally {
fs.rmSync(notesDir, { recursive: true, force: true });
fs.rmSync(warningsDir, { recursive: true, force: true });
}
};
19 changes: 19 additions & 0 deletions packages/gamut-codemods/src/lib/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,28 @@ export interface DeepImport {
note?: string;
}

export interface PropChange {
/* The prop's new name, when the prop itself was renamed. */
rename?: string;
/* Old string value -> new string value. Other values are left alone. */
values?: Record<string, string>;
}

export interface ComponentPropChanges {
/*
The package whose components are matched. This is the NEW name, since
these rules run on code that already imports @skillsoft/gamut.
*/
package: string;
/* Component export name -> prop name -> change. */
components: Record<string, Record<string, PropChange>>;
}

export interface Manifest {
/* Old package -> new package. */
packages: Record<string, PackageRename>;
/* Prop renames and value changes on specific components, by name. */
componentProps?: ComponentPropChanges;
/* Packages with no replacement under the new name. */
removedPackages: Record<string, RemovedPackage>;
/* Version range written to package.json, keyed by NEW package name. */
Expand Down
1 change: 1 addition & 0 deletions packages/gamut-codemods/src/lib/run-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const mentionsOldNames = (manifest: Manifest, source: string) =>
...Object.keys(manifest.packages),
...Object.keys(manifest.removedPackages),
`${manifest.eslintPlugin.from}/`,
...(manifest.componentProps ? [manifest.componentProps.package] : []),
].some((name) => source.includes(name));

/* `only` and `skip` are comma-separated migration names. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Alert, InfoTip, Popover, PopoverContainer } from '@skillsoft/gamut';

export const Placement = () => (
<>
<Alert iconPosition="left">Heads up</Alert>
<Popover align="right" beak="left" />
<PopoverContainer alignment="top-left" />
<InfoTip alignment="left-center" info="More" />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Alert, InfoTip, Popover, PopoverContainer } from '@skillsoft/gamut';

export const Placement = () => (
<>
<Alert iconPosition="start">Heads up</Alert>
<Popover align="end" beak="start" />
<PopoverContainer alignment="top-start" />
<InfoTip alignment="start-center" info="More" />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FillButton } from '@skillsoft/gamut';

const textSize = 'normal';

export const Declared = () => <FillButton size={textSize}>Go</FillButton>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FillButton } from '@skillsoft/gamut';

const textSize = 'normal';

export const Declared = () => <FillButton size={textSize}>Go</FillButton>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["FillButton size is not a string literal", "textSize is declared at line 3"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FillButton } from '@skillsoft/gamut';

export const Dynamic = ({ size }: { size: string }) => (
<FillButton size={size}>Go</FillButton>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FillButton } from '@skillsoft/gamut';

export const Dynamic = ({ size }: { size: string }) => (
<FillButton size={size}>Go</FillButton>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["FillButton size is not a string literal"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FillButton } from '@skillsoft/gamut';

type Props = { size?: 'small' | 'normal' };

export const Thing = ({ size = 'normal' }: Props) => (
<FillButton size={size}>Go</FillButton>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FillButton } from '@skillsoft/gamut';

type Props = { size?: 'sm' | 'md' };

export const Thing = ({ size = 'md' }: Props) => (
<FillButton size={size}>Go</FillButton>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Badge as CodecademyBadge } from '@codecademy/gamut';
import { FillButton as Btn, Modal } from '@skillsoft/gamut';

const Button = (props: { size: string }) => <span {...props} />;

export const Scoped = () => (
<>
<Btn size="small">Aliased</Btn>
<Button size="small" />
<Modal size="small" />
<CodecademyBadge size="base" />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Badge as CodecademyBadge } from '@codecademy/gamut';
import { FillButton as Btn, Modal } from '@skillsoft/gamut';

const Button = (props: { size: string }) => <span {...props} />;

export const Scoped = () => (
<>
<Btn size="sm">Aliased</Btn>
<Button size="small" />
<Modal size="small" />
<CodecademyBadge size="base" />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Select } from '@skillsoft/gamut';

export const Choices = () => (
<>
<Select sizeVariant="base" options={['a']} />
<Select sizeVariant="small" options={['a']} />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Select } from '@skillsoft/gamut';

export const Choices = () => (
<>
<Select size="md" options={['a']} />
<Select size="sm" options={['a']} />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Badge, FillButton, Input, ProgressBar } from '@skillsoft/gamut';

export const Sizes = () => (
<>
<FillButton size="normal">Normal</FillButton>
<FillButton size='small'>Small</FillButton>
<FillButton size={'large'}>Large</FillButton>
<Input size="base" />
<Input size="small" />
<Badge size="base">Label</Badge>
<ProgressBar size="medium" variant="blue" percent={40} />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Badge, FillButton, Input, ProgressBar } from '@skillsoft/gamut';

export const Sizes = () => (
<>
<FillButton size="md">Normal</FillButton>
<FillButton size='sm'>Small</FillButton>
<FillButton size={'lg'}>Large</FillButton>
<Input size="md" />
<Input size="sm" />
<Badge size="md">Label</Badge>
<ProgressBar size="md" variant="blue" percent={40} />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FillButton, TextButton } from '@skillsoft/gamut';

export const Ternary = ({ mobile, size }: { mobile: boolean; size: string }) => (
<>
<TextButton size={mobile ? 'small' : 'normal'}>Go</TextButton>
<FillButton size={mobile ? 'small' : size}>Go</FillButton>
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FillButton, TextButton } from '@skillsoft/gamut';

export const Ternary = ({ mobile, size }: { mobile: boolean; size: string }) => (
<>
<TextButton size={mobile ? 'sm' : 'md'}>Go</TextButton>
<FillButton size={mobile ? 'sm' : size}>Go</FillButton>
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["FillButton size has a branch that is not a string literal", "size is declared at line 3"]
Loading
Loading