UNPKG

2.96 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");
10const workspaces_1 = require("nx/src/config/workspaces");
11const devkit_2 = require("@nrwl/devkit");
12function formatFiles(options = { skipFormat: false }, directory = '') {
13 let prettier;
14 try {
15 prettier = require('prettier');
16 }
17 catch (e) { }
18 if (options.skipFormat) {
19 return (0, schematics_1.noop)();
20 }
21 return (host, context) => {
22 updateWorkspaceJsonToMatchFormatVersion(host);
23 if (!prettier) {
24 return host;
25 }
26 const files = new Set(host.actions
27 .filter((action) => action.kind !== 'd' && action.kind !== 'r')
28 .map((action) => ({
29 path: action.path,
30 content: action.content.toString(),
31 })));
32 if (files.size === 0) {
33 return host;
34 }
35 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* () {
36 const systemPath = path.join(devkit_1.workspaceRoot, file.path);
37 let options = {
38 filepath: systemPath,
39 };
40 const resolvedOptions = yield prettier.resolveConfig(systemPath);
41 if (resolvedOptions) {
42 options = Object.assign(Object.assign({}, options), resolvedOptions);
43 }
44 const support = yield prettier.getFileInfo(systemPath, options);
45 if (support.ignored || !support.inferredParser) {
46 return;
47 }
48 try {
49 host.overwrite(file.path, prettier.format(file.content, options));
50 }
51 catch (e) {
52 context.logger.warn(`Could not format ${file.path} because ${e.message}`);
53 }
54 })), (0, operators_1.map)(() => host));
55 };
56}
57exports.formatFiles = formatFiles;
58function updateWorkspaceJsonToMatchFormatVersion(host) {
59 const workspaceConfigPath = (0, workspaces_1.workspaceConfigName)(devkit_1.workspaceRoot);
60 try {
61 if (workspaceConfigPath) {
62 const workspaceJson = (0, devkit_2.parseJson)(host.read(workspaceConfigPath).toString());
63 const reformatted = (0, workspaces_1.reformattedWorkspaceJsonOrNull)(workspaceJson);
64 if (reformatted) {
65 host.overwrite(workspaceConfigPath, (0, devkit_2.serializeJson)(reformatted));
66 }
67 }
68 }
69 catch (e) {
70 console.error(`Failed to format workspace config: ${workspaceConfigPath}`);
71 console.error(e);
72 }
73}
74//# sourceMappingURL=format-files.js.map
\No newline at end of file