4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

すべての依存モジュールを開発版(リポジトリ)からインストールするように変更する

Last updated at Posted at 2015-05-01

書き捨てです

do.js
var npm = require("npm")
var fs = require('fs')

getPackages(require('./package'), function(err, package){
    fs.writeFileSync('./package_new.json', JSON.stringify(package, null, '  '), {flag: 'w'})
})

function getPackages(package, cb){
    Object.keys(package["dependencies"]).map(function(p){
        fetchRepositoryURL(p, function (err, url) {
            package["dependencies"][p] = url
            cb(err, package)
        })
    })
}

function fetchRepositoryURL(name, cb) {
    npm.load({}, function (er, api) {
        api.commands.view([name], function(err, result){
            var p = result[Object.keys(result)[0]]
            if (p.repository) cb(null, p.repository.url)
        })
    })
}
node do.js
mv package_new.json package.json

こうなってる

  "dependencies": {
    "babel": "https://github.com/babel/babel",
    "body-parser": "https://github.com/expressjs/body-parser",
    "cookie-parser": "https://github.com/expressjs/cookie-parser",
    "csurf": "https://github.com/expressjs/csurf",
    "debug": "git://github.com/visionmedia/debug.git",
    "express": "https://github.com/strongloop/express",
    "express-state": "git://github.com/yahoo/express-state.git",
    "flux-router-component": "git://github.com/yahoo/flux-router-component.git",
    "fluxible": "https://github.com/yahoo/fluxible",
    "fluxible-plugin-fetchr": "git://github.com/yahoo/fluxible-plugin-fetchr.git",
    "fluxible-plugin-routr": "^0.3.0",
    "react": "https://github.com/facebook/react",
    "serialize-javascript": "https://github.com/yahoo/serialize-javascript.git",
    "serve-favicon": "https://github.com/expressjs/serve-favicon"
  },

npm API非依存版

ローカルに既にnode_modules/ がある前提のスクリプトも書いた。標準モジュールしか使ってないので ~/bin/rewrite_repo_urls.js とかで呼び出せる。

rewrite_repo_urls.js
#!/usr/bin/env node

var fs = require('fs');
var exec = require('child_process').execSync;

var myPackage = require(process.cwd() + '/package');

var paths = exec('find ./node_modules -type f -depth 2 -name package.json').toString().split("\n");
paths.map(function(path){
    if(path.length === 0){
        return;
    }

    var name = /node_modules\/(.+)\/package.json/.exec(path)[1];
    var package = require(process.cwd() + '/' + path);
    if(myPackage['dependencies'][name] && package.repository){
        myPackage['dependencies'][name] = package.repository.url;
    }
});

fs.writeFile('./package.json', JSON.stringify(myPackage,  null, '  '), {flag: 'w'});
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?