-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced support for package git dependency urls (coauthored with @…
- Loading branch information
1 parent
86db2fc
commit 11b4445
Showing
15 changed files
with
569 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { escapeRegExp } from "lodash"; | ||
import hostedGitInfo from "hosted-git-info"; | ||
|
||
export default class GitVersionParser { | ||
constructor(versionPrefix = "v") { | ||
this._gitUrlPattern = new RegExp(`(.+?#${escapeRegExp(versionPrefix)})(.+)$`); | ||
} | ||
|
||
parseVersion(version) { | ||
const gitInfo = hostedGitInfo.fromUrl(version); | ||
let targetMatches; | ||
|
||
if (gitInfo && gitInfo.committish) { | ||
targetMatches = this._gitUrlPattern.exec(version); | ||
} | ||
|
||
return { | ||
prefix: targetMatches ? targetMatches[1] : null, | ||
version: targetMatches ? targetMatches[2] : version | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export default class VersionSerializer { | ||
constructor({ monorepoDependencies, versionParser }) { | ||
this._monorepoDependencies = monorepoDependencies; | ||
this._versionParser = versionParser; | ||
this._dependenciesKeys = ["dependencies", "devDependencies", "peerDependencies"]; | ||
this._strippedPrefixes = new Map(); | ||
} | ||
|
||
serialize(pkg) { | ||
this._dependenciesKeys.forEach((key) => { | ||
this._prependPrefix(pkg[key] || {}); | ||
}); | ||
|
||
return pkg; | ||
} | ||
|
||
deserialize(pkg) { | ||
this._dependenciesKeys.forEach((key) => { | ||
this._stripPrefix(pkg[key] || {}); | ||
}); | ||
|
||
return pkg; | ||
} | ||
|
||
_prependPrefix(dependencies) { | ||
this._strippedPrefixes.forEach((prefix, name) => { | ||
const version = dependencies[name]; | ||
if (version) { | ||
dependencies[name] = `${prefix}${version}`; | ||
} | ||
}); | ||
} | ||
|
||
_stripPrefix(dependencies) { | ||
Object.keys(dependencies).forEach((name) => { | ||
if (this._monorepoDependencies.includes(name)) { | ||
const result = this._versionParser.parseVersion(dependencies[name]); | ||
|
||
if (result.prefix) { | ||
dependencies[name] = result.version; | ||
this._strippedPrefixes.set(name, result.prefix); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import log from "npmlog"; | ||
|
||
// file under test | ||
import GitVersionParser from "../src/GitVersionParser"; | ||
|
||
// silence logs | ||
log.level = "silent"; | ||
|
||
describe("GitVersionParser", () => { | ||
|
||
describe("parseVersion - without prefix", () => { | ||
let parser; | ||
|
||
beforeEach(() => { | ||
parser = new GitVersionParser(""); | ||
}); | ||
|
||
it("should work for semver version", () => { | ||
expect(parser.parseVersion("0.0.2")).toEqual({ | ||
prefix: null, | ||
version: "0.0.2" | ||
}); | ||
|
||
expect(parser.parseVersion("~0.0.2")).toEqual({ | ||
prefix: null, | ||
version: "~0.0.2" | ||
}); | ||
}); | ||
|
||
it("should work for git url", () => { | ||
expect(parser.parseVersion("github:user-foo/project-foo#v0.0.1")).toEqual({ | ||
prefix: "github:user-foo/project-foo#", | ||
version: "v0.0.1" | ||
}); | ||
|
||
expect(parser.parseVersion("git@github.com:user-foo/project-foo#0.0.5")).toEqual({ | ||
prefix: "git@github.com:user-foo/project-foo#", | ||
version: "0.0.5" | ||
}); | ||
}); | ||
}); | ||
|
||
describe("parseVersion - with version prefix", () => { | ||
let parser; | ||
|
||
beforeEach(() => { | ||
parser = new GitVersionParser("v"); | ||
}); | ||
|
||
it("should work for semver version", () => { | ||
expect(parser.parseVersion("0.0.2")).toEqual({ | ||
prefix: null, | ||
version: "0.0.2" | ||
}); | ||
|
||
expect(parser.parseVersion("~0.0.2")).toEqual({ | ||
prefix: null, | ||
version: "~0.0.2" | ||
}); | ||
}); | ||
|
||
it("should work for git url", () => { | ||
expect(parser.parseVersion("github:user-foo/project-foo#v0.0.1")).toEqual({ | ||
prefix: "github:user-foo/project-foo#v", | ||
version: "0.0.1" | ||
}); | ||
|
||
expect(parser.parseVersion("git@github.com:user-foo/project-foo#0.0.5")).toEqual({ | ||
prefix: null, | ||
version: "git@github.com:user-foo/project-foo#0.0.5" | ||
}); | ||
|
||
expect(parser.parseVersion("git@github.com:user-foo/project-foo#v0.0.5")).toEqual({ | ||
prefix: "git@github.com:user-foo/project-foo#v", | ||
version: "0.0.5" | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.