Skip to content

Commit

Permalink
refactor: re-use some local plugin logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fgreinacher committed Nov 18, 2020
1 parent e7dd47c commit 0afd653
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 44 deletions.
Expand Up @@ -20,8 +20,7 @@ import { PluginDeployerResolver, PluginDeployerResolverContext } from '../../../
import { injectable } from 'inversify';
import * as fs from 'fs';
import * as path from 'path';
import { FileUri } from '@theia/core/lib/node';
import URI from '@theia/core/lib/common/uri';
import { resolveLocalPluginPath } from './util';

@injectable()
export class LocalDirectoryPluginDeployerResolver implements PluginDeployerResolver {
Expand All @@ -32,30 +31,20 @@ export class LocalDirectoryPluginDeployerResolver implements PluginDeployerResol
* Check all files/folder from the local-dir referenced and add them as plugins.
*/
async resolve(pluginResolverContext: PluginDeployerResolverContext): Promise<void> {
// get directory
const localDirUri = new URI(pluginResolverContext.getOriginId());
if (localDirUri.scheme !== LocalDirectoryPluginDeployerResolver.LOCAL_DIR) {
return;
}
// get fs path
let dirPath = FileUri.fsPath(localDirUri);
if (!path.isAbsolute(dirPath)) {
dirPath = path.resolve(process.cwd(), dirPath);
}
// check directory exists
if (!fs.existsSync(dirPath)) {
console.warn(`The directory referenced by ${pluginResolverContext.getOriginId()} does not exist.`);
return;
}
// list all stuff from this directory
await new Promise((resolve: any, reject: any) => {
fs.readdir(dirPath, (err: any, files: any) => {
files.forEach((file: any) => {
pluginResolverContext.addPlugin(file, path.resolve(dirPath, file));
const dirPath = await resolveLocalPluginPath(
pluginResolverContext,
LocalDirectoryPluginDeployerResolver.LOCAL_DIR);
if (dirPath) {
// list all stuff from this directory
await new Promise((resolve: any, reject: any) => {
fs.readdir(dirPath, (err: any, files: any) => {
files.forEach((file: any) => {
pluginResolverContext.addPlugin(file, path.resolve(dirPath, file));
});
resolve(true);
});
resolve(true);
});
});
}
}
accept(pluginId: string): boolean {
return pluginId.startsWith(LocalDirectoryPluginDeployerResolver.LOCAL_DIR);
Expand Down
Expand Up @@ -18,37 +18,25 @@

import { PluginDeployerResolver, PluginDeployerResolverContext } from '../../../common/plugin-protocol';
import { injectable } from 'inversify';
import * as fs from 'fs';
import * as path from 'path';
import { FileUri } from '@theia/core/lib/node';
import URI from '@theia/core/lib/common/uri';
import { resolveLocalPluginPath } from './util';

@injectable()
export class LocalFilePluginDeployerResolver implements PluginDeployerResolver {

static LOCAL_FILE = 'local-file';

/**
* Check all files/folder from the local-dir referenced and add them as plugins.
* Adds the file specified by local-file as plugin.
*/
async resolve(pluginResolverContext: PluginDeployerResolverContext): Promise<void> {
// get directory
const localFileUri = new URI(pluginResolverContext.getOriginId());
if (localFileUri.scheme !== LocalFilePluginDeployerResolver.LOCAL_FILE) {
return;
const filePath = await resolveLocalPluginPath(
pluginResolverContext,
LocalFilePluginDeployerResolver.LOCAL_FILE);
if (filePath) {
const fileName = path.basename(filePath);
pluginResolverContext.addPlugin(fileName, filePath);
}
// get fs path
let filePath = FileUri.fsPath(localFileUri);
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(process.cwd(), filePath);
}
// check file exists
if (!fs.existsSync(filePath)) {
console.warn(`The file referenced by ${pluginResolverContext.getOriginId()} does not exist.`);
return;
}
const fileName = path.basename(filePath);
pluginResolverContext.addPlugin(fileName, filePath);
}
accept(pluginId: string): boolean {
return pluginId.startsWith(LocalFilePluginDeployerResolver.LOCAL_FILE);
Expand Down
41 changes: 41 additions & 0 deletions packages/plugin-ext/src/main/node/resolvers/util.ts
@@ -0,0 +1,41 @@
/********************************************************************************
* Copyright (C) 2020 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

/* eslint-disable @typescript-eslint/no-explicit-any */

import { PluginDeployerResolverContext } from '../../../common/plugin-protocol';
import * as fs from 'fs';
import * as path from 'path';
import { FileUri } from '@theia/core/lib/node';
import URI from '@theia/core/lib/common/uri';

export async function resolveLocalPluginPath(
pluginResolverContext: PluginDeployerResolverContext,
expectedScheme: string): Promise<string | null> {
const localUri = new URI(pluginResolverContext.getOriginId());
if (localUri.scheme !== expectedScheme) {
return null;
}
let fsPath = FileUri.fsPath(localUri);
if (!path.isAbsolute(fsPath)) {
fsPath = path.resolve(process.cwd(), fsPath);
}
if (!fs.existsSync(fsPath)) {
console.warn(`The plugin referenced by ${pluginResolverContext.getOriginId()} does not exist.`);
return null;
}
return fsPath;
}

0 comments on commit 0afd653

Please sign in to comment.