meocli 0.0.0 → 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
@@ -1,13 +1,13 @@
1
- # memecli
1
+ # meocli
2
2
 
3
3
  A new CLI generated with oclif
4
4
 
5
5
  [![oclif](https://img.shields.io/badge/cli-oclif-brightgreen.svg)](https://oclif.io)
6
- [![Version](https://img.shields.io/npm/v/memecli.svg)](https://npmjs.org/package/memecli)
7
- [![Downloads/week](https://img.shields.io/npm/dw/memecli.svg)](https://npmjs.org/package/memecli)
6
+ [![Version](https://img.shields.io/npm/v/meocli.svg)](https://npmjs.org/package/meocli)
7
+ [![Downloads/week](https://img.shields.io/npm/dw/meocli.svg)](https://npmjs.org/package/meocli)
8
8
 
9
9
  <!-- toc -->
10
- * [memecli](#memecli)
10
+ * [meocli](#meocli)
11
11
  * [Dev](#dev)
12
12
  * [Usage](#usage)
13
13
  * [Commands](#commands)
@@ -32,7 +32,7 @@ $ npm install -g meocli
32
32
  $ me COMMAND
33
33
  running command...
34
34
  $ me (--version)
35
- meocli/0.0.0 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
@@ -56,6 +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 prettier FILEPATH`](#me-prettier-filepath)
59
60
 
60
61
  ## `me hello PERSON`
61
62
 
@@ -79,7 +80,7 @@ EXAMPLES
79
80
  hello friend from oclif! (./src/commands/hello/index.ts)
80
81
  ```
81
82
 
82
- _See code: [src/commands/hello/index.ts](https://github.com/meme2046/meocli/blob/v0.0.0/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)_
83
84
 
84
85
  ## `me hello world`
85
86
 
@@ -97,7 +98,7 @@ EXAMPLES
97
98
  hello world! (./src/commands/hello/world.ts)
98
99
  ```
99
100
 
100
- _See code: [src/commands/hello/world.ts](https://github.com/meme2046/meocli/blob/v0.0.0/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)_
101
102
 
102
103
  ## `me help [COMMAND]`
103
104
 
@@ -408,4 +409,30 @@ DESCRIPTION
408
409
  ```
409
410
 
410
411
  _See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.4.54/src/commands/plugins/update.ts)_
412
+
413
+ ## `me prettier FILEPATH`
414
+
415
+ Use Prettier to format file
416
+
417
+ ```
418
+ USAGE
419
+ $ me prettier FILEPATH [-c <value>] [-i <value>]
420
+
421
+ ARGUMENTS
422
+ FILEPATH file path that need to be formatted by Prettier
423
+
424
+ FLAGS
425
+ -c, --config=<value> Prettier config file path
426
+ -i, --ignore=<value> Prettier ignore file path
427
+
428
+ DESCRIPTION
429
+ Use Prettier to format file
430
+
431
+ EXAMPLES
432
+ $ me prettier ./tests/test.json
433
+
434
+ $ me prettier ./src/file.ts --config ./.prettierrc.yaml
435
+ ```
436
+
437
+ _See code: [src/commands/prettier/index.ts](https://github.com/meme2046/meocli/blob/v0.0.2/src/commands/prettier/index.ts)_
411
438
  <!-- commandsstop -->
@@ -9,7 +9,6 @@ hello world! (./src/commands/hello/world.ts)
9
9
  ];
10
10
  static flags = {};
11
11
  async run() {
12
- await this.parse(World);
13
12
  this.log('hello world! (./src/commands/hello/world.ts)');
14
13
  }
15
14
  }
@@ -0,0 +1,16 @@
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: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
11
+ };
12
+ run(): Promise<void>;
13
+ private detectPackageManager;
14
+ private findProjectPrettierConfig;
15
+ private findProjectPrettierIgnore;
16
+ }
@@ -0,0 +1,136 @@
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
+ import { DEFAULT_ARGS } from '../../consts/prettierrc.js';
6
+ export default class Prettier extends Command {
7
+ static args = {
8
+ filePath: Args.string({
9
+ description: 'file path that need to be formatted by Prettier',
10
+ required: true,
11
+ }),
12
+ };
13
+ static description = 'Use Prettier to format file';
14
+ static examples = [
15
+ '<%= config.bin %> <%= command.id %> ./tests/test.json',
16
+ '<%= config.bin %> <%= command.id %> ./src/file.ts --config ./.prettierrc.yaml',
17
+ ];
18
+ static flags = {
19
+ config: Flags.string({
20
+ char: 'c',
21
+ // default: './src/files/.prettierrc.yaml',
22
+ description: 'Prettier config file path',
23
+ required: false,
24
+ }),
25
+ ignore: Flags.string({
26
+ char: 'i',
27
+ // default: './src/files/.prettierignore',
28
+ description: 'Prettier ignore file path',
29
+ required: false,
30
+ }),
31
+ };
32
+ async run() {
33
+ const { args, flags } = await this.parse(Prettier);
34
+ const { filePath } = args;
35
+ const { config, ignore } = flags;
36
+ // 检查文件是否存在
37
+ if (!existsSync(filePath)) {
38
+ this.error(`file 『${filePath}』 not found`);
39
+ return;
40
+ }
41
+ // 检测当前使用的包管理器
42
+ const packageManager = this.detectPackageManager();
43
+ // 根据包管理器类型构建参数
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));
72
+ // this.log(`Formatting file: ${filePath}`)
73
+ // 返回一个 Promise 以等待子进程完成
74
+ await new Promise((resolve, reject) => {
75
+ const prettierProcess = spawn(packageManager, prettierArgs, {
76
+ env: { ...process.env }, // 确保子进程继承当前环境变量
77
+ shell: false, // 设置为 false 以避免安全警告
78
+ // stdio: 'inherit',
79
+ stdio: 'pipe', // 更改为 pipe 以便捕获输出
80
+ });
81
+ prettierProcess.on('close', (code) => {
82
+ if (code === 0) {
83
+ this.log(`Successfully formatted ${filePath}`);
84
+ resolve(null);
85
+ }
86
+ else {
87
+ reject(new Error(`Prettier exited with code ${code}`));
88
+ }
89
+ });
90
+ prettierProcess.on('error', (error) => {
91
+ reject(error);
92
+ });
93
+ }).catch((error) => {
94
+ this.error(`Error executing prettier: ${error.message}`);
95
+ });
96
+ }
97
+ detectPackageManager() {
98
+ // 检查项目根目录是否存在 pnpm-lock.yaml 来判断是否使用 pnpm
99
+ if (existsSync(join(process.cwd(), 'pnpm-lock.yaml'))) {
100
+ return 'pnpx';
101
+ }
102
+ // 检查是否存在 yarn.lock 来判断是否使用 yarn
103
+ if (existsSync(join(process.cwd(), 'yarn.lock'))) {
104
+ return 'yarn';
105
+ }
106
+ // 默认使用 npm
107
+ return 'npx';
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
+ }
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
+ ];
@@ -0,0 +1 @@
1
+ export declare function getEnv(name: string): string;
@@ -0,0 +1,3 @@
1
+ export function getEnv(name) {
2
+ return process.env[name] ?? '';
3
+ }
@@ -0,0 +1 @@
1
+ export declare const sleep: (ms: number) => Promise<unknown>;
@@ -0,0 +1,3 @@
1
+ export const sleep = (ms) => new Promise((r) => {
2
+ setTimeout(r, ms);
3
+ });
@@ -63,7 +63,57 @@
63
63
  "hello",
64
64
  "world.js"
65
65
  ]
66
+ },
67
+ "prettier": {
68
+ "aliases": [],
69
+ "args": {
70
+ "filePath": {
71
+ "description": "file path that need to be formatted by Prettier",
72
+ "name": "filePath",
73
+ "required": true
74
+ }
75
+ },
76
+ "description": "Use Prettier to format file",
77
+ "examples": [
78
+ "<%= config.bin %> <%= command.id %> ./tests/test.json",
79
+ "<%= config.bin %> <%= command.id %> ./src/file.ts --config ./.prettierrc.yaml"
80
+ ],
81
+ "flags": {
82
+ "config": {
83
+ "char": "c",
84
+ "description": "Prettier config file path",
85
+ "name": "config",
86
+ "required": false,
87
+ "hasDynamicHelp": false,
88
+ "multiple": false,
89
+ "type": "option"
90
+ },
91
+ "ignore": {
92
+ "char": "i",
93
+ "description": "Prettier ignore file path",
94
+ "name": "ignore",
95
+ "required": false,
96
+ "hasDynamicHelp": false,
97
+ "multiple": false,
98
+ "type": "option"
99
+ }
100
+ },
101
+ "hasDynamicHelp": false,
102
+ "hiddenAliases": [],
103
+ "id": "prettier",
104
+ "pluginAlias": "meocli",
105
+ "pluginName": "meocli",
106
+ "pluginType": "core",
107
+ "strict": true,
108
+ "enableJsonFlag": false,
109
+ "isESM": true,
110
+ "relativePath": [
111
+ "dist",
112
+ "commands",
113
+ "prettier",
114
+ "index.js"
115
+ ]
66
116
  }
67
117
  },
68
- "version": "0.0.0"
118
+ "version": "0.0.2"
69
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.0",
4
+ "version": "0.0.2",
5
5
  "author": "meme2046",
6
6
  "bin": {
7
7
  "me": "./bin/run.js"
@@ -10,7 +10,12 @@
10
10
  "dependencies": {
11
11
  "@oclif/core": "^4",
12
12
  "@oclif/plugin-help": "^6",
13
- "@oclif/plugin-plugins": "^5"
13
+ "@oclif/plugin-plugins": "^5",
14
+ "@prettier/plugin-xml": "^3.4.2",
15
+ "prettier": "^3.7.4",
16
+ "prettier-plugin-nginx": "^1.0.3",
17
+ "prettier-plugin-sh": "^0.18.0",
18
+ "prettier-plugin-toml": "^2.0.6"
14
19
  },
15
20
  "devDependencies": {
16
21
  "@eslint/compat": "^1",
@@ -62,8 +67,9 @@
62
67
  "repository": "meme2046/meocli",
63
68
  "types": "dist/index.d.ts",
64
69
  "scripts": {
65
- "dev": "node bin/dev.js",
70
+ "dev": "node --no-deprecation --no-warnings --loader ts-node/esm --disable-warning=ExperimentalWarning bin/dev.js",
66
71
  "prod": "node bin/run.js",
72
+ "prettier": "prettier",
67
73
  "build": "shx rm -rf dist && tsc -b",
68
74
  "lint": "eslint",
69
75
  "posttest": "pnpm run lint",