@devframes/service-open 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/dist/index.d.mts +66 -0
- package/dist/index.mjs +90 -0
- package/package.json +48 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-PRESENT Anthony Fu <https://github.com/antfu>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { KnownEditor } from "devframe/recipes/common-rpc-functions";
|
|
2
|
+
import { DevframeServiceDefinition } from "devframe/types";
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare const OPEN_SERVICE_PACKAGE = "@devframes/service-open";
|
|
5
|
+
declare const OPEN_SERVICE_SCOPE = "devframes:service:open";
|
|
6
|
+
interface OpenServiceOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Preferred editor command — one of the `KNOWN_EDITORS` `launch-editor`
|
|
9
|
+
* recognizes. Auto-detected (via `LAUNCH_EDITOR` and common defaults)
|
|
10
|
+
* when omitted. On merge, the later installer's choice wins.
|
|
11
|
+
*/
|
|
12
|
+
editor?: KnownEditor;
|
|
13
|
+
/**
|
|
14
|
+
* Additional directories files may be opened from, on top of the
|
|
15
|
+
* context's `workspaceRoot` — e.g. a plugin's managed storage dir that
|
|
16
|
+
* lives outside the workspace. Merged as a union across installers.
|
|
17
|
+
*/
|
|
18
|
+
roots?: string[];
|
|
19
|
+
}
|
|
20
|
+
interface OpenInEditorInput {
|
|
21
|
+
/**
|
|
22
|
+
* File to open — absolute, or relative to the service's `workspaceRoot`
|
|
23
|
+
* (so a client with only a workspace-relative path, e.g. a message's file
|
|
24
|
+
* position, can call this directly without a server-side bridge).
|
|
25
|
+
*/
|
|
26
|
+
path: string;
|
|
27
|
+
line?: number;
|
|
28
|
+
column?: number;
|
|
29
|
+
/** Per-call editor override (one of `KNOWN_EDITORS`). */
|
|
30
|
+
editor?: KnownEditor;
|
|
31
|
+
}
|
|
32
|
+
interface OpenServiceApi {
|
|
33
|
+
/** Open a file (optionally at a line/column) in the user's editor. */
|
|
34
|
+
openInEditor: (input: OpenInEditorInput) => Promise<void>;
|
|
35
|
+
/** Reveal a path in the OS file explorer. */
|
|
36
|
+
openInFinder: (input: {
|
|
37
|
+
path: string;
|
|
38
|
+
}) => Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
declare module 'devframe' {
|
|
41
|
+
interface DevframeRpcServerFunctions {
|
|
42
|
+
'devframes:service:open:open-in-editor': (input: OpenInEditorInput) => Promise<void>;
|
|
43
|
+
'devframes:service:open:open-in-finder': (input: {
|
|
44
|
+
path: string;
|
|
45
|
+
}) => Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
interface DevframeServicesRegistry {
|
|
48
|
+
'@devframes/service-open': OpenServiceApi;
|
|
49
|
+
}
|
|
50
|
+
interface DevframeServicesScopeRegistry {
|
|
51
|
+
'@devframes/service-open': 'devframes:service:open';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The open wire service — `open-in-editor` / `open-in-finder` RPC shared by
|
|
56
|
+
* every plugin on the host, replacing per-plugin registrations of the
|
|
57
|
+
* (deprecated) `devframe/recipes/common-rpc-functions` recipes. Paths may be
|
|
58
|
+
* absolute or relative to the `workspaceRoot`; the service refuses paths
|
|
59
|
+
* outside the workspace root and the configured extra
|
|
60
|
+
* {@link OpenServiceOptions.roots} (`DS_OPEN_0002`), and gates editor
|
|
61
|
+
* commands to the `KNOWN_EDITORS` picklist so the RPC surface can't spawn an
|
|
62
|
+
* arbitrary command.
|
|
63
|
+
*/
|
|
64
|
+
declare function createOpenService(options?: OpenServiceOptions): DevframeServiceDefinition<OpenServiceApi, OpenServiceOptions>;
|
|
65
|
+
//#endregion
|
|
66
|
+
export { OPEN_SERVICE_PACKAGE, OPEN_SERVICE_SCOPE, OpenInEditorInput, OpenServiceApi, OpenServiceOptions, createOpenService, createOpenService as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { defineRpcFunction } from "devframe";
|
|
2
|
+
import { KNOWN_EDITORS } from "devframe/recipes/common-rpc-functions";
|
|
3
|
+
import { s } from "devframe/utils/simple-schema";
|
|
4
|
+
import { isAbsolute, relative, resolve } from "pathe";
|
|
5
|
+
import { defineDiagnostics } from "devframe/utils/nostics";
|
|
6
|
+
//#region package.json
|
|
7
|
+
var version = "0.9.0";
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/diagnostics.ts
|
|
10
|
+
const diagnostics = defineDiagnostics({
|
|
11
|
+
docsBase: "https://devfra.me/errors",
|
|
12
|
+
codes: { DS_OPEN_0002: {
|
|
13
|
+
why: (p) => `Refusing to open "${p.path}": the path is outside the workspace root and every configured extra root.`,
|
|
14
|
+
fix: "The open service only touches files under the workspace root by default. Pass additional allowed directories via the service's `roots` option when your tool manages files elsewhere (e.g. a global storage dir)."
|
|
15
|
+
} }
|
|
16
|
+
});
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/index.ts
|
|
19
|
+
const OPEN_SERVICE_PACKAGE = "@devframes/service-open";
|
|
20
|
+
const OPEN_SERVICE_SCOPE = "devframes:service:open";
|
|
21
|
+
/**
|
|
22
|
+
* The open wire service — `open-in-editor` / `open-in-finder` RPC shared by
|
|
23
|
+
* every plugin on the host, replacing per-plugin registrations of the
|
|
24
|
+
* (deprecated) `devframe/recipes/common-rpc-functions` recipes. Paths may be
|
|
25
|
+
* absolute or relative to the `workspaceRoot`; the service refuses paths
|
|
26
|
+
* outside the workspace root and the configured extra
|
|
27
|
+
* {@link OpenServiceOptions.roots} (`DS_OPEN_0002`), and gates editor
|
|
28
|
+
* commands to the `KNOWN_EDITORS` picklist so the RPC surface can't spawn an
|
|
29
|
+
* arbitrary command.
|
|
30
|
+
*/
|
|
31
|
+
function createOpenService(options) {
|
|
32
|
+
return {
|
|
33
|
+
package: OPEN_SERVICE_PACKAGE,
|
|
34
|
+
version,
|
|
35
|
+
scope: OPEN_SERVICE_SCOPE,
|
|
36
|
+
options,
|
|
37
|
+
setup(ctx, { options }) {
|
|
38
|
+
const allowedRoots = [ctx.workspaceRoot, ...options?.roots ?? []].map((root) => resolve(root));
|
|
39
|
+
/**
|
|
40
|
+
* Resolve `path` (relative paths against `workspaceRoot`) and assert it
|
|
41
|
+
* lands inside one of the allowed roots, or throw.
|
|
42
|
+
*/
|
|
43
|
+
function assertAllowedPath(path) {
|
|
44
|
+
const resolved = isAbsolute(path) ? resolve(path) : resolve(ctx.workspaceRoot, path);
|
|
45
|
+
if (!allowedRoots.some((root) => {
|
|
46
|
+
const rel = relative(root, resolved);
|
|
47
|
+
return rel === "" || !rel.startsWith("..") && !isAbsolute(rel);
|
|
48
|
+
})) throw diagnostics.DS_OPEN_0002({ path });
|
|
49
|
+
return resolved;
|
|
50
|
+
}
|
|
51
|
+
const api = {
|
|
52
|
+
async openInEditor(input) {
|
|
53
|
+
const path = assertAllowedPath(input.path);
|
|
54
|
+
const target = input.line != null ? `${path}:${input.line}${input.column != null ? `:${input.column}` : ""}` : path;
|
|
55
|
+
const { launchEditor } = await import("devframe/utils/launch-editor");
|
|
56
|
+
launchEditor(target, input.editor ?? options?.editor);
|
|
57
|
+
},
|
|
58
|
+
async openInFinder(input) {
|
|
59
|
+
const path = assertAllowedPath(input.path);
|
|
60
|
+
const { open } = await import("devframe/utils/open");
|
|
61
|
+
await open(path);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
ctx.rpc.register(defineRpcFunction({
|
|
65
|
+
name: "open-in-editor",
|
|
66
|
+
type: "action",
|
|
67
|
+
jsonSerializable: true,
|
|
68
|
+
args: [s.object({
|
|
69
|
+
path: s.string(),
|
|
70
|
+
line: s.optional(s.number()),
|
|
71
|
+
column: s.optional(s.number()),
|
|
72
|
+
editor: s.optional(s.picklist(KNOWN_EDITORS))
|
|
73
|
+
})],
|
|
74
|
+
returns: s.void(),
|
|
75
|
+
handler: (input) => api.openInEditor(input)
|
|
76
|
+
}));
|
|
77
|
+
ctx.rpc.register(defineRpcFunction({
|
|
78
|
+
name: "open-in-finder",
|
|
79
|
+
type: "action",
|
|
80
|
+
jsonSerializable: true,
|
|
81
|
+
args: [s.object({ path: s.string() })],
|
|
82
|
+
returns: s.void(),
|
|
83
|
+
handler: (input) => api.openInFinder(input)
|
|
84
|
+
}));
|
|
85
|
+
return api;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
export { OPEN_SERVICE_PACKAGE, OPEN_SERVICE_SCOPE, createOpenService, createOpenService as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@devframes/service-open",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.9.0",
|
|
5
|
+
"description": "Devframe wire service that opens files in the user's editor or reveals them in the OS file explorer.",
|
|
6
|
+
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/devframes/devframe#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"directory": "services/open",
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/devframes/devframe.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/devframes/devframe/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"devframe",
|
|
17
|
+
"devframe-service",
|
|
18
|
+
"devtools",
|
|
19
|
+
"open-in-editor"
|
|
20
|
+
],
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": "./dist/index.mjs",
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"types": "./dist/index.d.mts",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"devframe": "0.9.1"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"pathe": "^2.0.3"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^26.2.0",
|
|
38
|
+
"tsdown": "^0.22.14",
|
|
39
|
+
"vitest": "^4.1.10",
|
|
40
|
+
"devframe": "0.9.1"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsdown",
|
|
44
|
+
"watch": "tsdown --watch",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"typecheck": "tsc --noEmit"
|
|
47
|
+
}
|
|
48
|
+
}
|