简体   繁体   English

为什么 glob promise 返回一个空字符串数组?

[英]Why does glob promise return an empty string array?

I am only using typescript, ts-node, npm and path我只使用 typescript、ts-node、npm 和路径

I am simply trying to return a string array of files defined by my pattern using glob-promise (An npm that I imported which uses glob but its promise based).我只是想使用 glob-promise 返回由我的模式定义的字符串文件数组(我导入的 npm 使用 glob 但它基于 promise)。

I created a custom npm script to run my ts file for the terminal to display info in my package.json below我创建了一个自定义 npm 脚本来运行我的终端 ts 文件,以在下面的 package.json 中显示信息

{
"name": "glob-test",
"version": "1.0.0",
"description": "A Glob Test",
"scripts": {
    "build:glob": "ts-node --files globby.ts"
},
"dependencies": {
    "glob-promise": "^6.0.1",
    "path": "^0.12.7",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.4"
}

} }

And this below is my globby.ts file which runs the glob script下面是我运行 glob 脚本的 globby.ts 文件

import * as path from 'path';
import glob from 'glob-promise';

const dir = path.dirname(__dirname);
    const txtURL = path.resolve(dir, 'glob-test', 'folder-area', '*.txt');

    glob(txtURL).then(function (cnt) {
    console.log('Path: ' + txtURL);
    console.log('Content: ', cnt);
});

My project directory looks like so:我的项目目录如下所示:

在此处输入图像描述

I type in my terminal npm run build:glob but it returns an empty array and the path displays correctly I'm not sure what I am doing wrong.我在我的终端 npm run build:glob 中输入,但它返回一个空数组并且路径显示正确我不确定我做错了什么。 I've attempted to use path.join and path.resolve but either give the same result.我尝试使用 path.join 和 path.resolve 但两者都给出了相同的结果。 It should return the joke.txt file.它应该返回 joke.txt 文件。 Anyone have any idea?有人知道吗?

Turns out the answer is that the path module with the glob-promise module are not playing nice in my windows environment.原来答案是带有 glob-promise 模块的路径模块在我的 windows 环境中运行不佳。 If I use a normal string with or without concats glob will return perfectly anything I ask.如果我使用带或不带 concats 的普通字符串,glob 将完美返回我所要求的任何内容。

This seems to be a bug/issue or incompatibility within the windows environment most likely.这似乎是 windows 环境中最有可能的错误/问题或不兼容。 Yet to test on Linux.尚未在 Linux 上进行测试。

eg: Works below例如:下面的作品

import * as path from 'path';
import glob from 'glob-promise';

// const dir = path.dirname(__dirname);
const txtURL = path.resolve('folder-area', '*.txt');
const testURL = './folder-area/*.txt';

const returnTxtFiles = async () => {
const txtFiles = await glob(testURL);
console.log(txtFiles);
};

returnTxtFiles();

The above correctly returns ['joke.txt', 'file1.txt']以上正确返回['joke.txt', 'file1.txt']

The other option is replace all the '/' to '' manually to any path.join("string", "string") by adding at the end .replace(/\\/g, "/")另一种选择是通过在末尾添加.replace(/\\/g, "/")手动将所有 '/' 替换为任何path.join("string", "string") )

Which is the method I went with as it took much less work adn I think is more understandable as well as it changes the original codebase the least.这是我采用的方法,因为它花费的工作少得多,而且我认为更容易理解,而且它对原始代码库的更改最少。

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

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