有没有办法将 运行 一个命令输入链接到 package.json 的终端?

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

任务

Build a program to analyze app usage data for a hypothetical menu planning calendar app. On any day, a user can plan multiple meals, and each meal may have multiple dishes. The program you build will analyze how engaged users are with the app

数据

In the ./data folder, you will find a bunch of files in the form userId.json. They contain app data for a bunch of users. The structure should be fairly obvious upon examination.

输出

./dist/run active 2016-09-01 2016-09-08 should result in a list of comma- separated user ids that were “active” during the specified period, where “active” means they had at least 5 meals.

./dist/run superactive 2016-09-01 2016-09-08 should result in a list of comma- separated user ids that were “super-active” during the specified period, which means they had more than 10 meals.

./dist/run bored 2016-09-01 2016-09-08 should result in a list of comma- separated user ids that were “bored” during the specified period, meaning that they were “active” in the preceding period, but didn’t make the “active” threshold in the specified period.

注意:我刚刚添加了任务和数据部分,以便更好地了解我被要求执行的任务。我怎样才能将一些东西(一些脚本)添加到 package.json 中,只要以上三个命令(例如 ./dist/run active 2016-09-01 2016-09-08)将在终端上 运行 时,它就会为我完成剩下的工作。

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

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

然后你可以输入,npm run static。之后,它会运行当前目录下的一些命令

如果我理解正确的话,你想要 运行 package.json 中的一个脚本来执行 ./dist/run active 2016-09-01 2016-09-08(或任何其他 2 个脚本)加上其他命令。

此答案假定您 运行 一个 bash 环境,例如 Linux、MacOS 或 WSL(Windows [=60= 的子系统])

一个想法是将您的执行放在每个流程的 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 (or hash bang)


最后,您可以执行流程:

npm run active

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