借nodejs实现在windows自动化部署vue项目

it2023-08-02  70

在你的vue项目根目录新建deploy.js,内容如下:

const path = require('path') const NginxConfFile = require('nginx-conf').NginxConfFile const { exec } = require('child_process') const fs = require('fs') const { COPYFILE_EXCL } = fs.constants const appName = 'test' const nginxpath = 'E:/deploy/nginx-1.18.0/nginx-1.18.0' function createFile (resource, target) { // 通过使用 COPYFILE_EXCL,如果目标文件存在,则操作将失败。 fs.copyFileSync(resource, target, COPYFILE_EXCL) } exec(`cd ${path.resolve(__dirname)} && npm run build`, { encoding: 'utf8' }, (error, stdout, stderr) => { if (error) { console.error(`执行的错误: ${error}`) return } console.log(`${stdout}`) if (!fs.existsSync(`${nginxpath}/conf/confd/${appName}.conf`)) { createFile(`${nginxpath}/conf/confd/nginx.conf`, `${nginxpath}/conf/confd/${appName}.conf`) } NginxConfFile.create(`${nginxpath}/conf/confd/${appName}.conf`, function (err, conf) { if (err) { console.log(err) return } conf.on('flushed', function () { console.log('finished writing to disk') }) conf.nginx.server._add('location', '/') conf.nginx.server.location[1]._add('root', `${path.resolve(__dirname, 'dist')}`.split(path.sep).join('/')) conf.nginx.server.location[1]._add('index', 'index.html index.htm') conf.nginx.server._add('listen', '8081') conf.flush() }) }) exec(`cd ${path.resolve(nginxpath)} && start nginx`, { encoding: 'utf8' }, (error, stdout, stderr) => { if (error) { console.error(`执行的错误: ${error}`) } } )

然后node deploy.js, 就在windows本地部署好了你的项目, 这时你就可以输入localhost:8081,访问你的项目了

最新回复(0)