Skip to content

Commit

Permalink
Merge pull request #91 from philippart-s/write-next-version-npm
Browse files Browse the repository at this point in the history
feat: Write version to a package.json
  • Loading branch information
ADI10HERO authored Aug 22, 2021
2 parents 8f7f01c + cf5d1fe commit 1746d34
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,48 @@ The table below resume the combined use of these options and the result:
| 0.1.0-alpha | - | X | - | beta | - | - | **0.2.0-beta** |
| 0.1.0-alpha | - | - | X | beta | - | - | **0.1.1-beta** |

### Write next version in the configuration file (pom.xml, package.json)
The optional parameter `writeVersion` allow writing back to the file the next calculated version.

**:warning: For some configurations files, the CLI is needed (maven fo example). :warning:**

The supported configurations files :
- pom.xml : need the Maven CLI in the path,
- package.json : need the Npm CLI in the path.

Example of use :
With a project with a package.json as follows :
```json
{
"name": "conventional-commits-plugin-example-npm",
"version": "1.0.0",
"description": "Npm example project"
}
```
The following pipeline with a commit with a commit message like _feat: my cool feature_:
```groovy
pipeline {
agent any
environment {
NEXT_VERSION = nextVersion(writeVersion: true)
}
stages {
stage('Hello') {
steps {
echo "next version = ${NEXT_VERSION}"
}
}
}
}
```
Will update the _package.json_ as follow :
```json
{
"name": "conventional-commits-plugin-example-npm",
"version": "1.1.0",
"description": "Npm example project"
}
```

## Issues

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -48,6 +50,21 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper) th
return Version.valueOf((String) map.get("version"));
}

/**
* Write back to the <code>package.json</code> file the next version.<br>
* Use the npm command <code>version</code>, see : https://docs.npmjs.com/cli/v6/commands/npm-version.
*
* @param directory The directory where write the file.
* @param nextVersion The next version to use.
* @param processHelper The helper to run the command.
* @throws IOException If errors occurs when write the file
* @throws InterruptedException It errors occurs with the npm command.
*/
@Override
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper) {}
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
throws IOException, InterruptedException {
List<String> command =
Arrays.asList("npm", "version", nextVersion.toString());
processHelper.runProcessBuilder(directory, command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.jenkins.plugins.conventionalcommits.utils;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class NpmProjectTypeTest {
@Rule
public TemporaryFolder rootFolder = new TemporaryFolder();

@Mock
private ProcessHelper mockProcessHelper;

/**
* Helper to create a package.json file with a version.
*
* @param npmDir The directory where create the file
* @param version The version to set
* @throws Exception If errors occurs when creation the file
*/
private void createPackageJson(File npmDir, String version) throws Exception {
Files.deleteIfExists(Paths.get(npmDir.getPath() + File.separator + "package.json"));
File packageJson = rootFolder.newFile(npmDir.getName() + File.separator + "package.json");
String packageJsonContent =
"{\n" +
"\"name\": \"conventional-commits-plugin-example-npm\",\n" +
"\"version\": " + version + ",\n" +
"\"description\": \"Npm example project\"\n" +
"}";
FileWriter packageWriter = new FileWriter(packageJson);
packageWriter.write(packageJsonContent);
packageWriter.close();

}

@Test
public void shouldWriteVersionBack() throws Exception {
// Set npm project
File npmDir = rootFolder.newFolder("SampleNpmProject");
createPackageJson(npmDir, "1.0.0");

// Set mock for npm version command
when(mockProcessHelper.runProcessBuilder(any(), any()))
.then(invocationOnMock -> {
createPackageJson(npmDir, "1.1.0");
return "1.1.0";
}
);
NpmProjectType npmProjectType = new NpmProjectType();

npmProjectType.writeVersion(npmDir, Version.valueOf("1.1.0"), mockProcessHelper);

verify(mockProcessHelper).runProcessBuilder(any(File.class),
eq(Arrays.asList("npm", "version", "1.1.0")));

assertThat(new String(
Files.readAllBytes(Paths.get(npmDir.getPath() + File.separator + "package.json"))),
containsString("1.1.0"));
}

@Test(expected = IOException.class)
public void shouldThrowIOException() throws Exception {
// Set foo project
File fooDir = rootFolder.newFolder("SampleFooProject");

// Set mock for throw IOException (npm not installed)
when(mockProcessHelper.runProcessBuilder(any(), any())).thenThrow(
new IOException("Cannot run program \"npm\""));
NpmProjectType npmProjectType = new NpmProjectType();

npmProjectType.writeVersion(fooDir, Version.valueOf("1.1.0"), mockProcessHelper);
}
}

0 comments on commit 1746d34

Please sign in to comment.