First, his technique is skilled to let a person feel distressed
A Yang is a blogger enthusiast, and he updates several blogs almost every week.
He has mastered the following techniques for updating his blog:
- npm run build
- Open the
xftp
software - Go to the corresponding directory
- Delete the existing
html
directory - I packed it up
dist
Rename it to ‘HTML - in
xftp
Upload the newly packed directory
With this speed, what can’t you do without a mouse?
A Yang’s girlfriend nodded again and again.
For the sake of Yang’s happiness, I recommended himXX Kidney treasure "ssh2-sftp-client"
.
What is sSH2-SFTP-client?
As the name implies, the SSH2-SFTP-client is designed to:
Node. js is an SFTP client encapsulated in SSH2. Three key words:
- Node.js: OK, this is a language used by almost every front-end these days, and you are no exception.
- ssh2: SSH2 is not a protocol, but a protocol
node.js
为ssh2 client
(SSH2 client) implementation;github/ssh2 - SFTP client: short for SSH File Transfer Protocol, secure File Transfer Protocol
In a nutshell: Ssh2-SFTP-client encapsulates based on the SSH2 library, making methods related to file transfer simpler and supporting Promise.
That’s why I recommend sSH2-sFTP-client over ssh2 directly, because it’s more developer-friendly when it comes to file transfers!
Three, how to use?
Just like you would with an XFTP client. Really, exactly the same, do not believe to read….
1. Install dependencies
Just like when you download the.exe installation package
npm install ssh2-sftp-client -dev
# or
yarn add ssh2-sftp-client -D
Copy the code
2. Start the client
Just like you would double-click a shortcut
let Client = require('ssh2-sftp-client');
let sftp = new Client();
Copy the code
3. Create an FTP link
Just like when you create a new link in the XFTP client. You need to enter the remote host, port, username, and password.
await sftp.connect({
host: 'nn.nnn.nn.n'.port: '22'.username: 'root'.password: '* * * * * *'
})
Copy the code
4. Delete the old directory
Just like when you find the catalog on a visual page and delete it.
// Check if the directory exists
if (await client.exists(remotePath)) {
// delete 👴 if it exists
await client.rmdir(remotePath, true)
console.log('Deleted successfully')}Copy the code
The 5.Blog/Website
Copy and paste it
Just like your copy and paste (no, it’s easier than that)
let rslt = await client.uploadDir(localPath, remotePath);
console.log('Upload successful')
Copy the code
The script is finishedDo it!
Add a line to the scripts tag of package.json:
"deploy": "node ./scripts/deploy.js"
Copy the code
This way, after you execute YARN Build, just execute YARN deploy and your build will zip to the target path of the remote server
Don’t forget this step if your code is going to commit to Git
Git commit is a dangerous thing to do at any time.
So, create a.auth/ftp.json file in the project root directory and add it to.gitignore:
.auth
Copy the code
Ftp. json is used to store your sensitive information:
{
"host": "139.9.1 xx, XXX"."port": 22."user": "root"."password": "* * * * * * * * * *"
}
Copy the code
Then, instead of hard-coding the account password IP, add JSON to your script file scripts/deploy.js.
const config = require('.. /.auth/ftp.json')
await sftp.connect(config) // Very important! Everything safety first!
Copy the code
Five, send you the source code pushed by my blog
Concatenated codes: scripts/deploy. Js
let Client = require('ssh2-sftp-client');
let path = require('path')
let client = new Client();
const config = require('.. /.auth/ftp.json')
const localPath = path.resolve(__dirname, '.. /blog/.vuepress/dist')
const remotePath = '/usr/share/nginx/html'
const main = async() = > {try {
await client.connect(config);
if (await client.exists(remotePath)) {
await client.rmdir(remotePath, true)
console.log('Deleted successfully')}let rslt = await client.uploadDir(localPath, remotePath);
console.log('Upload successful')
return rslt;
} finally {
client.end();
}
}
main();
Copy the code
Vueuse: I am not allowed to be a Vuer, your toolset is only Lodash! Yalc: Probably the best front-end link debugging solution (already very modest)