milkee-plugin-prettier 0.0.2 → 0.0.4

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
@@ -13,8 +13,7 @@ A Milkee plugin for working with Prettier for CoffeeScript without version confl
13
13
  - Automatically resolves config via `prettier.resolveConfig()` or `package.json#prettier` when `prettierrc` is not provided.
14
14
 
15
15
  ## Usage
16
-
17
- ### coffee.config.cjs
16
+ ### As a Milkee Plugin (coffee.config.cjs)
18
17
 
19
18
  ```js
20
19
  const prettierPlugin = require('milkee-plugin-prettier');
@@ -34,6 +33,23 @@ module.exports = {
34
33
  };
35
34
  ```
36
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
+
37
53
  ### Notes
38
54
 
39
55
  - `.prettierignore` is resolved relative to `process.cwd()` by default. You can pass an absolute or relative path via the `prettierignore` option.
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'));
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,133 +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, entry, err, error, file, files, formatted, formattedCount, ig, ignorePath, ignorePatterns, info, j, len1, pkgPath, prettierOptions, prettierrc, projectRoot, rcPath, rel, text;
61
- ({config, compiledFiles} = compilationResult);
62
- // Determine entry directory
63
- entry = config.entry;
64
- projectRoot = process.cwd();
65
- // Handle prettierignore: can be a path (string) or an array of patterns
66
- ignorePath = null;
67
- ignorePatterns = null;
68
- ig = null;
69
- if (Array.isArray(opts != null ? opts.prettierignore : void 0)) {
70
- ignorePatterns = opts.prettierignore.filter(function(p) {
71
- return typeof p === 'string';
72
- });
73
- if (ignorePatterns.length > 0) {
74
- ig = ignore().add(ignorePatterns);
75
- }
76
- } else if (typeof (opts != null ? opts.prettierignore : void 0) === 'string') {
77
- ignorePath = path.isAbsolute(opts.prettierignore) ? opts.prettierignore : path.join(projectRoot, opts.prettierignore);
78
- if (!fs.existsSync(ignorePath)) {
79
- c.warn(`prettierignore not found at ${ignorePath}`);
80
- ignorePath = null;
81
- }
82
- } else {
83
- defaultIgnore = path.join(projectRoot, '.prettierignore');
84
- if (fs.existsSync(defaultIgnore)) {
85
- ignorePath = defaultIgnore;
86
- }
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);
87
71
  }
88
- // Load prettierrc: opts.prettierrc can be an object or a path to a file. If not provided, try resolveConfig, then package.json
89
- prettierrc = null;
90
- if ((opts != null ? opts.prettierrc : void 0) != null) {
91
- if (typeof opts.prettierrc === 'object') {
92
- prettierrc = opts.prettierrc;
93
- } else if (typeof opts.prettierrc === 'string') {
94
- rcPath = path.isAbsolute(opts.prettierrc) ? opts.prettierrc : path.join(projectRoot, opts.prettierrc);
95
- try {
96
- prettierrc = JSON.parse(fs.readFileSync(rcPath, 'utf8'));
97
- } catch (error1) {
98
- error = error1;
99
- c.warn(`Failed to read prettierrc at ${rcPath}: ${error.message}`);
100
- }
101
- }
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;
102
77
  }
103
- if (prettierrc == null) {
104
- try {
105
- prettierrc = (await prettier.resolveConfig(projectRoot));
106
- } catch (error1) {
107
- error = error1;
108
- prettierrc = null;
109
- }
78
+ } else {
79
+ defaultIgnore = path.join(projectRoot, '.prettierignore');
80
+ if (fs.existsSync(defaultIgnore)) {
81
+ ignorePath = defaultIgnore;
110
82
  }
111
- 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);
112
91
  try {
113
- pkgPath = path.join(projectRoot, 'package.json');
114
- if (fs.existsSync(pkgPath)) {
115
- pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
116
- if ((pkg != null ? pkg.prettier : void 0) != null) {
117
- prettierrc = pkg.prettier;
118
- }
119
- }
92
+ prettierrc = JSON.parse(fs.readFileSync(rcPath, 'utf8'));
120
93
  } catch (error1) {
121
94
  error = error1;
95
+ c.warn(`Failed to read prettierrc at ${rcPath}: ${error.message}`);
122
96
  }
123
97
  }
124
- // ignore
125
- c.info(`Searching .coffee files in ${entry}`);
126
- if (!fs.existsSync(entry)) {
127
- c.warn(`Entry directory not found: ${entry}`);
128
- return;
98
+ }
99
+ if (prettierrc == null) {
100
+ try {
101
+ prettierrc = (await prettier.resolveConfig(projectRoot));
102
+ } catch (error1) {
103
+ error = error1;
104
+ prettierrc = null;
129
105
  }
130
- files = collectCoffeeFiles(entry);
131
- if (files.length === 0) {
132
- c.info(`No .coffee files found in ${entry}`);
133
- 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;
134
118
  }
135
- c.info(`Found ${files.length} .coffee file(s)`);
136
- formattedCount = 0;
137
- for (j = 0, len1 = files.length; j < len1; j++) {
138
- file = files[j];
139
- try {
140
- // Ensure we only format CoffeeScript files
141
- if (path.extname(file) !== '.coffee') {
142
- 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}`);
143
146
  continue;
144
147
  }
145
- // Check ignore via patterns or ignore file
146
- if (ig != null) {
147
- rel = path.relative(projectRoot, file).split(path.sep).join('/');
148
- if (ig.ignores(rel)) {
149
- 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}`);
150
153
  continue;
151
154
  }
152
- } else if (ignorePath != null) {
153
- try {
154
- info = (await prettier.getFileInfo(file, {ignorePath}));
155
- if (info != null ? info.ignored : void 0) {
156
- c.info(`Ignored by .prettierignore: ${file}`);
157
- continue;
158
- }
159
- } catch (error1) {
160
- err = error1;
161
- c.debug(`Failed to check ignore for ${file}: ${err.message}`);
162
- }
163
- }
164
- text = fs.readFileSync(file, 'utf8');
165
- prettierOptions = Object.assign({}, prettierrc || {}, {
166
- filepath: file,
167
- plugins: [require.resolve('prettier-plugin-coffeescript')]
168
- });
169
- formatted = prettier.format(text, prettierOptions);
170
- if (formatted !== text) {
171
- fs.writeFileSync(file, formatted, 'utf8');
172
- formattedCount += 1;
173
- c.success(`Formatted: ${file}`);
174
- } else {
175
- c.debug(`Already formatted: ${file}`);
155
+ } catch (error1) {
156
+ err = error1;
157
+ c.debug(`Failed to check ignore for ${file}: ${err.message}`);
176
158
  }
177
- } catch (error1) {
178
- error = error1;
179
- c.error(`Failed to format ${file}: ${error.message}`);
180
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}`);
181
176
  }
182
- 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));
183
189
  };
184
190
  };
185
191
 
186
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.2",
3
+ "version": "0.0.4",
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,8 @@
25
29
  },
26
30
  "devDependencies": {
27
31
  "coffeescript": "^2.7.0",
28
- "milkee": "^3.1.2"
32
+ "milkee": "^3.1.2",
33
+ "milkee-plugin-shebang": "^1.0.1"
29
34
  },
30
35
  "keywords": [
31
36
  "milkee",