use
- Front and rear end separate items
- After the front-end project is packaged, copy the corresponding packaged file (usually dist folder) to the server folder (usually public folder)
use
npm install
Copy the code
The command that
NPM run test: runs the copy command NPM run pmcopy: manages the copy of processes. (This command is not recommended.) NPM run build: directly runs the NPM run build in the VUE project for easy execution
For details about copy.js, see copy.js
Unresolved issues
After pM2 execution, some files in dist folder in VUE will need administrator permission to execute. Subsequent execution of NPM run build will report an error that access is not allowed, and pM2 needs to be killed before it can be removed. The cause has not been found
Here’s the code
copy.js
var fs = require('fs'),
stat = fs.stat;
var chokidar = require('chokidar');
/* 05 * Copy all files in the directory including subdirectories 06 * @param{String} Directory to be copied 07 * @param{String} Copy to the specified directory 08 */
var copy = function (src, dst) {
// Read all files/directories in the directory
fs.readdir(src, function (err, paths) {
if (err) {
throw err;
}
paths.forEach(function (path) {
var _src = src + '/' + path,
_dst = dst + '/' + path,
readable, writable;
stat(_src, function (err, st) {
if (err) {
throw err;
}
// Check whether it is a file
if (st.isFile()) {
// Create a read stream
readable = fs.createReadStream(_src);
// Create a write stream
writable = fs.createWriteStream(_dst);
// Stream through a pipe
readable.pipe(writable);
}
// Call itself recursively if it is a directory
else if(st.isDirectory()) { exists(_src, _dst, copy); }}); }); }); };// Check whether the directory exists before copying it. If the directory does not exist, create it first
var exists = function (src, dst, callback) {
fs.exists(dst, function (exists) {
/ / has been in existence
if (exists) {
callback(src, dst);
}
/ / does not exist
else {
fs.mkdir(dst, function () { callback(src, dst); }); }}); };// Delete all folders
function deleteall(path) {
let files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function (file, index) {
// console.log(file);
let curPath = path + "/" + file;
console.log(curPath);
if (fs.statSync(curPath).isDirectory()) { // recurse
deleteall(curPath);
} else { // delete filefs.unlinkSync(curPath); }}); fs.rmdirSync(path); }};// The source folder to copy
const dirFrom = './marketing.shenkonghao.UI/dist'
// Copy the target folder
const dirTo = './marketing-shenkonghao-node/app/public'
// const dirTo = './haha'
// Create a listener to listen for file changes
var watcher = chokidar.watch(dirFrom, {
ignored: / [\ / \ \] \. /, persistent: true
});
var log = console.log.bind(console);
watcher
.on('add'.function (path) {
// Delete the files in the target file
deleteall(dirTo)
// Copy directory files to target files
exists(dirFrom, dirTo, copy);
log('File', path, 'has been added');
})
.on('addDir'.function (path) { log('Directory', path, 'has been added'); })
.on('change'.function (path) {
log('File', path, 'has been changed');
// Delete the files in the target file
deleteall(dirTo)
// Copy directory files to target files
exists(dirFrom, dirTo, copy);
})
.on('unlink'.function (path) { log('File', path, 'has been removed'); })
.on('unlinkDir'.function (path) { log('Directory', path, 'has been removed'); })
.on('error'.function (error) { log('Error happened', error); })
.on('ready'.function () { log('Initial scan complete. Ready for changes.'); })
.on('raw'.function (event, path, details) { log('Raw event info:', event, path, details); })
Copy the code
Package. The json
{
"name": "shengkong"."version": "1.0.0"."description": "Process daemon, listens for file changes and copies files to target folder."."main": "copy.js"."scripts": {
"test": "node copy.js"."pmcopy":"pm2 start copy.js"."vuebuild":"cd ./marketing.shenkonghao.UI && npm run build"
},
"author": "shenqiang"."license": "ISC"."dependencies": {
"chokidar": "^ 3.5.1 track of"."fs": "0.0.1 ws-security"}}Copy the code