UNPKG

2.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.formatFiles = void 0;
4const tslib_1 = require("tslib");
5const schematics_1 = require("@angular-devkit/schematics");
6const rxjs_1 = require("rxjs");
7const operators_1 = require("rxjs/operators");
8const path = require("path");
9const devkit_1 = require("@nrwl/devkit");
10function formatFiles(options = { skipFormat: false }, directory = '') {
11 let prettier;
12 try {
13 prettier = require('prettier');
14 }
15 catch (e) { }
16 if (options.skipFormat) {
17 return (0, schematics_1.noop)();
18 }
19 return (host, context) => {
20 if (!prettier) {
21 return host;
22 }
23 const files = new Set(host.actions
24 .filter((action) => action.kind !== 'd' && action.kind !== 'r')
25 .map((action) => ({
26 path: action.path,
27 content: action.content.toString(),
28 })));
29 if (files.size === 0) {
30 return host;
31 }
32 return (0, rxjs_1.from)(files).pipe((0, operators_1.filter)((file) => host.exists(file.path)), (0, operators_1.mergeMap)((file) => tslib_1.__awaiter(this, void 0, void 0, function* () {
33 const systemPath = path.join(devkit_1.workspaceRoot, file.path);
34 let options = {
35 filepath: systemPath,
36 };
37 const resolvedOptions = yield prettier.resolveConfig(systemPath);
38 if (resolvedOptions) {
39 options = Object.assign(Object.assign({}, options), resolvedOptions);
40 }
41 const support = yield prettier.getFileInfo(systemPath, options);
42 if (support.ignored || !support.inferredParser) {
43 return;
44 }
45 try {
46 host.overwrite(file.path, prettier.format(file.content, options));
47 }
48 catch (e) {
49 context.logger.warn(`Could not format ${file.path} because ${e.message}`);
50 }
51 })), (0, operators_1.map)(() => host));
52 };
53}
54exports.formatFiles = formatFiles;
55//# sourceMappingURL=format-files.js.map
\No newline at end of file