-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25538 from storybookjs/shlman/add-cli-remove-command
CLI: Add addon `remove` command
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { getStorybookInfo } from '@storybook/core-common'; | ||
import { readConfig, writeConfig } from '@storybook/csf-tools'; | ||
import dedent from 'ts-dedent'; | ||
|
||
import { JsPackageManagerFactory, type PackageManagerName } from './js-package-manager'; | ||
|
||
const logger = console; | ||
|
||
/** | ||
* Remove the given addon package and remove it from main.js | ||
* | ||
* Usage: | ||
* - sb remove @storybook/addon-links | ||
*/ | ||
export async function remove(addon: string, options: { packageManager: PackageManagerName }) { | ||
const { packageManager: pkgMgr } = options; | ||
|
||
const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr }); | ||
const packageJson = await packageManager.retrievePackageJson(); | ||
const { mainConfig, configDir } = getStorybookInfo(packageJson); | ||
|
||
if (typeof configDir === 'undefined') { | ||
throw new Error(dedent` | ||
Unable to find storybook config directory | ||
`); | ||
} | ||
|
||
if (!mainConfig) { | ||
logger.error('Unable to find storybook main.js config'); | ||
return; | ||
} | ||
const main = await readConfig(mainConfig); | ||
|
||
// remove from package.json | ||
logger.log(`Uninstalling ${addon}`); | ||
await packageManager.removeDependencies({ packageJson }, [addon]); | ||
|
||
// add to main.js | ||
logger.log(`Removing '${addon}' from main.js addons field.`); | ||
try { | ||
main.removeEntryFromArray(['addons'], addon); | ||
await writeConfig(main); | ||
} catch (err) { | ||
logger.warn(`Failed to remove '${addon}' from main.js addons field.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters