Skip to content

Commit 1746d34

Browse files
authored
Merge pull request #91 from philippart-s/write-next-version-npm
feat: Write version to a package.json
2 parents 8f7f01c + cf5d1fe commit 1746d34

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,48 @@ The table below resume the combined use of these options and the result:
111111
| 0.1.0-alpha | - | X | - | beta | - | - | **0.2.0-beta** |
112112
| 0.1.0-alpha | - | - | X | beta | - | - | **0.1.1-beta** |
113113

114+
### Write next version in the configuration file (pom.xml, package.json)
115+
The optional parameter `writeVersion` allow writing back to the file the next calculated version.
116+
117+
**:warning: For some configurations files, the CLI is needed (maven fo example). :warning:**
118+
119+
The supported configurations files :
120+
- pom.xml : need the Maven CLI in the path,
121+
- package.json : need the Npm CLI in the path.
122+
123+
Example of use :
124+
With a project with a package.json as follows :
125+
```json
126+
{
127+
"name": "conventional-commits-plugin-example-npm",
128+
"version": "1.0.0",
129+
"description": "Npm example project"
130+
}
131+
```
132+
The following pipeline with a commit with a commit message like _feat: my cool feature_:
133+
```groovy
134+
pipeline {
135+
agent any
136+
environment {
137+
NEXT_VERSION = nextVersion(writeVersion: true)
138+
}
139+
stages {
140+
stage('Hello') {
141+
steps {
142+
echo "next version = ${NEXT_VERSION}"
143+
}
144+
}
145+
}
146+
}
147+
```
148+
Will update the _package.json_ as follow :
149+
```json
150+
{
151+
"name": "conventional-commits-plugin-example-npm",
152+
"version": "1.1.0",
153+
"description": "Npm example project"
154+
}
155+
```
114156

115157
## Issues
116158

src/main/java/io/jenkins/plugins/conventionalcommits/utils/NpmProjectType.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.io.File;
77
import java.io.IOException;
88
import java.nio.file.Paths;
9+
import java.util.Arrays;
10+
import java.util.List;
911
import java.util.Map;
1012
import java.util.Objects;
1113

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

53+
/**
54+
* Write back to the <code>package.json</code> file the next version.<br>
55+
* Use the npm command <code>version</code>, see : https://docs.npmjs.com/cli/v6/commands/npm-version.
56+
*
57+
* @param directory The directory where write the file.
58+
* @param nextVersion The next version to use.
59+
* @param processHelper The helper to run the command.
60+
* @throws IOException If errors occurs when write the file
61+
* @throws InterruptedException It errors occurs with the npm command.
62+
*/
5163
@Override
52-
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper) {}
64+
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
65+
throws IOException, InterruptedException {
66+
List<String> command =
67+
Arrays.asList("npm", "version", nextVersion.toString());
68+
processHelper.runProcessBuilder(directory, command);
69+
}
5370
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package io.jenkins.plugins.conventionalcommits.utils;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.core.StringContains.containsString;
5+
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.ArgumentMatchers.eq;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.when;
9+
10+
import com.github.zafarkhaja.semver.Version;
11+
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
12+
import java.io.File;
13+
import java.io.FileWriter;
14+
import java.io.IOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Paths;
17+
import java.util.Arrays;
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
import org.junit.rules.TemporaryFolder;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.Mock;
23+
import org.mockito.junit.MockitoJUnitRunner;
24+
25+
@RunWith(MockitoJUnitRunner.class)
26+
public class NpmProjectTypeTest {
27+
@Rule
28+
public TemporaryFolder rootFolder = new TemporaryFolder();
29+
30+
@Mock
31+
private ProcessHelper mockProcessHelper;
32+
33+
/**
34+
* Helper to create a package.json file with a version.
35+
*
36+
* @param npmDir The directory where create the file
37+
* @param version The version to set
38+
* @throws Exception If errors occurs when creation the file
39+
*/
40+
private void createPackageJson(File npmDir, String version) throws Exception {
41+
Files.deleteIfExists(Paths.get(npmDir.getPath() + File.separator + "package.json"));
42+
File packageJson = rootFolder.newFile(npmDir.getName() + File.separator + "package.json");
43+
String packageJsonContent =
44+
"{\n" +
45+
"\"name\": \"conventional-commits-plugin-example-npm\",\n" +
46+
"\"version\": " + version + ",\n" +
47+
"\"description\": \"Npm example project\"\n" +
48+
"}";
49+
FileWriter packageWriter = new FileWriter(packageJson);
50+
packageWriter.write(packageJsonContent);
51+
packageWriter.close();
52+
53+
}
54+
55+
@Test
56+
public void shouldWriteVersionBack() throws Exception {
57+
// Set npm project
58+
File npmDir = rootFolder.newFolder("SampleNpmProject");
59+
createPackageJson(npmDir, "1.0.0");
60+
61+
// Set mock for npm version command
62+
when(mockProcessHelper.runProcessBuilder(any(), any()))
63+
.then(invocationOnMock -> {
64+
createPackageJson(npmDir, "1.1.0");
65+
return "1.1.0";
66+
}
67+
);
68+
NpmProjectType npmProjectType = new NpmProjectType();
69+
70+
npmProjectType.writeVersion(npmDir, Version.valueOf("1.1.0"), mockProcessHelper);
71+
72+
verify(mockProcessHelper).runProcessBuilder(any(File.class),
73+
eq(Arrays.asList("npm", "version", "1.1.0")));
74+
75+
assertThat(new String(
76+
Files.readAllBytes(Paths.get(npmDir.getPath() + File.separator + "package.json"))),
77+
containsString("1.1.0"));
78+
}
79+
80+
@Test(expected = IOException.class)
81+
public void shouldThrowIOException() throws Exception {
82+
// Set foo project
83+
File fooDir = rootFolder.newFolder("SampleFooProject");
84+
85+
// Set mock for throw IOException (npm not installed)
86+
when(mockProcessHelper.runProcessBuilder(any(), any())).thenThrow(
87+
new IOException("Cannot run program \"npm\""));
88+
NpmProjectType npmProjectType = new NpmProjectType();
89+
90+
npmProjectType.writeVersion(fooDir, Version.valueOf("1.1.0"), mockProcessHelper);
91+
}
92+
}

0 commit comments

Comments
 (0)