meocli 0.0.8 → 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
@@ -43,7 +43,7 @@ $ npm install -g meocli
43
43
  $ me COMMAND
44
44
  running command...
45
45
  $ me (--version)
46
- meocli/0.0.8 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.8/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.8/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
 
@@ -448,5 +448,5 @@ EXAMPLES
448
448
  $ me pr ./src/file.ts --config ./.prettierrc.yaml
449
449
  ```
450
450
 
451
- _See code: [src/commands/pr/index.ts](https://github.com/meme2046/meocli/blob/v0.0.8/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)_
452
452
  <!-- commandsstop -->
@@ -15,6 +15,7 @@ export default class Prettier extends Command {
15
15
  private findProjectPrettierConfig;
16
16
  private findProjectPrettierIgnore;
17
17
  private replacePluginArgs;
18
+ private resolvePlugin;
18
19
  private resolvePluginArgs;
19
20
  private resolvePluginPaths;
20
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 = {
@@ -74,11 +75,24 @@ export default class Prettier extends Command {
74
75
  this.error(`prettier not found`);
75
76
  return;
76
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
+ }
77
92
  // 根据包管理器类型构建参数
78
- let prettierArgs = ['--write', filePath];
93
+ const prettierArgs = ['--write', filePath];
79
94
  if (ignore === 'built_in') {
80
- const projectRoot = cwd();
81
- prettierArgs.unshift('--ignore-path', join(projectRoot, '.prettierignore'));
95
+ prettierArgs.unshift('--ignore-path', ignorePath);
82
96
  }
83
97
  else if (existsSync(ignore)) {
84
98
  prettierArgs.unshift('--ignore-path', ignore);
@@ -88,14 +102,17 @@ export default class Prettier extends Command {
88
102
  if (projectIgnore) {
89
103
  prettierArgs.unshift('--ignore-path', projectIgnore);
90
104
  }
105
+ else {
106
+ prettierArgs.unshift('--ignore-path', ignorePath);
107
+ }
91
108
  }
92
109
  // const resolvedPluginArgs = this.resolvePluginPaths(DEFAULT_PLUGINS, projectRoot)
93
110
  // const argsWithResolvedPlugins = this.replacePluginArgs(DEFAULT_ARGS, resolvedPluginArgs)
94
- const pluginArgs = this.resolvePluginArgs(DEFAULT_PLUGINS);
95
- const completeArgs = [...DEFAULT_ARGS, ...pluginArgs, ...prettierArgs];
111
+ // const pluginArgs = this.resolvePluginArgs(DEFAULT_PLUGINS)
112
+ // const completeArgs = [...DEFAULT_ARGS, ...pluginArgs, ...prettierArgs]
96
113
  if (config === 'built_in') {
97
114
  // 为插件参数使用绝对路径,避免全局安装时找不到插件
98
- prettierArgs = completeArgs;
115
+ prettierArgs.unshift('--config', configPath);
99
116
  }
100
117
  else if (existsSync(config)) {
101
118
  prettierArgs.unshift('--config', config);
@@ -108,7 +125,7 @@ export default class Prettier extends Command {
108
125
  }
109
126
  else {
110
127
  // 为插件参数使用绝对路径,避免全局安装时找不到插件
111
- prettierArgs = completeArgs;
128
+ prettierArgs.unshift('--config', configPath);
112
129
  }
113
130
  }
114
131
  this.debug('prettier path:', prettierBin);
@@ -172,6 +189,17 @@ export default class Prettier extends Command {
172
189
  }
173
190
  return newArgs;
174
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
+ }
175
203
  resolvePluginArgs(plugins) {
176
204
  const pluginArgs = [];
177
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);
@@ -123,5 +123,5 @@
123
123
  ]
124
124
  }
125
125
  },
126
- "version": "0.0.8"
126
+ "version": "0.0.9"
127
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.8",
4
+ "version": "0.0.9",
5
5
  "author": "meme2046",
6
6
  "bin": {
7
7
  "me": "./bin/run.js"
@@ -14,6 +14,7 @@
14
14
  "@prettier/plugin-xml": "^3.4.2",
15
15
  "debug": "^4.4.3",
16
16
  "execa": "^9.6.1",
17
+ "js-yaml": "^4.1.1",
17
18
  "prettier": "^3.7.4",
18
19
  "prettier-plugin-nginx": "^1.0.3",
19
20
  "prettier-plugin-sh": "^0.18.0",
@@ -24,6 +25,7 @@
24
25
  "@oclif/prettier-config": "^0.2.1",
25
26
  "@oclif/test": "^4",
26
27
  "@types/chai": "^4",
28
+ "@types/js-yaml": "^4.0.9",
27
29
  "@types/mocha": "^10",
28
30
  "@types/node": "^18",
29
31
  "chai": "^4",