|
| 1 | +/* eslint-disable quotes, no-console */ |
| 2 | +/** |
| 3 | + * @source https://blog.logrocket.com/build-deploy-vue-js-app-github-pages/ |
| 4 | + * @modified by https://github.com/dazecoop/github-page-deploy |
| 5 | + */ |
| 6 | + |
| 7 | +// UPDATE THE FOLLOWING TO YOUR PROJECT NAME WITHIN GITHUB |
| 8 | +// eg https://dazecoop.github.io/PROJECT_NAME/ <- This bit |
| 9 | +const projectName = 'PROJECT_NAME'; |
| 10 | + |
| 11 | +const execa = require('execa'); |
| 12 | +const fs = require('fs'); |
| 13 | + |
| 14 | +// List of files, regex find & replaces to perform for Github pages |
| 15 | +const replaces = [ |
| 16 | + { |
| 17 | + file: './src/router.js', |
| 18 | + find: "mode: 'history'", |
| 19 | + replace: "mode: 'hash'", |
| 20 | + }, |
| 21 | + { |
| 22 | + file: './vue.config.js', |
| 23 | + find: "publicPath: '/'", |
| 24 | + replace: `publicPath: '/${projectName}/'`, |
| 25 | + }, |
| 26 | +]; |
| 27 | + |
| 28 | +(async () => { |
| 29 | + try { |
| 30 | + // Perform file replaces in prep for GitHub page deploy |
| 31 | + replaces.forEach(({ file, find, replace }) => { |
| 32 | + fs.readFile(file, function (err, data) { |
| 33 | + if (err) throw err; |
| 34 | + const regex = new RegExp(find, 'gm'); |
| 35 | + data = data.toString(); |
| 36 | + data = data.replace(regex, replace); |
| 37 | + fs.writeFile(file, data, function (err) { |
| 38 | + err; |
| 39 | + }); |
| 40 | + }); |
| 41 | + }) |
| 42 | + |
| 43 | + await execa('git', ['checkout', '--orphan', 'gh-pages']); |
| 44 | + // eslint-disable-next-line no-console |
| 45 | + console.log('Building started...'); |
| 46 | + await execa('npm', ['run', 'build']); |
| 47 | + // Understand if it's dist or build folder |
| 48 | + const folderName = fs.existsSync('dist') ? 'dist' : 'build'; |
| 49 | + await execa('git', ['--work-tree', folderName, 'add', '--all']); |
| 50 | + await execa('git', ['--work-tree', folderName, 'commit', '-m', 'gh-pages']); |
| 51 | + console.log('Pushing to gh-pages...'); |
| 52 | + await execa('git', ['push', 'origin', 'HEAD:gh-pages', '--force']); |
| 53 | + await execa('rm', ['-r', folderName]); |
| 54 | + await execa('git', ['checkout', '-f', 'master']); |
| 55 | + await execa('git', ['branch', '-D', 'gh-pages']); |
| 56 | + |
| 57 | + // Revert file replaces that we did earlier |
| 58 | + replaces.forEach(({ file, find, replace }) => { |
| 59 | + fs.readFile(file, function (err, data) { |
| 60 | + if (err) throw err; |
| 61 | + const regex = new RegExp(replace, 'gm'); |
| 62 | + data = data.toString(); |
| 63 | + data = data.replace(regex, find); |
| 64 | + fs.writeFile(file, data, function (err) { |
| 65 | + err; |
| 66 | + }); |
| 67 | + }); |
| 68 | + }) |
| 69 | + |
| 70 | + console.log('Successfully deployed, check your settings'); |
| 71 | + } catch (e) { |
| 72 | + // eslint-disable-next-line no-console |
| 73 | + console.log(e.message); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | +})(); |
0 commit comments