@lsst/pik 0.2.0 → 0.4.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../../src/lib/commands/list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../../src/lib/commands/list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAUpC,eAAO,MAAM,WAAW,SA6DpB,CAAC"}
|
|
@@ -6,14 +6,34 @@ import { Scanner } from '../scanner.js';
|
|
|
6
6
|
export const listCommand = new Command('list')
|
|
7
7
|
.alias('ls')
|
|
8
8
|
.description('List all selectors and their current state')
|
|
9
|
-
.
|
|
9
|
+
.option('--json', 'Output in JSON format')
|
|
10
|
+
.action(async (options) => {
|
|
10
11
|
const config = await loadConfig();
|
|
11
12
|
if (!config) {
|
|
12
|
-
|
|
13
|
+
if (options.json) {
|
|
14
|
+
console.log(JSON.stringify({ error: 'No pik.config.ts found' }));
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
console.error(pc.red('No pik.config.ts found'));
|
|
18
|
+
}
|
|
13
19
|
process.exit(1);
|
|
14
20
|
}
|
|
15
21
|
const scanner = new Scanner(config);
|
|
16
22
|
const results = await scanner.scan();
|
|
23
|
+
if (options.json) {
|
|
24
|
+
const jsonOutput = results.flatMap((file) => file.selectors.map((selector) => ({
|
|
25
|
+
name: selector.name,
|
|
26
|
+
file: relative(process.cwd(), file.path),
|
|
27
|
+
line: selector.line,
|
|
28
|
+
activeOption: selector.options.find((o) => o.isActive)?.name ?? null,
|
|
29
|
+
options: selector.options.map((o) => ({
|
|
30
|
+
name: o.name,
|
|
31
|
+
isActive: o.isActive,
|
|
32
|
+
})),
|
|
33
|
+
})));
|
|
34
|
+
console.log(JSON.stringify(jsonOutput, null, 2));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
17
37
|
if (results.length === 0) {
|
|
18
38
|
console.log(pc.yellow('No selectors found'));
|
|
19
39
|
return;
|
|
@@ -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;
|
|
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
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
});
|