简体   繁体   English

需要帮助来了解我的应用如何启动

[英]Need help understanding how my app starts

I have an app that plays Tic-Tac-Toe and Connect Four in the command line. 我有一个可在命令行中播放井字游戏和四连音的应用程序。 Some of the code is boilerplate that I didn't write, and I'm having trouble understanding how it works. 其中一些代码是我未编写的样板,并且在理解其工作方式时遇到了麻烦。

When I run npm start , Connect Four plays as expected, but there is no way to play Tic-Tac-Toe. 当我运行npm start ,“连接四人”会按预期方式播放,但无法播放井字游戏。 I want the user to choose to play either game. 我希望用户选择玩任一游戏。 Ideally I would prompt the user, but having a command for each game would be fine. 理想情况下,我会提示用户,但每个游戏都有一个命令就可以了。

There's a short server.js file that looks like this: 有一个简短的server.js文件,如下所示:

require('babel-polyfill');
require('babel-register');
require('.');

And in my package.json , I have main: "./connect4/index.js" . 在我的package.json ,我有main: "./connect4/index.js"

As far as I can tell, this is all of the code relevant to starting the app. 据我所知,这是与启动应用程序相关的所有代码。 I apologize for the vague question. 对于这个模糊的问题,我表示歉意。 But the app isn't very complicated, so hopefully it makes some sense. 但是该应用程序不是很复杂,因此希望它有意义。

There's two ways you could do this. 您可以通过两种方式执行此操作。 The first is by using the npm package inquirer which will allow you to prompt the user in the command line. 第一种是使用npm软件包查询器,它允许您在命令行中提示用户。

The alternative would be to use nodes built-in process.argv which is a property that will return an array with all the command line arguments you passed. 替代方法是使用节点内置的process.argv ,它是一个属性,它将返回包含您传递的所有命令行参数的数组。 Note that when using this, position [0] will default to the location of node on your machine, and position [1] will default to the current file location. 请注意,使用此选项时,位置[0]将默认为计算机上节点的位置,位置[1]将默认为当前文件位置。 To access custom arguments you will have to start at position [2] etc. So you could write logic that will run tic-tac-toe or connect 4 based on the value of argv at position 2. so somewhere in your code you would something like 要访问自定义参数,您必须从位置[2]等处开始。因此,您可以根据位置2处的argv值编写运行tic-tac-toe或连接4的逻辑,因此在代码中的某个位置您会喜欢

 if (process.argv[2] === "tick-tack-toe") {
  console.log("do tick tack toe logic");
}else if (process.argv[2] === "connect-4") {
  console.log("do connect 4 logic");
}

Hope this helps. 希望这可以帮助。 You can read more about process.argv here https://nodejs.org/docs/latest/api/process.html#process_process_argv 您可以在此处阅读有关process.argv的更多信息https://nodejs.org/docs/latest/api/process.html#process_process_argv

and here's a link to inquirer on github https://github.com/SBoudrias/Inquirer.js#readme 这是github上查询者的链接https://github.com/SBoudrias/Inquirer.js#readme

As background, when executing npm start npm will attempt to run the command node server.js if scripts.start is not specified in the package.json file. 作为背景,如果在package.json文件中未指定scripts.start则执行npm start npm将尝试运行命令node server.js

Assuming executing node ./connect4/index.js works and that tic-tac-toe is similarly organized as connect4 then either of the following would work. 假设执行node ./connect4/index.js工作并且tic-tac-toe的组织方式与connect4类似,则以下任一方法都可以工作。

  • Simply execute the command directly with node ./ticTacToe/index.js (assuming ticTacToe is the directory and index.js is the main file to be executed). 只需直接使用node ./ticTacToe/index.js执行命令(假设ticTacToe是目录,而index.js是要执行的主要文件)。
  • Add a scripts: {ticTacToe: "node ./ticTacToe/index.js"} (or anything you want to call it) to package.json and then execute npm run ticTacToe . scripts: {ticTacToe: "node ./ticTacToe/index.js"} (或任何您想调用的scripts: {ticTacToe: "node ./ticTacToe/index.js"} )添加到package.json,然后执行npm run ticTacToe

Thanks for the feedback. 感谢您的反馈。 I added a top-level index.js file, imported the relevant code from each game into that file, and used Inquirer to prompt the user to choose a game. 我添加了一个顶级index.js文件,将每个游戏的相关代码导入到该文件中,并使用Inquirer提示用户选择游戏。

Adding scripts for ticTacToe and c4 to my package.json didn't work because I think my server.js file is needed to parse import/export syntax. 在我的package.json添加ticTacToec4脚本不起作用,因为我认为需要我的server.js文件来解析导入/导出语法。

Repo is here , if curious. 如果好奇的话,回购在这里

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

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