Skip to content

Commit

Permalink
Merge pull request #466 from pascalgrimaud/npm-add-dependency
Browse files Browse the repository at this point in the history
Package Manager NPM: add method for adding dependency
  • Loading branch information
pascalgrimaud authored Jan 5, 2022
2 parents 0ba5c6d + d77b3e3 commit f91c2ac
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/main/java/tech/jhipster/lite/common/domain/WordUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
public class WordUtils {

public static final int DEFAULT_INDENTATION = 2;
public static final String DQ = "\"";

public static final String OB = "\\{";
public static final String CB = "\\}";

private WordUtils() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class NpmApplicationService {

private final NpmService npmService;

public void addDependency(Project project, String dependency, String version) {
this.npmService.addDependency(project, dependency, version);
}

public NpmApplicationService(NpmService npmService) {
this.npmService = npmService;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
package tech.jhipster.lite.generator.packagemanager.npm.domain;

import static tech.jhipster.lite.common.domain.WordUtils.*;
import static tech.jhipster.lite.generator.project.domain.Constants.PACKAGE_JSON;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.*;

import tech.jhipster.lite.generator.project.domain.CommandRepository;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

public class NpmDomainService implements NpmService {

public static final String DEPENDENCIES = DQ + "dependencies" + DQ;
public static final String DEV_DEPENDENCIES = DQ + "devDependencies" + DQ;
public static final String SCRIPTS = DQ + "scripts" + DQ;

private final CommandRepository commandRepository;
private final ProjectRepository projectRepository;

public NpmDomainService(CommandRepository commandRepository) {
public NpmDomainService(CommandRepository commandRepository, ProjectRepository projectRepository) {
this.commandRepository = commandRepository;
this.projectRepository = projectRepository;
}

@Override
public void addDependency(Project project, String dependency, String version) {
// Need to be implemented
project.addDefaultConfig(PRETTIER_DEFAULT_INDENT);
int indent = (Integer) project.getConfig(PRETTIER_DEFAULT_INDENT).orElse(2);

String needle = DEPENDENCIES + ": " + OB;
String newText = needle + System.lineSeparator() + indent(2, indent) + DQ + dependency + DQ + ": " + DQ + version + DQ;

String devDependenciesNeedle = DEV_DEPENDENCIES + ": " + OB;
if (!projectRepository.containsRegexp(project, "", PACKAGE_JSON, needle)) {
newText =
newText +
System.lineSeparator() +
indent(1, indent) +
CB +
"," +
System.lineSeparator() +
indent(1, indent) +
devDependenciesNeedle;
projectRepository.replaceText(project, "", PACKAGE_JSON, devDependenciesNeedle, newText);
} else if (projectRepository.containsRegexp(project, "", PACKAGE_JSON, needle + CB)) {
projectRepository.replaceText(project, "", PACKAGE_JSON, needle, newText + System.lineSeparator() + indent(1, indent));
} else {
projectRepository.replaceText(project, "", PACKAGE_JSON, needle, newText + ",");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
import tech.jhipster.lite.generator.packagemanager.npm.domain.NpmDomainService;
import tech.jhipster.lite.generator.packagemanager.npm.domain.NpmService;
import tech.jhipster.lite.generator.project.domain.CommandRepository;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

@Configuration
public class NpmBeanConfiguration {

private final CommandRepository commandRepository;
private final ProjectRepository projectRepository;

public NpmBeanConfiguration(CommandRepository commandRepository) {
public NpmBeanConfiguration(CommandRepository commandRepository, ProjectRepository projectRepository) {
this.commandRepository = commandRepository;
this.projectRepository = projectRepository;
}

@Bean
public NpmService npmService() {
return new NpmDomainService(commandRepository);
return new NpmDomainService(commandRepository, projectRepository);
}
}
27 changes: 27 additions & 0 deletions src/test/java/tech/jhipster/lite/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ public static Project tmpProjectWithPackageJson() {
return project;
}

public static Project tmpProjectWithPackageJsonNoDependencies() {
Project project = tmpProject();
copyPackageJsonByName(project, "package-no-dependencies.json");
return project;
}

public static Project tmpProjectWithPackageJsonEmpty() {
Project project = tmpProject();
copyPackageJsonByName(project, "package-empty.json");
return project;
}

public static Project tmpProjectWithPackageJsonComplete() {
Project project = tmpProject();
copyPackageJsonByName(project, "package-complete.json");
return project;
}

public static Project tmpProjectWithBuildGradle() {
Project project = tmpProject();
copyBuildGradle(project);
Expand Down Expand Up @@ -143,6 +161,15 @@ public static void copyPackageJson(Project project) {
}
}

public static void copyPackageJsonByName(Project project, String packageJsonFilename) {
try {
FileUtils.createFolder(project.getFolder());
Files.copy(getPathOf("src/test/resources/generator/command/", packageJsonFilename), getPathOf(project.getFolder(), PACKAGE_JSON));
} catch (IOException e) {
throw new AssertionError(e);
}
}

public static void copySpringBootProperties(Project project) {
try {
FileUtils.createFolder(getPath(project.getFolder(), MAIN_RESOURCES, "config"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static tech.jhipster.lite.TestUtils.*;
import static tech.jhipster.lite.generator.project.domain.Constants.PACKAGE_JSON;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBean;
Expand All @@ -20,6 +22,40 @@ class NpmApplicationServiceIT {
@SpyBean
CommandRepository commandRepository;

@Test
void shouldAddDependencyWhenNoDependencyEntry() {
Project project = tmpProjectWithPackageJsonNoDependencies();
String dependency = "husky";
String version = "7.0.4";

npmApplicationService.addDependency(project, dependency, version);

assertFileContent(project, PACKAGE_JSON, List.of("\"dependencies\": {", "\"husky\": \"7.0.4\"", "},", "\"devDependencies\": {"));
}

@Test
void shouldAddDependencyWhenDependencyEmpty() {
Project project = tmpProjectWithPackageJsonEmpty();
String dependency = "husky";
String version = "7.0.4";

npmApplicationService.addDependency(project, dependency, version);

assertFileContent(project, PACKAGE_JSON, List.of("\"dependencies\": {", "\"husky\": \"7.0.4\""));
assertFileNoContent(project, PACKAGE_JSON, List.of("\"dependencies\": {", "\"husky\": \"7.0.4\","));
}

@Test
void shouldAddDependency() {
Project project = tmpProjectWithPackageJsonComplete();
String dependency = "husky";
String version = "7.0.4";

npmApplicationService.addDependency(project, dependency, version);

assertFileContent(project, PACKAGE_JSON, List.of("\"dependencies\": {", "\"husky\": \"7.0.4\","));
}

@Test
void shouldNpmInstall() {
Project project = tmpProjectWithPackageJson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static tech.jhipster.lite.TestUtils.tmpProject;

Expand All @@ -13,6 +14,7 @@
import tech.jhipster.lite.UnitTest;
import tech.jhipster.lite.generator.project.domain.CommandRepository;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

@UnitTest
@ExtendWith(MockitoExtension.class)
Expand All @@ -21,6 +23,9 @@ class NpmDomainServiceTest {
@Mock
CommandRepository commandRepository;

@Mock
ProjectRepository projectRepository;

@InjectMocks
NpmDomainService npmDomainService;

Expand All @@ -31,6 +36,8 @@ void shouldAddDependency() {
String version = "2.5.1";

assertThatCode(() -> npmDomainService.addDependency(project, dependency, version)).doesNotThrowAnyException();

verify(projectRepository).replaceText(any(Project.class), anyString(), anyString(), anyString(), anyString());
}

@Test
Expand Down
18 changes: 18 additions & 0 deletions src/test/resources/generator/command/package-complete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "jhlite",
"version": "0.0.1",
"scripts": {
"prettier:check": "prettier --check \"{,src/**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java,xml}\"",
"prettier:format": "prettier --write \"{,src/**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java,xml}\""
},
"dependencies": {
"prettier": "2.5.1"
},
"devDependencies": {
"prettier": "2.5.1"
},
"engines": {
"node": ">=14.18.1"
},
"cacheDirectories": ["node_modules"]
}
16 changes: 16 additions & 0 deletions src/test/resources/generator/command/package-empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "jhlite",
"version": "0.0.1",
"scripts": {
"prettier:check": "prettier --check \"{,src/**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java,xml}\"",
"prettier:format": "prettier --write \"{,src/**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java,xml}\""
},
"dependencies": {},
"devDependencies": {
"prettier": "2.5.1"
},
"engines": {
"node": ">=14.18.1"
},
"cacheDirectories": ["node_modules"]
}
15 changes: 15 additions & 0 deletions src/test/resources/generator/command/package-no-dependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "jhlite",
"version": "0.0.1",
"scripts": {
"prettier:check": "prettier --check \"{,src/**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java,xml}\"",
"prettier:format": "prettier --write \"{,src/**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java,xml}\""
},
"devDependencies": {
"prettier": "2.5.1"
},
"engines": {
"node": ">=14.18.1"
},
"cacheDirectories": ["node_modules"]
}

0 comments on commit f91c2ac

Please sign in to comment.