自定义node.js上的建议/最佳实践CLI工具配置文件:位置和命名?

时间:2023-01-12 23:26:01

I'll try to keep this question short, but it is in 2 parts, please:

我会尽量保持这个问题的简短,但请分为两部分,请:

  1. Where should config files for nodejs/npm CLI tools be saved?
  2. 应该在哪里保存nodejs / npm CLI工具的配置文件?

  3. What should they be called?
  4. 他们应该叫什么?

Let's say I write a node.js CLI tool that, for example, grabs today's weather and displays it in terminal. I call it weather-getter. Note, the main goal is not to be called programmatically, but typed into terminal like BASH. It is intended to be run by typing its simple name after installing globally, or via a directory in the user's local /bin. (Sudo is not required for its install.)

假设我编写了一个node.js CLI工具,例如,抓取今天的天气并在终端中显示它。我称之为天气吸气者。注意,主要目标不是以编程方式调用,而是像BASH一样输入终端。它旨在通过在全局安装后键入其简单名称或通过用户的local / bin中的目录来运行。 (安装时不需要Sudo。)

This project would be installed normally via npm. It can receive a zipcode via an argument like:

该项目将通过npm正常安装。它可以通过以下参数接收邮政编码:

gavin@localhost:~$ weather-getter -z "12345"

OK the program works fine like this. My next step would be to allow the user to save a config file somewhere, and pull from that config file for defaults. Similar to a .vimrc file. This config might look like this:

好的程序运行正常。我的下一步是允许用户在某处保存配置文件,并从该配置文件中提取默认值。类似于.vimrc文件。此配置可能如下所示:

{
  "zipcode": "12345",
  "language": "en",
  "unit": "fahrenheit"
}

I suppose it should begin with a dot. I also suppose it should be located in the npm module install, and not in ~/. Or should I consider using ~/ or /etc/ or ~/.config or ~/.local like many other programs? Should node programs try to use a common directory, such as ~/.config/node/ or ~/.config/npm/? And if the file is in there, should it begin without the dot?

我想它应该以点开头。我还假设它应该位于npm模块安装中,而不是〜/。或者我应该考虑像许多其他程序一样使用〜/或/ etc /或〜/ .config或〜/ .local?节点程序是否应该尝试使用公共目录,例如〜/ .config / node /或〜/ .config / npm /?如果文件在那里,它应该在没有点的情况下开始吗?

Note: My question is not about reading/writing a file with node.js, just recommendations on the config location and naming convention. Thank you!

注意:我的问题不是关于使用node.js读取/写入文件,而是关于配置位置和命名约定的建议。谢谢!

1 个解决方案

#1


4  

Since this is a generic CLI application (which only so happens to be implemented in Node.js) installed into the system path, you should follow the best practices or rules established for the target operating system.

由于这是安装在系统路径中的通用CLI应用程序(只在Node.js中实现),因此应遵循为目标操作系统建立的最佳实践或规则。

Unix/Linux/OS X, similar

In order of priority, these would be (but are not limited to):

按优先顺序,这些将是(但不限于):

  • ~ (User's home folder) - many programs store user-level config in their home directory, usually in a dot-prefixed file, followed by the application's name (or similar) - i.e. ~/.weather-getter

    〜(用户的主文件夹) - 许多程序在其主目录中存储用户级配置,通常在带点前缀的文件中,后跟应用程序的名称(或类似) - 即〜/ .weather-getter

  • /usr/local/etc, /etc - system-level configuration files. These should generally apply to all users in the system and thus should take less precedence than settings in home folder. The difference between these two etc paths is usually that the former is used for user-installed programs, whereas the latter is for system-level programs (this is especially true for Mac users using Homebrew). This distinction is, however, not always respected and therefore both locations should be checked for config files (preferrably with the /etc directory having lesser priority).

    / usr / local / etc,/ etc - 系统级配置文件。这些应该通常适用于系统中的所有用户,因此应该优先于主文件夹中的设置。这两个等路径之间的区别通常是前者用于用户安装的程序,而后者用于系统级程序(对于使用Homebrew的Mac用户尤其如此)。但是,这种区别并不总是得到尊重,因此应检查两个位置的配置文件(最好是/ etc目录的优先级较低)。

  • Your application's root - these should be the default settings for your application, a fallback when no user or system config has been found.

    您的应用程序的根目录 - 这些应该是您的应用程序的默认设置,当找不到用户或系统配置时,这是一个回退。

  • Other locations may be considered if needed.

    如果需要,可以考虑其他位置。

Windows

This is usually somewhere within %APPDATA% directory if your app allows GUI or at least CLI configuration management, or the Windows registry (using winreg, for example). I have personally little experience with Windows development/CLI so I welcome any further comments or answers on this topic. I believe using the user's homefolder would also be acceptable as long as the file can be marked as hidden (so it does not clutter the view).

如果您的应用程序允许GUI或至少CLI配置管理,或者Windows注册表(例如,使用winreg),这通常位于%APPDATA%目录内。我个人对Windows开发/ CLI没什么经验,所以我欢迎任何关于这个主题的进一步评论或答案。我相信使用用户的homefolder也是可以接受的,只要该文件可以被标记为隐藏(因此它不会使视图混乱)。

Some general considerations

  • Many CLI applications install their default configurations into one of the mentioned locations so the user has a good starting point when configuring your app
  • 许多CLI应用程序将其默认配置安装到上述某个位置,以便用户在配置应用程序时有一个良好的起点

  • The way your configuration options are treated when multiple configuration files are present (are they merged in some order? Is only one used? Which one takes precedence?) is completely up to you, but you should make it clear in your documentation, perhaps even mention it in the configuration files themselves
  • 当存在多个配置文件时,它们处理配置选项的方式(它们是否以某种顺序合并?只使用了一个?哪个优先??)完全取决于您,但您应该在文档中明确说明,甚至可能在配置文件中提及它

  • If your application requires multiple configuration files it is preferred that they are grouped in their own folder
  • 如果您的应用程序需要多个配置文件,则最好将它们分组到自己的文件夹中

Update about dotfiles

有关dotfiles的更新

The reason why some files or folders are prefixed with a dot is to hide them from users' normal view (i.e. when browsing their home directory via a GUI). It is therefore common practice to use dot-prefixed file/folder names when storing configuration files in directories where users normally operate, but not do so when storing config files in system-level folders.

某些文件或文件夹以点为前缀的原因是将它们隐藏在用户的正常视图中(即,当通过GUI浏览其主目录时)。因此,在将配置文件存储在用户正常操作的目录中时,通常会使用以前缀为前缀的文件/文件夹名称,但在将配置文件存储在系统级文件夹中时则不这样做。

#1


4  

Since this is a generic CLI application (which only so happens to be implemented in Node.js) installed into the system path, you should follow the best practices or rules established for the target operating system.

由于这是安装在系统路径中的通用CLI应用程序(只在Node.js中实现),因此应遵循为目标操作系统建立的最佳实践或规则。

Unix/Linux/OS X, similar

In order of priority, these would be (but are not limited to):

按优先顺序,这些将是(但不限于):

  • ~ (User's home folder) - many programs store user-level config in their home directory, usually in a dot-prefixed file, followed by the application's name (or similar) - i.e. ~/.weather-getter

    〜(用户的主文件夹) - 许多程序在其主目录中存储用户级配置,通常在带点前缀的文件中,后跟应用程序的名称(或类似) - 即〜/ .weather-getter

  • /usr/local/etc, /etc - system-level configuration files. These should generally apply to all users in the system and thus should take less precedence than settings in home folder. The difference between these two etc paths is usually that the former is used for user-installed programs, whereas the latter is for system-level programs (this is especially true for Mac users using Homebrew). This distinction is, however, not always respected and therefore both locations should be checked for config files (preferrably with the /etc directory having lesser priority).

    / usr / local / etc,/ etc - 系统级配置文件。这些应该通常适用于系统中的所有用户,因此应该优先于主文件夹中的设置。这两个等路径之间的区别通常是前者用于用户安装的程序,而后者用于系统级程序(对于使用Homebrew的Mac用户尤其如此)。但是,这种区别并不总是得到尊重,因此应检查两个位置的配置文件(最好是/ etc目录的优先级较低)。

  • Your application's root - these should be the default settings for your application, a fallback when no user or system config has been found.

    您的应用程序的根目录 - 这些应该是您的应用程序的默认设置,当找不到用户或系统配置时,这是一个回退。

  • Other locations may be considered if needed.

    如果需要,可以考虑其他位置。

Windows

This is usually somewhere within %APPDATA% directory if your app allows GUI or at least CLI configuration management, or the Windows registry (using winreg, for example). I have personally little experience with Windows development/CLI so I welcome any further comments or answers on this topic. I believe using the user's homefolder would also be acceptable as long as the file can be marked as hidden (so it does not clutter the view).

如果您的应用程序允许GUI或至少CLI配置管理,或者Windows注册表(例如,使用winreg),这通常位于%APPDATA%目录内。我个人对Windows开发/ CLI没什么经验,所以我欢迎任何关于这个主题的进一步评论或答案。我相信使用用户的homefolder也是可以接受的,只要该文件可以被标记为隐藏(因此它不会使视图混乱)。

Some general considerations

  • Many CLI applications install their default configurations into one of the mentioned locations so the user has a good starting point when configuring your app
  • 许多CLI应用程序将其默认配置安装到上述某个位置,以便用户在配置应用程序时有一个良好的起点

  • The way your configuration options are treated when multiple configuration files are present (are they merged in some order? Is only one used? Which one takes precedence?) is completely up to you, but you should make it clear in your documentation, perhaps even mention it in the configuration files themselves
  • 当存在多个配置文件时,它们处理配置选项的方式(它们是否以某种顺序合并?只使用了一个?哪个优先??)完全取决于您,但您应该在文档中明确说明,甚至可能在配置文件中提及它

  • If your application requires multiple configuration files it is preferred that they are grouped in their own folder
  • 如果您的应用程序需要多个配置文件,则最好将它们分组到自己的文件夹中

Update about dotfiles

有关dotfiles的更新

The reason why some files or folders are prefixed with a dot is to hide them from users' normal view (i.e. when browsing their home directory via a GUI). It is therefore common practice to use dot-prefixed file/folder names when storing configuration files in directories where users normally operate, but not do so when storing config files in system-level folders.

某些文件或文件夹以点为前缀的原因是将它们隐藏在用户的正常视图中(即,当通过GUI浏览其主目录时)。因此,在将配置文件存储在用户正常操作的目录中时,通常会使用以前缀为前缀的文件/文件夹名称,但在将配置文件存储在系统级文件夹中时则不这样做。