Skip to content

Commit

Permalink
feat(upgrade): add codemod, rewrite imports to @carbon/react (#10523)
Browse files Browse the repository at this point in the history
* wip: augment updates to include migrations, add prompts, run migrate functions

* wip(cli): add separate migrate command, run all migrations during upgrade

* wip: add migrate command file

* fix(cli): await migrate calls, use flatMap for gathering migrations

* chore(project): move transforms from codemods package to upgrade package

* chore(project): remove codemods directory, update transforms arch doc

* chore: update yarn.lock removing codemods package

* feat(upgrade): add imports-to-unified-package migration, associated runner updates

* feat(upgrade): add imports-to-unified-package migration, associated runner updates

* chore(upgrade): ignore future updates to test file in sample project

* fix(upgrade): add transformDir argument to migrate() call

* chore(upgrade): don't hoist dependencies all the way up to the monorepo

* chore(upgrade): minor code cleanup

* chore(project): run yarn dedupe

* fix(upgrade): remove prompt for migrations, clean up, review feedback

* fix(upgrade): use execa to invoke jscodeshift, gather jscodeshift bin directly

* chore(project): run yarn dedupe

* test(codemod): imports-to-unified-package tests

* feat(upgrade): provide codemod to rewrite imports to '@carbon/react'

* chore: re-add execa as a dep

* test(upgrade): handle syntax/formatting changes from output, correct filename

* Update packages/upgrade/transforms/imports-to-unified-package.js

Co-authored-by: Josh Black <josh@josh.black>

* Update packages/upgrade/transforms/imports-to-unified-package.js

Co-authored-by: Josh Black <josh@josh.black>

* Update packages/upgrade/transforms/imports-to-unified-package.js

Co-authored-by: Josh Black <josh@josh.black>

* fix(upgrade): rename import rewrite codemod

* fix(upgrade): rename import rewrite codemod

* fix(upgrade): clarify code comment

* fix(upgrade): correct replace syntax method

Co-authored-by: Josh Black <josh@josh.black>
Co-authored-by: Abbey Hart <abbeyhrt@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
4 people committed Jan 28, 2022
1 parent de76f3a commit 98d46e1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* DO NOT COMMIT CHANGES TO THIS FILE.
* THIS FILE IS INTENDED TO BE MODIFIED LOCALLY
* BY A MIGRATION(CODEMOD) VIA THE CLI AS AN EXAMPLE.
*/

/* eslint-disable */
import { Button } from 'carbon-components-react';
import { Tile, Tooltip } from 'carbon-components-react';
import CodeSnippet from 'carbon-components-react';
import Something from 'somewhere-else';
/* eslint-enable */
36 changes: 17 additions & 19 deletions packages/upgrade/src/upgrades.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,23 @@ export const upgrades = [
TRANSFORM_DIR,
'icons-react-size-prop.js'
);
console.log(options);
await run({
transform: transform,
paths: options.workspaceDir,
dry: !options.write,
...options,
});
},
},
{
name: 'update-carbon-components-react-import-to-scoped',
description:
'Rewrites imports from `carbon-components-react` to `@carbon/react`',
migrate: async (options) => {
const transform = path.join(
TRANSFORM_DIR,
'update-carbon-components-react-import-to-scoped.js'
);
await run({
transform: transform,
paths: options.workspaceDir,
Expand Down Expand Up @@ -181,23 +197,5 @@ export const upgrades = [
],
},
],
migrations: [
{
name: 'migration 1',
description: 'migration 1',
migrate: async (options) => {
console.log(`migration 1 function`);
console.log(options);
},
},
{
name: 'migration 2',
description: 'migration 2',
migrate: async (options) => {
console.log(`migration 2 function`);
console.log(options);
},
},
],
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//prettier-ignore
import { Button } from 'carbon-components-react';
//prettier-ignore
import { Tile, Tooltip } from 'carbon-components-react';
//prettier-ignore
import CodeSnippet from 'carbon-components-react';
import Something from 'somewhere-else';
import { SomethingElse } from 'somewhere-else';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//prettier-ignore
import { Button } from "@carbon/react";
//prettier-ignore
import { Tile, Tooltip } from "@carbon/react";
//prettier-ignore
import CodeSnippet from "@carbon/react";
import Something from 'somewhere-else';
import { SomethingElse } from 'somewhere-else';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright IBM Corp. 2016, 2020
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const { defineTest } = require('jscodeshift/dist/testUtils');

defineTest(__dirname, 'update-carbon-components-react-import-to-scoped');
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright IBM Corp. 2016, 2020
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*
* Rewrites imports from 'carbon-components-react' to '@carbon/react'
*
* Transforms:
*
* import { Button } from 'carbon-components-react';
*
* Into:
*
* import { Button } from "@carbon/react";
*/

export const parser = 'babel';

export default function transformer(file, api) {
const j = api.jscodeshift;

return j(file.source)
.find(j.ImportDeclaration, {
source: {
value: 'carbon-components-react',
},
})
.forEach((path) => {
path.get('source').replace(j.stringLiteral('@carbon/react'));
})
.toSource();
}

0 comments on commit 98d46e1

Please sign in to comment.