meocli 0.0.7 → 0.0.9

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
@@ -22,7 +22,7 @@ $ pnpm run dev hello world
22
22
  $ pnpm run prod hello world
23
23
  $ pnpm run dev hello foo -f bar
24
24
  <!-- prettier -->
25
- $ pnpm run dev pr ./tmp/test.svg
25
+ $ pnpm run dev pr ./tmp/test.svg --verbose
26
26
  $ pnpm run dev pr ./tmp/test.json --config=auto --ignore=auto
27
27
  $ pnpm run dev pr ./tmp/test.svg --config=built_in --ignore=auto
28
28
  ```
@@ -43,7 +43,7 @@ $ npm install -g meocli
43
43
  $ me COMMAND
44
44
  running command...
45
45
  $ me (--version)
46
- meocli/0.0.7 win32-x64 node-v24.12.0
46
+ meocli/0.0.9 win32-x64 node-v24.12.0
47
47
  $ me --help [COMMAND]
48
48
  USAGE
49
49
  $ me COMMAND
@@ -91,7 +91,7 @@ EXAMPLES
91
91
  hello friend from oclif! (./src/commands/hello/index.ts)
92
92
  ```
93
93
 
94
- _See code: [src/commands/hello/index.ts](https://github.com/meme2046/meocli/blob/v0.0.7/src/commands/hello/index.ts)_
94
+ _See code: [src/commands/hello/index.ts](https://github.com/meme2046/meocli/blob/v0.0.9/src/commands/hello/index.ts)_
95
95
 
96
96
  ## `me hello world`
97
97
 
@@ -109,7 +109,7 @@ EXAMPLES
109
109
  hello world! (./src/commands/hello/world.ts)
110
110
  ```
111
111
 
112
- _See code: [src/commands/hello/world.ts](https://github.com/meme2046/meocli/blob/v0.0.7/src/commands/hello/world.ts)_
112
+ _See code: [src/commands/hello/world.ts](https://github.com/meme2046/meocli/blob/v0.0.9/src/commands/hello/world.ts)_
113
113
 
114
114
  ## `me help [COMMAND]`
115
115
 
@@ -427,7 +427,7 @@ Use Prettier to format file
427
427
 
428
428
  ```
429
429
  USAGE
430
- $ me pr FILEPATH [-c <value>] [--ignore <value>]
430
+ $ me pr FILEPATH [-c <value>] [--ignore <value>] [-v]
431
431
 
432
432
  ARGUMENTS
433
433
  FILEPATH file path that need to be formatted by Prettier
@@ -435,6 +435,7 @@ ARGUMENTS
435
435
  FLAGS
436
436
  -c, --config=<value> [default: built_in] built_in:使用内置规则(默认值), 传入路径则是使用自定义配置,
437
437
  auto:自动检测config file
438
+ -v, --verbose Show verbose output
438
439
  --ignore=<value> [default: built_in] built_in:使用内置规则(默认值), 传入路径则是使用自定义规则,
439
440
  auto:自动检测ignore file
440
441
 
@@ -447,5 +448,5 @@ EXAMPLES
447
448
  $ me pr ./src/file.ts --config ./.prettierrc.yaml
448
449
  ```
449
450
 
450
- _See code: [src/commands/pr/index.ts](https://github.com/meme2046/meocli/blob/v0.0.7/src/commands/pr/index.ts)_
451
+ _See code: [src/commands/pr/index.ts](https://github.com/meme2046/meocli/blob/v0.0.9/src/commands/pr/index.ts)_
451
452
  <!-- commandsstop -->
@@ -8,12 +8,14 @@ export default class Prettier extends Command {
8
8
  static flags: {
9
9
  config: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
10
10
  ignore: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
11
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
12
  };
12
13
  run(): Promise<void>;
13
14
  private detectPackageManager;
14
15
  private findProjectPrettierConfig;
15
16
  private findProjectPrettierIgnore;
16
17
  private replacePluginArgs;
18
+ private resolvePlugin;
17
19
  private resolvePluginArgs;
18
20
  private resolvePluginPaths;
19
21
  }
@@ -1,11 +1,12 @@
1
1
  import { Args, Command, Flags } from '@oclif/core';
2
2
  import { execa } from 'execa';
3
3
  // import {spawn} from 'node:child_process'
4
- import { existsSync } from 'node:fs';
4
+ import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
5
5
  import { createRequire } from 'node:module';
6
+ import { homedir } from 'node:os';
6
7
  import { dirname, join } from 'node:path';
7
- import { cwd } from 'node:process';
8
- import { DEFAULT_ARGS, DEFAULT_PLUGINS } from '../../consts/prettierrc.js';
8
+ import { DEFAULT_CONFIG, DEFAULT_IGNORE_PATTERNS } from '../../consts/prettierrc.js';
9
+ import { toYamlFile } from '../../lib/to-yaml-file.js';
9
10
  const require = createRequire(import.meta.url);
10
11
  export default class Prettier extends Command {
11
12
  static args = {
@@ -31,6 +32,11 @@ export default class Prettier extends Command {
31
32
  description: 'built_in:使用内置规则(默认值), 传入路径则是使用自定义规则, auto:自动检测ignore file',
32
33
  required: false,
33
34
  }),
35
+ verbose: Flags.boolean({
36
+ char: 'v',
37
+ default: false,
38
+ description: 'Show verbose output',
39
+ }),
34
40
  // 'no-config': Flags.boolean({
35
41
  // default: false,
36
42
  // description: 'Disable config file detection',
@@ -43,7 +49,11 @@ export default class Prettier extends Command {
43
49
  async run() {
44
50
  const { args, flags } = await this.parse(Prettier);
45
51
  const { filePath } = args;
46
- const { config, ignore } = flags;
52
+ const { config, ignore, verbose } = flags;
53
+ if (verbose) {
54
+ process.env.DEBUG = 'oclif:me:pr';
55
+ require('debug').enable(process.env.DEBUG);
56
+ }
47
57
  // 检查文件是否存在
48
58
  if (!existsSync(filePath)) {
49
59
  this.error(`file『${filePath}』not found`);
@@ -65,11 +75,24 @@ export default class Prettier extends Command {
65
75
  this.error(`prettier not found`);
66
76
  return;
67
77
  }
78
+ const meocliPath = `${homedir}/.meocli`;
79
+ const ignorePath = `${meocliPath}/.prettierignore`;
80
+ const configPath = `${meocliPath}/.prettierrc.yaml`;
81
+ if (!existsSync(meocliPath)) {
82
+ mkdirSync(meocliPath);
83
+ }
84
+ if (!existsSync(ignorePath)) {
85
+ writeFileSync(ignorePath, DEFAULT_IGNORE_PATTERNS.join('\n'), { encoding: 'utf8' });
86
+ }
87
+ if (!existsSync(configPath)) {
88
+ DEFAULT_CONFIG.plugins = this.resolvePlugin(DEFAULT_CONFIG.plugins);
89
+ this.debug('prettier config:', DEFAULT_CONFIG);
90
+ toYamlFile(configPath, DEFAULT_CONFIG);
91
+ }
68
92
  // 根据包管理器类型构建参数
69
- let prettierArgs = ['--write', filePath];
93
+ const prettierArgs = ['--write', filePath];
70
94
  if (ignore === 'built_in') {
71
- const projectRoot = cwd();
72
- prettierArgs.unshift('--ignore-path', join(projectRoot, '.prettierignore'));
95
+ prettierArgs.unshift('--ignore-path', ignorePath);
73
96
  }
74
97
  else if (existsSync(ignore)) {
75
98
  prettierArgs.unshift('--ignore-path', ignore);
@@ -79,14 +102,17 @@ export default class Prettier extends Command {
79
102
  if (projectIgnore) {
80
103
  prettierArgs.unshift('--ignore-path', projectIgnore);
81
104
  }
105
+ else {
106
+ prettierArgs.unshift('--ignore-path', ignorePath);
107
+ }
82
108
  }
83
109
  // const resolvedPluginArgs = this.resolvePluginPaths(DEFAULT_PLUGINS, projectRoot)
84
110
  // const argsWithResolvedPlugins = this.replacePluginArgs(DEFAULT_ARGS, resolvedPluginArgs)
85
- const pluginArgs = this.resolvePluginArgs(DEFAULT_PLUGINS);
86
- const completeArgs = [...DEFAULT_ARGS, ...pluginArgs, ...prettierArgs];
111
+ // const pluginArgs = this.resolvePluginArgs(DEFAULT_PLUGINS)
112
+ // const completeArgs = [...DEFAULT_ARGS, ...pluginArgs, ...prettierArgs]
87
113
  if (config === 'built_in') {
88
114
  // 为插件参数使用绝对路径,避免全局安装时找不到插件
89
- prettierArgs = completeArgs;
115
+ prettierArgs.unshift('--config', configPath);
90
116
  }
91
117
  else if (existsSync(config)) {
92
118
  prettierArgs.unshift('--config', config);
@@ -99,11 +125,11 @@ export default class Prettier extends Command {
99
125
  }
100
126
  else {
101
127
  // 为插件参数使用绝对路径,避免全局安装时找不到插件
102
- prettierArgs = completeArgs;
128
+ prettierArgs.unshift('--config', configPath);
103
129
  }
104
130
  }
105
- this.debug(prettierBin);
106
- this.debug(JSON.stringify(prettierArgs));
131
+ this.debug('prettier path:', prettierBin);
132
+ this.debug('args:', JSON.stringify(prettierArgs));
107
133
  // this.log(`Formatting file: ${filePath}`)
108
134
  const { stderr, stdout } = await execa(prettierBin, prettierArgs, {
109
135
  env: { ...process.env },
@@ -163,6 +189,17 @@ export default class Prettier extends Command {
163
189
  }
164
190
  return newArgs;
165
191
  }
192
+ resolvePlugin(plugins) {
193
+ return plugins.map((plugin) => {
194
+ // 尝试查找插件的实际路径
195
+ const pluginPath = require.resolve(plugin);
196
+ if (existsSync(pluginPath)) {
197
+ return pluginPath;
198
+ }
199
+ // 如果插件路径不存在,则返回原始名称(让 prettier 自己处理)
200
+ return plugin;
201
+ });
202
+ }
166
203
  resolvePluginArgs(plugins) {
167
204
  const pluginArgs = [];
168
205
  for (const plugin of plugins) {
@@ -1,4 +1,42 @@
1
1
  export declare const DEFAULT_ARGS: string[];
2
+ export declare const DEFAULT_CONFIG: {
3
+ arrowParens: string;
4
+ bracketSameLine: boolean;
5
+ bracketSpacing: boolean;
6
+ continuationIndent: number;
7
+ embeddedLanguageFormatting: string;
8
+ endOfLine: string;
9
+ experimentalOperatorPosition: string;
10
+ experimentalTernaries: boolean;
11
+ htmlWhitespaceSensitivity: string;
12
+ indentEntries: boolean;
13
+ insertPragma: boolean;
14
+ jsxSingleQuote: boolean;
15
+ objectWrap: string;
16
+ overrides: {
17
+ files: string[];
18
+ options: {
19
+ parser: string;
20
+ };
21
+ }[];
22
+ plugins: string[];
23
+ printWidth: number;
24
+ proseWrap: string;
25
+ quoteProps: string;
26
+ reorderKeys: boolean;
27
+ requirePragma: boolean;
28
+ semi: boolean;
29
+ singleAttributePerLine: boolean;
30
+ singleQuote: boolean;
31
+ tabWidth: number;
32
+ trailingComma: string;
33
+ useTabs: boolean;
34
+ vueIndentScriptAndStyle: boolean;
35
+ wrapParameters: boolean;
36
+ xmlSelfClosingSpace: boolean;
37
+ xmlSortAttributesByKey: boolean;
38
+ xmlWhitespaceSensitivity: string;
39
+ };
2
40
  export declare const DEFAULT_PLUGINS: {
3
41
  main: string;
4
42
  name: string;
@@ -65,6 +65,42 @@ export const DEFAULT_ARGS = [
65
65
  '--continuation-indent',
66
66
  '2',
67
67
  ];
68
+ export const DEFAULT_CONFIG = {
69
+ arrowParens: 'always',
70
+ bracketSameLine: false,
71
+ bracketSpacing: true,
72
+ continuationIndent: 2, // nginx
73
+ embeddedLanguageFormatting: 'auto',
74
+ endOfLine: 'lf',
75
+ experimentalOperatorPosition: 'end',
76
+ experimentalTernaries: false,
77
+ htmlWhitespaceSensitivity: 'css',
78
+ indentEntries: true, // toml
79
+ insertPragma: false,
80
+ jsxSingleQuote: false,
81
+ objectWrap: 'preserve',
82
+ overrides: [
83
+ { files: ['*.env'], options: { parser: 'sh' } },
84
+ { files: ['*.txt'], options: { parser: 'markdown' } },
85
+ ],
86
+ plugins: ['@prettier/plugin-xml', 'prettier-plugin-toml', 'prettier-plugin-sh', 'prettier-plugin-nginx'],
87
+ printWidth: 80,
88
+ proseWrap: 'preserve',
89
+ quoteProps: 'as-needed',
90
+ reorderKeys: true, // toml
91
+ requirePragma: false,
92
+ semi: true,
93
+ singleAttributePerLine: false,
94
+ singleQuote: false,
95
+ tabWidth: 2,
96
+ trailingComma: 'all',
97
+ useTabs: false, // useTabs 和 tabWidth 不能同时使用
98
+ vueIndentScriptAndStyle: false,
99
+ wrapParameters: false, // nginx
100
+ xmlSelfClosingSpace: true, // xml
101
+ xmlSortAttributesByKey: true, // xml
102
+ xmlWhitespaceSensitivity: 'strict', // xml
103
+ };
68
104
  // 默认的 Prettier 插件列表
69
105
  export const DEFAULT_PLUGINS = [
70
106
  { main: 'src/plugin.js', name: '@prettier/plugin-xml' },
@@ -0,0 +1 @@
1
+ export declare function toYamlFile(fp: string, data: unknown): void;
@@ -0,0 +1,6 @@
1
+ import { dump } from 'js-yaml';
2
+ import fs from 'node:fs';
3
+ export function toYamlFile(fp, data) {
4
+ const yamlStr = dump(data, { indent: 2 });
5
+ fs.writeFileSync(fp, yamlStr, 'utf8');
6
+ }
@@ -0,0 +1 @@
1
+ export declare function ensureDirectoryExists(filePath: string): void;
@@ -0,0 +1,11 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
2
+ import { dirname } from 'node:path';
3
+ import { DEFAULT_IGNORE_PATTERNS } from '../consts/prettierrc.js';
4
+ const ignoreStr = [...DEFAULT_IGNORE_PATTERNS].join('\n');
5
+ writeFileSync('tmp/tmp/.prettierignore', ignoreStr, { encoding: 'utf8' });
6
+ export function ensureDirectoryExists(filePath) {
7
+ const dir = dirname(filePath);
8
+ if (!existsSync(dir)) {
9
+ mkdirSync(dir, { recursive: true });
10
+ }
11
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import { DEFAULT_CONFIG, DEFAULT_IGNORE_PATTERNS } from '../consts/prettierrc.js';
2
+ console.log('hello world!');
3
+ console.log(DEFAULT_IGNORE_PATTERNS.join('\n'));
4
+ console.log(DEFAULT_CONFIG.plugins);
@@ -97,6 +97,13 @@
97
97
  "hasDynamicHelp": false,
98
98
  "multiple": false,
99
99
  "type": "option"
100
+ },
101
+ "verbose": {
102
+ "char": "v",
103
+ "description": "Show verbose output",
104
+ "name": "verbose",
105
+ "allowNo": false,
106
+ "type": "boolean"
100
107
  }
101
108
  },
102
109
  "hasDynamicHelp": false,
@@ -116,5 +123,5 @@
116
123
  ]
117
124
  }
118
125
  },
119
- "version": "0.0.7"
126
+ "version": "0.0.9"
120
127
  }
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.7",
4
+ "version": "0.0.9",
5
5
  "author": "meme2046",
6
6
  "bin": {
7
7
  "me": "./bin/run.js"
@@ -12,7 +12,9 @@
12
12
  "@oclif/plugin-help": "^6",
13
13
  "@oclif/plugin-plugins": "^5",
14
14
  "@prettier/plugin-xml": "^3.4.2",
15
+ "debug": "^4.4.3",
15
16
  "execa": "^9.6.1",
17
+ "js-yaml": "^4.1.1",
16
18
  "prettier": "^3.7.4",
17
19
  "prettier-plugin-nginx": "^1.0.3",
18
20
  "prettier-plugin-sh": "^0.18.0",
@@ -23,6 +25,7 @@
23
25
  "@oclif/prettier-config": "^0.2.1",
24
26
  "@oclif/test": "^4",
25
27
  "@types/chai": "^4",
28
+ "@types/js-yaml": "^4.0.9",
26
29
  "@types/mocha": "^10",
27
30
  "@types/node": "^18",
28
31
  "chai": "^4",