Skip to content

Commit 83af428

Browse files
authored
fix: revert clack unfork (#564)
* Revert "chore: unfork clack (#526)" This reverts commit 30c09ac. * changeset * remvoed unused import
1 parent 7438f8c commit 83af428

32 files changed

+1954
-41
lines changed

.changeset/chilly-clocks-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
fix: directory selection placeholder value was taken as folder path

packages/clack-core/LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
MIT License
2+
3+
Copyright (c) Nate Moore
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.
10+
11+
---
12+
13+
`ansi-regex` is adapted from https://github.com/chalk/ansi-regex
14+
15+
MIT License
16+
17+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
18+
19+
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:
20+
21+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
22+
23+
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.

packages/clack-core/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# `@clack/core`
2+
3+
Clack contains low-level primitives for implementing your own command-line applications.
4+
5+
Currently, `TextPrompt`, `SelectPrompt`, and `ConfirmPrompt` are exposed as well as the base `Prompt` class.
6+
7+
Each `Prompt` accepts a `render` function.
8+
9+
```js
10+
import { TextPrompt, isCancel } from '@clack/core';
11+
12+
const p = new TextPrompt({
13+
render() {
14+
return `What's your name?\n${this.valueWithCursor}`;
15+
}
16+
});
17+
18+
const name = await p.prompt();
19+
if (isCancel(name)) {
20+
process.exit(0);
21+
}
22+
```

packages/clack-core/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export { default as ConfirmPrompt } from './src/prompts/confirm.ts';
2+
export { default as GroupMultiSelectPrompt } from './src/prompts/group-multiselect.ts';
3+
export { default as MultiSelectPrompt } from './src/prompts/multi-select.ts';
4+
export { default as PasswordPrompt } from './src/prompts/password.ts';
5+
export { default as Prompt, isCancel } from './src/prompts/prompt.ts';
6+
export type { State } from './src/prompts/prompt.ts';
7+
export { default as SelectPrompt } from './src/prompts/select.ts';
8+
export { default as SelectKeyPrompt } from './src/prompts/select-key.ts';
9+
export { default as TextPrompt } from './src/prompts/text.ts';
10+
export { block } from './src/utils.ts';

packages/clack-core/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@sveltejs/clack-core",
3+
"private": true,
4+
"version": "0.4.2",
5+
"type": "module",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/sveltejs/cli/tree/main/packages/clack-prompts"
10+
},
11+
"bugs": "https://github.com/sveltejs/cli/issues",
12+
"scripts": {
13+
"check": "tsc",
14+
"format": "pnpm lint --write",
15+
"lint": "prettier --check . --config ../../prettier.config.js --ignore-path ../../.gitignore --ignore-path .gitignore --ignore-path ../../.prettierignore"
16+
},
17+
"files": [
18+
"dist"
19+
],
20+
"module": "./dist/index.mjs",
21+
"types": "./dist/index.d.ts",
22+
"exports": {
23+
".": {
24+
"types": "./dist/index.d.ts",
25+
"default": "./dist/index.js"
26+
},
27+
"./package.json": "./package.json"
28+
},
29+
"devDependencies": {
30+
"picocolors": "^1.1.1",
31+
"sisteransi": "^1.0.5",
32+
"wrap-ansi": "^8.1.0"
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { cursor } from 'sisteransi';
2+
import Prompt, { type PromptOptions } from './prompt.ts';
3+
4+
interface ConfirmOptions extends PromptOptions<ConfirmPrompt> {
5+
active: string;
6+
inactive: string;
7+
initialValue?: boolean;
8+
}
9+
export default class ConfirmPrompt extends Prompt {
10+
get cursor(): 0 | 1 {
11+
return this.value ? 0 : 1;
12+
}
13+
14+
private get _value() {
15+
return this.cursor === 0;
16+
}
17+
18+
constructor(opts: ConfirmOptions) {
19+
super(opts, false);
20+
this.value = opts.initialValue ? true : false;
21+
22+
this.on('value', () => {
23+
this.value = this._value;
24+
});
25+
26+
this.on('confirm', (confirm) => {
27+
this.output.write(cursor.move(0, -1));
28+
this.value = confirm;
29+
this.state = 'submit';
30+
this.close();
31+
});
32+
33+
this.on('cursor', () => {
34+
this.value = !this.value;
35+
});
36+
}
37+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import Prompt, { type PromptOptions } from './prompt.ts';
2+
3+
interface GroupMultiSelectOptions<T extends { value: any }>
4+
extends PromptOptions<GroupMultiSelectPrompt<T>> {
5+
options: Record<string, T[]>;
6+
initialValues?: Array<T['value']>;
7+
required?: boolean;
8+
cursorAt?: T['value'];
9+
selectableGroups?: boolean;
10+
}
11+
export default class GroupMultiSelectPrompt<T extends { value: any }> extends Prompt {
12+
options: Array<T & { group: string | boolean }>;
13+
cursor: number = 0;
14+
#selectableGroups: boolean;
15+
16+
getGroupItems(group: string): T[] {
17+
return this.options.filter((o) => o.group === group);
18+
}
19+
20+
isGroupSelected(group: string): boolean {
21+
const items = this.getGroupItems(group);
22+
return this.#selectableGroups && items.every((i) => this.value.includes(i.value));
23+
}
24+
25+
private toggleValue() {
26+
const item = this.options[this.cursor]!;
27+
if (item.group === true) {
28+
const group = item.value;
29+
const groupedItems = this.getGroupItems(group);
30+
if (this.isGroupSelected(group)) {
31+
this.value = this.value.filter(
32+
(v: string) => groupedItems.findIndex((i) => i.value === v) === -1
33+
);
34+
} else {
35+
this.value = [...this.value, ...groupedItems.map((i) => i.value)];
36+
}
37+
this.value = Array.from(new Set(this.value));
38+
} else {
39+
const selected = this.value.includes(item.value);
40+
this.value = selected
41+
? this.value.filter((v: T['value']) => v !== item.value)
42+
: [...this.value, item.value];
43+
}
44+
}
45+
46+
constructor(opts: GroupMultiSelectOptions<T>) {
47+
super(opts, false);
48+
const { options } = opts;
49+
this.#selectableGroups = opts.selectableGroups ?? true;
50+
this.options = Object.entries(options).flatMap(([key, option]) => [
51+
{ value: key, group: true, label: key },
52+
...option.map((opt) => ({ ...opt, group: key }))
53+
]) as any;
54+
this.value = [...(opts.initialValues ?? [])];
55+
this.cursor = Math.max(
56+
this.options.findIndex(({ value }) => value === opts.cursorAt),
57+
this.#selectableGroups ? 0 : 1
58+
);
59+
60+
this.on('cursor', (key) => {
61+
switch (key) {
62+
case 'left':
63+
case 'up':
64+
this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
65+
if (!this.#selectableGroups && this.options[this.cursor]!.group === true) {
66+
this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
67+
}
68+
break;
69+
case 'down':
70+
case 'right':
71+
this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
72+
if (!this.#selectableGroups && this.options[this.cursor]!.group === true) {
73+
this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
74+
}
75+
break;
76+
case 'space':
77+
this.toggleValue();
78+
break;
79+
}
80+
});
81+
}
82+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Prompt, { type PromptOptions } from './prompt.ts';
2+
3+
interface MultiSelectOptions<T extends { value: any }> extends PromptOptions<MultiSelectPrompt<T>> {
4+
options: T[];
5+
initialValues?: Array<T['value']>;
6+
required?: boolean;
7+
cursorAt?: T['value'];
8+
}
9+
export default class MultiSelectPrompt<T extends { value: any }> extends Prompt {
10+
options: T[];
11+
cursor: number = 0;
12+
13+
private get _value() {
14+
return this.options[this.cursor]!.value;
15+
}
16+
17+
private toggleAll() {
18+
const allSelected = this.value.length === this.options.length;
19+
this.value = allSelected ? [] : this.options.map((v) => v.value);
20+
}
21+
22+
private toggleValue() {
23+
const selected = this.value.includes(this._value);
24+
this.value = selected
25+
? this.value.filter((value: T['value']) => value !== this._value)
26+
: [...this.value, this._value];
27+
}
28+
29+
constructor(opts: MultiSelectOptions<T>) {
30+
super(opts, false);
31+
32+
this.options = opts.options;
33+
this.value = [...(opts.initialValues ?? [])];
34+
this.cursor = Math.max(
35+
this.options.findIndex(({ value }) => value === opts.cursorAt),
36+
0
37+
);
38+
this.on('key', (char) => {
39+
if (char === 'a') {
40+
this.toggleAll();
41+
}
42+
});
43+
44+
this.on('cursor', (key) => {
45+
switch (key) {
46+
case 'left':
47+
case 'up':
48+
this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
49+
break;
50+
case 'down':
51+
case 'right':
52+
this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
53+
break;
54+
case 'space':
55+
this.toggleValue();
56+
break;
57+
}
58+
});
59+
}
60+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import color from 'picocolors';
2+
import Prompt, { type PromptOptions } from './prompt.ts';
3+
4+
interface PasswordOptions extends PromptOptions<PasswordPrompt> {
5+
mask?: string;
6+
}
7+
export default class PasswordPrompt extends Prompt {
8+
valueWithCursor = '';
9+
private _mask = '•';
10+
get cursor(): number {
11+
return this._cursor;
12+
}
13+
get masked(): string {
14+
return this.value.replaceAll(/./g, this._mask);
15+
}
16+
constructor({ mask, ...opts }: PasswordOptions) {
17+
super(opts);
18+
this._mask = mask ?? '•';
19+
20+
this.on('finalize', () => {
21+
this.valueWithCursor = this.masked;
22+
});
23+
this.on('value', () => {
24+
if (this.cursor >= this.value.length) {
25+
this.valueWithCursor = `${this.masked}${color.inverse(color.hidden('_'))}`;
26+
} else {
27+
const s1 = this.masked.slice(0, this.cursor);
28+
const s2 = this.masked.slice(this.cursor);
29+
this.valueWithCursor = `${s1}${color.inverse(s2[0])}${s2.slice(1)}`;
30+
}
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)