2

I'm using ncp to copy files as following:

import ncp from "ncp";
import { promisify } from "util";

const ncpPromise = promisify(ncp);
const copyAssets = async (exportFolderName, includeSourceMaps) => {
  const assets = glob.sync("**/", { cwd: distPath });
  const options = { clobber: true, stopOnErr: true };
  if (!includeSourceMaps) {
    options.filter = (f) => {
      return !f.endsWith(".map");
    };
  }
  return Promise.all(
    assets.map((asset) => {
      return ncpPromise(
        path.join(distPath, asset),
        path.join(exportPath, exportFolderName, asset),
        options
      );
    })
  );
};

But this sometimes fails with the following error:

"ENOENT: no such file or directory, mkdir '/path/to/folder'"

How can I solve this ?

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191

2 Answers2

2

I guess you are trying to copy all files matching for the given glob, so you need to do:

const assets = glob.sync("**/*.*", { cwd: distPath }); // note the *.*

For example, your current glob in question will result into:

[
  'folder1/',
  'folder2/',
]

whereas the glob in this answer will result into (This is what you want):

[
  'folder1/file1.txt',
  'folder1/file2.txt',
  'folder2/anotherfile.txt',
]

An Alternative:

Seems like ncp isn't being maintained. So, you can use fs-extra, it can copy file and directory as well:

const glob = require("glob");
const path = require("path");
const fs = require("fs-extra");

const copyAssets = async (exportFolderName, includeSourceMaps) => {
  const assets = glob.sync("**/*.*", { cwd: distPath });

  const options = { overwrite: true };

  if (!includeSourceMaps) {
    options.filter = (f) => {
      return !f.endsWith(".map");
    };
  }

  return Promise.all(
    assets.map((asset) => {
      return fs
        .copy(
          path.join(distPath, asset),
          path.join(exportPath, exportFolderName, asset),
          options
        )
        .catch((e) => console.log(e));
    })
  );
};
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
  • Hello, I tried that but the error message got changed to `"ENOENT: no such file or directory, open '/path/to/file'"` (notice `open` on the file instead of `mkdir`), the path here is inside the export folder and not where I'm copying files from. – Renaud is Not Bill Gates Feb 21 '21 at 18:39
  • The error with `open` means the destination directory doesn't exist. I would suggest that you use `fs-extra` as written in this answer. – Ajeet Shah Feb 21 '21 at 19:51
1

NPM qir (yes, it is published by myself) is another choice:

const qir = require('qir');
qir.asyncing.copy('/A/path/to/src', '/B/path/to/dest')
    .then(() => { /* OK */ }
    .catch(ex => { /* Something wrong */ }
    ;

Here, /A/path/to/src may be a file or a folder, and /B/path/to is not required to exist already.

There is a synchronous way:

const qir = require('qir');
qir.syncing.copy('/A/path/to/src', '/B/path/to/dest');

And, if both src and dest located in the same directory:

const qir = require('qir');
let q = new qir.AsyncDir('/current/working/dir');
q.copy('A/path/to/src', 'B/path/to/dest')
    .then(() => { /* OK */ }
    .catch(ex => { /* Something wrong */ }
    ;

It will copy /current/working/dir/A/path/to/src to /current/working/dir/B/path/to/dest.

Youngoat
  • 11
  • 3