繁体   English   中英

有没有办法在连接到 package.json 的终端中运行命令?

[英]Is there an way to run a command into terminal that linked to package.json?

任务

构建一个程序来分析假设菜单计划日历应用程序的应用程序使用数据。 在任何一天,用户都可以计划多餐,每餐可能有多道菜。 您构建的程序将分析用户对应用的参与程度

数据

在 ./data 文件夹中,您会发现一堆格式为 userId.json 的文件。 它们包含大量用户的应用数据。 检查时结构应该相当明显。

Output

./dist/run active 2016-09-01 2016-09-08应该会生成一个逗号分隔的用户 ID 列表,这些用户 ID 在指定时间段内是“活跃的”,其中“活跃”意味着他们至少吃了 5 顿饭。

./dist/run superactive 2016-09-01 2016-09-08应该会生成一个逗号分隔的用户 ID 列表,这些用户 ID 在指定时间段内“超级活跃”,这意味着他们吃了超过 10 顿饭。

./dist/run bored 2016-09-01 2016-09-08应该生成一个逗号分隔的用户 ID 列表,这些用户 ID 在指定时间段内“无聊”,这意味着它们在前一个时间段内是“活跃的”,但是没有在指定时间段内设置“活跃”阈值。

注意:我刚刚添加了任务和数据部分,以便更好地了解我被要求执行的任务。 How can i add something (some scripts) into package.json that will hit and do the rest job for me whenever above three command (eg ./dist/run active 2016-09-01 2016-09-08 ) will be run on终端。

在 package.json 中,您可以定义脚本。

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "static": "some comand"
  },

然后您可以输入npm run static 之后,它将在当前目录中运行some 命令

如果我理解正确,您想在package.json中运行一个脚本,该脚本执行./dist/run active 2016-09-01 2016-09-08 (或任何其他 2 个脚本)以及其他命令。

此答案假定您运行 bash 环境,例如 Linux、MacOS 或 WSL(适用于 Linux 的 Windows 子系统)

一个想法是将您的执行放在每个流的Shell脚本中(例如active ),然后将每个流作为单独的命令放在package.json上。 像这样的东西:

文件active.sh

#!/usr/bin/env bash

./dist/run active 2016-09-01 2016-09-08

echo "Put some other logic here as well"

文件package.json

...
{
  "scripts": {
    "active": "./active.sh"
  }
}
...

笔记

确保您的.sh文件具有正确的执行权限

chmod +x ./active.sh

笔记2

.sh文件上的#!/usr/bin/env bash称为Shebang(或 hash bang)


最后,您可以通过执行以下操作来执行流程:

npm run active

并为您的其他脚本重复该过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM