diff --git a/packages/upgrade/fixtures/sample-project/example-imports-to-unified-package.js b/packages/upgrade/fixtures/sample-project/example-imports-to-unified-package.js new file mode 100644 index 000000000000..cfaf8f4a0fe8 --- /dev/null +++ b/packages/upgrade/fixtures/sample-project/example-imports-to-unified-package.js @@ -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 */ diff --git a/packages/upgrade/src/upgrades.js b/packages/upgrade/src/upgrades.js index 26aec05bef52..8ac8cd5cf5aa 100644 --- a/packages/upgrade/src/upgrades.js +++ b/packages/upgrade/src/upgrades.js @@ -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, @@ -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); - }, - }, - ], }, ]; diff --git a/packages/upgrade/transforms/__testfixtures__/update-carbon-components-react-import-to-scoped.input.js b/packages/upgrade/transforms/__testfixtures__/update-carbon-components-react-import-to-scoped.input.js new file mode 100644 index 000000000000..e23dfd697817 --- /dev/null +++ b/packages/upgrade/transforms/__testfixtures__/update-carbon-components-react-import-to-scoped.input.js @@ -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'; diff --git a/packages/upgrade/transforms/__testfixtures__/update-carbon-components-react-import-to-scoped.output.js b/packages/upgrade/transforms/__testfixtures__/update-carbon-components-react-import-to-scoped.output.js new file mode 100644 index 000000000000..9e02a2f117b4 --- /dev/null +++ b/packages/upgrade/transforms/__testfixtures__/update-carbon-components-react-import-to-scoped.output.js @@ -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'; diff --git a/packages/upgrade/transforms/__tests__/update-carbon-components-react-import-to-scoped.js b/packages/upgrade/transforms/__tests__/update-carbon-components-react-import-to-scoped.js new file mode 100644 index 000000000000..426838caef51 --- /dev/null +++ b/packages/upgrade/transforms/__tests__/update-carbon-components-react-import-to-scoped.js @@ -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'); diff --git a/packages/upgrade/transforms/update-carbon-components-react-import-to-scoped.js b/packages/upgrade/transforms/update-carbon-components-react-import-to-scoped.js new file mode 100644 index 000000000000..4a9c81de5b20 --- /dev/null +++ b/packages/upgrade/transforms/update-carbon-components-react-import-to-scoped.js @@ -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(); +}