首页 > 解决方案 > 编辑 package.js 文件

问题描述

我很难理解如何使用 package.js 文件运行我的所有 .js 文件我有将近 2000.js 脚本我需要一个一个运行它们,我使用的是由 gameflip 制作的 api我找到了 package.js 文件夹,但我不知道如何使用它,谁能告诉我该怎么做?在这里谢谢你的脚本:

    {
  "name": "gfapi",
  "version": "0.1.1",
  "description": "Gameflip API",
  "keywords": "Gameflip",
  "homepage": "https://github.com/iJJi/gfapi",
  "bugs": "https://github.com/iJJi/gfapi/issues",
  "author": {
    "name": "Eng-Shien Wu",
    "email": "engshien.wu@ijji.com"
  },
  "license": "MIT",
  "private": true,
  "files": [
    "index.js"
  ],
  "repository": "iJJi/gfapi",                
  "engines": {
    "node": ">=8.5.0"
  },
  "scripts": {
    "bulk_listing": "node src/samples/bulk_listing.js",
    "test": "ENVIRONMENT=mocha mocha src/test --recursive",
    "docs": "jsdoc -c jsdoc_conf.js -d docs -P package.json index.js; docco -o docs/samples src/samples/*.js src/samples/*.rb"
  },
  "dependencies": {
    "base-64": "^0.1.0",
    "bluebird": "^3.5.0",
    "bunyan": "^1.8.12",
    "file-type": "^8.1.0",
    "http-errors": "^1.6.2",
    "node-rest-client-promise": "^3.1.1",
    "promise-ratelimit": "^0.0.3",
    "request": "^2.85.0",
    "request-promise": "^4.2.2",
    "speakeasy": "^2.0.0"
  },
  "devDependencies": {
    "marked": "^0.3.19",
    "docco": "^0.7.0",``
    "jsdoc": "^3.5.5"
  }
}

标签: javascriptnode.js

解决方案


您发布的不是 a package.js(我什至不知道它是否存在),而是package.json. 它由节点包管理器NPM生成。它是所有项目依赖项的列表。我认为您正在寻找的是 npm 脚本,它们script位于package.json.

npm run <script>

# For example :
npm run bulk_listing
npm run test
npm run docs

每个脚本都将在 this 中运行其关联的命令package.json

npm run bulk_listing
# Will do the same thing as:
node src/samples/bulk_listing.js

更多关于package.json.


我在下面谈到的脚本

如果您想运行所有脚本,这应该可以完成工作:

const fileNames = ["path/to/fileA", "fileB"]; // I assume you have something to get all the files path. Isn't that npm run bulk_listing ?

fileNames.forEach(async (path, index) => {
    // It's pretty much like 'node <path>'
    await require(path);
    // All the code here is executed AFTER the script has been launched
    console.log(`LAUNCHED ${index} | ${path}`)
});

推荐阅读