Nick
Nick

Reputation: 339

node.js require modules at different git tag points

I am developing a node.js express app along with several private node_modules.

I would like to be to expose my users to different versions of these modules by using git tags. Is this a sensible idea, and is there an equally sensible way of doing this by auto-detecting the tags, and requiring the modules at each of those git tag references?

Perhaps something like 'git-helpers' might exist? (code is coffeescript)

git = require('git-helpers?').dir('./node_modules/module/')
tags = git.getGitTags()

modules = []

for tag in tags
  git.requireAtGitTag tag, (err, res) ->
    modules.push {version: tag, module: res}

then modules variable would look something like this

[
 { version: "0.8", module: required_module_here_at_tag},
 { version: "0.9", module: required_module_here_at_tag},
 { version: "1.0", module: required_module_here_at_tag}
]

Edit

I have come up with an idea using a node.js git cli wrapper called Gift. Is this a sensible way of doing it?

gift = require("gift")
repo = gift("./node_modules/moduleA")

modules = []

repo.tags (err, tags) ->
  for tag in tags
    repo.checkout tag.name, (err, a) ->
      modules.push
        version: tag.name
        module: require("./node_modules/moduleA")

Upvotes: 0

Views: 381

Answers (1)

LoG
LoG

Reputation: 1141

the best way to archieve something like this is to use npm: Git URLs as Dependencies

example package.json in your app folder:

{
  "name": "foobar",
  "version": "0.0.1",
  "dependencies": {
    "library": "git://example.com/user/project.git#tag_name"
  }
}

you can then:

var library = require('library')

Upvotes: 1

Related Questions