forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator-transforms.js
48 lines (41 loc) · 1.31 KB
/
generator-transforms.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
const through = require('through2');
const prettier = require('prettier');
const defaultPrettierOptions = {
printWidth: 140,
singleQuote: true,
useTabs: false
};
const defaultTsPrettierOptions = Object.assign(defaultPrettierOptions, {
jsxBracketSameLine: false,
arrowParens: 'avoid',
parser: 'typescript',
});
const defaultCssPrettierOptions = Object.assign(defaultPrettierOptions, {
parser: 'css',
});
const defaultScssPrettierOptions = Object.assign(defaultPrettierOptions, {
parser: 'scss',
});
const prettierTransform = function (defaultOptions) {
const transform = (file, encoding, callback) => {
/* resolve from the projects config */
prettier.resolveConfig(file.relative).then((options) => {
const str = file.contents.toString('utf8');
if (!options || Object.keys(options).length === 0) {
options = defaultOptions;
}
// for better errors
options.filepath = file.relative;
const data = prettier.format(str, options);
file.contents = Buffer.from(data);
callback(null, file);
});
};
return through.obj(transform);
};
module.exports = {
prettierTransform,
defaultTsPrettierOptions,
defaultCssPrettierOptions,
defaultScssPrettierOptions
};