Skip to content

Commit fc06db9

Browse files
committed
refactor: extract base npm options to a mixin
1 parent 8fc8b93 commit fc06db9

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ output =
280280
-->
281281

282282
```
283-
Usage: spotless prettier [-hV] [-C=<cacheDir>] [-n=<explicitNpmExecutable>]
283+
Usage: spotless prettier [-hV] [-C=<npmInstallCacheDir>]
284+
[-n=<explicitNpmExecutable>]
284285
[-N=<explicitNodeExecutable>]
285286
[-P=<prettierConfigPath>] [-R=<explicitNpmrcFile>]
286287
[-A=<additionalNpmrcLocations>]...
@@ -292,8 +293,9 @@ Runs prettier, the opinionated code formatter.
292293
A prettier configuration options.
293294
The format is 'OPTION=VALUE'.
294295
example: 'printWidth=80'
295-
-C, --cache-dir=<cacheDir>
296-
The directory to use for caching prettier.
296+
-C, --npm-install-cache-dir=<npmInstallCacheDir>
297+
The directory to use for caching libraries retrieved by 'npm
298+
install'.
297299
-D, --dev-dependency='PACKAGE=VERSION'
298300
An entry to add to the package.json for running prettier.
299301
The format is 'PACKAGE=VERSION'.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.cli.steps;
17+
18+
import java.nio.file.Path;
19+
import java.util.List;
20+
21+
import picocli.CommandLine;
22+
23+
public class NpmOptions {
24+
@CommandLine.Option(
25+
names = {"--npm-install-cache-dir", "-C"},
26+
description = "The directory to use for caching libraries retrieved by @|YELLOW 'npm install'|@.")
27+
Path npmInstallCacheDir;
28+
29+
@CommandLine.Option(
30+
names = {"--npm-exec", "-n"},
31+
description = "The explicit path to the npm executable.")
32+
Path explicitNpmExecutable;
33+
34+
@CommandLine.Option(
35+
names = {"--node-exec", "-N"},
36+
description = "The explicit path to the node executable.")
37+
Path explicitNodeExecutable;
38+
39+
@CommandLine.Option(
40+
names = {"--npmrc-file", "-R"},
41+
description = "The explicit path to the .npmrc file.")
42+
Path explicitNpmrcFile;
43+
44+
@CommandLine.Option(
45+
names = {"--additional-npmrc-location", "-A"},
46+
description = "Additional locations to search for .npmrc files.")
47+
List<Path> additionalNpmrcLocations;
48+
}

app/src/main/java/com/diffplug/spotless/cli/steps/Prettier.java

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,8 @@ public class Prettier extends SpotlessFormatterStep {
5050
paramLabel = "'PACKAGE=VERSION'")
5151
Map<String, String> devDependencies;
5252

53-
@CommandLine.Option(
54-
names = {"--cache-dir", "-C"},
55-
description = "The directory to use for caching prettier.")
56-
Path cacheDir;
57-
58-
@CommandLine.Option(
59-
names = {"--npm-exec", "-n"},
60-
description = "The explicit path to the npm executable.")
61-
Path explicitNpmExecutable;
62-
63-
@CommandLine.Option(
64-
names = {"--node-exec", "-N"},
65-
description = "The explicit path to the node executable.")
66-
Path explicitNodeExecutable;
67-
68-
@CommandLine.Option(
69-
names = {"--npmrc-file", "-R"},
70-
description = "The explicit path to the .npmrc file.")
71-
Path explicitNpmrcFile;
72-
73-
@CommandLine.Option(
74-
names = {"--additional-npmrc-location", "-A"},
75-
description = "Additional locations to search for .npmrc files.")
76-
List<Path> additionalNpmrcLocations;
53+
@CommandLine.Mixin
54+
NpmOptions npmOptions;
7755

7856
@CommandLine.Option(
7957
names = {"--prettier-config-path", "-P"},
@@ -91,10 +69,10 @@ public class Prettier extends SpotlessFormatterStep {
9169
public List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
9270
FormatterStep prettierFormatterStep = builder(context)
9371
.withDevDependencies(devDependencies())
94-
.withCacheDir(cacheDir)
95-
.withExplicitNpmExecutable(explicitNpmExecutable)
96-
.withExplicitNodeExecutable(explicitNodeExecutable)
97-
.withExplicitNpmrcFile(explicitNpmrcFile)
72+
.withCacheDir(npmOptions.npmInstallCacheDir)
73+
.withExplicitNpmExecutable(npmOptions.explicitNpmExecutable)
74+
.withExplicitNodeExecutable(npmOptions.explicitNodeExecutable)
75+
.withExplicitNpmrcFile(npmOptions.explicitNpmrcFile)
9876
.withAdditionalNpmrcLocations(additionalNpmrcLocations())
9977
.withPrettierConfigOptions(prettierConfigOptions())
10078
.withPrettierConfigPath(prettierConfigPath)
@@ -134,7 +112,7 @@ private Map<String, String> devDependencies() {
134112
}
135113

136114
private List<Path> additionalNpmrcLocations() {
137-
return use(additionalNpmrcLocations).orIfNullGet(Collections::emptyList);
115+
return use(npmOptions.additionalNpmrcLocations).orIfNullGet(Collections::emptyList);
138116
}
139117

140118
private PrettierFormatterStepBuilder builder(@NotNull SpotlessActionContext context) {

0 commit comments

Comments
 (0)