meocli 0.0.1 → 0.0.3
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/README.md +13 -11
- package/dist/commands/pr/index.d.ts +18 -0
- package/dist/commands/pr/index.js +139 -0
- package/dist/consts/prettierrc.d.ts +3 -0
- package/dist/consts/prettierrc.js +84 -0
- package/dist/consts/prettierrc.tmp.d.ts +3 -0
- package/dist/consts/prettierrc.tmp.js +54 -0
- package/oclif.manifest.json +18 -9
- package/package.json +3 -2
- package/dist/commands/prettier/index.d.ts +0 -14
- package/dist/commands/prettier/index.js +0 -81
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ $ npm install -g meocli
|
|
|
32
32
|
$ me COMMAND
|
|
33
33
|
running command...
|
|
34
34
|
$ me (--version)
|
|
35
|
-
meocli/0.0.
|
|
35
|
+
meocli/0.0.3 win32-x64 node-v24.12.0
|
|
36
36
|
$ me --help [COMMAND]
|
|
37
37
|
USAGE
|
|
38
38
|
$ me COMMAND
|
|
@@ -56,7 +56,7 @@ USAGE
|
|
|
56
56
|
* [`me plugins uninstall [PLUGIN]`](#me-plugins-uninstall-plugin)
|
|
57
57
|
* [`me plugins unlink [PLUGIN]`](#me-plugins-unlink-plugin)
|
|
58
58
|
* [`me plugins update`](#me-plugins-update)
|
|
59
|
-
* [`me
|
|
59
|
+
* [`me pr FILEPATH`](#me-pr-filepath)
|
|
60
60
|
|
|
61
61
|
## `me hello PERSON`
|
|
62
62
|
|
|
@@ -80,7 +80,7 @@ EXAMPLES
|
|
|
80
80
|
hello friend from oclif! (./src/commands/hello/index.ts)
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
_See code: [src/commands/hello/index.ts](https://github.com/meme2046/meocli/blob/v0.0.
|
|
83
|
+
_See code: [src/commands/hello/index.ts](https://github.com/meme2046/meocli/blob/v0.0.3/src/commands/hello/index.ts)_
|
|
84
84
|
|
|
85
85
|
## `me hello world`
|
|
86
86
|
|
|
@@ -98,7 +98,7 @@ EXAMPLES
|
|
|
98
98
|
hello world! (./src/commands/hello/world.ts)
|
|
99
99
|
```
|
|
100
100
|
|
|
101
|
-
_See code: [src/commands/hello/world.ts](https://github.com/meme2046/meocli/blob/v0.0.
|
|
101
|
+
_See code: [src/commands/hello/world.ts](https://github.com/meme2046/meocli/blob/v0.0.3/src/commands/hello/world.ts)_
|
|
102
102
|
|
|
103
103
|
## `me help [COMMAND]`
|
|
104
104
|
|
|
@@ -410,29 +410,31 @@ DESCRIPTION
|
|
|
410
410
|
|
|
411
411
|
_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.4.54/src/commands/plugins/update.ts)_
|
|
412
412
|
|
|
413
|
-
## `me
|
|
413
|
+
## `me pr FILEPATH`
|
|
414
414
|
|
|
415
415
|
Use Prettier to format file
|
|
416
416
|
|
|
417
417
|
```
|
|
418
418
|
USAGE
|
|
419
|
-
$ me
|
|
419
|
+
$ me pr FILEPATH [-c <value>] [--ignore-path <value>] [--no-config] [--no-ignore]
|
|
420
420
|
|
|
421
421
|
ARGUMENTS
|
|
422
422
|
FILEPATH file path that need to be formatted by Prettier
|
|
423
423
|
|
|
424
424
|
FLAGS
|
|
425
|
-
-c, --config=<value>
|
|
426
|
-
|
|
425
|
+
-c, --config=<value> Prettier config file path
|
|
426
|
+
--ignore-path=<value> Prettier ignore file path
|
|
427
|
+
--no-config Disable config file detection
|
|
428
|
+
--no-ignore Disable ignore file detection
|
|
427
429
|
|
|
428
430
|
DESCRIPTION
|
|
429
431
|
Use Prettier to format file
|
|
430
432
|
|
|
431
433
|
EXAMPLES
|
|
432
|
-
$ me
|
|
434
|
+
$ me pr ./tests/test.json
|
|
433
435
|
|
|
434
|
-
$ me
|
|
436
|
+
$ me pr ./src/file.ts --config ./.prettierrc.yaml
|
|
435
437
|
```
|
|
436
438
|
|
|
437
|
-
_See code: [src/commands/
|
|
439
|
+
_See code: [src/commands/pr/index.ts](https://github.com/meme2046/meocli/blob/v0.0.3/src/commands/pr/index.ts)_
|
|
438
440
|
<!-- commandsstop -->
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
export default class Prettier extends Command {
|
|
3
|
+
static args: {
|
|
4
|
+
filePath: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
5
|
+
};
|
|
6
|
+
static description: string;
|
|
7
|
+
static examples: string[];
|
|
8
|
+
static flags: {
|
|
9
|
+
config: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
+
'ignore-path': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
'no-config': import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
12
|
+
'no-ignore': import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
13
|
+
};
|
|
14
|
+
run(): Promise<void>;
|
|
15
|
+
private detectPackageManager;
|
|
16
|
+
private findProjectPrettierConfig;
|
|
17
|
+
private findProjectPrettierIgnore;
|
|
18
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Args, Command, Flags } from '@oclif/core';
|
|
2
|
+
import { execa } from 'execa';
|
|
3
|
+
// import {spawn} from 'node:child_process'
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { platform } from 'node:os';
|
|
6
|
+
import { dirname, join } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { DEFAULT_ARGS } from '../../consts/prettierrc.js';
|
|
9
|
+
export default class Prettier extends Command {
|
|
10
|
+
static args = {
|
|
11
|
+
filePath: Args.string({
|
|
12
|
+
description: 'file path that need to be formatted by Prettier',
|
|
13
|
+
required: true,
|
|
14
|
+
}),
|
|
15
|
+
};
|
|
16
|
+
static description = 'Use Prettier to format file';
|
|
17
|
+
static examples = [
|
|
18
|
+
'<%= config.bin %> <%= command.id %> ./tests/test.json',
|
|
19
|
+
'<%= config.bin %> <%= command.id %> ./src/file.ts --config ./.prettierrc.yaml',
|
|
20
|
+
];
|
|
21
|
+
static flags = {
|
|
22
|
+
config: Flags.string({
|
|
23
|
+
char: 'c',
|
|
24
|
+
// default: './src/files/.prettierrc.yaml',
|
|
25
|
+
description: 'Prettier config file path',
|
|
26
|
+
required: false,
|
|
27
|
+
}),
|
|
28
|
+
'ignore-path': Flags.string({
|
|
29
|
+
description: 'Prettier ignore file path',
|
|
30
|
+
required: false,
|
|
31
|
+
}),
|
|
32
|
+
'no-config': Flags.boolean({
|
|
33
|
+
default: false,
|
|
34
|
+
description: 'Disable config file detection',
|
|
35
|
+
}),
|
|
36
|
+
'no-ignore': Flags.boolean({
|
|
37
|
+
default: false,
|
|
38
|
+
description: 'Disable ignore file detection',
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
async run() {
|
|
42
|
+
const { args, flags } = await this.parse(Prettier);
|
|
43
|
+
const { filePath } = args;
|
|
44
|
+
const { config, 'ignore-path': ignorePath, 'no-config': noConfig, 'no-ignore': noIgnore } = flags;
|
|
45
|
+
// 检查文件是否存在
|
|
46
|
+
if (!existsSync(filePath)) {
|
|
47
|
+
this.error(`file『${filePath}』not found`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
// 检测当前使用的包管理器
|
|
51
|
+
// const packageManager = this.detectPackageManager()
|
|
52
|
+
// 获取 prettier 可执行文件的路径
|
|
53
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
54
|
+
const __dirname = dirname(__filename);
|
|
55
|
+
const projectRoot = join(__dirname, '../../../'); // 回到项目根目录
|
|
56
|
+
// let prettierBin = join(projectRoot, 'node_modules', 'prettier', 'node_modules', '.bin', 'prettier')
|
|
57
|
+
let prettierBin = join(projectRoot, 'node_modules', '.bin', 'prettier');
|
|
58
|
+
if (platform() === 'win32') {
|
|
59
|
+
prettierBin += '.cmd';
|
|
60
|
+
}
|
|
61
|
+
// this.log(prettierBin)
|
|
62
|
+
if (!existsSync(prettierBin)) {
|
|
63
|
+
this.error(`prettier not found`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
// 根据包管理器类型构建参数
|
|
67
|
+
let prettierArgs = ['--write', filePath];
|
|
68
|
+
if (noIgnore) {
|
|
69
|
+
prettierArgs.unshift('--ignore-path', '');
|
|
70
|
+
}
|
|
71
|
+
else if (ignorePath && existsSync(ignorePath)) {
|
|
72
|
+
prettierArgs.unshift('--ignore-path', ignorePath);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const projectIgnore = this.findProjectPrettierIgnore();
|
|
76
|
+
if (projectIgnore) {
|
|
77
|
+
prettierArgs.unshift('--ignore-path', projectIgnore);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (noConfig) {
|
|
81
|
+
prettierArgs = [...DEFAULT_ARGS, ...prettierArgs];
|
|
82
|
+
}
|
|
83
|
+
else if (config && existsSync(config)) {
|
|
84
|
+
prettierArgs.unshift('--config', config);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
// 否则尝试查找项目中的配置文件
|
|
88
|
+
const projectConfig = this.findProjectPrettierConfig();
|
|
89
|
+
if (projectConfig) {
|
|
90
|
+
prettierArgs.unshift('--config', projectConfig);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
prettierArgs = [...DEFAULT_ARGS, ...prettierArgs];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// this.log(JSON.stringify(prettierArgs))
|
|
97
|
+
// this.log(`Formatting file: ${filePath}`)
|
|
98
|
+
const { stderr, stdout } = await execa(prettierBin, prettierArgs, {
|
|
99
|
+
env: { ...process.env },
|
|
100
|
+
});
|
|
101
|
+
if (stdout) {
|
|
102
|
+
this.log(stdout);
|
|
103
|
+
}
|
|
104
|
+
if (stderr) {
|
|
105
|
+
this.warn(stderr);
|
|
106
|
+
}
|
|
107
|
+
this.log(`Successfully formatted ${filePath}`);
|
|
108
|
+
}
|
|
109
|
+
detectPackageManager() {
|
|
110
|
+
// 检查项目根目录是否存在 pnpm-lock.yaml 来判断是否使用 pnpm
|
|
111
|
+
if (existsSync(join(process.cwd(), 'pnpm-lock.yaml'))) {
|
|
112
|
+
return 'pnpx';
|
|
113
|
+
}
|
|
114
|
+
// 检查是否存在 yarn.lock 来判断是否使用 yarn
|
|
115
|
+
if (existsSync(join(process.cwd(), 'yarn.lock'))) {
|
|
116
|
+
return 'yarn';
|
|
117
|
+
}
|
|
118
|
+
// 默认使用 npm
|
|
119
|
+
return 'npx';
|
|
120
|
+
}
|
|
121
|
+
findProjectPrettierConfig() {
|
|
122
|
+
const possibleConfigs = ['.prettierrc', '.prettierrc.json', '.prettierrc.yaml', '.prettierrc.yml', '.prettierrc.js'];
|
|
123
|
+
for (const configFile of possibleConfigs) {
|
|
124
|
+
if (existsSync(configFile)) {
|
|
125
|
+
return configFile;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
findProjectPrettierIgnore() {
|
|
131
|
+
const possibleIgnoreFiles = ['.prettierignore', '.gitignore'];
|
|
132
|
+
for (const ignoreFile of possibleIgnoreFiles) {
|
|
133
|
+
if (existsSync(ignoreFile)) {
|
|
134
|
+
return ignoreFile;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export const DEFAULT_ARGS = [
|
|
2
|
+
'--arrow-parens',
|
|
3
|
+
'always',
|
|
4
|
+
'--bracket-same-line',
|
|
5
|
+
'false',
|
|
6
|
+
'--object-wrap',
|
|
7
|
+
'preserve',
|
|
8
|
+
'--experimental-operator-position',
|
|
9
|
+
'end',
|
|
10
|
+
'--no-experimental-ternaries',
|
|
11
|
+
'--single-quote',
|
|
12
|
+
'false',
|
|
13
|
+
'--jsx-single-quote',
|
|
14
|
+
'false',
|
|
15
|
+
'--quote-props',
|
|
16
|
+
'as-needed',
|
|
17
|
+
'--trailing-comma',
|
|
18
|
+
'all',
|
|
19
|
+
'--single-attribute-per-line',
|
|
20
|
+
'false',
|
|
21
|
+
'--html-whitespace-sensitivity',
|
|
22
|
+
'css',
|
|
23
|
+
'--vue-indent-script-and-style',
|
|
24
|
+
'false',
|
|
25
|
+
'--prose-wrap',
|
|
26
|
+
'preserve',
|
|
27
|
+
'--end-of-line',
|
|
28
|
+
'lf',
|
|
29
|
+
'--insert-pragma',
|
|
30
|
+
'false',
|
|
31
|
+
'--print-width',
|
|
32
|
+
'80',
|
|
33
|
+
'--require-pragma',
|
|
34
|
+
'false',
|
|
35
|
+
'--tab-width',
|
|
36
|
+
'2',
|
|
37
|
+
'--use-tabs',
|
|
38
|
+
'false',
|
|
39
|
+
'--embedded-language-formatting',
|
|
40
|
+
'auto',
|
|
41
|
+
// plugins
|
|
42
|
+
'--plugin',
|
|
43
|
+
'@prettier/plugin-xml',
|
|
44
|
+
'--plugin',
|
|
45
|
+
'prettier-plugin-toml',
|
|
46
|
+
'--plugin',
|
|
47
|
+
'prettier-plugin-sh',
|
|
48
|
+
'--plugin',
|
|
49
|
+
'prettier-plugin-nginx',
|
|
50
|
+
// xml
|
|
51
|
+
'--xml-whitespace-sensitivity',
|
|
52
|
+
'strict',
|
|
53
|
+
'--xml-sort-attributes-by-key',
|
|
54
|
+
'true',
|
|
55
|
+
'--xml-self-closing-space',
|
|
56
|
+
'true',
|
|
57
|
+
// toml
|
|
58
|
+
'--indent-entries',
|
|
59
|
+
'true',
|
|
60
|
+
'--reorder-keys',
|
|
61
|
+
'true',
|
|
62
|
+
// nginx
|
|
63
|
+
'--wrap-parameters',
|
|
64
|
+
'false',
|
|
65
|
+
'--continuation-indent',
|
|
66
|
+
'2',
|
|
67
|
+
];
|
|
68
|
+
// 默认的 Prettier 插件列表
|
|
69
|
+
export const DEFAULT_PLUGINS = [
|
|
70
|
+
'@prettier/plugin-xml',
|
|
71
|
+
'prettier-plugin-toml',
|
|
72
|
+
'prettier-plugin-nginx',
|
|
73
|
+
'prettier-plugin-sh',
|
|
74
|
+
];
|
|
75
|
+
// 默认的忽略模式
|
|
76
|
+
export const DEFAULT_IGNORE_PATTERNS = [
|
|
77
|
+
'node_modules/',
|
|
78
|
+
'dist/',
|
|
79
|
+
'build/',
|
|
80
|
+
'.nyc_output/',
|
|
81
|
+
'coverage/',
|
|
82
|
+
'.next/',
|
|
83
|
+
'out/',
|
|
84
|
+
];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export const DEFAULT_ARGS = [
|
|
2
|
+
'--arrow-parens=always',
|
|
3
|
+
'--bracket-same-line=false',
|
|
4
|
+
'--object-wrap=preserve',
|
|
5
|
+
'--experimental-operator-position=end',
|
|
6
|
+
'--no-experimental-ternaries',
|
|
7
|
+
'--single-quote=false',
|
|
8
|
+
'--jsx-single-quote=false',
|
|
9
|
+
'--quote-props=as-needed',
|
|
10
|
+
'--trailing-comma=all',
|
|
11
|
+
'--single-attribute-per-line=false',
|
|
12
|
+
'--html-whitespace-sensitivity=css',
|
|
13
|
+
'--vue-indent-script-and-style=false',
|
|
14
|
+
'--prose-wrap=preserve',
|
|
15
|
+
'--end-of-line=lf',
|
|
16
|
+
'--insert-pragma=false',
|
|
17
|
+
'--print-width=80',
|
|
18
|
+
'--require-pragma=false',
|
|
19
|
+
'--tab-width=2',
|
|
20
|
+
'--use-tabs=false',
|
|
21
|
+
'--embedded-language-formatting=auto',
|
|
22
|
+
// plugins
|
|
23
|
+
'--plugin=@prettier/plugin-xml',
|
|
24
|
+
'--plugin=prettier-plugin-toml',
|
|
25
|
+
'--plugin=prettier-plugin-sh',
|
|
26
|
+
'--plugin=prettier-plugin-nginx',
|
|
27
|
+
// xml
|
|
28
|
+
'--xml-whitespace-sensitivity=strict',
|
|
29
|
+
'--xml-sort-attributes-by-key=true',
|
|
30
|
+
'--xml-self-closing-space=true',
|
|
31
|
+
// toml
|
|
32
|
+
'--indent-entries=true',
|
|
33
|
+
'--reorder-keys=true',
|
|
34
|
+
// nginx
|
|
35
|
+
'--wrap-parameters=false',
|
|
36
|
+
'--continuation-indent=2',
|
|
37
|
+
];
|
|
38
|
+
// 默认的 Prettier 插件列表
|
|
39
|
+
export const DEFAULT_PLUGINS = [
|
|
40
|
+
'@prettier/plugin-xml',
|
|
41
|
+
'prettier-plugin-toml',
|
|
42
|
+
'prettier-plugin-nginx',
|
|
43
|
+
'prettier-plugin-sh',
|
|
44
|
+
];
|
|
45
|
+
// 默认的忽略模式
|
|
46
|
+
export const DEFAULT_IGNORE_PATTERNS = [
|
|
47
|
+
'node_modules/',
|
|
48
|
+
'dist/',
|
|
49
|
+
'build/',
|
|
50
|
+
'.nyc_output/',
|
|
51
|
+
'coverage/',
|
|
52
|
+
'.next/',
|
|
53
|
+
'out/',
|
|
54
|
+
];
|
package/oclif.manifest.json
CHANGED
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"world.js"
|
|
65
65
|
]
|
|
66
66
|
},
|
|
67
|
-
"
|
|
67
|
+
"pr": {
|
|
68
68
|
"aliases": [],
|
|
69
69
|
"args": {
|
|
70
70
|
"filePath": {
|
|
@@ -84,25 +84,34 @@
|
|
|
84
84
|
"description": "Prettier config file path",
|
|
85
85
|
"name": "config",
|
|
86
86
|
"required": false,
|
|
87
|
-
"default": "./src/files/.prettierrc.yaml",
|
|
88
87
|
"hasDynamicHelp": false,
|
|
89
88
|
"multiple": false,
|
|
90
89
|
"type": "option"
|
|
91
90
|
},
|
|
92
|
-
"ignore": {
|
|
93
|
-
"char": "i",
|
|
91
|
+
"ignore-path": {
|
|
94
92
|
"description": "Prettier ignore file path",
|
|
95
|
-
"name": "ignore",
|
|
93
|
+
"name": "ignore-path",
|
|
96
94
|
"required": false,
|
|
97
|
-
"default": "./src/files/.prettierignore",
|
|
98
95
|
"hasDynamicHelp": false,
|
|
99
96
|
"multiple": false,
|
|
100
97
|
"type": "option"
|
|
98
|
+
},
|
|
99
|
+
"no-config": {
|
|
100
|
+
"description": "Disable config file detection",
|
|
101
|
+
"name": "no-config",
|
|
102
|
+
"allowNo": false,
|
|
103
|
+
"type": "boolean"
|
|
104
|
+
},
|
|
105
|
+
"no-ignore": {
|
|
106
|
+
"description": "Disable ignore file detection",
|
|
107
|
+
"name": "no-ignore",
|
|
108
|
+
"allowNo": false,
|
|
109
|
+
"type": "boolean"
|
|
101
110
|
}
|
|
102
111
|
},
|
|
103
112
|
"hasDynamicHelp": false,
|
|
104
113
|
"hiddenAliases": [],
|
|
105
|
-
"id": "
|
|
114
|
+
"id": "pr",
|
|
106
115
|
"pluginAlias": "meocli",
|
|
107
116
|
"pluginName": "meocli",
|
|
108
117
|
"pluginType": "core",
|
|
@@ -112,10 +121,10 @@
|
|
|
112
121
|
"relativePath": [
|
|
113
122
|
"dist",
|
|
114
123
|
"commands",
|
|
115
|
-
"
|
|
124
|
+
"pr",
|
|
116
125
|
"index.js"
|
|
117
126
|
]
|
|
118
127
|
}
|
|
119
128
|
},
|
|
120
|
-
"version": "0.0.
|
|
129
|
+
"version": "0.0.3"
|
|
121
130
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "meocli",
|
|
3
3
|
"description": "A new CLI generated with oclif",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"author": "meme2046",
|
|
6
6
|
"bin": {
|
|
7
7
|
"me": "./bin/run.js"
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"@oclif/plugin-help": "^6",
|
|
13
13
|
"@oclif/plugin-plugins": "^5",
|
|
14
14
|
"@prettier/plugin-xml": "^3.4.2",
|
|
15
|
+
"execa": "^9.6.1",
|
|
15
16
|
"prettier": "^3.7.4",
|
|
16
17
|
"prettier-plugin-nginx": "^1.0.3",
|
|
17
18
|
"prettier-plugin-sh": "^0.18.0",
|
|
@@ -67,7 +68,7 @@
|
|
|
67
68
|
"repository": "meme2046/meocli",
|
|
68
69
|
"types": "dist/index.d.ts",
|
|
69
70
|
"scripts": {
|
|
70
|
-
"dev": "node bin/dev.js",
|
|
71
|
+
"dev": "node --no-deprecation --no-warnings --loader ts-node/esm --disable-warning=ExperimentalWarning bin/dev.js",
|
|
71
72
|
"prod": "node bin/run.js",
|
|
72
73
|
"prettier": "prettier",
|
|
73
74
|
"build": "shx rm -rf dist && tsc -b",
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Command } from '@oclif/core';
|
|
2
|
-
export default class Prettier extends Command {
|
|
3
|
-
static args: {
|
|
4
|
-
filePath: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
5
|
-
};
|
|
6
|
-
static description: string;
|
|
7
|
-
static examples: string[];
|
|
8
|
-
static flags: {
|
|
9
|
-
config: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
|
-
ignore: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
-
};
|
|
12
|
-
run(): Promise<void>;
|
|
13
|
-
private detectPackageManager;
|
|
14
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { Args, Command, Flags } from '@oclif/core';
|
|
2
|
-
import { spawn } from 'node:child_process';
|
|
3
|
-
import { existsSync } from 'node:fs';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
export default class Prettier extends Command {
|
|
6
|
-
static args = {
|
|
7
|
-
filePath: Args.string({
|
|
8
|
-
description: 'file path that need to be formatted by Prettier',
|
|
9
|
-
required: true,
|
|
10
|
-
}),
|
|
11
|
-
};
|
|
12
|
-
static description = 'Use Prettier to format file';
|
|
13
|
-
static examples = [
|
|
14
|
-
'<%= config.bin %> <%= command.id %> ./tests/test.json',
|
|
15
|
-
'<%= config.bin %> <%= command.id %> ./src/file.ts --config ./.prettierrc.yaml',
|
|
16
|
-
];
|
|
17
|
-
static flags = {
|
|
18
|
-
config: Flags.string({
|
|
19
|
-
char: 'c',
|
|
20
|
-
default: './src/files/.prettierrc.yaml',
|
|
21
|
-
description: 'Prettier config file path',
|
|
22
|
-
required: false,
|
|
23
|
-
}),
|
|
24
|
-
ignore: Flags.string({
|
|
25
|
-
char: 'i',
|
|
26
|
-
default: './src/files/.prettierignore',
|
|
27
|
-
description: 'Prettier ignore file path',
|
|
28
|
-
required: false,
|
|
29
|
-
}),
|
|
30
|
-
};
|
|
31
|
-
async run() {
|
|
32
|
-
const { args, flags } = await this.parse(Prettier);
|
|
33
|
-
const { filePath } = args;
|
|
34
|
-
const { config, ignore } = flags;
|
|
35
|
-
// 检测当前使用的包管理器
|
|
36
|
-
const packageManager = this.detectPackageManager();
|
|
37
|
-
// 根据包管理器类型构建参数
|
|
38
|
-
const prettierArgs = [
|
|
39
|
-
'prettier',
|
|
40
|
-
'--write',
|
|
41
|
-
filePath,
|
|
42
|
-
...(config ? [`--config=${config}`] : []),
|
|
43
|
-
...(ignore ? [`--ignore-path=${ignore}`] : []),
|
|
44
|
-
];
|
|
45
|
-
// this.log(`Formatting file: ${filePath}`)
|
|
46
|
-
// 返回一个 Promise 以等待子进程完成
|
|
47
|
-
await new Promise((resolve, reject) => {
|
|
48
|
-
const prettierProcess = spawn(packageManager, prettierArgs, {
|
|
49
|
-
env: { ...process.env }, // 确保子进程继承当前环境变量
|
|
50
|
-
shell: false, // 设置为 false 以避免安全警告
|
|
51
|
-
stdio: 'inherit',
|
|
52
|
-
});
|
|
53
|
-
prettierProcess.on('close', (code) => {
|
|
54
|
-
if (code === 0) {
|
|
55
|
-
this.log(`Successfully formatted ${filePath}`);
|
|
56
|
-
resolve(null);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
reject(new Error(`Prettier exited with code ${code}`));
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
prettierProcess.on('error', (error) => {
|
|
63
|
-
reject(error);
|
|
64
|
-
});
|
|
65
|
-
}).catch((error) => {
|
|
66
|
-
this.error(`Error executing prettier: ${error.message}`);
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
detectPackageManager() {
|
|
70
|
-
// 检查项目根目录是否存在 pnpm-lock.yaml 来判断是否使用 pnpm
|
|
71
|
-
if (existsSync(join(process.cwd(), 'pnpm-lock.yaml'))) {
|
|
72
|
-
return 'pnpx';
|
|
73
|
-
}
|
|
74
|
-
// 检查是否存在 yarn.lock 来判断是否使用 yarn
|
|
75
|
-
if (existsSync(join(process.cwd(), 'yarn.lock'))) {
|
|
76
|
-
return 'yarn';
|
|
77
|
-
}
|
|
78
|
-
// 默认使用 npm
|
|
79
|
-
return 'npx';
|
|
80
|
-
}
|
|
81
|
-
}
|