是否可以使用 npm 在多个子文件夹中运行脚本? [英] Is it possible to use npm to run scripts in multiple subfolders?

查看:55
本文介绍了是否可以使用 npm 在多个子文件夹中运行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个子文件夹(客户端、服务器、资产)的文件夹(一个项目).每个子文件夹都有一个不同的命令来启动和处理我需要启动 3 个应用程序的项目.这是我用来启动每个子项目的文件夹布局和命令:

I have a folder (a project) with 3 subfolders (client, server, assets). Each subfolder has a different command to start and to work on the project I need to start the 3 apps. This is the folder layout and the commands I use to start each subproject:

  • 项目
    • 客户端(离子服务)
    • 服务器(节点索引)
    • 资产(http-server -p 8082)

    目前,我转到三个文件夹中的每一个并启动每个应用程序.为了使流程更加标准,每个子项目都有一个带有启动脚本的 package.json,所以我只需要 cd 子文件夹 &&npm start.

    Currently, I go to each of the three folders and start each of the apps. To make the process more standard, each subproject has a package.json with a start script, so I just cd subfolder && npm start.

    我的问题:是否可以在父文件夹上使用 npm(即,在那里写一个 package.json),这样我就可以运行遵循命令并具有相同(或相似)的效果?

    My question: is it possible to use npm on the parent folder (i.e., write a package.json there) in such a way that I can just run the following command and have the same (or similar) effect?

    项目> npm start

    project> npm start

    我曾尝试使用 parallelshell 包,但它不起作用(可能是因为 cd:

    I have tried using the package parallelshell, but it didnt work (probably because of the cd:

    "scripts": {
      "start": "parallelshell 'cd app && ionic serve' 'cd api && npm start' 'cd assets && npm start'",
    }
    

    推荐答案

    您可以使用并发"来完成此操作.因此,您将创建一个如下所示的 package.json:

    You can use "concurrently" to accomplish this. So you would create a package.json which looks something like the following:

    ...
    "scripts": {
      "client": "cd client && npm start",
      "server": "cd server && npm start",
      "assets": "cd assets && ionic serve",
      "start": "concurrently \"npm run client\" \"npm run server\" \"npm run assets\" ",
    },
    ...
    "devDependencies": {
      "concurrently": "^1.0.0"
    }
    ...
    

    注意:这将同时启动所有三个进程,这意味着您将获得所有三个的混合输出(如已经提到的 topheman)

    Note: This will start all three processes concurrently which means that you get mixed Output of all three (like topheman already mentioned)

    这篇关于是否可以使用 npm 在多个子文件夹中运行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆