从 Gulp 中的目录获取不带扩展名的文件名数组?

Get an array of file names without extensions from a directory in Gulp?

我想将 2 个目录(helpers/dialogs/)中的每个文件名都放入一个数组中 不带文件扩展名 使用 Gulp and/or NPM。请注意,该数组将预先填充值 - 我需要将文件名 附加 到此数组。

也就是说,我的目录结构是这样的:

helpers/
    a.js
    b.js
    c.js
dialogs/
    x.js
    y.js
    z.js

我有这个:

var modules = ['main-module'];

我需要根据目录像这样填充我的数组:

var modules = ['main-module', 'a', 'b', 'c', 'x', 'y', 'z'];

我该怎么做?

我已经尝试使用 fs 模块 fs.readdirSync along with gulp-rename,但如果可能的话,最好在单个流式操作中完成此任务。

将每个目录放入自己的数组以进行其他操作也很有用 - 换句话说,根据目录输出一个组合数组和 2 个单独的数组(总共 3 个数组)。

var modules = ['main-module', 'a', 'b', 'c', 'x', 'y', 'z'];
var helpers = ['a', 'b', 'c'];
var dialogs = ['x', 'y', 'z'];

您可以使用 glob 轻松完成,这里有一段代码可以满足您的需求。

var glob = require('glob');
var path = require('path');

var modules = ['main-module'];
var helpers = [];
var dialogs = [];

glob.sync("@(helpers|dialogs)/*.js")
.forEach(function(file) {
  var module = path.basename(file, path.extname(file))
  modules.push(module);

  switch(path.dirname(file)) {
    case 'helpers':
      helpers.push(module);
      break;
    case 'dialogs':
      dialogs.push(module);
      break;
  }
});