summon-open 1.0.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/lib/resolve.js ADDED
@@ -0,0 +1,198 @@
1
+ // A conservative "looks like a bare domain" matcher, e.g. `github.com`,
2
+ // `example.co.uk`, `localhost:3000`, `192.168.0.1:8080/path`.
3
+ const DOMAIN_PATTERN = /^(?:localhost|(?:[a-z\d\-]+\.)+[a-z]{2,}|\d{1,3}(?:\.\d{1,3}){3})(?::\d+)?(?:[\/?#].*)?$/vi;
4
+
5
+ // When a dotted name ends in one of these, treat it as a (possibly missing)
6
+ // file rather than a domain — so `summon report.pdf` never becomes a URL.
7
+ const FILE_EXTENSIONS = new Set([
8
+ 'txt',
9
+ 'md',
10
+ 'pdf',
11
+ 'png',
12
+ 'jpg',
13
+ 'jpeg',
14
+ 'gif',
15
+ 'webp',
16
+ 'svg',
17
+ 'ico',
18
+ 'bmp',
19
+ 'tiff',
20
+ 'mp3',
21
+ 'mp4',
22
+ 'mkv',
23
+ 'mov',
24
+ 'avi',
25
+ 'wav',
26
+ 'flac',
27
+ 'ogg',
28
+ 'aac',
29
+ 'm4a',
30
+ 'webm',
31
+ 'zip',
32
+ 'gz',
33
+ 'tar',
34
+ '7z',
35
+ 'rar',
36
+ 'xz',
37
+ 'zst',
38
+ 'doc',
39
+ 'docx',
40
+ 'xls',
41
+ 'xlsx',
42
+ 'ppt',
43
+ 'pptx',
44
+ 'odt',
45
+ 'csv',
46
+ 'json',
47
+ 'xml',
48
+ 'yml',
49
+ 'yaml',
50
+ 'toml',
51
+ 'ini',
52
+ 'html',
53
+ 'htm',
54
+ 'css',
55
+ 'js',
56
+ 'ts',
57
+ 'jsx',
58
+ 'tsx',
59
+ 'exe',
60
+ 'dmg',
61
+ 'iso',
62
+ 'bin',
63
+ 'app',
64
+ 'deb',
65
+ 'rpm',
66
+ 'msi',
67
+ ]);
68
+
69
+ /**
70
+ Whether a string starts with an explicit URL scheme (http, https, mailto, file, …).
71
+
72
+ A single-letter prefix (Windows drive letter `C:`) and a colon followed by a
73
+ digit (a `host:port`) are intentionally not treated as schemes.
74
+
75
+ @param {string} value
76
+ @returns {boolean}
77
+ */
78
+ export function hasScheme(value) {
79
+ return /^[a-z][a-z\d+.\-]+:(?!\d)/vi.test(value);
80
+ }
81
+
82
+ /**
83
+ Whether a string is a well-formed absolute URL.
84
+
85
+ @param {string} value
86
+ @returns {boolean}
87
+ */
88
+ export function isUrl(value) {
89
+ if (!hasScheme(value)) {
90
+ return false;
91
+ }
92
+
93
+ try {
94
+ new URL(value); // eslint-disable-line no-new
95
+ return true;
96
+ } catch {
97
+ return false;
98
+ }
99
+ }
100
+
101
+ /**
102
+ Whether a bare string looks like a domain we should turn into an `https://` URL.
103
+
104
+ @param {string} value
105
+ @returns {boolean}
106
+ */
107
+ export function looksLikeDomain(value) {
108
+ if (hasScheme(value) || !DOMAIN_PATTERN.test(value)) {
109
+ return false;
110
+ }
111
+
112
+ const [host] = value.split(/[\/?#]/v);
113
+ const [hostname] = host.split(':');
114
+ const tld = hostname.split('.').pop().toLowerCase();
115
+ return !FILE_EXTENSIONS.has(tld);
116
+ }
117
+
118
+ /**
119
+ Normalize a target for opening. Bare domains get an `https://` prefix; existing
120
+ URLs and file paths are returned unchanged.
121
+
122
+ @param {string} target
123
+ @returns {string}
124
+ */
125
+ export function normalizeTarget(target) {
126
+ return looksLikeDomain(target) ? `https://${target}` : target;
127
+ }
128
+
129
+ /**
130
+ Build a search URL from a query and an engine template containing `%s`.
131
+
132
+ @param {string} query
133
+ @param {string} engineTemplate
134
+ @returns {string}
135
+ */
136
+ export function buildSearchUrl(query, engineTemplate) {
137
+ const encoded = encodeURIComponent(query);
138
+
139
+ return engineTemplate.includes('%s')
140
+ ? engineTemplate.replaceAll('%s', encoded)
141
+ : engineTemplate + encoded;
142
+ }
143
+
144
+ /**
145
+ Expand a bookmark alias into its stored target.
146
+
147
+ Handles explicit `@name` syntax and bare names that match a bookmark. Anything
148
+ else (file paths, URLs) is returned unchanged.
149
+
150
+ @param {string} input
151
+ @param {Record<string, string>} [bookmarks]
152
+ @returns {string}
153
+ */
154
+ export function expandBookmark(input, bookmarks = {}) {
155
+ if (input.startsWith('@')) {
156
+ const name = input.slice(1);
157
+ if (bookmarks[name]) {
158
+ return bookmarks[name];
159
+ }
160
+
161
+ throw new SummonError(`Unknown bookmark: ${input}`, 3);
162
+ }
163
+
164
+ if (bookmarks[input] && !hasScheme(input) && !input.includes('/') && !input.includes('\\')) {
165
+ return bookmarks[input];
166
+ }
167
+
168
+ return input;
169
+ }
170
+
171
+ /**
172
+ Fully resolve a raw input into a final target (bookmark expansion + normalization).
173
+
174
+ Note: this does not touch the filesystem. The CLI additionally prefers an
175
+ existing file over domain normalization.
176
+
177
+ @param {string} input
178
+ @param {Record<string, string>} [bookmarks]
179
+ @returns {string}
180
+ */
181
+ export function resolveTarget(input, bookmarks = {}) {
182
+ return normalizeTarget(expandBookmark(input, bookmarks));
183
+ }
184
+
185
+ /**
186
+ Error type carrying an intended process exit code.
187
+ */
188
+ export class SummonError extends Error {
189
+ /**
190
+ @param {string} message
191
+ @param {number} [exitCode]
192
+ */
193
+ constructor(message, exitCode = 1) {
194
+ super(message);
195
+ this.name = 'SummonError';
196
+ this.exitCode = exitCode;
197
+ }
198
+ }
package/lib/reveal.js ADDED
@@ -0,0 +1,52 @@
1
+ import process from 'node:process';
2
+ import path from 'node:path';
3
+ import {spawn} from 'node:child_process';
4
+ import {existsSync} from 'node:fs';
5
+ import open from 'open';
6
+ import {SummonError} from './resolve.js';
7
+
8
+ function run(command, arguments_) {
9
+ return new Promise((resolve, reject) => {
10
+ const subprocess = spawn(command, arguments_, {stdio: 'ignore', detached: false});
11
+ subprocess.on('error', reject);
12
+ subprocess.on('close', code => {
13
+ if (code === 0 || code === null) {
14
+ resolve();
15
+ } else {
16
+ reject(new Error(`${command} exited with code ${code}`));
17
+ }
18
+ });
19
+ });
20
+ }
21
+
22
+ /**
23
+ Reveal a file or folder in the OS file manager, selecting/highlighting it.
24
+
25
+ - macOS: `open -R <path>`
26
+ - Windows: `explorer /select,<path>`
27
+ - Linux/other: open the containing directory (reliable, no highlight)
28
+
29
+ @param {string} target
30
+ @returns {Promise<void>}
31
+ */
32
+ export async function revealInFileManager(target) {
33
+ const resolved = path.resolve(target);
34
+
35
+ if (!existsSync(resolved)) {
36
+ throw new SummonError(`Cannot reveal, path not found: ${target}`, 2);
37
+ }
38
+
39
+ if (process.platform === 'darwin') {
40
+ await run('open', ['-R', resolved]);
41
+ return;
42
+ }
43
+
44
+ if (process.platform === 'win32') {
45
+ // `explorer` returns exit code 1 even on success, so we don't treat that as failure.
46
+ await run('explorer', [`/select,${resolved}`]).catch(() => undefined);
47
+ return;
48
+ }
49
+
50
+ // Best effort on Linux and everything else: open the containing folder.
51
+ await open(path.dirname(resolved));
52
+ }
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Aditya Pandey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "summon-open",
3
+ "version": "1.0.0",
4
+ "description": "Open URLs, files, folders, and apps from one cross-platform command — with bookmarks, search, clipboard, reveal, dry-run, and stdin support.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Aditya060806/summon.git"
9
+ },
10
+ "homepage": "https://github.com/Aditya060806/summon#readme",
11
+ "bugs": "https://github.com/Aditya060806/summon/issues",
12
+ "author": {
13
+ "name": "Aditya Pandey"
14
+ },
15
+ "type": "module",
16
+ "bin": {
17
+ "summon": "cli.js"
18
+ },
19
+ "engines": {
20
+ "node": ">=20"
21
+ },
22
+ "scripts": {
23
+ "test": "xo && ava"
24
+ },
25
+ "files": [
26
+ "cli.js",
27
+ "lib",
28
+ "completions"
29
+ ],
30
+ "keywords": [
31
+ "summon",
32
+ "cross-platform",
33
+ "bookmark",
34
+ "bookmarks",
35
+ "clipboard",
36
+ "reveal",
37
+ "search",
38
+ "dry-run",
39
+ "cli-app",
40
+ "cli",
41
+ "app",
42
+ "open",
43
+ "opener",
44
+ "opens",
45
+ "launch",
46
+ "start",
47
+ "xdg-open",
48
+ "xdg",
49
+ "default",
50
+ "cmd",
51
+ "browser",
52
+ "editor",
53
+ "executable",
54
+ "exe",
55
+ "url",
56
+ "urls",
57
+ "arguments",
58
+ "spawn",
59
+ "exec",
60
+ "child",
61
+ "process",
62
+ "website",
63
+ "file"
64
+ ],
65
+ "dependencies": {
66
+ "clipboardy": "^4.0.0",
67
+ "file-type": "^21.3.4",
68
+ "meow": "^14.1.0",
69
+ "open": "^11.0.0",
70
+ "tempy": "^3.2.0"
71
+ },
72
+ "devDependencies": {
73
+ "ava": "^7.0.0",
74
+ "execa": "^9.6.1",
75
+ "xo": "^2.0.2"
76
+ }
77
+ }