mac-helper 0.0.4 → 0.1.1

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.
@@ -0,0 +1,8 @@
1
+ export type ActiveTab = {
2
+ url: string;
3
+ title: string;
4
+ };
5
+ export declare const Chrome: {
6
+ getActiveTab(): Promise<ActiveTab>;
7
+ };
8
+ export declare function getChromeActiveTab(): Promise<ActiveTab>;
package/lib/chrome.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getChromeActiveTab = exports.Chrome = void 0;
4
+ const run_1 = require("@jxa/run");
5
+ exports.Chrome = {
6
+ async getActiveTab() {
7
+ return await getChromeActiveTab();
8
+ },
9
+ };
10
+ async function getChromeActiveTab() {
11
+ return await (0, run_1.run)(() => {
12
+ const app = Application('Google Chrome');
13
+ app.includeStandardAdditions = true;
14
+ const wins = (app.windows() || []).filter((w) => !w.minimized());
15
+ const tab = wins[0]?.activeTab();
16
+ return {
17
+ url: tab?.url(),
18
+ title: tab?.title(),
19
+ };
20
+ });
21
+ }
22
+ exports.getChromeActiveTab = getChromeActiveTab;
23
+ // getChromeActiveTab().then(console.log)
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './chrome';
2
+ export * from './dialog';
1
3
  export * from './finder';
2
4
  export * from './iterm';
3
- export * from './dialog';
package/lib/index.js CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
17
  };
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
+ __exportStar(require("./chrome"), exports);
20
+ __exportStar(require("./dialog"), exports);
19
21
  __exportStar(require("./finder"), exports);
20
22
  __exportStar(require("./iterm"), exports);
21
- __exportStar(require("./dialog"), exports);
package/lib/iterm.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare function runInIterm(command: string): Promise<void>;
1
+ export declare function runInIterm(command: string, reuse?: boolean): Promise<void>;
2
2
  export declare const iTerm: {
3
3
  run: typeof runInIterm;
4
4
  runInIterm: typeof runInIterm;
package/lib/iterm.js CHANGED
@@ -2,15 +2,30 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.iTerm = exports.runInIterm = void 0;
4
4
  const run_1 = require("@jxa/run");
5
- async function runInIterm(command) {
6
- await (0, run_1.run)((command) => {
5
+ async function runInIterm(command, reuse = true) {
6
+ await (0, run_1.run)((command, reuse) => {
7
7
  const app = Application('iTerm');
8
8
  app.includeStandardAdditions = true;
9
9
  app.activate();
10
- const tab = app.createTabWithDefaultProfile(app.currentWindow);
11
- const sess = tab.currentSession;
12
- app.write(sess, { text: command });
13
- }, command);
10
+ let usingSession;
11
+ // reuse when possible
12
+ if (reuse) {
13
+ const session = app.currentWindow().currentSession();
14
+ const isIdle = session.isAtShellPrompt();
15
+ if (isIdle) {
16
+ usingSession = session;
17
+ }
18
+ }
19
+ // create new tab when necessary
20
+ if (!usingSession) {
21
+ const tab = app.createTabWithDefaultProfile(app.currentWindow);
22
+ usingSession = tab.currentSession();
23
+ }
24
+ if (!usingSession) {
25
+ return;
26
+ }
27
+ app.write(usingSession, { text: command });
28
+ }, command, reuse);
14
29
  }
15
30
  exports.runInIterm = runInIterm;
16
31
  exports.iTerm = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mac-helper",
3
- "version": "0.0.4",
3
+ "version": "0.1.1",
4
4
  "description": "useful helper for macOS",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -39,24 +39,24 @@
39
39
  "@jxa/sdef-to-dts": "^1.3.6",
40
40
  "@jxa/types": "^1.3.6",
41
41
  "@magicdawn/prettier-config": "^0.0.2",
42
- "@swc/core": "^1.3.40",
42
+ "@swc/core": "^1.3.55",
43
43
  "@types/fs-extra": "^11.0.1",
44
44
  "@types/mocha": "^10.0.1",
45
- "@types/node": "^18.15.3",
46
- "@typescript-eslint/eslint-plugin": "^5.55.0",
47
- "@typescript-eslint/parser": "^5.55.0",
45
+ "@types/node": "^18.16.1",
46
+ "@typescript-eslint/eslint-plugin": "^5.59.1",
47
+ "@typescript-eslint/parser": "^5.59.1",
48
48
  "delay": "^5.0.0",
49
- "eslint": "^8.36.0",
50
- "eslint-config-prettier": "^8.7.0",
51
- "fs-extra": "^11.1.0",
49
+ "eslint": "^8.39.0",
50
+ "eslint-config-prettier": "^8.8.0",
51
+ "fs-extra": "^11.1.1",
52
52
  "husky": "^8.0.3",
53
- "jxa-common-used": "^0.0.2",
54
- "lint-staged": "^13.2.0",
53
+ "jxa-common-used": "^0.0.3",
54
+ "lint-staged": "^13.2.2",
55
55
  "mocha": "^10.2.0",
56
56
  "nyc": "^15.1.0",
57
- "prettier": "^2.8.4",
57
+ "prettier": "^2.8.8",
58
58
  "should": "^13.2.3",
59
- "typescript": "^4.9.5"
59
+ "typescript": "^5.0.4"
60
60
  },
61
61
  "packageManager": "pnpm@7.17.0",
62
62
  "publishConfig": {
package/src/chrome.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { run } from '@jxa/run'
2
+ import { type GoogleChrome } from 'jxa-common-used'
3
+
4
+ export type ActiveTab = { url: string; title: string }
5
+
6
+ export const Chrome = {
7
+ async getActiveTab(): Promise<ActiveTab> {
8
+ return await getChromeActiveTab()
9
+ },
10
+ }
11
+
12
+ export async function getChromeActiveTab(): Promise<ActiveTab> {
13
+ return await run(() => {
14
+ const app = Application<GoogleChrome>('Google Chrome')
15
+ app.includeStandardAdditions = true
16
+
17
+ const wins = (app.windows() || []).filter((w) => !w.minimized())
18
+ const tab = wins[0]?.activeTab()
19
+ return {
20
+ url: tab?.url(),
21
+ title: tab?.title(),
22
+ }
23
+ })
24
+ }
25
+
26
+ // getChromeActiveTab().then(console.log)
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /// <reference types='@jxa/global-type' />
2
2
  /// <reference types='@jxa/types' />
3
3
 
4
+ export * from './chrome'
5
+ export * from './dialog'
4
6
  export * from './finder'
5
7
  export * from './iterm'
6
- export * from './dialog'
package/src/iterm.ts CHANGED
@@ -1,16 +1,39 @@
1
1
  import { run } from '@jxa/run'
2
2
  import { iTerm2 } from 'jxa-common-used'
3
3
 
4
- export async function runInIterm(command: string) {
5
- await run((command) => {
6
- const app = Application<iTerm2>('iTerm')
7
- app.includeStandardAdditions = true
8
- app.activate()
4
+ export async function runInIterm(command: string, reuse = true) {
5
+ await run(
6
+ (command, reuse) => {
7
+ const app = Application<iTerm2>('iTerm')
8
+ app.includeStandardAdditions = true
9
+ app.activate()
9
10
 
10
- const tab = app.createTabWithDefaultProfile(app.currentWindow)
11
- const sess = tab.currentSession
12
- app.write(sess, { text: command })
13
- }, command)
11
+ let usingSession: iTerm2.Session | undefined
12
+
13
+ // reuse when possible
14
+ if (reuse) {
15
+ const session: iTerm2.Session = app.currentWindow().currentSession()
16
+ const isIdle = session.isAtShellPrompt()
17
+ if (isIdle) {
18
+ usingSession = session
19
+ }
20
+ }
21
+
22
+ // create new tab when necessary
23
+ if (!usingSession) {
24
+ const tab = app.createTabWithDefaultProfile(app.currentWindow)
25
+ usingSession = tab.currentSession()
26
+ }
27
+
28
+ if (!usingSession) {
29
+ return
30
+ }
31
+
32
+ app.write(usingSession, { text: command })
33
+ },
34
+ command,
35
+ reuse
36
+ )
14
37
  }
15
38
 
16
39
  export const iTerm = {