如何让npm运行委托给子package.json?

时间:2021-10-19 01:10:52

I've got 2 levels of package.json files.

我有2级package.json文件。

Example is here:

示例在这里:

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

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

The reason is that the top level is a Rails App, and I'm putting all node tools under a directory called client, with it's own package.json file. The top level package.json file is a convenience as well as a hook for the node buildpack to run the npm install script.

原因是顶层是Rails应用程序,我将所有节点工具放在一个名为client的目录下,并使用它自己的package.json文件。*package.json文件是一个方便,也是节点buildpack运行npm安装脚本的钩子。

I've got an example of forwarding the gulp command. Any way to generically forward anything not found from the top level package.json to the child one?

我有一个转发gulp命令的例子。有什么方法可以将从*package.json找不到的任何东西转发给孩子一个?

Top Level 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"
}

Subdirectory package.json

子目录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"
  }
}

2 个解决方案

#1


9  

You can use npm run scripts to simplify the transaction (see npm-scripts). In the parent package.json:

您可以使用npm run脚本来简化事务(请参阅npm-scripts)。在父package.json中:

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

Where the client has a package.json with the npm run build command for building the client-side code.

客户端有一个带有npm run build命令的package.json,用于构建客户端代码。

Then invoke npm run client-build as part of the shell command of other tasks. For instance:

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

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

It may help to break the child project out into a separate module with its own git repo and building it through a postinstall script. In that case, when running npm install on the parent project, the child will have a chance to build itself.

将子项目拆分为具有自己的git repo的单独模块并通过postinstall脚本构建它可能会有所帮助。在这种情况下,当在父项目上运行npm install时,子项将有机会自行构建。

#2


-1  

you could write a batch file where you put the gulp-command. Then you have to check the errorstate. That could look like this:

您可以编写一个批处理文件,在其中放置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;

Then you just have to call the script in your package.json like this:

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

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

Did the same on my project....

我的项目也一样......

EDIT: For all scripts.... you could create one batchfile that takes the script name as parameter. the script does the same like above, but it should work for every command.

编辑:对于所有脚本....您可以创建一个将脚本名称作为参数的批处理文件。脚本与上面的相同,但它应该适用于每个命令。

NOTE: You have to use something like start path/to/batchfile.bat gulp instead of npm run gulp. Errorhandling do not work for npm errors!

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

This could look like this:

这看起来像这样:

@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

#1


9  

You can use npm run scripts to simplify the transaction (see npm-scripts). In the parent package.json:

您可以使用npm run脚本来简化事务(请参阅npm-scripts)。在父package.json中:

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

Where the client has a package.json with the npm run build command for building the client-side code.

客户端有一个带有npm run build命令的package.json,用于构建客户端代码。

Then invoke npm run client-build as part of the shell command of other tasks. For instance:

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

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

It may help to break the child project out into a separate module with its own git repo and building it through a postinstall script. In that case, when running npm install on the parent project, the child will have a chance to build itself.

将子项目拆分为具有自己的git repo的单独模块并通过postinstall脚本构建它可能会有所帮助。在这种情况下,当在父项目上运行npm install时,子项将有机会自行构建。

#2


-1  

you could write a batch file where you put the gulp-command. Then you have to check the errorstate. That could look like this:

您可以编写一个批处理文件,在其中放置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;

Then you just have to call the script in your package.json like this:

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

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

Did the same on my project....

我的项目也一样......

EDIT: For all scripts.... you could create one batchfile that takes the script name as parameter. the script does the same like above, but it should work for every command.

编辑:对于所有脚本....您可以创建一个将脚本名称作为参数的批处理文件。脚本与上面的相同,但它应该适用于每个命令。

NOTE: You have to use something like start path/to/batchfile.bat gulp instead of npm run gulp. Errorhandling do not work for npm errors!

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

This could look like this:

这看起来像这样:

@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