@markuplint/cli-utils 4.0.0-alpha.10
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.
- package/LICENSE +21 -0
- package/README.md +18 -0
- package/lib/const.d.ts +6 -0
- package/lib/const.js +6 -0
- package/lib/get-width.d.ts +1 -0
- package/lib/get-width.js +12 -0
- package/lib/header.d.ts +1 -0
- package/lib/header.js +38 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +10 -0
- package/lib/install-module.d.ts +5 -0
- package/lib/install-module.js +64 -0
- package/lib/invisible-space.d.ts +7 -0
- package/lib/invisible-space.js +13 -0
- package/lib/logo.d.ts +1 -0
- package/lib/logo.js +3 -0
- package/lib/message-to-string.d.ts +1 -0
- package/lib/message-to-string.js +6 -0
- package/lib/name.d.ts +1 -0
- package/lib/name.js +3 -0
- package/lib/pad.d.ts +1 -0
- package/lib/pad.js +7 -0
- package/lib/prompt.d.ts +18 -0
- package/lib/prompt.js +67 -0
- package/lib/space.d.ts +7 -0
- package/lib/space.js +15 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-2024 Yusuke Hirao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @markuplint/cli-utils
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@markuplint/cli-utils)
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
[`markuplint`](https://www.npmjs.com/package/markuplint) package includes this package.
|
|
8
|
+
|
|
9
|
+
<details>
|
|
10
|
+
<summary>If you are installing purposely, how below:</summary>
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
$ npm install @markuplint/cli-utils
|
|
14
|
+
|
|
15
|
+
$ yarn add @markuplint/cli-utils
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
</details>
|
package/lib/const.d.ts
ADDED
package/lib/const.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getWidth(s: string): number;
|
package/lib/get-width.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import eastasianwidth from 'eastasianwidth';
|
|
3
|
+
const eaw = eastasianwidth;
|
|
4
|
+
export function getWidth(s) {
|
|
5
|
+
return s.replaceAll(
|
|
6
|
+
// All characters
|
|
7
|
+
/./g,
|
|
8
|
+
// Wide characters to multi dots
|
|
9
|
+
char => '•'.repeat(
|
|
10
|
+
// Get the number of character width
|
|
11
|
+
eaw.characterLength(char))).length;
|
|
12
|
+
}
|
package/lib/header.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const header: (subCommandName: string, noColor?: boolean) => string;
|
package/lib/header.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import module from 'node:module';
|
|
2
|
+
import c from 'cli-color';
|
|
3
|
+
import stripAnsi from 'strip-ansi';
|
|
4
|
+
import { logo } from './logo.js';
|
|
5
|
+
import { name } from './name.js';
|
|
6
|
+
const require = module.createRequire(import.meta.url);
|
|
7
|
+
const version = require('../package.json').version;
|
|
8
|
+
export const header = (subCommandName, noColor) => box([
|
|
9
|
+
// Logo and name
|
|
10
|
+
`${logo} ${name}`,
|
|
11
|
+
// Version
|
|
12
|
+
c.blackBright(`v${version}`),
|
|
13
|
+
// Break line
|
|
14
|
+
'',
|
|
15
|
+
// Command name
|
|
16
|
+
c.bold(subCommandName),
|
|
17
|
+
], {
|
|
18
|
+
center: true,
|
|
19
|
+
noColor,
|
|
20
|
+
});
|
|
21
|
+
function box(lines, { width = 40, padding = 1, center = false, noColor = false }) {
|
|
22
|
+
const bt = `┌${'─'.repeat(width - 2)}┐`;
|
|
23
|
+
const pd = `│${' '.repeat(width - 2)}│`;
|
|
24
|
+
const bb = `└${'─'.repeat(width - 2)}┘`;
|
|
25
|
+
const texts = lines.map(line => {
|
|
26
|
+
const nc = stripAnsi(line);
|
|
27
|
+
const length = nc.length;
|
|
28
|
+
const pad = width - 2 - 1 - length;
|
|
29
|
+
const pad2 = Math.floor(pad / 2);
|
|
30
|
+
const padD = pad % 2;
|
|
31
|
+
const padL = center ? pad2 : 0;
|
|
32
|
+
const padR = center ? pad2 + padD : pad;
|
|
33
|
+
const text = noColor ? nc : line;
|
|
34
|
+
return `│ ${' '.repeat(padL)}${text}${' '.repeat(padR)}│`;
|
|
35
|
+
});
|
|
36
|
+
const result = [bt, pd, ...texts, pd, bb];
|
|
37
|
+
return result.join('\n');
|
|
38
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { default as font } from 'cli-color';
|
|
2
|
+
export { input, confirm, confirmSequence, select, multiSelect } from './prompt.js';
|
|
3
|
+
export { installModule, InstallModuleResult } from './install-module.js';
|
|
4
|
+
export { getWidth } from './get-width.js';
|
|
5
|
+
export { header } from './header.js';
|
|
6
|
+
export { invisibleSpace } from './invisible-space.js';
|
|
7
|
+
export { messageToString } from './message-to-string.js';
|
|
8
|
+
export { name } from './name.js';
|
|
9
|
+
export { pad } from './pad.js';
|
|
10
|
+
export { space } from './space.js';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { default as font } from 'cli-color';
|
|
2
|
+
export { input, confirm, confirmSequence, select, multiSelect } from './prompt.js';
|
|
3
|
+
export { installModule } from './install-module.js';
|
|
4
|
+
export { getWidth } from './get-width.js';
|
|
5
|
+
export { header } from './header.js';
|
|
6
|
+
export { invisibleSpace } from './invisible-space.js';
|
|
7
|
+
export { messageToString } from './message-to-string.js';
|
|
8
|
+
export { name } from './name.js';
|
|
9
|
+
export { pad } from './pad.js';
|
|
10
|
+
export { space } from './space.js';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import c from 'cli-color';
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import detectInstalled from 'detect-installed';
|
|
5
|
+
import hasYarn from 'has-yarn';
|
|
6
|
+
export async function installModule(module, dev = false) {
|
|
7
|
+
module = module.map(m => m.trim());
|
|
8
|
+
const uninstallMods = [];
|
|
9
|
+
try {
|
|
10
|
+
for (const mod of module) {
|
|
11
|
+
const installed = await isInstalled(mod);
|
|
12
|
+
if (!installed) {
|
|
13
|
+
uninstallMods.push(mod);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// void
|
|
19
|
+
}
|
|
20
|
+
if (uninstallMods.length === 0) {
|
|
21
|
+
return {
|
|
22
|
+
success: true,
|
|
23
|
+
alreadyExists: true,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const mod = hasYarn() ? 'yarn' : 'npm';
|
|
27
|
+
const installOpt = hasYarn() ? 'add' : 'install';
|
|
28
|
+
const opt = [installOpt];
|
|
29
|
+
if (dev) {
|
|
30
|
+
opt.push('-D');
|
|
31
|
+
}
|
|
32
|
+
if (!hasYarn()) {
|
|
33
|
+
opt.push('--legacy-peer-deps');
|
|
34
|
+
}
|
|
35
|
+
opt.push(...uninstallMods);
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
process.stdout.write(c.blackBright(`${mod} ${opt.join(' ')}\n`));
|
|
38
|
+
const result = spawnSync(mod, opt, { stdio: 'inherit' });
|
|
39
|
+
if (result.error || result.status !== 0) {
|
|
40
|
+
const message = 'Error running command.';
|
|
41
|
+
const error = new Error(message);
|
|
42
|
+
error.stack = message;
|
|
43
|
+
reject(error);
|
|
44
|
+
}
|
|
45
|
+
resolve({
|
|
46
|
+
success: true,
|
|
47
|
+
alreadyExists: false,
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function isInstalled(module) {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
try {
|
|
54
|
+
detectInstalled(module, {
|
|
55
|
+
local: true,
|
|
56
|
+
}).then((exists) => {
|
|
57
|
+
resolve(exists);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
reject(error);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
package/lib/logo.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const logo: string;
|
package/lib/logo.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function messageToString(message: string, reason?: string): string;
|
package/lib/name.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const name: string;
|
package/lib/name.js
ADDED
package/lib/pad.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function pad(s: number | string, pad: number, start?: boolean): string;
|
package/lib/pad.js
ADDED
package/lib/prompt.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type SelectQuestion<T> = {
|
|
2
|
+
readonly message: string;
|
|
3
|
+
readonly choices: readonly {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly value: T;
|
|
6
|
+
}[];
|
|
7
|
+
};
|
|
8
|
+
export declare function select<T>(question: SelectQuestion<T>): Promise<T>;
|
|
9
|
+
export declare function multiSelect<T>(question: SelectQuestion<T>): Promise<T[]>;
|
|
10
|
+
export declare function input<T extends string = string>(question: string, validation?: Readonly<RegExp>): Promise<T>;
|
|
11
|
+
export declare function confirm(question: string, options?: {
|
|
12
|
+
readonly initial?: boolean;
|
|
13
|
+
}): Promise<boolean>;
|
|
14
|
+
export declare function confirmSequence<T extends string = string>(questions: readonly {
|
|
15
|
+
readonly message: string;
|
|
16
|
+
readonly name: T;
|
|
17
|
+
}[]): Promise<Record<T, boolean>>;
|
|
18
|
+
export {};
|
package/lib/prompt.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import c from 'cli-color';
|
|
2
|
+
import Enquirer from 'enquirer';
|
|
3
|
+
export async function select(question) {
|
|
4
|
+
const res = await Enquirer.prompt({
|
|
5
|
+
...question,
|
|
6
|
+
name: '__Q__',
|
|
7
|
+
type: 'select',
|
|
8
|
+
result(resName) {
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
return this.options.choices.find(c => c.name === resName)?.value;
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
return res['__Q__'];
|
|
15
|
+
}
|
|
16
|
+
export async function multiSelect(question) {
|
|
17
|
+
const res = await Enquirer.prompt({
|
|
18
|
+
...question,
|
|
19
|
+
name: '__Q__',
|
|
20
|
+
type: 'multiselect',
|
|
21
|
+
result(names) {
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
const map = this.map(names);
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
const values = names.map(name => map[name]);
|
|
26
|
+
return values;
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
return res['__Q__'];
|
|
31
|
+
}
|
|
32
|
+
export async function input(question, validation) {
|
|
33
|
+
// eslint-disable-next-line no-constant-condition
|
|
34
|
+
while (true) {
|
|
35
|
+
const _res = await Enquirer.prompt({
|
|
36
|
+
message: question,
|
|
37
|
+
name: '__Q__',
|
|
38
|
+
type: 'input',
|
|
39
|
+
});
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
const res = _res['__Q__'];
|
|
42
|
+
if (validation && !validation.test(res)) {
|
|
43
|
+
process.stdout.write(c.yellow('Oops! The name that you type is an invalid format.\n'));
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
return res;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export async function confirm(question, options) {
|
|
50
|
+
const res = await Enquirer.prompt({
|
|
51
|
+
message: question,
|
|
52
|
+
name: '__Q__',
|
|
53
|
+
type: 'confirm',
|
|
54
|
+
initial: !!options?.initial,
|
|
55
|
+
});
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
return !!res['__Q__'];
|
|
58
|
+
}
|
|
59
|
+
export async function confirmSequence(questions) {
|
|
60
|
+
const res = await Enquirer.prompt(questions.map(question => {
|
|
61
|
+
return {
|
|
62
|
+
...question,
|
|
63
|
+
type: 'confirm',
|
|
64
|
+
};
|
|
65
|
+
}));
|
|
66
|
+
return res;
|
|
67
|
+
}
|
package/lib/space.d.ts
ADDED
package/lib/space.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import c from 'cli-color';
|
|
2
|
+
/**
|
|
3
|
+
* Replace space to visible characters.
|
|
4
|
+
*
|
|
5
|
+
* @param str
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export function space(str) {
|
|
9
|
+
return str
|
|
10
|
+
.replaceAll(/\s+/g, $0 => {
|
|
11
|
+
return c.xterm(8)($0);
|
|
12
|
+
})
|
|
13
|
+
.replaceAll(' ', $0 => '•')
|
|
14
|
+
.replaceAll('\t', $0 => '→ ');
|
|
15
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@markuplint/cli-utils",
|
|
3
|
+
"version": "4.0.0-alpha.10",
|
|
4
|
+
"description": "Utilities for CLI of Markuplint",
|
|
5
|
+
"repository": "git@github.com:markuplint/markuplint.git",
|
|
6
|
+
"author": "Yusuke Hirao <yusukehirao@me.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"private": false,
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./lib/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"types": "./lib/index.d.ts",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"dev": "tsc --build --watch",
|
|
22
|
+
"clean": "tsc --build --clean"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"cli-color": "^2.0.3",
|
|
26
|
+
"detect-installed": "^2.0.4",
|
|
27
|
+
"eastasianwidth": "^0.2.0",
|
|
28
|
+
"enquirer": "^2.4.1",
|
|
29
|
+
"has-yarn": "^3.0.0",
|
|
30
|
+
"strip-ansi": "^7.1.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/cli-color": "^2.0.6"
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "b41153ea665aa8f091daf6114a06047f4ccb8350"
|
|
36
|
+
}
|