2015-04-05 80 views
8

我有2个级别的package.json文件。如何让npm运行<script>委托给child package.json?

例子是在这里:

https://github.com/justin808/react-webpack-rails-tutorial

的原因是,顶层是一个Rails应用程序,并且我把一个目录中称为客户下的所有节点的工具,用它自己的package.json文件。顶级package.json文件是一个方便的以及节点buildpack运行npm install脚本的钩子。

我有一个转发gulp命令的例子。任何方式一般地将顶级package.json找不到的任何东西发送给孩子?

顶级package.json

{ 
    "name": "react-webpack-rails-tutorial", 
    "version": "1.1.1", 
    "description": "Code from the React Webpack tutorial.", 
    "main": "server.js", 
    "engines": { 
    "node": "0.10.32" 
    }, 
    "scripts": { 
    "postinstall": "cd ./client && npm install", 
    "gulp": "cd ./client && npm run gulp" 
    }, 
    "repository": { 
    "type": "git", 
    "url": "https://github.com/justin808/react-webpack-rails-tutorial.git" 
    }, 
    "keywords": [ 
    "react", 
    "tutorial", 
    "comment", 
    "example" 
    ], 
    "author": "justin808", 
    "license": "MIT", 
    "bugs": { 
    "url": "https://github.com/justin808/react-webpack-rails-tutorial/issues" 
    }, 
    "homepage": "https://github.com/justin808/react-webpack-rails-tutorial" 
} 

子目录package.json

{ 
    "name": "react-webpack-rails-tutorial", 
    "version": "1.1.0", 
    "description": "Code from the React Webpack tutorial.", 
    "main": "server.js", 
    "engines": { 
    "node": "0.10.32" 
    }, 
    "repository": { 
    "type": "git", 
    "url": "https://github.com/justin808/react-webpack-rails-tutorial.git" 
    }, 
    "keywords": [ 
    "react", 
    "tutorial", 
    "comment", 
    "example" 
    ], 
    "author": "justin808", 
    "license": "MIT", 
    "bugs": { 
    "url": "https://github.com/justin808/react-webpack-rails-tutorial/issues" 
    }, 
    "homepage": "https://github.com/justin808/react-webpack-rails-tutorial", 
    "dependencies": { 
    "babel-core": "^5.0.8", 
    "babel-loader": "^5.0.0", 
    "body-parser": "^1.12.2", 
    "es5-shim": "^4.1.0", 
    "imports-loader": "^0.6.3", 
    "jquery": "^2.1.3", 
    "loader-utils": "^0.2.6", 
    "marked": "^0.3.3", 
    "react": "^0.13.1", 
    "react-bootstrap": "^0.20.1", 
    "sleep": "^2.0.0", 
    "webpack": "^1.7.3" 
    }, 
    "devDependencies": { 
    "babel-eslint": "^2.0.2", 
    "bootstrap-sass": "^3.3.4", 
    "bootstrap-sass-loader": "^1.0.3", 
    "css-loader": "^0.9.1", 
    "eslint": "^0.18.0", 
    "eslint-plugin-react": "^2.0.2", 
    "expose-loader": "^0.6.0", 
    "express": "^4.12.3", 
    "file-loader": "^0.8.1", 
    "gulp": "^3.8.11", 
    "gulp-eslint": "^0.8.0", 
    "node-sass": "^2.1.1", 
    "react-hot-loader": "^1.2.4", 
    "sass-loader": "^0.6.0", 
    "style-loader": "^0.9.0", 
    "url-loader": "^0.5.5", 
    "webpack-dev-server": "^1.8.0" 
    }, 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1", 
    "start": "node server.js", 
    "gulp": "gulp" 
    } 
} 

回答

0

你可以写,你把gulp -command一个批处理文件。那么你必须检查errorstate。这可能是这样的:

@echo off 

:RUN_GULP 
echo Running Gulp... 
gulp 
goto END 

:END 
if %ERRORLEVEL% neq 0 goto PROCESS_ERROR 
exit 

:PROCESS_ERROR 
cd ./client 
gulp 
exit; 

然后你只需要调用脚本在你的package.json这样的:

"gulp": "call ./path/to/batfile.bat" 

做同样的对我的项目.. ..

编辑:对于所有脚本....你可以创建一个批处理文件,将脚本名称作为参数安静。这个脚本和上面一样,但它应该适用于每个命令。

注意:您必须使用类似start path/to/batchfile.bat gulp而不是npm run gulp。错误处理不适用于npm错误!

这可能是这样的:

@echo off 

:: Check if script is defined 
set _script=%1 
if "%_script%"=="" goto NO_SCRIPT_DEFINED 

:START_APP 
npm run %_script% 
goto END 

:NO_SCRIPT_DEFINED 
echo ERROR: script was not defined 
pause 
exit 

:END 
if %ERRORLEVEL% neq 0 goto NO_PARENT_SCRIPT 
exit 

:NO_PARENT_SCRIPT 
echo searching in ./client ... 
cd ./client 
npm run %_script% 
exit 
7

您可以使用npm run脚本来简化交易(见npm-scripts)。在父package.json

"scripts": { 
     ... 
    "client-build": "cd client && npm run build" 
    } 

当客户端与建立客户端代码npm run build命令package.json

然后调用npm run client-build作为其他任务的shell命令的一部分。例如:

"scripts": { 
    "start": "npm run client-build && gulp some-task", 
    ... 
    } 

这可能有助于打破子项目出与自己的git的回购,并通过后脚本构建它一个单独的模块。在那种情况下,当在父项目上运行npm install时,孩子将有机会自己创建。