Skip to content

Commit 2dff074

Browse files
committed
feature: @putout/cli-process-file: printer: package.json
1 parent 047bba2 commit 2dff074

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

packages/cli-process-file/lib/printer/printer.mjs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,36 @@ const JSON = {
1818
},
1919
};
2020

21+
const PACKAGE = {
22+
...JSON,
23+
format: {
24+
...JSON.format,
25+
indent: ' ',
26+
},
27+
};
28+
2129
export const configurePrinter = (name, printerOptions) => {
2230
const [printer = 'putout', options = {}] = maybeArray(printerOptions);
23-
const ext = name
24-
.split('.')
25-
.pop();
2631

2732
if (printer !== 'putout')
2833
return printerOptions;
2934

30-
const mergedOptions = deepMerge(parseOptions(ext), options);
35+
const mergedOptions = deepMerge(parseOptions(name), options);
3136

3237
return [printer, mergedOptions];
3338
};
3439

35-
function parseOptions(ext) {
36-
if (ext === 'json')
40+
function parseOptions(name) {
41+
const ext = name
42+
.split('.')
43+
.pop();
44+
45+
if (ext === 'json') {
46+
if (name.endsWith('package.json'))
47+
return PACKAGE;
48+
3749
return JSON;
50+
}
3851

3952
if (ext === 'yml{json}')
4053
return JSON;

packages/cli-process-file/lib/printer/printer.spec.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ test('putout: cli: printer: md{js}', (t) => {
5353
t.end();
5454
});
5555

56+
test('putout: cli: printer: json: package.json', (t) => {
57+
const result = configurePrinter('/package.json');
58+
const expected = ['putout', {
59+
format: {
60+
quote: '"',
61+
indent: ' ',
62+
},
63+
semantics: {
64+
trailingComma: false,
65+
},
66+
}];
67+
68+
t.deepEqual(result, expected);
69+
t.end();
70+
});
71+
5672
test('putout: cli: printer: printer: md{ts}', (t) => {
5773
const result = configurePrinter('hello.md{ts}', ['putout', {}]);
5874

0 commit comments

Comments
 (0)