@dotcom-tool-kit/prettier 1.0.2-beta.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/.toolkitrc.yml +3 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +18 -0
- package/lib/tasks/prettier.d.ts +9 -0
- package/lib/tasks/prettier.d.ts.map +1 -0
- package/lib/tasks/prettier.js +71 -0
- package/package.json +41 -0
- package/readme.md +52 -0
package/.toolkitrc.yml
ADDED
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PackageJsonHook } from '@dotcom-tool-kit/package-json-hook';
|
|
2
|
+
import Prettier from './tasks/prettier';
|
|
3
|
+
export declare const tasks: (typeof Prettier)[];
|
|
4
|
+
declare class FormatLocal extends PackageJsonHook {
|
|
5
|
+
static description: string;
|
|
6
|
+
key: string;
|
|
7
|
+
hook: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const hooks: {
|
|
10
|
+
'format:local': typeof FormatLocal;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAA;AACpE,OAAO,QAAQ,MAAM,kBAAkB,CAAA;AAEvC,eAAO,MAAM,KAAK,qBAAa,CAAA;AAE/B,cAAM,WAAY,SAAQ,eAAe;IACvC,MAAM,CAAC,WAAW,SAAoB;IAEtC,GAAG,SAAW;IACd,IAAI,SAAiB;CACtB;AAED,eAAO,MAAM,KAAK;;CAEjB,CAAA"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hooks = exports.tasks = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const package_json_hook_1 = require("@dotcom-tool-kit/package-json-hook");
|
|
6
|
+
const prettier_1 = (0, tslib_1.__importDefault)(require("./tasks/prettier"));
|
|
7
|
+
exports.tasks = [prettier_1.default];
|
|
8
|
+
class FormatLocal extends package_json_hook_1.PackageJsonHook {
|
|
9
|
+
constructor() {
|
|
10
|
+
super(...arguments);
|
|
11
|
+
this.key = 'format';
|
|
12
|
+
this.hook = `format:local`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
FormatLocal.description = 'format prettier';
|
|
16
|
+
exports.hooks = {
|
|
17
|
+
'format:local': FormatLocal
|
|
18
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PrettierOptions, PrettierSchema } from '@dotcom-tool-kit/types/lib/schema/prettier';
|
|
2
|
+
import { Task } from '@dotcom-tool-kit/types';
|
|
3
|
+
export default class Prettier extends Task<typeof PrettierSchema> {
|
|
4
|
+
static description: string;
|
|
5
|
+
static defaultOptions: PrettierOptions;
|
|
6
|
+
run(files?: string[]): Promise<void>;
|
|
7
|
+
formatFile: (filepath: string, options: PrettierOptions) => Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=prettier.d.ts.map
|
|
@@ -0,0 +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;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"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const prettier_1 = (0, tslib_1.__importDefault)(require("prettier"));
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const fast_glob_1 = (0, tslib_1.__importDefault)(require("fast-glob"));
|
|
7
|
+
const logger_1 = require("@dotcom-tool-kit/logger");
|
|
8
|
+
const types_1 = require("@dotcom-tool-kit/types");
|
|
9
|
+
const error_1 = require("@dotcom-tool-kit/error");
|
|
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
|
+
}
|
|
41
|
+
async run(files) {
|
|
42
|
+
try {
|
|
43
|
+
const filepaths = await (0, fast_glob_1.default)(files !== null && files !== void 0 ? files : this.options.files);
|
|
44
|
+
for (const filepath of filepaths) {
|
|
45
|
+
const { ignored } = await prettier_1.default.getFileInfo(filepath);
|
|
46
|
+
if (!ignored) {
|
|
47
|
+
await this.formatFile(filepath, this.options);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
const error = new error_1.ToolKitError('there was an error running the prettier plugin');
|
|
53
|
+
if (err instanceof Error) {
|
|
54
|
+
error.details = err.message;
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.default = Prettier;
|
|
61
|
+
Prettier.description = '';
|
|
62
|
+
Prettier.defaultOptions = {
|
|
63
|
+
files: ['**/*.js'],
|
|
64
|
+
configOptions: {
|
|
65
|
+
singleQuote: true,
|
|
66
|
+
useTabs: true,
|
|
67
|
+
bracketSpacing: true,
|
|
68
|
+
arrowParens: 'always',
|
|
69
|
+
trailingComma: 'none'
|
|
70
|
+
}
|
|
71
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dotcom-tool-kit/prettier",
|
|
3
|
+
"version": "1.0.2-beta.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "lib",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "cd ../../ ; npx jest --silent --projects plugins/prettier"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "FT.com Platforms Team <platforms-team.customer-products@ft.com>",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@dotcom-tool-kit/error": "^1.0.2-beta.1",
|
|
14
|
+
"@dotcom-tool-kit/logger": "^1.0.2-beta.1",
|
|
15
|
+
"@dotcom-tool-kit/package-json-hook": "^1.0.2-beta.1",
|
|
16
|
+
"@dotcom-tool-kit/types": "^1.0.2-beta.1",
|
|
17
|
+
"fast-glob": "^3.2.7",
|
|
18
|
+
"hook-std": "^2.0.0",
|
|
19
|
+
"prettier": "^2.2.1"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/financial-times/dotcom-tool-kit.git",
|
|
24
|
+
"directory": "plugins/prettier"
|
|
25
|
+
},
|
|
26
|
+
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues",
|
|
27
|
+
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/prettier",
|
|
28
|
+
"files": [
|
|
29
|
+
"/lib",
|
|
30
|
+
".toolkitrc.yml"
|
|
31
|
+
],
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@jest/globals": "^27.4.6",
|
|
34
|
+
"jest": "^27.4.7",
|
|
35
|
+
"ts-jest": "^27.1.3",
|
|
36
|
+
"winston": "^3.5.1"
|
|
37
|
+
},
|
|
38
|
+
"volta": {
|
|
39
|
+
"extends": "../../package.json"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# `prettier` Tool Kit plugin
|
|
2
|
+
|
|
3
|
+
This plugin is for adding prettier onto your apps.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install `@dotcom-tool-kit/prettier` as a `devDependency` in your app:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install --save-dev @dotcom-tool-kit/prettier
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Add the plugin to your [Tool Kit configuration](https://github.com/financial-times/dotcom-tool-kit/blob/main/readme.md#configuration):
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
plugins:
|
|
17
|
+
- '@dotcom-tool-kit/prettier'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
And install this plugin's hooks:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npx dotcom-tool-kit --install
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This will modify your `package.json`. You should commit this change.
|
|
27
|
+
|
|
28
|
+
### Options
|
|
29
|
+
|
|
30
|
+
#### `files`
|
|
31
|
+
|
|
32
|
+
A required Array of strings of filepath(s) or filepath pattern(s) to be formatted. Defaults to `['{,!(node_modules)/**/}*.js'].
|
|
33
|
+
|
|
34
|
+
#### `configOptions`
|
|
35
|
+
|
|
36
|
+
An optional prettier configuration object. Defaults to:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
{
|
|
40
|
+
singleQuote: true,
|
|
41
|
+
useTabs: true,
|
|
42
|
+
bracketSpacing: true,
|
|
43
|
+
arrowParens: 'always',
|
|
44
|
+
trailingComma: 'none'
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### `configFile`
|
|
49
|
+
|
|
50
|
+
An optional String that specifies the prettier configuration file (.prettierrc.json). The configuration file will be resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn’t) found. If the configFile is not found the prettier plugin will default to configOptions.
|
|
51
|
+
|
|
52
|
+
For more information on prettier configuration, visit the [Prettier docs](https://prettier.io/docs/en/configuration.html).
|