@dotcom-tool-kit/prettier 1.2.2 → 1.4.1
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.
- package/lib/tasks/prettier.d.ts +1 -0
- package/lib/tasks/prettier.d.ts.map +1 -1
- package/lib/tasks/prettier.js +32 -18
- package/package.json +10 -7
package/lib/tasks/prettier.d.ts
CHANGED
|
@@ -4,5 +4,6 @@ export default class Prettier extends Task<typeof PrettierSchema> {
|
|
|
4
4
|
static description: string;
|
|
5
5
|
static defaultOptions: PrettierOptions;
|
|
6
6
|
run(files?: string[]): Promise<void>;
|
|
7
|
+
formatFile: (filepath: string, options: PrettierOptions) => Promise<void>;
|
|
7
8
|
}
|
|
8
9
|
//# sourceMappingURL=prettier.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prettier.d.ts","sourceRoot":"","sources":["../../src/tasks/prettier.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAA;
|
|
1
|
+
{"version":3,"file":"prettier.d.ts","sourceRoot":"","sources":["../../src/tasks/prettier.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAA;AAI5F,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAG7C,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,IAAI,CAAC,OAAO,cAAc,CAAC;IAC/D,MAAM,CAAC,WAAW,SAAK;IAEvB,MAAM,CAAC,cAAc,EAAE,eAAe,CASrC;IAEK,GAAG,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAkB1C,UAAU,aAAoB,MAAM,WAAW,eAAe,KAAG,QAAQ,IAAI,CAAC,CAgC7E;CACF"}
|
package/lib/tasks/prettier.js
CHANGED
|
@@ -4,16 +4,47 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
const prettier_1 = (0, tslib_1.__importDefault)(require("prettier"));
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const fast_glob_1 = (0, tslib_1.__importDefault)(require("fast-glob"));
|
|
7
|
+
const logger_1 = require("@dotcom-tool-kit/logger");
|
|
7
8
|
const types_1 = require("@dotcom-tool-kit/types");
|
|
8
9
|
const error_1 = require("@dotcom-tool-kit/error");
|
|
9
10
|
class Prettier extends types_1.Task {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(...arguments);
|
|
13
|
+
this.formatFile = async (filepath, options) => {
|
|
14
|
+
const fileContent = await fs_1.promises.readFile(filepath, 'utf8');
|
|
15
|
+
let prettierConfig;
|
|
16
|
+
try {
|
|
17
|
+
prettierConfig = await prettier_1.default.resolveConfig(filepath, { config: options.configFile });
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
if (err.code !== 'ENOENT') {
|
|
21
|
+
const error = new error_1.ToolKitError('there was an error when resolving the prettier config');
|
|
22
|
+
if (err instanceof Error) {
|
|
23
|
+
error.details = err.message;
|
|
24
|
+
}
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (!prettierConfig && options.configOptions) {
|
|
29
|
+
this.logger.warn(`prettier could not find the specified configFile${options.configFile ? ` (${logger_1.styles.filepath(options.configFile)})` : ''}), using ${logger_1.styles.option('configOptions')} instead`);
|
|
30
|
+
prettierConfig = options.configOptions;
|
|
31
|
+
}
|
|
32
|
+
const unhook = (0, logger_1.hookConsole)(this.logger, 'prettier');
|
|
33
|
+
try {
|
|
34
|
+
await fs_1.promises.writeFile(filepath, prettier_1.default.format(fileContent, { ...prettierConfig, filepath }));
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
unhook();
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
10
41
|
async run(files) {
|
|
11
42
|
try {
|
|
12
43
|
const filepaths = await (0, fast_glob_1.default)(files !== null && files !== void 0 ? files : this.options.files);
|
|
13
44
|
for (const filepath of filepaths) {
|
|
14
45
|
const { ignored } = await prettier_1.default.getFileInfo(filepath);
|
|
15
46
|
if (!ignored) {
|
|
16
|
-
await formatFile(filepath, this.options);
|
|
47
|
+
await this.formatFile(filepath, this.options);
|
|
17
48
|
}
|
|
18
49
|
}
|
|
19
50
|
}
|
|
@@ -38,20 +69,3 @@ Prettier.defaultOptions = {
|
|
|
38
69
|
trailingComma: 'none'
|
|
39
70
|
}
|
|
40
71
|
};
|
|
41
|
-
const formatFile = async (filepath, options) => {
|
|
42
|
-
const fileContent = await fs_1.promises.readFile(filepath, 'utf8');
|
|
43
|
-
let prettierConfig;
|
|
44
|
-
try {
|
|
45
|
-
prettierConfig = await prettier_1.default.resolveConfig(filepath, { config: options.configFile });
|
|
46
|
-
}
|
|
47
|
-
catch (err) {
|
|
48
|
-
if (err.code !== 'ENOENT') {
|
|
49
|
-
throw err;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
if (!prettierConfig && options.configOptions) {
|
|
53
|
-
console.log(`prettier could not find the specified configFile (${options.configFile}), using configOptions instead`);
|
|
54
|
-
prettierConfig = options.configOptions;
|
|
55
|
-
}
|
|
56
|
-
await fs_1.promises.writeFile(filepath, prettier_1.default.format(fileContent, { ...prettierConfig, filepath }));
|
|
57
|
-
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotcom-tool-kit/prettier",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib",
|
|
6
6
|
"scripts": {
|
|
@@ -10,10 +10,12 @@
|
|
|
10
10
|
"author": "FT.com Platforms Team <platforms-team.customer-products@ft.com>",
|
|
11
11
|
"license": "ISC",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@dotcom-tool-kit/error": "^1.
|
|
14
|
-
"@dotcom-tool-kit/
|
|
15
|
-
"@dotcom-tool-kit/
|
|
13
|
+
"@dotcom-tool-kit/error": "^1.4.1",
|
|
14
|
+
"@dotcom-tool-kit/logger": "^1.4.1",
|
|
15
|
+
"@dotcom-tool-kit/package-json-hook": "^1.4.1",
|
|
16
|
+
"@dotcom-tool-kit/types": "^1.4.1",
|
|
16
17
|
"fast-glob": "^3.2.7",
|
|
18
|
+
"hook-std": "^2.0.0",
|
|
17
19
|
"prettier": "^2.2.1"
|
|
18
20
|
},
|
|
19
21
|
"repository": {
|
|
@@ -28,9 +30,10 @@
|
|
|
28
30
|
".toolkitrc.yml"
|
|
29
31
|
],
|
|
30
32
|
"devDependencies": {
|
|
31
|
-
"@jest/globals": "^
|
|
32
|
-
"jest": "^
|
|
33
|
-
"ts-jest": "^
|
|
33
|
+
"@jest/globals": "^27.4.6",
|
|
34
|
+
"jest": "^27.4.7",
|
|
35
|
+
"ts-jest": "^27.1.3",
|
|
36
|
+
"winston": "^3.4.0"
|
|
34
37
|
},
|
|
35
38
|
"volta": {
|
|
36
39
|
"extends": "../../package.json"
|