Skip to content

Commit dabb916

Browse files
edgardmessiasJohnstonCode
authored andcommitted
fix: Improved svn detection (close #389) (#391)
1 parent 98bce43 commit dabb916

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

package-lock.json

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"is-utf8": "^0.2.1",
5252
"jschardet": "^1.6.0",
5353
"minimatch": "^3.0.4",
54+
"semver": "^5.6.0",
5455
"tmp": "0.0.33",
5556
"xml2js": "^0.4.19"
5657
},
@@ -60,6 +61,7 @@
6061
"@types/glob": "^7.1.0",
6162
"@types/mocha": "^5.2.5",
6263
"@types/node": "^10.11.7",
64+
"@types/semver": "^5.5.0",
6365
"@types/tmp": "0.0.33",
6466
"@types/xml2js": "^0.4.3",
6567
"commitizen": "^3.0.2",

src/svnFinder.ts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import * as cp from "child_process";
22
import * as path from "path";
3+
import * as semver from "semver";
34
import { cpErrorHandler } from "./svn";
45

56
export interface ISvn {
67
path: string;
78
version: string;
89
}
910

10-
export function parseVersion(raw: string): string {
11-
const match = raw.match(/(\d+\.\d+\.\d+ \(r\d+\))/);
12-
13-
if (match && match[0]) {
14-
return match[0];
15-
}
16-
return raw.split(/[\r\n]+/)[0];
17-
}
18-
1911
export class SvnFinder {
2012
public findSvn(hint?: string): Promise<ISvn> {
2113
const first = hint
@@ -33,7 +25,7 @@ export class SvnFinder {
3325
return this.findSpecificSvn("svn");
3426
}
3527
})
36-
.then(svn => this.checkSvnCommand(svn))
28+
.then(svn => this.checkSvnVersion(svn))
3729
.then(null, () =>
3830
Promise.reject(new Error("Svn installation not found."))
3931
);
@@ -69,12 +61,12 @@ export class SvnFinder {
6961

7062
function getVersion(path: string) {
7163
// make sure svn executes
72-
cp.exec("svn --version", (err, stdout) => {
64+
cp.exec("svn --version --quiet", (err, stdout) => {
7365
if (err) {
7466
return e("svn not found");
7567
}
7668

77-
return c({ path, version: parseVersion(stdout.trim()) });
69+
return c({ path, version: stdout.trim() });
7870
});
7971
}
8072

@@ -100,7 +92,7 @@ export class SvnFinder {
10092
public findSpecificSvn(path: string): Promise<ISvn> {
10193
return new Promise<ISvn>((c, e) => {
10294
const buffers: Buffer[] = [];
103-
const child = cp.spawn(path, ["--version"]);
95+
const child = cp.spawn(path, ["--version", "--quiet"]);
10496
child.stdout.on("data", (b: Buffer) => buffers.push(b));
10597
child.on("error", cpErrorHandler(e));
10698
child.on(
@@ -110,29 +102,23 @@ export class SvnFinder {
110102
? e(new Error("Not found"))
111103
: c({
112104
path,
113-
version: parseVersion(
114-
Buffer.concat(buffers)
115-
.toString("utf8")
116-
.trim()
117-
)
105+
version: Buffer.concat(buffers)
106+
.toString("utf8")
107+
.trim()
118108
})
119109
);
120110
});
121111
}
122112

123-
public checkSvnCommand(svn: ISvn): Promise<ISvn> {
113+
public checkSvnVersion(svn: ISvn): Promise<ISvn> {
124114
return new Promise<ISvn>((c, e) => {
125-
const buffers: Buffer[] = [];
126-
const child = cp.spawn(svn.path, ["help", "checkout"]);
127-
child.stdout.on("data", (b: Buffer) => buffers.push(b));
128-
child.on("error", cpErrorHandler(e));
129-
child.on(
130-
"close",
131-
code =>
132-
code || Buffer.concat(buffers).toString("utf8").length < 100
133-
? e(new Error("Not found"))
134-
: c(svn)
135-
);
115+
if (!semver.valid(svn.version)) {
116+
e(new Error("Invalid svn version"));
117+
} else if (!semver.gte(svn.version, "1.6.0")) {
118+
e(new Error("Required svn version must be >= 1.6"));
119+
} else {
120+
c(svn);
121+
}
136122
});
137123
}
138124
}

0 commit comments

Comments
 (0)