简体   繁体   English

如何在节点模块中保存用户指定的配置?

[英]How can I save a user specifig config in a node module?

I am trying to build a CLI with node (using commander.js) and I would like to save user specific configuration, so that a user only has to enter it on the first usage. 我正在尝试使用node(使用commander.js)构建带有节点的CLI,并且我想保存用户特定的配置,以便用户仅在第一次使用时输入它。 How would I do this? 我该怎么做?

You can write to a hidden file in, say, ~/.mycliconfig and the just read the file synchronously 您可以在~/.mycliconfig写入隐藏文件,然后同步读取文件

try{
  var userData = fs.readFileSync(process.env.HOME + '/.mycliconfig');
} catch(e){
  if (e.code === 'ENOENT') {
    console.log('File not found!');
  } else {
    throw e;
  }
}

If you need to save more stuff you can always create a wrapper folder and drop all files in there. 如果您需要保存更多内容,则始终可以创建包装器文件夹并将所有文件放在该文件夹中。

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

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