|
| 1 | +module.exports = { |
| 2 | + name: 'yarn-up-all-plugin', |
| 3 | + factory: require => { |
| 4 | + |
| 5 | + const {Configuration, Project} = require('@yarnpkg/core'); |
| 6 | + const {Cli, Command} = require('clipanion'); |
| 7 | + const yup = require('yup'); |
| 8 | + const Essentials = require('@yarnpkg/plugin-essentials'); |
| 9 | + |
| 10 | + /** |
| 11 | + * Retrieve dependencies and devDependencies |
| 12 | + * @param workspace The current Workspace |
| 13 | + * @returns {*[]} an array of dependencies |
| 14 | + */ |
| 15 | + const getDependencies = (workspace) => { |
| 16 | + const dependencies = workspace.manifest.dependencies; |
| 17 | + const devDependencies = workspace.manifest.devDependencies; |
| 18 | + |
| 19 | + return [...dependencies, ...devDependencies]; |
| 20 | + }; |
| 21 | + |
| 22 | + /** |
| 23 | + * Resolve the name of a package |
| 24 | + * @param scope The scope of the descriptor |
| 25 | + * @param name The name of the descriptor |
| 26 | + * @returns {string|*} The full package name |
| 27 | + */ |
| 28 | + const resolveFullPackageName = (scope, name) => { |
| 29 | + return scope ? `@${scope}/${name}` : name; |
| 30 | + }; |
| 31 | + |
| 32 | + /** |
| 33 | + * Get the descriptor |
| 34 | + * @param dependencies The dependencies |
| 35 | + * @param exclude The list of full package names that should be excluded |
| 36 | + * @returns {*[]} The descriptors |
| 37 | + */ |
| 38 | + const getDescriptors = (dependencies, exclude) => { |
| 39 | + const descriptions = [...dependencies.values()]; |
| 40 | + if (exclude) { |
| 41 | + return descriptions |
| 42 | + .filter(descriptor => { |
| 43 | + const data = resolveFullPackageName(descriptor[1].scope, descriptor[1].name); |
| 44 | + return !exclude.includes(data); |
| 45 | + }); |
| 46 | + } |
| 47 | + return descriptions; |
| 48 | + }; |
| 49 | + |
| 50 | + class UpAllCommand extends Command { |
| 51 | + |
| 52 | + /** |
| 53 | + * Execute the command |
| 54 | + * @returns {Promise<*>} |
| 55 | + */ |
| 56 | + async execute() { |
| 57 | + if (!Essentials.default.commands) { |
| 58 | + throw new Error('Yarn commands are not available!'); |
| 59 | + } |
| 60 | + |
| 61 | + const configuration = await Configuration.find(this.context.cwd, this.context.plugins); |
| 62 | + const {workspace} = await Project.find(configuration, this.context.cwd); |
| 63 | + |
| 64 | + const dependencies = getDependencies(workspace); |
| 65 | + const descriptors = getDescriptors(dependencies, this.exclude ? this.exclude.split(" ") : null); |
| 66 | + |
| 67 | + const packageNames = descriptors.map(e => { |
| 68 | + return resolveFullPackageName(e[1].scope, e[1].name); |
| 69 | + }); |
| 70 | + |
| 71 | + const cli = Cli.from(Essentials.default.commands); |
| 72 | + return await cli.runExit(['up', ...packageNames], this.context); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + UpAllCommand.addOption('exclude', Command.String('--exclude')); |
| 77 | + UpAllCommand.addPath('up-all'); |
| 78 | + |
| 79 | + UpAllCommand.schema = yup.object().shape({ |
| 80 | + exclude: yup.string() |
| 81 | + }); |
| 82 | + |
| 83 | + UpAllCommand.usage = Command.Usage({ |
| 84 | + description: 'Yarn2 plugin that will upgrade all dependencies to their latest version with one simple command', |
| 85 | + details: 'This command will upgrade all dependencies to their latest version', |
| 86 | + examples: [ |
| 87 | + [ |
| 88 | + `Upgrade all dependencies`, |
| 89 | + `yarn up-all` |
| 90 | + ], |
| 91 | + [ |
| 92 | + `Upgrade all dependencies but exclude a single dependency`, |
| 93 | + `yarn up-all --exclude react-dom` |
| 94 | + ], |
| 95 | + [ |
| 96 | + `Upgrade all dependencies but exclude multiple dependencies`, |
| 97 | + `yarn up-all --exclude "react-dom react-router"` |
| 98 | + ]] |
| 99 | + }); |
| 100 | + |
| 101 | + return { |
| 102 | + commands: [ |
| 103 | + UpAllCommand, |
| 104 | + ], |
| 105 | + }; |
| 106 | + } |
| 107 | +}; |
0 commit comments