简体   繁体   English

如何从grunt模板正确访问用户的主目录?

[英]How do I correctly access the user's home directory from a grunt template?

I'd like to use template in a Grunt file selection to copy a file to a user's home directory (don't ask why). 我想在Grunt文件选择中使用模板将文件复制到用户的主目录(不要问为什么)。

files: [ 
  {src: ['build/my_file.txt'], dest: "<%= grunt.process.HOME %>/my_file.txt"}
]

But this probably isn't right because then I get this: 但这可能不正确,因为然后我得到了:

Warning: An error occurred while processing a template (Cannot read property 'env' of undefined)

HOWEVER, if I force a javascript statement to be evaluated it works fine: 但是,如果我强制对javascript语句进行评估,则效果很好:

files: [ 
  {src: ['build/my_file.txt'], dest: "<%= grunt.process.HOME + '' %>/my_file.txt"}
]

What's the correct way to do this? 正确的方法是什么?

You might take a look at the os-homedir or userhome packages. 您可能会看一下os-homediruserhome软件包。 If you don't want to use them directly, their source code is fairly concise and illustrate a cross-platform process for getting the home directory. 如果您不想直接使用它们,则它们的源代码非常简洁,并说明了获取主目录的跨平台过程。

They mostly use process.env.HOME on Linux/MacOS, and process.env.USERPROFILE on Windows, but also have some logic to make something when those are missing. 他们大多使用process.env.HOME在Linux / MacOS和process.env.USERPROFILE Windows版本,但也有一些逻辑,使这些东西丢失的情况下。

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

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