-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reset --reinstall --hard (#777)
* feat: reset --reinstall --hard * chore: code review * fix: show linked plugin version * chore: missing arrow, reinstall 'hearder' --------- Co-authored-by: Eric Willhoit <[email protected]>
- Loading branch information
1 parent
fcb046f
commit 27504a2
Showing
2 changed files
with
77 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,89 @@ | ||
import {Command} from '@oclif/core' | ||
/* eslint-disable no-await-in-loop */ | ||
import {Command, Flags} from '@oclif/core' | ||
import chalk from 'chalk' | ||
import {rm} from 'node:fs/promises' | ||
import {join} from 'node:path' | ||
|
||
import Plugins from '../../plugins.js' | ||
|
||
export default class Reset extends Command { | ||
static flags = { | ||
hard: Flags.boolean({ | ||
summary: 'Delete node_modules and package manager related files in addition to uninstalling plugins.', | ||
}), | ||
reinstall: Flags.boolean({ | ||
summary: 'Reinstall all plugins after uninstalling.', | ||
}), | ||
} | ||
|
||
static summary = 'Remove all user-installed and linked plugins.' | ||
|
||
async run(): Promise<void> { | ||
const {flags} = await this.parse(Reset) | ||
const plugins = new Plugins(this.config) | ||
const userPlugins = await plugins.list() | ||
|
||
this.log(`Uninstalling ${userPlugins.length} plugin${userPlugins.length === 0 ? '' : 's'}`) | ||
this.log(`Found ${userPlugins.length} plugin${userPlugins.length === 0 ? '' : 's'}:`) | ||
for (const plugin of userPlugins) { | ||
this.log(`• ${plugin.name} ${chalk.dim(`(${plugin.type})`)}`) | ||
this.log( | ||
`- ${plugin.name} ${chalk.dim(this.config.plugins.get(plugin.name)?.version)} ${chalk.dim(`(${plugin.type})`)}`, | ||
) | ||
} | ||
|
||
await Promise.all(userPlugins.map(async (plugin) => plugins.uninstall(plugin.name))) | ||
if (flags.hard) { | ||
const filesToDelete = [ | ||
join(this.config.dataDir, 'node_modules'), | ||
join(this.config.dataDir, 'package.json'), | ||
join(this.config.dataDir, 'yarn.lock'), | ||
join(this.config.dataDir, 'package-lock.json'), | ||
] | ||
|
||
this.log('✅ Removed the following files:') | ||
for (const file of filesToDelete) { | ||
this.log(`- ${file}`) | ||
} | ||
|
||
await Promise.all(filesToDelete.map((file) => rm(file, {force: true, recursive: true}))) | ||
|
||
for (const plugin of userPlugins) { | ||
this.log(`✅ ${plugin.type === 'link' ? 'Unlinked' : 'Uninstalled'} ${plugin.name}`) | ||
} | ||
} else { | ||
// These need to run sequentially so as to avoid write conflicts to the package.json | ||
for (const plugin of userPlugins) { | ||
try { | ||
await plugins.uninstall(plugin.name) | ||
this.log(`✅ ${plugin.type === 'link' ? 'Unlinked' : 'Uninstalled'} ${plugin.name}`) | ||
} catch { | ||
this.warn(`Failed to uninstall ${plugin.name}`) | ||
} | ||
} | ||
} | ||
|
||
if (flags.reinstall) { | ||
this.log('Reinstall flag passed:') | ||
// These need to run sequentially so as to avoid write conflicts to the package.json | ||
for (const plugin of userPlugins) { | ||
if (plugin.type === 'link') { | ||
try { | ||
const newPlugin = await plugins.link(plugin.root, {install: false}) | ||
const newVersion = chalk.dim(`-> ${newPlugin.version}`) | ||
this.log(`✅ Relinked ${plugin.name} ${newVersion}`) | ||
} catch { | ||
this.warn(`Failed to relink ${plugin.name}`) | ||
} | ||
} | ||
|
||
if (plugin.type === 'user') { | ||
try { | ||
const newPlugin = await plugins.install(plugin.name, {tag: plugin.tag}) | ||
const newVersion = chalk.dim(`-> ${newPlugin.version}`) | ||
this.log(`✅ Reinstalled ${plugin.name}@${plugin.tag} ${newVersion}`) | ||
} catch { | ||
this.warn(`Failed to reinstall ${plugin.name}`) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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