meocli 0.0.1 → 0.0.2

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 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.1 win32-x64 node-v24.12.0
35
+ meocli/0.0.2 win32-x64 node-v24.12.0
36
36
  $ me --help [COMMAND]
37
37
  USAGE
38
38
  $ me COMMAND
@@ -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.1/src/commands/hello/index.ts)_
83
+ _See code: [src/commands/hello/index.ts](https://github.com/meme2046/meocli/blob/v0.0.2/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.1/src/commands/hello/world.ts)_
101
+ _See code: [src/commands/hello/world.ts](https://github.com/meme2046/meocli/blob/v0.0.2/src/commands/hello/world.ts)_
102
102
 
103
103
  ## `me help [COMMAND]`
104
104
 
@@ -422,8 +422,8 @@ ARGUMENTS
422
422
  FILEPATH file path that need to be formatted by Prettier
423
423
 
424
424
  FLAGS
425
- -c, --config=<value> [default: ./src/files/.prettierrc.yaml] Prettier config file path
426
- -i, --ignore=<value> [default: ./src/files/.prettierignore] Prettier ignore file path
425
+ -c, --config=<value> Prettier config file path
426
+ -i, --ignore=<value> Prettier ignore file path
427
427
 
428
428
  DESCRIPTION
429
429
  Use Prettier to format file
@@ -434,5 +434,5 @@ EXAMPLES
434
434
  $ me prettier ./src/file.ts --config ./.prettierrc.yaml
435
435
  ```
436
436
 
437
- _See code: [src/commands/prettier/index.ts](https://github.com/meme2046/meocli/blob/v0.0.1/src/commands/prettier/index.ts)_
437
+ _See code: [src/commands/prettier/index.ts](https://github.com/meme2046/meocli/blob/v0.0.2/src/commands/prettier/index.ts)_
438
438
  <!-- commandsstop -->
@@ -6,9 +6,11 @@ export default class Prettier extends Command {
6
6
  static description: string;
7
7
  static examples: string[];
8
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>;
9
+ config: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
10
+ ignore: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
11
11
  };
12
12
  run(): Promise<void>;
13
13
  private detectPackageManager;
14
+ private findProjectPrettierConfig;
15
+ private findProjectPrettierIgnore;
14
16
  }
@@ -2,6 +2,7 @@ import { Args, Command, Flags } from '@oclif/core';
2
2
  import { spawn } from 'node:child_process';
3
3
  import { existsSync } from 'node:fs';
4
4
  import { join } from 'node:path';
5
+ import { DEFAULT_ARGS } from '../../consts/prettierrc.js';
5
6
  export default class Prettier extends Command {
6
7
  static args = {
7
8
  filePath: Args.string({
@@ -17,13 +18,13 @@ export default class Prettier extends Command {
17
18
  static flags = {
18
19
  config: Flags.string({
19
20
  char: 'c',
20
- default: './src/files/.prettierrc.yaml',
21
+ // default: './src/files/.prettierrc.yaml',
21
22
  description: 'Prettier config file path',
22
23
  required: false,
23
24
  }),
24
25
  ignore: Flags.string({
25
26
  char: 'i',
26
- default: './src/files/.prettierignore',
27
+ // default: './src/files/.prettierignore',
27
28
  description: 'Prettier ignore file path',
28
29
  required: false,
29
30
  }),
@@ -32,23 +33,50 @@ export default class Prettier extends Command {
32
33
  const { args, flags } = await this.parse(Prettier);
33
34
  const { filePath } = args;
34
35
  const { config, ignore } = flags;
36
+ // 检查文件是否存在
37
+ if (!existsSync(filePath)) {
38
+ this.error(`file 『${filePath}』 not found`);
39
+ return;
40
+ }
35
41
  // 检测当前使用的包管理器
36
42
  const packageManager = this.detectPackageManager();
37
43
  // 根据包管理器类型构建参数
38
- const prettierArgs = [
39
- 'prettier',
40
- '--write',
41
- filePath,
42
- ...(config ? [`--config=${config}`] : []),
43
- ...(ignore ? [`--ignore-path=${ignore}`] : []),
44
- ];
44
+ let prettierArgs = ['prettier', '--write', filePath];
45
+ if (config && existsSync(config)) {
46
+ prettierArgs.push(`--config=${config}`);
47
+ }
48
+ else {
49
+ // 否则尝试查找项目中的配置文件
50
+ const projectConfig = this.findProjectPrettierConfig();
51
+ if (projectConfig) {
52
+ prettierArgs.push(`--config=${projectConfig}`);
53
+ }
54
+ else {
55
+ prettierArgs = [...prettierArgs, ...DEFAULT_ARGS];
56
+ }
57
+ }
58
+ if (ignore && existsSync(ignore)) {
59
+ prettierArgs.push(`--ignore-path=${ignore}`);
60
+ }
61
+ else {
62
+ const projectIgnore = this.findProjectPrettierIgnore();
63
+ if (projectIgnore) {
64
+ prettierArgs.push(`--ignore-path=${projectIgnore}`);
65
+ }
66
+ else {
67
+ // 如果没有找到项目 ignore 文件,跳过此参数
68
+ // Prettier 会使用默认的忽略规则
69
+ }
70
+ }
71
+ this.log(JSON.stringify(prettierArgs));
45
72
  // this.log(`Formatting file: ${filePath}`)
46
73
  // 返回一个 Promise 以等待子进程完成
47
74
  await new Promise((resolve, reject) => {
48
75
  const prettierProcess = spawn(packageManager, prettierArgs, {
49
76
  env: { ...process.env }, // 确保子进程继承当前环境变量
50
77
  shell: false, // 设置为 false 以避免安全警告
51
- stdio: 'inherit',
78
+ // stdio: 'inherit',
79
+ stdio: 'pipe', // 更改为 pipe 以便捕获输出
52
80
  });
53
81
  prettierProcess.on('close', (code) => {
54
82
  if (code === 0) {
@@ -78,4 +106,31 @@ export default class Prettier extends Command {
78
106
  // 默认使用 npm
79
107
  return 'npx';
80
108
  }
109
+ findProjectPrettierConfig() {
110
+ const possibleConfigs = [
111
+ './.prettierrc',
112
+ './.prettierrc.json',
113
+ './.prettierrc.yaml',
114
+ './.prettierrc.yml',
115
+ './.prettierrc.js',
116
+ ];
117
+ for (const configFile of possibleConfigs) {
118
+ if (existsSync(configFile)) {
119
+ return configFile;
120
+ }
121
+ }
122
+ return null;
123
+ }
124
+ findProjectPrettierIgnore() {
125
+ const possibleIgnoreFiles = [
126
+ './.prettierignore',
127
+ // './.gitignore'
128
+ ];
129
+ for (const ignoreFile of possibleIgnoreFiles) {
130
+ if (existsSync(ignoreFile)) {
131
+ return ignoreFile;
132
+ }
133
+ }
134
+ return null;
135
+ }
81
136
  }
@@ -0,0 +1,3 @@
1
+ export declare const DEFAULT_ARGS: string[];
2
+ export declare const DEFAULT_PLUGINS: string[];
3
+ export declare const DEFAULT_IGNORE_PATTERNS: string[];
@@ -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
+ ];
@@ -84,7 +84,6 @@
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"
@@ -94,7 +93,6 @@
94
93
  "description": "Prettier ignore file path",
95
94
  "name": "ignore",
96
95
  "required": false,
97
- "default": "./src/files/.prettierignore",
98
96
  "hasDynamicHelp": false,
99
97
  "multiple": false,
100
98
  "type": "option"
@@ -117,5 +115,5 @@
117
115
  ]
118
116
  }
119
117
  },
120
- "version": "0.0.1"
118
+ "version": "0.0.2"
121
119
  }
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.1",
4
+ "version": "0.0.2",
5
5
  "author": "meme2046",
6
6
  "bin": {
7
7
  "me": "./bin/run.js"
@@ -67,7 +67,7 @@
67
67
  "repository": "meme2046/meocli",
68
68
  "types": "dist/index.d.ts",
69
69
  "scripts": {
70
- "dev": "node bin/dev.js",
70
+ "dev": "node --no-deprecation --no-warnings --loader ts-node/esm --disable-warning=ExperimentalWarning bin/dev.js",
71
71
  "prod": "node bin/run.js",
72
72
  "prettier": "prettier",
73
73
  "build": "shx rm -rf dist && tsc -b",