@lsst/pik 0.6.0 → 0.6.2

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,3 @@
1
+ import type { PikPlugin } from '@lsst/pik-core';
2
+ export declare const killportPlugin: PikPlugin;
3
+ //# sourceMappingURL=killport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"killport.d.ts","sourceRoot":"","sources":["../../../src/lib/plugins/killport.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAyEhD,eAAO,MAAM,cAAc,EAAE,SAgD5B,CAAC"}
@@ -0,0 +1,111 @@
1
+ import { execSync } from 'child_process';
2
+ import { confirm, input } from '@inquirer/prompts';
3
+ import pc from 'picocolors';
4
+ function getProcessesOnPort(port) {
5
+ try {
6
+ const output = execSync(`lsof -ti:${port}`, { encoding: 'utf-8' });
7
+ const pids = output
8
+ .trim()
9
+ .split('\n')
10
+ .filter(Boolean)
11
+ .map((pid) => parseInt(pid, 10));
12
+ return pids.map((pid) => {
13
+ let command = 'unknown';
14
+ try {
15
+ command = execSync(`ps -p ${pid} -o comm=`, { encoding: 'utf-8' }).trim();
16
+ }
17
+ catch {
18
+ // Process may have already exited
19
+ }
20
+ return { pid, command };
21
+ });
22
+ }
23
+ catch {
24
+ return [];
25
+ }
26
+ }
27
+ function killProcess(pid) {
28
+ try {
29
+ process.kill(pid, 'SIGKILL');
30
+ return true;
31
+ }
32
+ catch {
33
+ return false;
34
+ }
35
+ }
36
+ async function killPortInteractive(port, skipConfirm) {
37
+ const processes = getProcessesOnPort(port);
38
+ if (processes.length === 0) {
39
+ console.log(pc.yellow(`No process found on port ${port}`));
40
+ return;
41
+ }
42
+ console.log(pc.bold(`Processes on port ${port}:`));
43
+ for (const proc of processes) {
44
+ console.log(` ${pc.cyan(proc.pid.toString())} - ${proc.command}`);
45
+ }
46
+ if (!skipConfirm) {
47
+ const shouldKill = await confirm({
48
+ message: `Kill ${processes.length} process${processes.length > 1 ? 'es' : ''}?`,
49
+ default: true,
50
+ });
51
+ if (!shouldKill) {
52
+ console.log(pc.dim('Cancelled'));
53
+ return;
54
+ }
55
+ }
56
+ for (const proc of processes) {
57
+ if (killProcess(proc.pid)) {
58
+ console.log(pc.green(`✓ Killed ${proc.pid} (${proc.command})`));
59
+ }
60
+ else {
61
+ console.log(pc.red(`✗ Failed to kill ${proc.pid}`));
62
+ }
63
+ }
64
+ }
65
+ export const killportPlugin = {
66
+ name: 'Killport',
67
+ description: 'Kill processes on specific ports',
68
+ command: 'killport',
69
+ aliases: ['kp'],
70
+ register(program) {
71
+ program
72
+ .command('killport')
73
+ .alias('kp')
74
+ .description('Kill process running on a specific port')
75
+ .argument('[port]', 'Port number (interactive if not provided)')
76
+ .option('-y, --yes', 'Skip confirmation')
77
+ .action(async (portArg, options) => {
78
+ if (portArg) {
79
+ const port = parseInt(portArg, 10);
80
+ if (isNaN(port) || port < 1 || port > 65535) {
81
+ console.error(pc.red(`Invalid port: ${portArg}`));
82
+ process.exit(1);
83
+ }
84
+ await killPortInteractive(port, options.yes ?? false);
85
+ }
86
+ else {
87
+ // Interactive mode - prompt for port number
88
+ try {
89
+ const portInput = await input({
90
+ message: 'Port number:',
91
+ validate: (value) => {
92
+ const num = parseInt(value, 10);
93
+ if (isNaN(num) || num < 1 || num > 65535) {
94
+ return 'Please enter a valid port number (1-65535)';
95
+ }
96
+ return true;
97
+ },
98
+ });
99
+ const port = parseInt(portInput, 10);
100
+ await killPortInteractive(port, options.yes ?? false);
101
+ }
102
+ catch (error) {
103
+ if (error instanceof Error && error.name === 'ExitPromptError') {
104
+ return;
105
+ }
106
+ throw error;
107
+ }
108
+ }
109
+ });
110
+ },
111
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"program.d.ts","sourceRoot":"","sources":["../../src/lib/program.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,gBAAgB,CAAC;AA6C3E,eAAO,MAAM,OAAO,SAGG,CAAC;AAExB;;GAEG;AACH,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,CAgE9D"}
1
+ {"version":3,"file":"program.d.ts","sourceRoot":"","sources":["../../src/lib/program.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,gBAAgB,CAAC;AA8C3E,eAAO,MAAM,OAAO,SAGG,CAAC;AAExB;;GAEG;AACH,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,CAgE9D"}
@@ -4,9 +4,10 @@ import pc from 'picocolors';
4
4
  import { loadConfig, isValidPlugin } from '@lsst/pik-core';
5
5
  import { selectPlugin } from '@lsst/pik-plugin-select';
6
6
  import { worktreePlugin } from '@lsst/pik-plugin-worktree';
7
+ import { killportPlugin } from './plugins/killport.js';
7
8
  import pkg from '../../package.json' with { type: 'json' };
8
9
  // Built-in plugins
9
- const builtinPlugins = [selectPlugin, worktreePlugin];
10
+ const builtinPlugins = [selectPlugin, worktreePlugin, killportPlugin];
10
11
  /**
11
12
  * Get plugins that are enabled in the config.
12
13
  * - Built-in plugins are enabled if their command key exists in config
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lsst/pik",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "CLI tool for switching config options in source files",
5
5
  "type": "module",
6
6
  "license": "MIT",