Write copyfile.js script implementation, copy all files and sub-folders in the folder to another folder
Notice copyfile. js belongs to the back-end script and needs to run node copyfile. js
-
Introduce two modules in the Node environment write file module and read file module
const fs = require('fs'); const path = require('path'); Copy the code
-
The following two pieces of code are integrated: this is done by directly calling the copyFolde function (relative path of source file, relative path of copy target)
-
Copy the source file to the destination file:
/ /! Copy files from the srcPath path to the tarPath CD as a callback var copyFile = function(srcPath, tarPath, cb) { var rs = fs.createReadStream(srcPath); rs.on('error'.function(err) { if (err) { console.log('read error', srcPath); } cb && cb(err); }) var ws = fs.createWriteStream(tarPath); ws.on('error'.function(err) { if (err) { console.log('write error', tarPath); } cb && cb(err); }) ws.on('close'.function(ex) { cb && cb(ex); }) rs.pipe(ws); } Copy the code
-
Copy all files and subfolders in the source folder to the target folder
/ /! To recursively copy files and folders under srcDir to CD under tarDir is a callback var copyFolder = function(srcDir, tarDir, cb) { fs.readdir(srcDir, function(err, files) { var count = 0; var checkEnd = function() { ++count == files.length && cb && cb(); } if (err) { checkEnd(); return; } files.forEach(function(file) { var srcPath = path.join(srcDir, file); var tarPath = path.join(tarDir, file); fs.stat(srcPath, function(err, stats) { if (stats.isDirectory()) { console.log('mkdir', tarPath); fs.mkdir(tarPath, function(err) { if (err) { console.log(err); return; } copyFolder(srcPath, tarPath, checkEnd); }); } else{ copyFile(srcPath, tarPath, checkEnd); }}); });// Directly callback when empty files.length === 0 && cb && cb(); }); } Copy the code
-
-
Node.js is a back-end script written based on Node.js. How to run the back-end script in a Vue project? It is known that the Vue project is executed NPM run XXX and the command of copyfile.js is node copyfile.js
- Option 1: Open it in the same project
Two command line Windows
Performed, respectively,npm run xxx
和node copyFile.js
Effects work in the same project
- Option 1: Open it in the same project
-
Plan 1 is too troublesome to open two Windows and execute two commands. Is there a way to achieve the effect of plan 1 simply by executing the NPM run XXX command
-
NPM run XXX: executes scripts written in scripts in pakeage.json, which are the file names in node_modules/.bin
-
Solution 2: Configure multiple commands in package.json
If the command contains Spaces, use () to include the command. In addition && && and the role of the different.
"scripts": { "serve": "(node copyFile.js) && (vue-cli-service serve)"."start": "webpack & (ng serve)" }, Copy the code
- The ➊ command executed successfully. To perform first
node copyFile.js
Run the command after the command is executedvue-cli-service serve
- “The call by bananas was carried out simultaneously. At the same time to perform
webpack
As well asng serve
Two commands
- The ➊ command executed successfully. To perform first
-
Note: the above method will only copy the file if you want to modify it later. After executing the command to overwrite the target file, you need to use # sync-copydir. After executing the command, no prompt is displayed in the COMMAND line window. You need to check whether the file is overwritten successfully.