UNPKG

1.87 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2019 TypeFox and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { injectable, inject } from 'inversify';
18import { CommandService } from '../common/command';
19import URI from '../common/uri';
20import { OpenHandler } from './opener-service';
21
22@injectable()
23export class CommandOpenHandler implements OpenHandler {
24
25 readonly id = 'command';
26
27 @inject(CommandService)
28 protected readonly commands: CommandService;
29
30 canHandle(uri: URI): number {
31 return uri.scheme === 'command' ? 500 : -1;
32 }
33
34 async open(uri: URI): Promise<boolean> {
35 // eslint-disable-next-line @typescript-eslint/no-explicit-any
36 let args: any = [];
37 try {
38 args = JSON.parse(decodeURIComponent(uri.query));
39 } catch {
40 // ignore and retry
41 try {
42 args = JSON.parse(uri.query);
43 } catch {
44 // ignore error
45 }
46 }
47 if (!Array.isArray(args)) {
48 args = [args];
49 }
50 await this.commands.executeCommand(uri.path.toString(), ...args);
51 return true;
52 }
53
54}