Skip to content

Commit 6e54fc8

Browse files
committed
Fix prettier issues. Fixes #34 and #36.
1 parent f41884a commit 6e54fc8

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

src/format.js

-19
This file was deleted.

src/handler.js

+43-22
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,56 @@ const fs = require("fs-extra");
22
const isFunction = require("lodash/isFunction");
33
const merge = require("lodash/merge");
44
const path = require("path");
5+
const prettier = require("prettier");
56
const stripIndent = require("strip-indent");
67
const uniq = require("lodash/uniq");
7-
const { formatCode, formatJson, formatMd } = require("./format");
88

9-
let currentHandler;
9+
async function getPrettierConfig(file) {
10+
return await prettier.resolveConfig(file);
11+
}
1012

11-
async function readIfExists(file) {
12-
return (await fs.exists(file)) ? (await fs.readFile(file)).toString() : null;
13+
async function formatCode(file, data) {
14+
return prettier.format(data, {
15+
parser: "babel",
16+
...(await getPrettierConfig(file))
17+
});
1318
}
1419

15-
async function handleArray(file) {
16-
let data;
17-
const curr = ((await readIfExists(file.name)) || "").split("\n");
18-
data = file.data.concat(curr);
19-
data = uniq(data);
20-
return data.filter(Boolean).join("\n");
20+
async function formatMd(file, data) {
21+
return prettier.format(data, {
22+
parser: "markdown",
23+
...(await getPrettierConfig(file))
24+
});
25+
}
26+
27+
async function readIfExists(file) {
28+
return (await fs.exists(file)) ? (await fs.readFile(file)).toString() : null;
2129
}
2230

2331
async function handleJs(file) {
24-
const curr = (await readIfExists(file.name)) || file.data;
25-
const data = file.overwrite ? file.data : curr;
26-
return formatCode(data);
32+
const currData = await readIfExists(file.name);
33+
if (file.overwrite || !currData) {
34+
return await formatCode(file.name, file.data);
35+
}
36+
return currData;
2737
}
2838

2939
async function handleJson(file) {
30-
const curr = JSON.parse(await readIfExists(file.name));
40+
const currData = await readIfExists(file.name);
41+
const currJson = JSON.parse(currData);
3142
let data;
3243

33-
if (curr) {
44+
if (currData) {
3445
if (file.overwrite) {
35-
data = file.merge ? merge({}, curr, file.data) : file.data;
46+
if (file.merge) {
47+
data = merge({}, currJson, file.data);
48+
} else {
49+
data = file.data;
50+
}
3651
} else if (file.merge) {
37-
data = merge({}, file.data, curr);
52+
data = merge({}, file.data, currJson);
3853
} else {
39-
data = curr;
54+
return currData;
4055
}
4156
} else {
4257
data = file.data;
@@ -46,13 +61,19 @@ async function handleJson(file) {
4661
}
4762

4863
async function handleMd(file) {
49-
return formatMd(await handleString(file));
64+
const currData = await readIfExists(file.name);
65+
if (file.overwrite || !currData) {
66+
return await formatMd(file.name, file.data);
67+
}
68+
return currData;
5069
}
5170

5271
async function handleString(file) {
53-
const curr = (await readIfExists(file.name)) || file.data;
54-
const data = file.overwrite ? file.data : curr;
55-
return stripIndent(data).trim();
72+
const currData = await readIfExists(file.name);
73+
if (file.overwrite || !currData) {
74+
return stripIndent(file.data).trim();
75+
}
76+
return currData;
5677
}
5778

5879
const mapExtname = {

0 commit comments

Comments
 (0)