forked from diffplug/spotless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrettierFormatterStep.java
More file actions
151 lines (128 loc) · 5.94 KB
/
PrettierFormatterStep.java
File metadata and controls
151 lines (128 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.npm;
import static java.util.Objects.requireNonNull;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterFunc.Closeable;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ThrowingEx;
public class PrettierFormatterStep {
private static final Logger logger = LoggerFactory.getLogger(PrettierFormatterStep.class);
public static final String NAME = "prettier-format";
public static final String DEFAULT_VERSION = "2.8.8";
public static final Map<String, String> defaultDevDependencies() {
return defaultDevDependenciesWithPrettier(DEFAULT_VERSION);
}
public static final Map<String, String> defaultDevDependenciesWithPrettier(String version) {
return Collections.singletonMap("prettier", version);
}
public static FormatterStep create(Map<String, String> devDependencies, Provisioner provisioner, File projectDir, File buildDir, File cacheDir, NpmPathResolver npmPathResolver, PrettierConfig prettierConfig) {
requireNonNull(devDependencies);
requireNonNull(provisioner);
requireNonNull(buildDir);
return FormatterStep.createLazy(NAME,
() -> new State(NAME, devDependencies, projectDir, buildDir, cacheDir, npmPathResolver, prettierConfig),
State::createFormatterFunc);
}
private static class State extends NpmFormatterStepStateBase implements Serializable {
private static final long serialVersionUID = -539537027004745812L;
private final PrettierConfig prettierConfig;
State(String stepName, Map<String, String> devDependencies, File projectDir, File buildDir, File cacheDir, NpmPathResolver npmPathResolver, PrettierConfig prettierConfig) throws IOException {
super(stepName,
new NpmConfig(
replaceDevDependencies(
NpmResourceHelper.readUtf8StringFromClasspath(PrettierFormatterStep.class, "/com/diffplug/spotless/npm/prettier-package.json"),
new TreeMap<>(devDependencies)),
NpmResourceHelper.readUtf8StringFromClasspath(PrettierFormatterStep.class,
"/com/diffplug/spotless/npm/common-serve.js",
"/com/diffplug/spotless/npm/prettier-serve.js"),
npmPathResolver.resolveNpmrcContent()),
new NpmFormatterStepLocations(
projectDir,
buildDir,
cacheDir,
npmPathResolver));
this.prettierConfig = requireNonNull(prettierConfig);
}
@Override
@Nonnull
public FormatterFunc createFormatterFunc() {
try {
logger.info("creating formatter function (starting server)");
ServerProcessInfo prettierRestServer = toRuntime().npmRunServer();
PrettierRestService restService = new PrettierRestService(prettierRestServer.getBaseUrl());
String prettierConfigOptions = restService.resolveConfig(this.prettierConfig.getPrettierConfigPath(), this.prettierConfig.getOptions());
return Closeable.ofDangerous(() -> endServer(restService, prettierRestServer), new PrettierFilePathPassingFormatterFunc(prettierConfigOptions, restService));
} catch (IOException e) {
throw ThrowingEx.asRuntime(e);
}
}
private void endServer(PrettierRestService restService, ServerProcessInfo restServer) throws Exception {
logger.info("Closing formatting function (ending server).");
try {
restService.shutdown();
} catch (Throwable t) {
logger.info("Failed to request shutdown of rest service via api. Trying via process.", t);
}
restServer.close();
}
}
private static class PrettierFilePathPassingFormatterFunc implements FormatterFunc.NeedsFile {
private final String prettierConfigOptions;
private final PrettierRestService restService;
public PrettierFilePathPassingFormatterFunc(String prettierConfigOptions, PrettierRestService restService) {
this.prettierConfigOptions = requireNonNull(prettierConfigOptions);
this.restService = requireNonNull(restService);
}
@Override
public String applyWithFile(String unix, File file) throws Exception {
final String prettierConfigOptionsWithFilepath = assertFilepathInConfigOptions(file);
try {
return restService.format(unix, prettierConfigOptionsWithFilepath);
} catch (SimpleRestClient.SimpleRestResponseException e) {
if (e.getStatusCode() != 200 && e.getResponseMessage().contains("No parser could be inferred")) {
throw new PrettierMissingParserException(file, e);
}
throw e;
}
}
private String assertFilepathInConfigOptions(File file) {
// if it is already in the options, we do nothing
if (prettierConfigOptions.contains("\"filepath\"") || prettierConfigOptions.contains("\"parser\"")) {
return prettierConfigOptions;
}
// if the file has no name, we cannot use it
if (file.getName().trim().length() == 0) {
return prettierConfigOptions;
}
// if it is not there, we add it at the beginning of the Options
final int startOfConfigOption = prettierConfigOptions.indexOf('{');
final boolean hasAnyConfigOption = prettierConfigOptions.indexOf(':', startOfConfigOption + 1) != -1;
final String filePathOption = "\"filepath\":\"" + file.getName() + "\"";
return "{" + filePathOption + (hasAnyConfigOption ? "," : "") + prettierConfigOptions.substring(startOfConfigOption + 1);
}
}
}