-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory.js
56 lines (45 loc) · 1.43 KB
/
factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const PluginError = require("plugin-error");
const through = require("through2");
/** @type {{ name: string }} */
const pkg = require("./package.json");
/**
* Creates a PluginError with the provided message
*
* @param {string} msg
* @return {PluginError}
*/
const withError = msg => new PluginError(pkg.name, msg);
/**
* Creats a Gulp plugin for formatting code with prettier
*
* @see https://github.com/prettier/prettier#options
*/
const pluginFactory = prettier => (options = {}) =>
through.obj((file, encoding, done) => {
if (file.isNull()) {
return done(null, file);
}
if (file.isStream()) {
return done(
withError(
"Streaming file contents not supported. Use vinyl-buffer or gulp-buffer before this plugin."
)
);
}
// Prettier can determine which parser to use based on the file path. This
// is useful for e.g. formatting CSS or TypeScript files.
options.filepath = file.path;
const originalCode = file.contents.toString();
try {
const prettierCode = prettier.format(originalCode, options);
file.didPrettierFormat = prettierCode !== originalCode;
// Only need to create a new buffer if the code actually changed
if (file.didPrettierFormat) {
file.contents = new Buffer(prettierCode);
}
return done(null, file);
} catch (err) {
return done(withError(err));
}
});
module.exports = pluginFactory;