Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(upgrade): add package.json update flow to cli #10388

Merged
Merged
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ packages/components/src/globals/scss/vendor/**

# Upgrade
**/__testfixtures__/**
packages/upgrade/cli.js

# React
**/storybook-static/**
Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ packages/icons-react/next/**

# Nextjs
.next

# Upgrade
packages/upgrade/cli.js
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion config/jest-config-carbon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
'<rootDir>/**/*-(spec|test).js?(x)',
],
transform: {
'^.+\\.(js|jsx)$': require.resolve('./transform/jsTransform.js'),
'^.+\\.(mjs|cjs|js|jsx)$': require.resolve('./transform/jsTransform.js'),
joshblack marked this conversation as resolved.
Show resolved Hide resolved
'^.+\\.s?css$': require.resolve('./transform/cssTransform.js'),
'^(?!.*\\.(js|jsx|css|json)$)': require.resolve(
'./transform/fileTransform.js'
Expand Down
2 changes: 2 additions & 0 deletions packages/upgrade/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by `esbuild` using `yarn build`
/cli.js
30 changes: 13 additions & 17 deletions packages/upgrade/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @carbon/upgrade

> A tool for upgrading Carbon versions
> A tool for upgrading projects that use Carbon

## Getting started

Expand All @@ -24,28 +24,24 @@ You can install `@carbon/upgrade` in your project, or use a tool like
[`npx`](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b)
by running the following command in your project:

# Runs the command in "dry" mode, which means no files are altered.
# To update the files, re-run the command without the `-d` flag.
npx @carbon/upgrade -d
```bash
npx @carbon/upgrade -d
```

Below is a full output of the options and commands available:

```bash
Usage: carbon-upgrade [options]

Commands:
carbon-upgrade run to upgrade your project[default]
carbon-upgrade migrate <package> <from> run a specific migration for a
<to> package
Usage: @carbon/upgrade [options]

Options:
--help Show help [boolean]
--version Show version number [boolean]
--verbose display the full output while running a command [default: false]
--dry, -d view the result of running this command without changing any
files [default: false]
--ignore, -i provide a glob pattern for directories you would like ignored
[default: ""]
--help Show help [boolean]
--version Show version number [boolean]
--force force execution if the cli encounters an error while doing
safety checks [boolean] [default: false]
-w, --write update the files with changes found by running the migration
[default: false]
-v, --verbose optionally include additional logs, useful for debugging
[boolean] [default: false]
```

## 🙌 Contributing
Expand Down
46 changes: 46 additions & 0 deletions packages/upgrade/__mocks__/inquirer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright IBM Corp. 2019, 2019
*
* 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 mockAnswers = new Map();

const prompt = jest.fn().mockImplementation(async (questions) => {
return questions
.map((question) => {
const answer = mockAnswers.get(question.name);
if (!answer) {
throw new Error(
`Invalid mock usage for \`inquirer\`. Expected an answer to be ` +
`mocked before prompt was called for question \`${question.name}\``
);
}
mockAnswers.delete(question.name);
return {
key: question.name,
value: answer,
};
})
.reduce((acc, { key, value }) => {
return {
...acc,
[key]: value,
};
}, {});
});

function mockAnswer(key, value) {
mockAnswers.set(key, value);
return {
mockAnswers,
};
}

module.exports = {
prompt,
mockAnswer,
};
12 changes: 4 additions & 8 deletions packages/upgrade/bin/carbon-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@ process.on('unhandledRejection', (error) => {
console.error(error);
});

var chalk = require('chalk');

var currentNodeVersion = process.versions.node;
var semver = currentNodeVersion.split('.');
var major = semver[0];

if (major < 14) {
console.error(
chalk.red(
`You are running Node ${currentNodeVersion}.\n` +
`carbon-upgrade requires Node 14 or higher, please update your ` +
`version of Node.`
)
`You are running Node ${currentNodeVersion}.\n` +
`carbon-upgrade requires Node 14 or higher, please update your ` +
`version of Node.`
);
process.exit(1);
}

var main = require('../src/cli');
var { main } = require('../cli');

main(process).catch((error) => {
console.error(error);
Expand Down
8 changes: 8 additions & 0 deletions packages/upgrade/fixtures/sample-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "sample-project",
"dependencies": {
"carbon-components": "^10.49.0",
"carbon-components-react": "^7.49.0",
"carbon-icons": "^7.0.7"
}
}
17 changes: 12 additions & 5 deletions packages/upgrade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"bugs": "https://github.com/carbon-design-system/carbon/issues",
"files": [
"bin",
"src"
"cli.js"
],
"keywords": [
"carbon",
Expand All @@ -29,10 +29,17 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"scripts": {
"build": "esbuild src/cli.js --bundle --platform=node --outfile=cli.js --target=node14",
"clean": "rimraf cli.js",
"watch": "yarn build --watch"
},
"devDependencies": {
"chalk": "^4.1.1",
"change-case": "^4.1.2",
"cross-spawn": "^7.0.3",
"esbuild": "^0.14.10",
"execa": "^5.1.1",
"fast-glob": "^3.2.7",
"fs-extra": "^10.0.0",
"inquirer": "^8.1.0",
Expand All @@ -41,11 +48,11 @@
"jscodeshift": "^0.13.0",
"lodash.clonedeep": "^4.5.0",
"lodash.merge": "^4.6.2",
"memfs": "^3.4.0",
"nanoid": "^3.1.30",
"npm-which": "^3.0.1",
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"yargs": "^17.0.1"
},
"devDependencies": {
"memfs": "^3.4.0"
}
}
19 changes: 19 additions & 0 deletions packages/upgrade/src/__mocks__/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright IBM Corp. 2019, 2019
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

const logger = {
setLevel: jest.fn(),
log: jest.fn(),
};

const levels = ['error', 'warn', 'info', 'verbose', 'debug', 'silly'];

for (const level of levels) {
logger[level] = jest.fn();
}

export { logger };
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ describe('Workspace', () => {
directory
)
);
const workspace = await Workspace.load(directory);

expect(
Array.from(workspace.getWorkspaces()).map((w) => w.directory)
).toEqual(['/test']);

await expect(Workspace.load(directory)).resolves.toBeInstanceOf(Workspace);
});

Expand Down Expand Up @@ -82,7 +76,6 @@ describe('Workspace', () => {
expect(
Array.from(workspace.getWorkspaces()).map((w) => w.directory)
).toEqual([
'/test',
'/test/packages/a',
'/test/packages/b',
'/test/packages/c',
Expand Down
Loading