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
164 lines (137 loc) · 6.06 KB
/
Copy pathPrettierFormatterStep.java
File metadata and controls
164 lines (137 loc) · 6.06 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
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2016 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.Arrays.asList;
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 javax.annotation.Nullable;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ThrowingEx;
public class PrettierFormatterStep {
public static final String NAME = "prettier-format";
public static final Map<String, String> defaultDevDependencies() {
return defaultDevDependenciesWithPrettier("1.16.4");
}
public static final Map<String, String> defaultDevDependenciesWithPrettier(String version) {
return Collections.singletonMap("prettier", version);
}
@Deprecated
public static FormatterStep create(Provisioner provisioner, File buildDir, @Nullable File npm, PrettierConfig prettierConfig) {
return create(defaultDevDependencies(), provisioner, buildDir, npm, prettierConfig);
}
public static FormatterStep create(Map<String, String> devDependencies, Provisioner provisioner, File buildDir, @Nullable File npm, PrettierConfig prettierConfig) {
requireNonNull(devDependencies);
requireNonNull(provisioner);
requireNonNull(buildDir);
return FormatterStep.createLazy(NAME,
() -> new State(NAME, devDependencies, provisioner, buildDir, npm, prettierConfig),
State::createFormatterFunc);
}
public static class State extends NpmFormatterStepStateBase implements Serializable {
private static final long serialVersionUID = -3811104513825329168L;
private final PrettierConfig prettierConfig;
State(String stepName, Map<String, String> devDependencies, Provisioner provisioner, File buildDir, @Nullable File npm, PrettierConfig prettierConfig) throws IOException {
super(stepName,
provisioner,
new NpmConfig(
replaceDevDependencies(
readFileFromClasspath(PrettierFormatterStep.class, "/com/diffplug/spotless/npm/prettier-package.json"),
new TreeMap<>(devDependencies)),
"prettier"),
buildDir,
npm);
this.prettierConfig = requireNonNull(prettierConfig);
}
@Override
@Nonnull
public FormatterFunc createFormatterFunc() {
try {
final NodeJSWrapper nodeJSWrapper = nodeJSWrapper();
final V8ObjectWrapper prettier = nodeJSWrapper.require(nodeModulePath());
@SuppressWarnings("unchecked")
final Map<String, Object>[] resolvedPrettierOptions = (Map<String, Object>[]) new Map[1];
if (this.prettierConfig.getPrettierConfigPath() != null) {
final Exception[] toThrow = new Exception[1];
try (
V8FunctionWrapper resolveConfigCallback = createResolveConfigFunction(nodeJSWrapper, resolvedPrettierOptions, toThrow);
V8ObjectWrapper resolveConfigOption = createResolveConfigOptionObj(nodeJSWrapper);
V8ArrayWrapper resolveConfigParams = createResolveConfigParamsArray(nodeJSWrapper, resolveConfigOption);
V8ObjectWrapper promise = prettier.executeObjectFunction("resolveConfig", resolveConfigParams);
V8ArrayWrapper callbacks = nodeJSWrapper.createNewArray(resolveConfigCallback);) {
promise.executeVoidFunction("then", callbacks);
executeResolution(nodeJSWrapper, resolvedPrettierOptions, toThrow);
}
} else {
resolvedPrettierOptions[0] = this.prettierConfig.getOptions();
}
final V8ObjectWrapper prettierConfig = nodeJSWrapper.createNewObject(resolvedPrettierOptions[0]);
return FormatterFunc.Closeable.of(() -> {
asList(prettierConfig, prettier, nodeJSWrapper).forEach(ReflectiveObjectWrapper::release);
}, input -> {
try (V8ArrayWrapper formatParams = nodeJSWrapper.createNewArray(input, prettierConfig)) {
String result = prettier.executeStringFunction("format", formatParams);
return result;
}
});
} catch (Exception e) {
throw ThrowingEx.asRuntime(e);
}
}
private V8FunctionWrapper createResolveConfigFunction(NodeJSWrapper nodeJSWrapper, Map<String, Object>[] outputOptions, Exception[] toThrow) {
return nodeJSWrapper.createNewFunction((receiver, parameters) -> {
try {
try (final V8ObjectWrapper configOptions = parameters.getObject(0)) {
if (configOptions == null) {
toThrow[0] = new IllegalArgumentException("Cannot find or read config file " + this.prettierConfig.getPrettierConfigPath());
} else {
Map<String, Object> resolvedOptions = new TreeMap<>(V8ObjectUtilsWrapper.toMap(configOptions));
resolvedOptions.putAll(this.prettierConfig.getOptions());
outputOptions[0] = resolvedOptions;
}
}
} catch (Exception e) {
toThrow[0] = e;
}
return receiver;
});
}
private V8ObjectWrapper createResolveConfigOptionObj(NodeJSWrapper nodeJSWrapper) {
return nodeJSWrapper.createNewObject()
.add("config", this.prettierConfig.getPrettierConfigPath().getAbsolutePath());
}
private V8ArrayWrapper createResolveConfigParamsArray(NodeJSWrapper nodeJSWrapper, V8ObjectWrapper resolveConfigOption) {
return nodeJSWrapper.createNewArray()
.pushNull()
.push(resolveConfigOption);
}
private void executeResolution(NodeJSWrapper nodeJSWrapper, Map<String, Object>[] resolvedPrettierOptions, Exception[] toThrow) {
while (resolvedPrettierOptions[0] == null && toThrow[0] == null) {
nodeJSWrapper.handleMessage();
}
if (toThrow[0] != null) {
throw ThrowingEx.asRuntime(toThrow[0]);
}
}
}
}