Home > Web Front-end > JS Tutorial > body text

How to use shelljs to implement automated front-end deployment

不言
Release: 2018-08-18 11:15:53
Original
4114 people have browsed it

The content of this article is about how to use shelljs to realize automatic deployment of the front-end. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Scenario

In the front-end development process, you must often use the following commands:

npm run build
git add .
git commit -m 'commit'
git push
Copy after login

I am using vue -cliWhen writing my personal blog, deploy it to coding pages. The reason why github pages is not used is purely because it is slow. . . Every time I deploy, I have to type the above command again, which is really painful for me. It would be much better if you could perform all the above tasks with one command.

Don’t say much, just do what you say.

2. Shelljs

This library allows us to execute shell commands in js files. Please refer to the documentation for details.

Installation

npm install [-g] shelljs
Copy after login

There are two ways to use it, one is global mode (corresponding to global installation), and the other is local mode. Look at the use case below to know the difference between the two.

3. Use

to create a new file shell.js in the root directory. The content is as follows:

//局部模式
var shell = require('shelljs');
//全局模式下,就不需要用shell开头了。
//require('shelljs/global');

if (shell.exec('npm run build').code !== 0) {//执行npm run build 命令
  shell.echo('Error: Git commit failed');
  shell.exit(1);
}

//由于我的用另外一个仓库存放dist目录,所以这里要将文件增量复制到目标目录。并切换到对应目录。
shell.cp ('-r', './dist/*', '../../Rychou');
shell.cd('../../Rychou');

shell.exec('git add .');
shell.exec("git commit -m 'autocommit'")
shell.exec('git push')
Copy after login

At this time, Just execute node shell.js in the root directory.

This is just the simplest use case.

4. Make it more convenient

Add in package.json:

"script":{
+    "push":"node ./shell.js"
}
Copy after login

in the root directory Execute npm run push and it will be done.

Related recommendations:

Automated deployment of Hadoop cluster based on Kickstart

php Laravel method to achieve deployment automation

php5.3.10 Automated Deployment Script First Edition_PHP Tutorial

The above is the detailed content of How to use shelljs to implement automated front-end deployment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template