Skip to content

Commit 6f0dd0b

Browse files
committed
1480: add npmInstallCache to api
1 parent e97764f commit 6f0dd0b

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@ public abstract static class NpmStepConfig<T extends NpmStepConfig<?>> {
528528
@Nullable
529529
protected Object nodeFile;
530530

531+
@Nullable
532+
protected Object npmInstallCache;
533+
531534
@Nullable
532535
protected Object npmrcFile;
533536

@@ -560,6 +563,18 @@ public T npmrc(final Object npmrcFile) {
560563
return (T) this;
561564
}
562565

566+
public T npmInstallCache(final Object npmInstallCache) {
567+
this.npmInstallCache = npmInstallCache;
568+
replaceStep();
569+
return (T) this;
570+
}
571+
572+
public T npmInstallCache() {
573+
this.npmInstallCache = new File(project.getBuildDir(), "spotless-npm-install-cache");
574+
replaceStep();
575+
return (T) this;
576+
}
577+
563578
File npmFileOrNull() {
564579
return fileOrNull(npmFile);
565580
}
@@ -572,6 +587,10 @@ File npmrcFileOrNull() {
572587
return fileOrNull(npmrcFile);
573588
}
574589

590+
File npmModulesCacheOrNull() {
591+
return fileOrNull(npmInstallCache);
592+
}
593+
575594
private File fileOrNull(Object npmFile) {
576595
return npmFile != null ? project.file(npmFile) : null;
577596
}
@@ -619,6 +638,7 @@ protected FormatterStep createStep() {
619638
provisioner(),
620639
project.getProjectDir(),
621640
project.getBuildDir(),
641+
npmModulesCacheOrNull(),
622642
new NpmPathResolver(npmFileOrNull(), nodeFileOrNull(), npmrcFileOrNull(), Arrays.asList(project.getProjectDir(), project.getRootDir())),
623643
new com.diffplug.spotless.npm.PrettierConfig(
624644
this.prettierConfigFile != null ? project.file(this.prettierConfigFile) : null,

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavascriptExtension.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public FormatterStep createStep() {
108108
provisioner(),
109109
project.getProjectDir(),
110110
project.getBuildDir(),
111+
npmModulesCacheOrNull(),
111112
new NpmPathResolver(npmFileOrNull(), nodeFileOrNull(), npmrcFileOrNull(), Arrays.asList(project.getProjectDir(), project.getRootDir())),
112113
eslintConfig());
113114
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/TypescriptExtension.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public FormatterStep createStep() {
117117
provisioner(),
118118
project.getProjectDir(),
119119
project.getBuildDir(),
120+
npmModulesCacheOrNull(),
120121
new NpmPathResolver(npmFileOrNull(), nodeFileOrNull(), npmrcFileOrNull(), Arrays.asList(project.getProjectDir(), project.getRootDir())),
121122
typedConfigFile(),
122123
config);
@@ -213,6 +214,7 @@ public FormatterStep createStep() {
213214
provisioner(),
214215
project.getProjectDir(),
215216
project.getBuildDir(),
217+
npmModulesCacheOrNull(),
216218
new NpmPathResolver(npmFileOrNull(), nodeFileOrNull(), npmrcFileOrNull(), Arrays.asList(project.getProjectDir(), project.getRootDir())),
217219
eslintConfig());
218220
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Prettier.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
9595
// create the format step
9696
File baseDir = baseDir(stepConfig);
9797
File buildDir = buildDir(stepConfig);
98+
File cacheDir = cacheDir(stepConfig);
9899
PrettierConfig prettierConfig = new PrettierConfig(configFileHandler, configInline);
99100
NpmPathResolver npmPathResolver = npmPathResolver(stepConfig);
100-
return PrettierFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, npmPathResolver, prettierConfig);
101+
return PrettierFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, cacheDir, npmPathResolver, prettierConfig);
101102
}
102103

103104
private static IllegalArgumentException onlyOneConfig() {

plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/AbstractEslint.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
6767

6868
File buildDir = buildDir(stepConfig);
6969
File baseDir = baseDir(stepConfig);
70+
File cacheDir = cacheDir(stepConfig);
7071
NpmPathResolver npmPathResolver = npmPathResolver(stepConfig);
71-
return EslintFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, npmPathResolver, eslintConfig(stepConfig));
72+
return EslintFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, cacheDir, npmPathResolver, eslintConfig(stepConfig));
7273
}
7374

7475
private static IllegalArgumentException onlyOneConfig() {

plugin-maven/src/main/java/com/diffplug/spotless/maven/npm/AbstractNpmFormatterStepFactory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.AbstractMap;
2020
import java.util.Arrays;
2121
import java.util.Collections;
22+
import java.util.Locale;
2223
import java.util.Map;
2324
import java.util.Objects;
2425
import java.util.Properties;
@@ -41,6 +42,9 @@ public abstract class AbstractNpmFormatterStepFactory implements FormatterStepFa
4142
@Parameter
4243
private String npmrc;
4344

45+
@Parameter
46+
private String npmInstallCache;
47+
4448
protected File npm(FormatterStepConfig stepConfig) {
4549
File npm = npmExecutable != null ? stepConfig.getFileLocator().locateFile(npmExecutable) : null;
4650
return npm;
@@ -60,6 +64,16 @@ protected File buildDir(FormatterStepConfig stepConfig) {
6064
return stepConfig.getFileLocator().getBuildDir();
6165
}
6266

67+
protected File cacheDir(FormatterStepConfig stepConfig) {
68+
if (this.npmInstallCache == null) {
69+
return null;
70+
}
71+
if ("true".equals(this.npmInstallCache.toLowerCase(Locale.ROOT))) {
72+
return new File(buildDir(stepConfig), "spotless-npm-install-cache");
73+
}
74+
return stepConfig.getFileLocator().locateFile(this.npmInstallCache);
75+
}
76+
6377
protected File baseDir(FormatterStepConfig stepConfig) {
6478
return stepConfig.getFileLocator().getBaseDir();
6579
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/typescript/Tsfmt.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
111111

112112
File buildDir = buildDir(stepConfig);
113113
File baseDir = baseDir(stepConfig);
114+
File cacheDir = cacheDir(stepConfig);
114115
NpmPathResolver npmPathResolver = npmPathResolver(stepConfig);
115-
return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, npmPathResolver, configFile, configInline);
116+
return TsFmtFormatterStep.create(devDependencies, stepConfig.getProvisioner(), baseDir, buildDir, cacheDir, npmPathResolver, configFile, configInline);
116117
}
117118

118119
private static IllegalArgumentException onlyOneConfig() {

0 commit comments

Comments
 (0)