NPC creates a file instead of a directory

2

What is the reason why npc creates a file .ebextensions. instead of a folder .ebextensions.

I try to use excec and mkdir simple .ebextensions. Command and it does not work.

ncp('package.config', './build/.ebextensions./.', function (err) {
	if (err) {
		return console.error(err);
	}
	console.log('API copied');
});

Module link

    
asked by Santiago D'Antuoni 17.03.2017 в 20:39
source

1 answer

2

You do not need to install a package to create directories in Node.js. The API to interact with the host file system is quite complete. For example, to create a directory you just need to use the function FileSystem#mkdir or if you want to create it synchronously, FileSystem#mkdirSync .

Asynchronous

fs.mkdir('.ebextensions./', function (err) {
  if (err) { ... }

  // correcto
});

Synchronous

try {
  fs.mkdirSync('.ebextensions./');

  // correcto
} catch (e) {
  ...
}

Update

  

Thanks to @Trauma for noticing that you need to create nested directories.

First, keep in mind that the package NCP (Node Copy Package) has the functionality of copying files and directories. According to your question, it seems that you indicate that you need to create .

Copy of files

You can copy a file using only the Node.js API. However, if the complexity increases, it is better to use a package that is exclusively for that.

let source = 'C:/Users/Gustavo/Pictures/bug-hero.svg';
let target = 'D:/ABC/images';
let parts = target.split('/');
let filename = source.split('/').reverse()[0];

parts.forEach((part, i) => {
  if (i > 0) {
    const path = '${parts[i - 1]}/${part}';

    if (!fs.existsSync(path)) {
      fs.mkdirSync(path);
    }
  } else {
    if (!fs.existsSync(part)) {
      fs.mkdirSync(part);
    }
  }
});

fs
  .createReadStream(source)
  .pipe(fs.createWriteStream('${target}/${filename}'));

The previous code copies a image and creates directories if they do not exist ; the same as NCP does. First it is checking directory by directory, if it does not exist, it creates it. Finally, we open a read stream from the source file or directory and link it with a write stream from the destination directory. As you can see there are only a few lines; Nothing out of the ordinary.

    
answered by 17.03.2017 в 21:55