milkee-plugin-prettier 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 CHANGED
@@ -1,3 +1,5 @@
1
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
2
+
1
3
  # milkee-plugin-prettier
2
4
 
3
5
  A Milkee plugin for working with Prettier for CoffeeScript without version conflicts.
@@ -6,13 +8,12 @@ A Milkee plugin for working with Prettier for CoffeeScript without version confl
6
8
 
7
9
  ## Features
8
10
 
9
- - Recursively finds `.coffee` files in the build output directory and formats them with Prettier.
11
+ - Recursively finds `.coffee` files in the build entry directory and formats them with Prettier.
10
12
  - Supports a `prettierrc` (object or a path to a config file) and `.prettierignore` to exclude files.
11
13
  - Automatically resolves config via `prettier.resolveConfig()` or `package.json#prettier` when `prettierrc` is not provided.
12
14
 
13
15
  ## Usage
14
-
15
- ### coffee.config.cjs
16
+ ### As a Milkee Plugin (coffee.config.cjs)
16
17
 
17
18
  ```js
18
19
  const prettierPlugin = require('milkee-plugin-prettier');
@@ -32,10 +33,27 @@ module.exports = {
32
33
  };
33
34
  ```
34
35
 
36
+ ### As a CLI Tool
37
+
38
+ You can also use this package as a standalone CLI tool to format CoffeeScript files:
39
+
40
+ ```bash
41
+ # Format files in the current directory
42
+ npx cprettier
43
+
44
+ # Format files in a specific directory
45
+ npx cprettier src
46
+ ```
47
+
48
+ The CLI will:
49
+ - Recursively find all `.coffee` files in the target directory
50
+ - Respect `.prettierignore` and prettier config files (`.prettierrc`, `prettier.config.js`, or `package.json#prettier`)
51
+ - Format files in place
52
+
35
53
  ### Notes
36
54
 
37
55
  - `.prettierignore` is resolved relative to `process.cwd()` by default. You can pass an absolute or relative path via the `prettierignore` option.
38
- - When `config.options.join` is used and a single output file is emitted, the plugin will use the directory of the output file as the search root for `.coffee` files.
56
+ - When `config.options.join` is used and a single entry file is emitted, the plugin will use the directory of the entry file as the search root for `.coffee` files.
39
57
 
40
58
  ## Development
41
59
 
package/dist/bin.js ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+ // Generated by CoffeeScript 2.7.0
3
+ //!/usr/bin/env node
4
+ var args, formatCoffeeFiles, targetDir;
5
+
6
+ ({formatCoffeeFiles} = require('./main.js'));
7
+
8
+ // Parse CLI arguments
9
+ args = process.argv.slice(2);
10
+
11
+ targetDir = args[0] || process.cwd();
12
+
13
+ // Run the formatter
14
+ await formatCoffeeFiles(targetDir);
package/dist/main.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
- var PREFIX, c, collectCoffeeFiles, consola, fs, i, ignore, len, main, method, path, pkg, prettier, ref;
2
+ var PREFIX, c, collectCoffeeFiles, consola, formatCoffeeFiles, fs, i, ignore, len, main, method, path, pkg, prettier, ref;
3
3
 
4
4
  fs = require('fs');
5
5
 
@@ -54,134 +54,141 @@ collectCoffeeFiles = function(dir, list = []) {
54
54
  return list;
55
55
  };
56
56
 
57
- // Export a plugin factory
58
- main = function(opts = {}) {
59
- return async function(compilationResult) {
60
- var compiledFiles, config, defaultIgnore, err, error, file, files, formatted, formattedCount, ig, ignorePath, ignorePatterns, info, j, len1, outDir, output, pkgPath, prettierOptions, prettierrc, projectRoot, rcPath, ref1, rel, text;
61
- ({config, compiledFiles} = compilationResult);
62
- // Determine output directory
63
- output = (config != null ? config.output : void 0) || 'dist';
64
- outDir = (config != null ? (ref1 = config.options) != null ? ref1.join : void 0 : void 0) ? path.dirname(output) : output;
65
- projectRoot = process.cwd();
66
- // Handle prettierignore: can be a path (string) or an array of patterns
67
- ignorePath = null;
68
- ignorePatterns = null;
69
- ig = null;
70
- if (Array.isArray(opts != null ? opts.prettierignore : void 0)) {
71
- ignorePatterns = opts.prettierignore.filter(function(p) {
72
- return typeof p === 'string';
73
- });
74
- if (ignorePatterns.length > 0) {
75
- ig = ignore().add(ignorePatterns);
76
- }
77
- } else if (typeof (opts != null ? opts.prettierignore : void 0) === 'string') {
78
- ignorePath = path.isAbsolute(opts.prettierignore) ? opts.prettierignore : path.join(projectRoot, opts.prettierignore);
79
- if (!fs.existsSync(ignorePath)) {
80
- c.warn(`prettierignore not found at ${ignorePath}`);
81
- ignorePath = null;
82
- }
83
- } else {
84
- defaultIgnore = path.join(projectRoot, '.prettierignore');
85
- if (fs.existsSync(defaultIgnore)) {
86
- ignorePath = defaultIgnore;
87
- }
57
+ // Core format function that can be used by both plugin and CLI
58
+ formatCoffeeFiles = async function(targetDir, opts = {}) {
59
+ var defaultIgnore, err, error, file, files, formatted, formattedCount, ig, ignorePath, ignorePatterns, info, j, len1, pkgData, pkgPath, prettierOptions, prettierrc, projectRoot, rcPath, rel, text;
60
+ projectRoot = process.cwd();
61
+ // Handle prettierignore: can be a path (string) or an array of patterns
62
+ ignorePath = null;
63
+ ignorePatterns = null;
64
+ ig = null;
65
+ if (Array.isArray(opts != null ? opts.prettierignore : void 0)) {
66
+ ignorePatterns = opts.prettierignore.filter(function(p) {
67
+ return typeof p === 'string';
68
+ });
69
+ if (ignorePatterns.length > 0) {
70
+ ig = ignore().add(ignorePatterns);
88
71
  }
89
- // Load prettierrc: opts.prettierrc can be an object or a path to a file. If not provided, try resolveConfig, then package.json
90
- prettierrc = null;
91
- if ((opts != null ? opts.prettierrc : void 0) != null) {
92
- if (typeof opts.prettierrc === 'object') {
93
- prettierrc = opts.prettierrc;
94
- } else if (typeof opts.prettierrc === 'string') {
95
- rcPath = path.isAbsolute(opts.prettierrc) ? opts.prettierrc : path.join(projectRoot, opts.prettierrc);
96
- try {
97
- prettierrc = JSON.parse(fs.readFileSync(rcPath, 'utf8'));
98
- } catch (error1) {
99
- error = error1;
100
- c.warn(`Failed to read prettierrc at ${rcPath}: ${error.message}`);
101
- }
102
- }
72
+ } else if (typeof (opts != null ? opts.prettierignore : void 0) === 'string') {
73
+ ignorePath = path.isAbsolute(opts.prettierignore) ? opts.prettierignore : path.join(projectRoot, opts.prettierignore);
74
+ if (!fs.existsSync(ignorePath)) {
75
+ c.warn(`prettierignore not found at ${ignorePath}`);
76
+ ignorePath = null;
103
77
  }
104
- if (prettierrc == null) {
105
- try {
106
- prettierrc = (await prettier.resolveConfig(projectRoot));
107
- } catch (error1) {
108
- error = error1;
109
- prettierrc = null;
110
- }
78
+ } else {
79
+ defaultIgnore = path.join(projectRoot, '.prettierignore');
80
+ if (fs.existsSync(defaultIgnore)) {
81
+ ignorePath = defaultIgnore;
111
82
  }
112
- if (prettierrc == null) {
83
+ }
84
+ // Load prettierrc: opts.prettierrc can be an object or a path to a file. If not provided, try resolveConfig, then package.json
85
+ prettierrc = null;
86
+ if ((opts != null ? opts.prettierrc : void 0) != null) {
87
+ if (typeof opts.prettierrc === 'object') {
88
+ prettierrc = opts.prettierrc;
89
+ } else if (typeof opts.prettierrc === 'string') {
90
+ rcPath = path.isAbsolute(opts.prettierrc) ? opts.prettierrc : path.join(projectRoot, opts.prettierrc);
113
91
  try {
114
- pkgPath = path.join(projectRoot, 'package.json');
115
- if (fs.existsSync(pkgPath)) {
116
- pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
117
- if ((pkg != null ? pkg.prettier : void 0) != null) {
118
- prettierrc = pkg.prettier;
119
- }
120
- }
92
+ prettierrc = JSON.parse(fs.readFileSync(rcPath, 'utf8'));
121
93
  } catch (error1) {
122
94
  error = error1;
95
+ c.warn(`Failed to read prettierrc at ${rcPath}: ${error.message}`);
123
96
  }
124
97
  }
125
- // ignore
126
- c.info(`Searching .coffee files in ${outDir}`);
127
- if (!fs.existsSync(outDir)) {
128
- c.warn(`Output directory not found: ${outDir}`);
129
- return;
98
+ }
99
+ if (prettierrc == null) {
100
+ try {
101
+ prettierrc = (await prettier.resolveConfig(projectRoot));
102
+ } catch (error1) {
103
+ error = error1;
104
+ prettierrc = null;
130
105
  }
131
- files = collectCoffeeFiles(outDir);
132
- if (files.length === 0) {
133
- c.info(`No .coffee files found in ${outDir}`);
134
- return;
106
+ }
107
+ if (prettierrc == null) {
108
+ try {
109
+ pkgPath = path.join(projectRoot, 'package.json');
110
+ if (fs.existsSync(pkgPath)) {
111
+ pkgData = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
112
+ if ((pkgData != null ? pkgData.prettier : void 0) != null) {
113
+ prettierrc = pkgData.prettier;
114
+ }
115
+ }
116
+ } catch (error1) {
117
+ error = error1;
135
118
  }
136
- c.info(`Found ${files.length} .coffee file(s)`);
137
- formattedCount = 0;
138
- for (j = 0, len1 = files.length; j < len1; j++) {
139
- file = files[j];
140
- try {
141
- // Ensure we only format CoffeeScript files
142
- if (path.extname(file) !== '.coffee') {
143
- c.debug(`Skip non-coffee file: ${file}`);
119
+ }
120
+ // ignore
121
+ c.info(`Searching .coffee files in ${targetDir}`);
122
+ if (!fs.existsSync(targetDir)) {
123
+ c.warn(`Target directory not found: ${targetDir}`);
124
+ return;
125
+ }
126
+ files = collectCoffeeFiles(targetDir);
127
+ if (files.length === 0) {
128
+ c.info(`No .coffee files found in ${targetDir}`);
129
+ return;
130
+ }
131
+ c.info(`Found ${files.length} .coffee file(s)`);
132
+ formattedCount = 0;
133
+ for (j = 0, len1 = files.length; j < len1; j++) {
134
+ file = files[j];
135
+ try {
136
+ // Ensure we only format CoffeeScript files
137
+ if (path.extname(file) !== '.coffee') {
138
+ c.debug(`Skip non-coffee file: ${file}`);
139
+ continue;
140
+ }
141
+ // Check ignore via patterns or ignore file
142
+ if (ig != null) {
143
+ rel = path.relative(projectRoot, file).split(path.sep).join('/');
144
+ if (ig.ignores(rel)) {
145
+ c.info(`Ignored by prettierignore patterns: ${file}`);
144
146
  continue;
145
147
  }
146
- // Check ignore via patterns or ignore file
147
- if (ig != null) {
148
- rel = path.relative(projectRoot, file).split(path.sep).join('/');
149
- if (ig.ignores(rel)) {
150
- c.info(`Ignored by prettierignore patterns: ${file}`);
148
+ } else if (ignorePath != null) {
149
+ try {
150
+ info = (await prettier.getFileInfo(file, {ignorePath}));
151
+ if (info != null ? info.ignored : void 0) {
152
+ c.info(`Ignored by .prettierignore: ${file}`);
151
153
  continue;
152
154
  }
153
- } else if (ignorePath != null) {
154
- try {
155
- info = (await prettier.getFileInfo(file, {ignorePath}));
156
- if (info != null ? info.ignored : void 0) {
157
- c.info(`Ignored by .prettierignore: ${file}`);
158
- continue;
159
- }
160
- } catch (error1) {
161
- err = error1;
162
- c.debug(`Failed to check ignore for ${file}: ${err.message}`);
163
- }
164
- }
165
- text = fs.readFileSync(file, 'utf8');
166
- prettierOptions = Object.assign({}, prettierrc || {}, {
167
- filepath: file,
168
- plugins: [require.resolve('prettier-plugin-coffeescript')]
169
- });
170
- formatted = prettier.format(text, prettierOptions);
171
- if (formatted !== text) {
172
- fs.writeFileSync(file, formatted, 'utf8');
173
- formattedCount += 1;
174
- c.success(`Formatted: ${file}`);
175
- } else {
176
- c.debug(`Already formatted: ${file}`);
155
+ } catch (error1) {
156
+ err = error1;
157
+ c.debug(`Failed to check ignore for ${file}: ${err.message}`);
177
158
  }
178
- } catch (error1) {
179
- error = error1;
180
- c.error(`Failed to format ${file}: ${error.message}`);
181
159
  }
160
+ text = fs.readFileSync(file, 'utf8');
161
+ prettierOptions = Object.assign({}, prettierrc || {}, {
162
+ filepath: file,
163
+ plugins: [require.resolve('prettier-plugin-coffeescript')]
164
+ });
165
+ formatted = prettier.format(text, prettierOptions);
166
+ if (formatted !== text) {
167
+ fs.writeFileSync(file, formatted, 'utf8');
168
+ formattedCount += 1;
169
+ c.success(`Formatted: ${file}`);
170
+ } else {
171
+ c.debug(`Already formatted: ${file}`);
172
+ }
173
+ } catch (error1) {
174
+ error = error1;
175
+ c.error(`Failed to format ${file}: ${error.message}`);
182
176
  }
183
- return c.success(`Prettier formatted ${formattedCount} file(s)`);
177
+ }
178
+ return c.success(`Prettier formatted ${formattedCount} file(s)`);
179
+ };
180
+
181
+ // Export a plugin factory
182
+ main = function(opts = {}) {
183
+ return async function(compilationResult) {
184
+ var compiledFiles, config, entry;
185
+ ({config, compiledFiles} = compilationResult);
186
+ // Determine entry directory
187
+ entry = config.entry;
188
+ return (await formatCoffeeFiles(entry, opts));
184
189
  };
185
190
  };
186
191
 
187
192
  module.exports = main;
193
+
194
+ module.exports.formatCoffeeFiles = formatCoffeeFiles;
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "milkee-plugin-prettier",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "A Milkee plugin for working with Prettier for CoffeeScript without version conflicts.",
5
5
  "main": "dist/main.js",
6
+ "bin": {
7
+ "milkee-plugin-prettier": "dist/bin.js",
8
+ "cprettier": "dist/bin.js"
9
+ },
6
10
  "scripts": {
7
11
  "test": "echo \"Error: no test specified\" && exit 0",
8
12
  "build": "milkee"
@@ -25,7 +29,9 @@
25
29
  },
26
30
  "devDependencies": {
27
31
  "coffeescript": "^2.7.0",
28
- "milkee": "^3.1.2"
32
+ "milkee": "^3.1.2",
33
+ "milkee-plugin-ext": "^1.0.3",
34
+ "milkee-plugin-shebang": "^1.0.1"
29
35
  },
30
36
  "keywords": [
31
37
  "milkee",