Skip to content

Commit

Permalink
feat(upgrade): add -w write option to upgrade command (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
kabir276 authored Jun 16, 2024
1 parent 18b5e5d commit 39804e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ nextui upgrade [components...] [options]
- `-p --packagePath` [string] The path to the package.json file
- `-a --all` [boolean] Upgrade all the NextUI components (default: `false`)
- `-w --write` [boolean] Write the upgrade commands to a file instead of executing them (default: `false`)
- `-h, --help` Display help for command
##### Example
Expand Down
37 changes: 29 additions & 8 deletions src/actions/upgrade-action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type {AppendKeyValue} from '@helpers/type';

import fs from 'node:fs';

import {checkIllegalComponents} from '@helpers/check';
import {detect} from '@helpers/detect';
import {exec} from '@helpers/exec';
Expand All @@ -22,14 +24,15 @@ interface UpgradeActionOptions {
major?: boolean;
minor?: boolean;
patch?: boolean;
write?: boolean;
}

type TransformComponent = Required<
AppendKeyValue<NextUIComponents[0], 'latestVersion', string> & {isLatest: boolean}
>;

export async function upgradeAction(components: string[], options: UpgradeActionOptions) {
const {all = false, packagePath = resolver('package.json')} = options;
const {all = false, packagePath = resolver('package.json'), write = false} = options;
const {allDependencies, currentComponents} = getPackageInfo(packagePath, false);

const isNextUIAll = !!allDependencies[NEXT_UI];
Expand Down Expand Up @@ -176,13 +179,31 @@ export async function upgradeAction(components: string[], options: UpgradeAction
return !ignoreList.some((ignore) => r.package === ignore);
});

await exec(
`${packageManager} ${install} ${result.reduce((acc, component, index) => {
return `${acc}${index === 0 ? '' : ' '}${
component.package
}@${component.latestVersion.replace(colorMatchRegex, '')}`;
}, '')}`
);
if (write) {
// Write the upgrade versions to the package file
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));

result.forEach((component) => {
packageJson.dependencies[component.package] = component.latestVersion.replace(
colorMatchRegex,
''
);
});

fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2));

Logger.newLine();
Logger.success('✅ Upgrade versions written to package.json');
process.exit(0);
} else {
await exec(
`${packageManager} ${install} ${result.reduce((acc, component, index) => {
return `${acc}${index === 0 ? '' : ' '}${
component.package
}@${component.latestVersion.replace(colorMatchRegex, '')}`;
}, '')}`
);
}
}

/** ======================== Setup Pnpm ======================== */
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ nextui
.argument('[components...]', 'Names of components to upgrade')
.option('-p --packagePath [string]', 'Specify the path to the package.json file')
.option('-a --all [boolean]', 'Upgrade all components', false)
.option(
'-w --write [boolean]',
'Write the upgrade versions to the package.json file without installing',
false
)
.action(upgradeAction);

nextui
Expand Down

0 comments on commit 39804e0

Please sign in to comment.