-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathuninstall.js
More file actions
156 lines (144 loc) · 6.39 KB
/
uninstall.js
File metadata and controls
156 lines (144 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import chalk from 'chalk'
import Project from '../Project.js'
import Target from '../Target.js'
import { eachOfLimitProgress } from '../../util/promises.js'
import { createPromptTask } from '../../util/createPromptTask.js'
import { errorPrinter, packageNamePrinter } from './print.js'
import { intersection } from 'lodash-es'
import path from 'path'
import { uninstall as npmUninstall } from './npm.js'
export default async function uninstall ({
plugins,
isInteractive = true,
cwd = process.cwd(),
logger = null
}) {
cwd = path.resolve(process.cwd(), cwd)
const project = new Project({ cwd, logger })
project.tryThrowInvalidPath()
logger?.log(chalk.cyan('uninstalling adapt dependencies...'))
const targets = await getUninstallTargets({ logger, project, plugins, isInteractive })
if (!targets?.length) return targets
await loadPluginData({ logger, targets })
const uninstallTargetsToBeUninstalled = targets.filter(target => target.isToBeUninstalled)
await eachOfLimitProgress(
uninstallTargetsToBeUninstalled,
target => target.uninstall(),
percentage => logger?.logProgress?.(`${chalk.bold.cyan('<info>')} Uninstalling plugins ${percentage / (project.isNPM ? 2 : 1)}% complete`)
)
if (project.isNPM) {
// Batch install npm plugins as it's faster
const installArgs = uninstallTargetsToBeUninstalled
.filter(target => target.isNPMUninstall)
.map(target => `${target.packageName}`)
const outputPath = path.join(cwd, 'src')
await npmUninstall({ logger, cwd: outputPath, args: installArgs })
await eachOfLimitProgress(
uninstallTargetsToBeUninstalled,
target => target.postUninstall(),
percentage => logger?.logProgress?.(`${chalk.bold.cyan('<info>')} Uninstalling plugins ${50 + (percentage / 2)}% complete`)
)
}
logger?.log(`${chalk.bold.cyan('<info>')} Uninstalling plugins 100% complete`)
const installedDependencies = await project.getInstalledDependencies()
await updateManifest({ project, targets, installedDependencies, isInteractive })
await summariseUninstallation({ logger, targets })
return targets
}
/**
* @param {Object} options
* @param {Project} options.project
* @param {[Target]} options.targets
*/
async function getUninstallTargets ({ logger, project, plugins, isInteractive }) {
if (typeof plugins === 'string') plugins = [plugins]
/** whether adapt.json is being used to compile the list of targets to install */
const isEmpty = !plugins?.length
if (isEmpty && isInteractive) {
const shouldContinue = await createPromptTask({
message: chalk.reset('This command will attempt to uninstall all installed plugins. Do you wish to continue?'),
type: 'confirm'
})
if (!shouldContinue) return
}
/** a list of plugin name/version pairs */
const itinerary = isEmpty
? await project.getInstalledDependencies()
: plugins.reduce((itinerary, arg) => {
const [name, version = '*'] = arg.split(/[#@]/)
// Duplicates are removed by assigning to object properties
itinerary[name] = version
return itinerary
}, {})
const pluginNames = Object.entries(itinerary).map(([name, version]) => `${name}@${version}`)
/** @type {[Target]} */
const targets = pluginNames
? pluginNames.map(nameVersion => {
const [name] = nameVersion.split(/[#@]/)
return new Target({ name, project, logger })
})
: await project.getUninstallTargets()
return targets
}
/**
* @param {Object} options
* @param {Project} options.project
* @param {[Target]} options.targets
*/
async function loadPluginData ({ logger, targets }) {
await eachOfLimitProgress(
targets,
target => target.fetchProjectInfo(),
percentage => logger?.logProgress?.(`${chalk.bold.cyan('<info>')} Getting plugin info ${percentage}% complete`)
)
logger?.log(`${chalk.bold.cyan('<info>')} Getting plugin info 100% complete`)
await eachOfLimitProgress(
targets,
target => target.markUninstallable(),
percentage => logger?.logProgress?.(`${chalk.bold.cyan('<info>')} Marking uninstallable ${percentage}% complete`)
)
logger?.log(`${chalk.bold.cyan('<info>')} Marking uninstallable 100% complete`)
}
/**
* @param {Object} options
* @param {Project} options.project
* @param {[Target]} options.targets
* @returns
*/
async function updateManifest ({ project, targets, installedDependencies, isInteractive }) {
if (targets.filter(target => target.isToBeUninstalled).length === 0) return
if (intersection(Object.keys(installedDependencies), targets.map(target => target.packageName)).length) return
if (isInteractive) {
const shouldUpdate = await createPromptTask({
message: chalk.white('Update the manifest (adapt.json)?'),
type: 'confirm',
default: true
})
if (!shouldUpdate) return
}
targets.forEach(target => target.isToBeUninstalled && project.remove(target))
}
/**
* @param {Object} options
* @param {[Target]} options.targets
*/
function summariseUninstallation ({ logger, targets }) {
const uninstallSucceeded = targets.filter(target => target.isUninstallSuccessful)
const uninstallSkipped = targets.filter(target => !target.isToBeUninstalled || target.isSkipped)
const uninstallErrored = targets.filter(target => target.isUninstallFailure)
const missing = targets.filter(target => target.isMissing)
const noneUninstalled = (uninstallSucceeded.length === 0)
const allUninstalledSuccessfully = (uninstallErrored.length === 0 && missing.length === 0)
const someUninstalledSuccessfully = (!noneUninstalled && !allUninstalledSuccessfully)
summarise(logger, uninstallSkipped, packageNamePrinter, 'The following plugins were skipped:')
summarise(logger, missing, packageNamePrinter, 'There was a problem locating the following plugins:')
summarise(logger, uninstallErrored, errorPrinter, 'The following plugins could not be uninstalled:')
if (noneUninstalled) logger?.log(chalk.cyanBright('None of the requested plugins could be uninstalled'))
else if (allUninstalledSuccessfully) summarise(logger, uninstallSucceeded, packageNamePrinter, 'All requested plugins were successfully uninstalled. Summary of uninstallation:')
else if (someUninstalledSuccessfully) summarise(logger, uninstallSucceeded, packageNamePrinter, 'The following plugins were successfully uninstalled:')
}
function summarise (logger, list, iterator, header) {
if (!list || !iterator || list.length === 0) return
logger?.log(chalk.cyanBright(header))
list.forEach(item => iterator(item, logger))
}