gulp-prettier 5.0.0 → 6.0.0

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.
Files changed (4) hide show
  1. package/LICENSE +0 -1
  2. package/README.md +13 -11
  3. package/index.js +100 -101
  4. package/package.json +20 -18
package/LICENSE CHANGED
@@ -1,6 +1,5 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2016-2020 Patel, Bhargav R.
4
3
  Copyright (c) 2020 Thomas Vantuycom
5
4
 
6
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
package/README.md CHANGED
@@ -1,7 +1,9 @@
1
- # gulp-prettier ![GitHub Workflow Status](https://github.com/TheDancingCode/gulp-prettier/actions/workflows/ci.yml/badge.svg) [![npm version](https://img.shields.io/npm/v/gulp-prettier.svg)](https://www.npmjs.com/package/gulp-prettier) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
1
+ # gulp-prettier
2
2
 
3
3
  > Format files with [Prettier](https://github.com/prettier/prettier)
4
4
 
5
+ [![GitHub Workflow Status](https://github.com/TheDancingCode/gulp-prettier/actions/workflows/ci.yml/badge.svg)](https://github.com/thomasvantuycom/gulp-prettier/actions/workflows/ci.yml) [![npm version](https://img.shields.io/npm/v/gulp-prettier.svg)](https://www.npmjs.com/package/gulp-prettier) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
6
+
5
7
  ## Install
6
8
 
7
9
  ```
@@ -11,30 +13,30 @@ npm install gulp-prettier --save-dev
11
13
  ## Usage
12
14
 
13
15
  ```js
14
- const { src, dest } = require('gulp');
15
- const prettier = require('gulp-prettier');
16
+ import gulp from 'gulp';
17
+ import prettier from 'gulp-prettier';
16
18
 
17
19
  function format() {
18
- return src('src/*.js')
20
+ return gulp.src('src/*.js')
19
21
  .pipe(prettier({ singleQuote: true }))
20
- .pipe(dest('dist'));
22
+ .pipe(gulp.dest('dist'));
21
23
  }
22
24
 
23
- exports.default = format;
25
+ export default format;
24
26
  ```
25
27
 
26
28
  To check whether or not your files adhere to Prettier's formatting, use `prettier.check`. This can be used as a validation step in CI scenarios.
27
29
 
28
30
  ```js
29
- const { src, dest } = require('gulp');
30
- const prettier = require('gulp-prettier');
31
+ import gulp from 'gulp';
32
+ import prettier from 'gulp-prettier';
31
33
 
32
34
  function validate() {
33
- return src('dist/*.js')
35
+ return gulp.src('dist/*.js')
34
36
  .pipe(prettier.check({ singleQuote: true }));
35
37
  }
36
38
 
37
- exports.default = validate;
39
+ export default validate;
38
40
  ```
39
41
 
40
42
  ## API
@@ -65,4 +67,4 @@ Consult the Prettier [options](https://prettier.io/docs/en/options.html).
65
67
 
66
68
  ## License
67
69
 
68
- MIT © [Bhargav R. Patel](https://github.com/bhargavrpatel), [Thomas Vantuycom](https://github.com/TheDancingCode)
70
+ MIT © [Thomas Vantuycom](https://github.com/thomasvantuycom)
package/index.js CHANGED
@@ -1,102 +1,101 @@
1
- 'use strict';
2
- const path = require('path');
3
- const through = require('through2');
4
- const PluginError = require('plugin-error');
5
- const prettier = require('prettier');
6
-
7
- const PLUGIN_NAME = 'gulp-prettier';
8
-
9
- module.exports = function (options) {
10
- options = options || {};
11
-
12
- return through.obj(async function (file, encoding, callback) {
13
- if (file.isNull()) {
14
- return callback(null, file);
15
- }
16
-
17
- if (file.isStream()) {
18
- return callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
19
- }
20
-
21
- const config = await prettier.resolveConfig(file.path, options);
22
- const fileOptions = { ...config, ...options, filepath: file.path };
23
-
24
- const unformattedCode = file.contents.toString('utf8');
25
-
26
- try {
27
- const formattedCode = await prettier.format(unformattedCode, fileOptions);
28
-
29
- if (formattedCode !== unformattedCode) {
30
- file.isPrettier = true;
31
- file.contents = Buffer.from(formattedCode);
32
- }
33
-
34
- this.push(file);
35
- } catch (error) {
36
- this.emit(
37
- 'error',
38
- new PluginError(PLUGIN_NAME, error, { fileName: file.path })
39
- );
40
- }
41
-
42
- callback();
43
- });
44
- };
45
-
46
- module.exports.check = function (options) {
47
- options = options || {};
48
-
49
- const unformattedFiles = [];
50
-
51
- return through.obj(
52
- async function (file, encoding, callback) {
53
- if (file.isNull()) {
54
- return callback(null, file);
55
- }
56
-
57
- if (file.isStream()) {
58
- return callback(
59
- new PluginError(PLUGIN_NAME, 'Streaming not supported')
60
- );
61
- }
62
-
63
- const config = await prettier.resolveConfig(file.path, options);
64
- const fileOptions = { ...config, ...options, filepath: file.path };
65
-
66
- const unformattedCode = file.contents.toString('utf8');
67
-
68
- try {
69
- const isFormatted = await prettier.check(unformattedCode, fileOptions);
70
-
71
- if (!isFormatted) {
72
- const filename = path
73
- .relative(process.cwd(), file.path)
74
- .replace(/\\/g, '/');
75
- unformattedFiles.push(filename);
76
- }
77
-
78
- this.push(file);
79
- } catch (error) {
80
- this.emit(
81
- 'error',
82
- new PluginError(PLUGIN_NAME, error, { fileName: file.path })
83
- );
84
- }
85
-
86
- callback();
87
- },
88
- function (callback) {
89
- if (unformattedFiles.length > 0) {
90
- const header =
91
- 'Code style issues found in the following file(s). Forgot to run Prettier?';
92
- const body = unformattedFiles.join('\n');
93
-
94
- const message = `${header}\n${body}`;
95
-
96
- this.emit('error', new PluginError(PLUGIN_NAME, message));
97
- }
98
-
99
- callback();
100
- }
101
- );
1
+ import path from 'node:path';
2
+ import {Transform} from 'node:stream';
3
+ import {Buffer} from 'node:buffer';
4
+ import process from 'node:process';
5
+ import PluginError from 'plugin-error';
6
+ import prettier from 'prettier';
7
+
8
+ export default function plugin(options = {}) {
9
+ return new Transform({
10
+ objectMode: true,
11
+ async transform(file, encoding, callback) {
12
+ if (file.isNull()) {
13
+ return callback(null, file);
14
+ }
15
+
16
+ if (file.isStream()) {
17
+ return callback(new PluginError('gulp-prettier', 'Streaming not supported'));
18
+ }
19
+
20
+ const config = await prettier.resolveConfig(file.path, options);
21
+ const fileOptions = {...config, ...options, filepath: file.path};
22
+
23
+ const unformattedCode = file.contents.toString('utf8');
24
+
25
+ try {
26
+ const formattedCode = await prettier.format(unformattedCode, fileOptions);
27
+
28
+ if (formattedCode !== unformattedCode) {
29
+ file.isPrettier = true;
30
+ file.contents = Buffer.from(formattedCode);
31
+ }
32
+
33
+ this.push(file);
34
+ } catch (error) {
35
+ this.emit(
36
+ 'error',
37
+ new PluginError('gulp-prettier', error, {fileName: file.path}),
38
+ );
39
+ }
40
+
41
+ callback();
42
+ },
43
+ });
44
+ }
45
+
46
+ plugin.check = function (options = {}) {
47
+ const unformattedFiles = [];
48
+
49
+ return new Transform({
50
+ objectMode: true,
51
+ async transform(file, encoding, callback) {
52
+ if (file.isNull()) {
53
+ return callback(null, file);
54
+ }
55
+
56
+ if (file.isStream()) {
57
+ return callback(
58
+ new PluginError('gulp-prettier', 'Streaming not supported'),
59
+ );
60
+ }
61
+
62
+ const config = await prettier.resolveConfig(file.path, options);
63
+ const fileOptions = {...config, ...options, filepath: file.path};
64
+
65
+ const unformattedCode = file.contents.toString('utf8');
66
+
67
+ try {
68
+ const isFormatted = await prettier.check(unformattedCode, fileOptions);
69
+
70
+ if (!isFormatted) {
71
+ const filename = path
72
+ .relative(process.cwd(), file.path)
73
+ .replaceAll('\\', '/');
74
+ unformattedFiles.push(filename);
75
+ }
76
+
77
+ this.push(file);
78
+ } catch (error) {
79
+ this.emit(
80
+ 'error',
81
+ new PluginError('gulp-prettier', error, {fileName: file.path}),
82
+ );
83
+ }
84
+
85
+ callback();
86
+ },
87
+ flush(callback) {
88
+ if (unformattedFiles.length > 0) {
89
+ const header
90
+ = 'Code style issues found in the following file(s). Forgot to run Prettier?';
91
+ const body = unformattedFiles.join('\n');
92
+
93
+ const message = `${header}\n${body}`;
94
+
95
+ this.emit('error', new PluginError('gulp-prettier', message));
96
+ }
97
+
98
+ callback();
99
+ },
100
+ });
102
101
  };
package/package.json CHANGED
@@ -1,14 +1,21 @@
1
1
  {
2
2
  "name": "gulp-prettier",
3
+ "version": "6.0.0",
3
4
  "description": "Format files with Prettier",
4
- "version": "5.0.0",
5
- "main": "index.js",
6
- "repository": {
7
- "type": "git",
8
- "url": "https://github.com/TheDancingCode/gulp-prettier.git"
5
+ "license": "MIT",
6
+ "repository": "thomasvantuycom/gulp-prettier",
7
+ "author": {
8
+ "name": "Thomas Vantuycom",
9
+ "email": "thomasvantuycom@proton.me",
10
+ "url": "https://www.thomasvantuycom.com"
11
+ },
12
+ "type": "module",
13
+ "exports": "./index.js",
14
+ "engines": {
15
+ "node": ">=18"
9
16
  },
10
17
  "scripts": {
11
- "test": "ava"
18
+ "test": "xo && ava"
12
19
  },
13
20
  "files": [
14
21
  "index.js"
@@ -19,23 +26,18 @@
19
26
  "gulp-prettier",
20
27
  "gulpplugin"
21
28
  ],
22
- "engines": {
23
- "node": ">=16"
24
- },
25
- "author": "Thomas Vantuycom",
26
- "license": "MIT",
27
29
  "dependencies": {
28
30
  "plugin-error": "^2.0.0",
29
- "prettier": "^3.0.0",
30
- "through2": "^4.0.2"
31
+ "prettier": "^3.0.0"
31
32
  },
32
33
  "devDependencies": {
33
- "@semantic-release/changelog": "^5.0.0",
34
- "@semantic-release/git": "^9.0.0",
34
+ "@semantic-release/changelog": "^6.0.0",
35
+ "@semantic-release/git": "^10.0.0",
35
36
  "ava": "^5.1.0",
36
- "p-event": "4.2.0",
37
- "semantic-release": "^17.2.3",
38
- "vinyl": "^3.0.0"
37
+ "p-event": "^6.0.0",
38
+ "semantic-release": "^22.0.0",
39
+ "vinyl": "^3.0.0",
40
+ "xo": "^0.56.0"
39
41
  },
40
42
  "release": {
41
43
  "plugins": [