@lsst/pik 0.2.0 → 0.4.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.
@@ -1 +1 @@
1
- {"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../../src/lib/commands/switch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAcpC,eAAO,MAAM,aAAa,SAoEtB,CAAC"}
1
+ {"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../../src/lib/commands/switch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAqBpC,eAAO,MAAM,aAAa,SA0GtB,CAAC"}
@@ -1,11 +1,16 @@
1
1
  import { Command } from 'commander';
2
2
  import { writeFile } from 'fs/promises';
3
3
  import { extname, relative } from 'path';
4
- import { select } from '@inquirer/prompts';
4
+ import { select, Separator } from '@inquirer/prompts';
5
5
  import pc from 'picocolors';
6
6
  import { SingleSwitcher } from '@lsst/pik-core';
7
7
  import { loadConfig } from '../config.js';
8
8
  import { Scanner } from '../scanner.js';
9
+ const BACK_VALUE = Symbol('back');
10
+ const EXIT_VALUE = Symbol('exit');
11
+ function isExitPromptError(error) {
12
+ return error instanceof Error && error.name === 'ExitPromptError';
13
+ }
9
14
  export const switchCommand = new Command('switch')
10
15
  .alias('sw')
11
16
  .description('Interactively switch options')
@@ -28,32 +33,68 @@ export const switchCommand = new Command('switch')
28
33
  choices.push({ file, selector });
29
34
  }
30
35
  }
31
- // Select which selector to switch
32
- const selectedChoice = await select({
33
- message: 'Select a selector to switch',
34
- choices: choices.map((choice) => {
35
- const relativePath = relative(process.cwd(), choice.file.path);
36
- const activeOption = choice.selector.options.find((o) => o.isActive);
37
- const current = activeOption ? pc.dim(` (${activeOption.name})`) : '';
38
- return {
39
- name: `${choice.selector.name}${current} ${pc.dim(`- ${relativePath}`)}`,
40
- value: choice,
41
- };
42
- }),
43
- });
44
- // Select which option to activate
45
- const selectedOption = await select({
46
- message: `Select option for ${pc.bold(selectedChoice.selector.name)}`,
47
- choices: selectedChoice.selector.options.map((option) => ({
48
- name: option.isActive ? `${option.name} ${pc.green('(current)')}` : option.name,
49
- value: option.name,
50
- })),
51
- });
52
- // Apply the change
53
- const extension = extname(selectedChoice.file.path);
54
- const switcher = SingleSwitcher.forExtension(extension);
55
- const newContent = switcher.switch(selectedChoice.file.content, selectedChoice.selector, selectedOption);
56
- await writeFile(selectedChoice.file.path, newContent);
57
- const relativePath = relative(process.cwd(), selectedChoice.file.path);
58
- console.log(pc.green(`✓ Set ${pc.bold(selectedChoice.selector.name)} to ${pc.bold(selectedOption)} in ${relativePath}`));
36
+ // Main loop for navigation
37
+ while (true) {
38
+ let selectedChoice;
39
+ // Select which selector to switch
40
+ try {
41
+ selectedChoice = await select({
42
+ message: 'Select a selector to switch',
43
+ choices: [
44
+ ...choices.map((choice) => {
45
+ const relativePath = relative(process.cwd(), choice.file.path);
46
+ const activeOption = choice.selector.options.find((o) => o.isActive);
47
+ const current = activeOption ? pc.dim(` (${activeOption.name})`) : '';
48
+ return {
49
+ name: `${choice.selector.name}${current} ${pc.dim(`- ${relativePath}`)}`,
50
+ value: choice,
51
+ };
52
+ }),
53
+ new Separator(),
54
+ { name: pc.dim('Exit'), value: EXIT_VALUE },
55
+ ],
56
+ });
57
+ }
58
+ catch (error) {
59
+ if (isExitPromptError(error)) {
60
+ return;
61
+ }
62
+ throw error;
63
+ }
64
+ if (selectedChoice === EXIT_VALUE) {
65
+ return;
66
+ }
67
+ // Select which option to activate
68
+ let selectedOption;
69
+ try {
70
+ selectedOption = await select({
71
+ message: `Select option for ${pc.bold(selectedChoice.selector.name)}`,
72
+ choices: [
73
+ ...selectedChoice.selector.options.map((option) => ({
74
+ name: option.isActive ? `${option.name} ${pc.green('(current)')}` : option.name,
75
+ value: option.name,
76
+ })),
77
+ new Separator(),
78
+ { name: pc.dim('← Back'), value: BACK_VALUE },
79
+ ],
80
+ });
81
+ }
82
+ catch (error) {
83
+ if (isExitPromptError(error)) {
84
+ return;
85
+ }
86
+ throw error;
87
+ }
88
+ if (selectedOption === BACK_VALUE) {
89
+ continue; // Go back to selector selection
90
+ }
91
+ // Apply the change
92
+ const extension = extname(selectedChoice.file.path);
93
+ const switcher = SingleSwitcher.forExtension(extension);
94
+ const newContent = switcher.switch(selectedChoice.file.content, selectedChoice.selector, selectedOption);
95
+ await writeFile(selectedChoice.file.path, newContent);
96
+ const relativePath = relative(process.cwd(), selectedChoice.file.path);
97
+ console.log(pc.green(`✓ Set ${pc.bold(selectedChoice.selector.name)} to ${pc.bold(selectedOption)} in ${relativePath}`));
98
+ return;
99
+ }
59
100
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lsst/pik",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "CLI tool for switching config options in source files",
5
5
  "type": "module",
6
6
  "license": "MIT",