2012-03-11 18 views
13

私はNodejitsuに配備しているアプリを持っています。最近では、npmの問題に悩まされ、依存関係をインストールすることができなかったため、再起動を試みた(そして失敗した)後、数時間にわたってオフラインになってしまいました。これは、私のパッケージ依存関係をすべて私のpackage.jsonにbundledDependenciesと表示して、依存関係を残りのアプリケーションとともにアップロードすることで、これが将来回避される可能性があると言われました。bundledDependenciesリストを自動的に生成する方法はありますか?

"dependencies": { 
    "express": "2.5.8", 
    "mongoose": "2.5.9", 
    "stylus": "0.24.0" 
}, 
"bundledDependencies": [ 
    "express", 
    "mongoose", 
    "stylus" 
] 

は今、DRYを理由に、これは魅力である:それは私がこのような何かを見て、私のpackage.jsonが必要であることを意味します。しかし、さらに悪いのはメンテナンスです。依存関係を追加または削除するたびに、私は2箇所で変更を加えなければなりません。 dependenciesbundledDependenciesを同期させるためのコマンドがありますか?

+0

PING :)これはあなたの質問に答えましたか、それとも解決すべきことがありますか? – wprl

答えて

10

grunt.jsタスクを実装する方法はありますか?これは動作します:

module.exports = function(grunt) { 

    grunt.registerTask('bundle', 'A task that bundles all dependencies.', function() { 
    // "package" is a reserved word so it's abbreviated to "pkg" 
    var pkg = grunt.file.readJSON('./package.json'); 
    // set the bundled dependencies to the keys of the dependencies property 
    pkg.bundledDependencies = Object.keys(pkg.dependencies); 
    // write back to package.json and indent with two spaces 
    grunt.file.write('./package.json', JSON.stringify(pkg, undefined, ' ')); 
    }); 

}; 

grunt.jsという名前のファイルにプロジェクトのルートディレクトリに入れます。 gruntをインストールするには、npm:npm install -g gruntを使用します。次に、grunt bundleを実行してパッケージをバンドルします。

commentorは有用である可能性NPMモジュール述べた:(。私はそれを試していない)https://www.npmjs.com/package/grunt-bundled-dependencies

+0

はあなたの答えをとり、図書館を作った。https://github.com/GuyMograbi/grunt-bundled-dependencies。あなたの答えに追加することを検討してください。 –

0
あなたが読んで bundleDependenciesプロパティを更新し、NPMを経由して、それを実行するための簡単なNode.jsのスクリプトを使用することができます

ライフサイクルフック/スクリプト。

マイフォルダ構造は次のとおりです。

  • scripts/update-bundle-dependencies.js
  • package.json

scripts/update-bundle-dependencies.js

#!/usr/bin/env node 
const fs = require('fs'); 
const path = require('path');  
const pkgPath = path.resolve(__dirname, "../package.json"); 
const pkg = require(pkgPath); 
pkg.bundleDependencies = Object.keys(pkg.dependencies);  
const newPkgContents = JSON.stringify(pkg, null, 2);  
fs.writeFileSync(pkgPath, newPkgContents); 
console.log("Updated bundleDependencies"); 

あなたはnpm(> 4.0.0)の最新バージョンを使用している場合、あなたはを使用することができますまたはprepackスクリプト:https://docs.npmjs.com/misc/scripts

prepublishOnly:パッケージを調製し、梱包される前に公開ONLY NPMに、実行します。 (下記参照)

プレパック:(gitの依存関係をインストールするときに、NPMパックに、NPMは、公開、および )tarボールがパックされる前に実行

package.json

{ 
    "scripts": { 
    "prepack": "npm run update-bundle-dependencies", 
    "update-bundle-dependencies": "node scripts/update-bundle-dependencies" 
    } 
} 

あなたがテストすることができますnpm run update-bundle-dependenciesを実行してください。

希望すると便利です。

関連する問題