javascript - Use glob node.js to know if a file exists in the parent folder using wildcard
问题描述
I need to search in the parent folder of where a node.js yeoman generator script is running to see if a file exists, but I won't know the file name - only the extension.
Glob: https://www.npmjs.com/package/glob
Folder Structure:
- C:\Work\
- C:\Work\Company\
- C:\Work\Company\Project\
Assume that the Project
folder is where the command prompt is... I want to run a Yeoman generator that looks into the Company
folder first to see if a specific file exists. It could be any file name, ending in .sln
.
There are plenty of beginner resources, but I can't find any examples that show:
- How to look in the parent folder successfully; and
- How to work with the output (true/false?) to use in a variable for logic later in the function.
Here's what I tried to do, but I am admittedly much more adept in C# than I am in JS.
var globbed = glob("../*.sln", function(err, files){
this.log(chalk.yellow("err = " + err));
this.log(chalk.yellow("files = " + files));
});
and this...
var gOptions = { cwd: "../" };
var globbed = glob("*.sln", gOptions, function(err, files){
this.log(chalk.yellow("err = " + err));
this.log(chalk.yellow("files = " + files));
});
In both examples, globbed
is an object, but I don't know what its properties are, and I am not able to access the internal function.
Essentially, I need to know if the file exists so that I can run an If/Then statement on it.
解决方案
Use glob.sync:
const files = glob.sync("*.sln", { cwd: "../" });
or simply
const files = glob.sync("../*.sln");
files
will be an array of *.sln
files, if any, in the parent directory. Obviously, glob.sync
is synchronous.
推荐阅读
- javascript - 将具有少量变量的静态 JS 函数转换为动态
- java - 当我点击一个recyclerview项目时,如何从一个片段移动到一个新的活动?
- kubernetes - 为什么必须为 ClusterRoleBinding.rbac.authorization.k8s.io 资源的 ServiceAccount 显式设置命名空间?
- oracle - PLSQL 文本文件创建行结束格式
- flutter - 错误:没有提供 AWSAccessKey - 颤振放大
- javascript - Danfo.js:使用 read_csv() 读取 .tsv 文件?
- python - 如果连续日期不存在,如何根据'ent_id'和'val'列在PANDAS中执行FFILL(),否则需要完成填充?
- android - 是否可以在没有数据库反应本机的情况下在 android / ios 中跟踪应用程序购买
- node.js - Discord.js Quick.db 过滤器
- java - 为什么在对父级使用paint方法后组件不可见