LoginSignup
3
1

More than 5 years have passed since last update.

vinyl-fsでUNCのファイルを引く

Posted at

vinyl-fsでUNCのファイルを引く

nodejsでfsからはUNCパスのファイルは参照できるのにvinyl-fsからは引けないので調べてみた。

調査結果

おもたる原因

  • minimatchはrootからパスをたどるのにopt.rootを使用する
  • なのでglobでUNCを引くときはopt.rootの指定が必要
  • しかしglob-streamはglobに送る前にcwdでresolveしている(複数ファイルに対応するため?)

globでopt.rootを指定する必要があるのに、それよ入りも前段階でC:をrootにされるのが引けない原因でした。
つまりglob-streamが悪い(暴論)~そもそも'\\'という表記自体が~

解決法

  • rootにUNCを指定する(globで動作するように)
  • glob-streamでresolveさせないように一部修正

手順

vinyl-fsをinstall
npm install vinyl-fs 
glob-streamを修正
//glob-stream/index.js

// 57行目
// NegativeMatchのためにopt.rootを追加
  create: function(globs, opt) {
    if (!opt) opt = {};
    if (typeof opt.cwd !== 'string') opt.cwd = process.cwd();
    if (typeof opt.root !== 'string') opt.root = process.cwd(); // 追加
    if (typeof opt.dot !== 'boolean') opt.dot = false;

// 77行目あたり
      if (globArray === negatives && typeof glob === 'string') {
        // glob = new Minimatch(unrelative(opt.cwd, glob), opt); // original
        glob = new Minimatch(unrelative(opt.root, glob), opt); // cwd->rootに変更
      }

// 119行目あたり
function unrelative(cwd, glob) {
  var mod = '';
  if (glob[0] === '!') {
    mod = glob[0];
    glob = glob.slice(1);
    glob = path.resolve(cwd, glob); // 不一致はglobに流れないので絶対パスにする
  }
  // return mod+path.resolve(cwd, glob); // original
  return mod+glob; // resolveをはずす
}

以上で修正完了
不一致の情報はglobには流さず、globの結果に対してフィルターをかける動きになっているので絶対パスに直す。

使うとき

\\PC名\共有名までをrootに指定する

var vfs = require('vinyl-fs');
vfs.src("/*", {root:"//PCNAME/share"})

感想

glob-streamがnpm上のとgithub上のでコードが違ったり(version表記は同じ)だったり、なにやら面倒な雰囲気がしたので簡単にできる範囲で終わらせました。
そもそもwindows上でnodejsからUNCでファイルを参照するという状況自体が少ないよなぁ...。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1