Skip to content

Commit

Permalink
refactor: added a new method to Configuration - addPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jj811208 committed Sep 30, 2022
1 parent e84fd78 commit bebc9db
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
44 changes: 10 additions & 34 deletions packages/plugin-essentials/sources/commands/plugin/import.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, MessageName, Project, ReportError, StreamReport, miscUtils, Report} from '@yarnpkg/core';
import {YarnVersion, formatUtils, httpUtils, structUtils, hashUtils} from '@yarnpkg/core';
import {PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
import {Command, Option, Usage} from 'clipanion';
import semver from 'semver';
import {URL} from 'url';
import {runInNewContext} from 'vm';
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, MessageName, Project, ReportError, StreamReport, Report} from '@yarnpkg/core';
import {YarnVersion, formatUtils, httpUtils, structUtils, hashUtils} from '@yarnpkg/core';
import {PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
import {Command, Option, Usage} from 'clipanion';
import semver from 'semver';
import {URL} from 'url';
import {runInNewContext} from 'vm';

import {getAvailablePlugins} from './list';
import {getAvailablePlugins} from './list';

// eslint-disable-next-line arca/no-default-export
export default class PluginImportCommand extends BaseCommand {
Expand Down Expand Up @@ -140,29 +140,5 @@ export async function savePlugin(pluginSpec: string, pluginBuffer: Buffer, {proj
checksum: await hashUtils.checksumFile(absolutePath),
};

await Configuration.updateConfiguration(project.cwd, (current: any) => {
const plugins = [];
let hasBeenReplaced = false;

for (const entry of current.plugins || []) {
const userProvidedPath = typeof entry !== `string`
? entry.path
: entry;

const pluginPath = ppath.resolve(project.cwd, npath.toPortablePath(userProvidedPath));
const {name} = miscUtils.dynamicRequire(pluginPath);

if (name !== pluginName) {
plugins.push(entry);
} else {
plugins.push(pluginMeta);
hasBeenReplaced = true;
}
}

if (!hasBeenReplaced)
plugins.push(pluginMeta);

return {...current, plugins};
});
await Configuration.addPlugin(project.cwd, [pluginMeta]);
}
35 changes: 35 additions & 0 deletions packages/yarnpkg-core/sources/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {WorkspaceFetcher}
import {WorkspaceResolver} from './WorkspaceResolver';
import * as folderUtils from './folderUtils';
import * as formatUtils from './formatUtils';
import * as hashUtils from './hashUtils';
import * as httpUtils from './httpUtils';
import * as miscUtils from './miscUtils';
import * as nodeUtils from './nodeUtils';
Expand Down Expand Up @@ -1325,6 +1326,40 @@ export class Configuration {
});
}

static async addPlugin(cwd: PortablePath, pluginMetaList: Array<{
path: PortablePath;
spec: string;
checksum: string;
}>) {
if (pluginMetaList.length === 0) return;

await Configuration.updateConfiguration(cwd, (current: any) => {
const currentPluginMetaList = current.plugins || [];
if (currentPluginMetaList.length === 0) return {...current, plugins: pluginMetaList};

const newPluginMetaList = [];
let notYetProcessedList = [...pluginMetaList];

for (const currentPluginMeta of currentPluginMetaList) {
const currentPluginPath = typeof currentPluginMeta !== `string`
? currentPluginMeta.path
: currentPluginMeta;
const updatingPlugin = notYetProcessedList.find(pluginMeta => pluginMeta.path === currentPluginPath);

if (updatingPlugin) {
newPluginMetaList.push(updatingPlugin);
notYetProcessedList = notYetProcessedList.filter(p => p !== updatingPlugin);
} else {
newPluginMetaList.push(currentPluginMeta);
}
}

newPluginMetaList.push(...notYetProcessedList);

return {...current, plugins: newPluginMetaList};
});
}

static async updateHomeConfiguration(patch: {[key: string]: ((current: unknown) => unknown) | {} | undefined} | ((current: {[key: string]: unknown}) => {[key: string]: unknown})) {
const homeFolder = folderUtils.getHomeFolder();

Expand Down

0 comments on commit bebc9db

Please sign in to comment.