如何获取Meteor包中文件的路径? [英] How do I obtain the path of a file in a Meteor package?
问题描述
我知道如何从Meteor程序包中获取当前目录,但是如何获取项目中的特定文件?
I know how to get the current directory from a Meteor package, but how do I get the path of a specific file in the project?
节点的 __ dirname
和 __ filename
在Meteor中不起作用.
node's __dirname
and __filename
don't work in Meteor.
推荐答案
这很复杂.
-
流星运行
将您的项目文件复制到< project-dir>/.meteor/local/build
内的目录树中,将其重新组织为非-显而易见的方式(例如,原始树中的private子目录成为Assets子目录),并将其与各种npm模块混合在一起,以创建可作为nodejs项目执行的捆绑软件.确实,为避免重复,在.meteor目录中自动建立了一个.gitignore文件,该文件告诉git(如果您将其用于版本控制),则不要复制.meteor/local目录.
meteor run
copies your project files to a tree of directories inside<project-dir>/.meteor/local/build
, reorganizes them in non-obvious ways (e.g.. the private subdirectory in the original tree becomes the assets subdirectory) and mixes it in with various npm modules to create a bundle that can be executed as a nodejs project. Indeed, to avoid duplications, there is a .gitignore file automatically set up in the .meteor directory that tells git, if you use it for version control, not to copy the .meteor/local directory.
如果更改文件,将监视原始项目目录.然后将更改复制到当前项目构建目录中,并重新构建项目.
The original project directory gets watched in case you change a file. The change then gets copied into the current project build directory and the project rebuilt.
如果部署到远程系统,则将构建版本复制到服务器,然后运行.
If you deploy to a remote system, the build gets copied to a server and then run.
进程
通常是已定义的全局服务器端对象,并根据node.js API进行工作,因为流星服务器代码最终在node.js中运行.
process
is usually a defined global server-side object, and works according to the node.js API, because the meteor server code is ultimately running in node.js.
因此,您可以在服务器端运行 console.log(process.cwd());
以获得服务器进程的当前工作目录,通常如下:
So you can run console.log(process.cwd());
in your server-side to obtain the current working directory for the server process, usually something like:
~/<meteor project directory>/.meteor/local/build/programs/server
这表明当在本地完成 meteor run
时,原始项目文件位于 ../../../../../
中,但不要不要使用它,因为将来可能会发生变化.
This suggests that when meteor run
is done locally, original project files are in ../../../../../
, but don't use that as it may change in the future.
相反,对于包含原始项目文件的目录,您可以使用:
Instead, for the directory containing the original project files, you could use:
baseDir = process.cwd().replace(/\/\.meteor.*$/, '');
这将获取工作目录,并截断以/.meteor
This will get the working directory, and truncate everything beginning with /.meteor
但是,这对于服务器部署将不起作用,因为服务器上不需要原始项目树,仅需要构建.不打算用作客户端或服务器代码的文件可能被卡在私有子目录中,正如我提到的,该文件成为构建中的资产子目录.当前在构建中查找文件的方法是在本地运行中手动检查.meteor/local,或使用调用或模仿gnu find的JS库.
This won't work for a server deploy, though, because the original project tree is not needed on the server, only the build. Files that aren't intended to be client or server code could possibly be stuck in the private subdir, which as I mentioned becomes the assets subdir in the build. Ways to currently find files in the build is either manual inspection .meteor/local in a local run, or use of a JS library that calls or imitates gnu find.
自从您提到 packages 以来,我注意到在构建过程中,服务器端的软件包代码最终以以下形式结束:
Since you mentioned packages, I note that in the build, server-side package code finally ends up in:
~/<project-dir>/.meteor/local/build/programs/server/packages
和客户端位于
~/<project-dir>/.meteor/local/build/programs/web.browser/packages
这篇关于如何获取Meteor包中文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!