|
1 | 1 | const gulp = require('gulp') |
2 | 2 | const path = require('../../data/path') |
3 | 3 | const travis = require('is-travis') |
| 4 | +const execa = require('execa') |
4 | 5 | const directory = require('directory-exists') |
5 | 6 | const glob = require('fast-glob') |
6 | | -const log = require('fancy-log') |
7 | | -const color = require('ansi-colors') |
8 | | - |
9 | | -const execa = require('execa') |
| 7 | +const delay = require('delay') |
| 8 | +const semver = require('semver') |
10 | 9 |
|
11 | | -console.log(process.env) |
| 10 | +gulp.task('deploy:prepare-repository', async () => { |
| 11 | + // check CI environment |
| 12 | + if (!travis) { |
| 13 | + throw Error('Script can be executed only in Travis CI.') |
| 14 | + } |
12 | 15 |
|
13 | | -gulp.task('deploy', async () => { |
| 16 | + // check token |
14 | 17 | const token = process.env.GH_TOKEN |
15 | 18 |
|
16 | 19 | if (!token) { |
17 | 20 | throw Error('GitHub token is not present.') |
18 | 21 | } |
19 | 22 |
|
| 23 | + // clone repository into a deploy directory |
20 | 24 | const repository = `https://${token}@github.com/nodewell/nodewell.github.io.git` |
21 | 25 |
|
22 | | - // const { stdout } = await execa('git', ['clone', repository], { cwd: path.deploy }) |
23 | | - // const { stdout } = await execa('git', ['clone', repository, path.deploy]) |
24 | | - // console.log(stdout) |
25 | | - |
26 | | - // await execa('git', ['clone', repository, path.deploy]) |
27 | | - |
28 | | - // if (!travis) { |
29 | | - // throw Error('Script can be executed only in Travis CI.') |
30 | | - // } |
| 26 | + await execa('git', ['clone', repository, path.deploy]) |
| 27 | +}) |
31 | 28 |
|
| 29 | +gulp.task('deploy:prepare-content', async () => { |
| 30 | + // check build directory |
32 | 31 | const exists = await directory(path.build) |
33 | 32 |
|
34 | 33 | if (!exists) { |
35 | 34 | throw Error('Build directory doesn\'t exist.') |
36 | 35 | } |
37 | 36 |
|
38 | | - // const files = await glob('**/*.*', { cwd: path.build }) |
39 | | - const files = await glob(path.build + '/**/*.*') |
| 37 | + // gather files |
| 38 | + const files = await glob('**/*.*', { cwd: path.build }) |
40 | 39 |
|
41 | 40 | if (files.length === 0) { |
42 | 41 | throw Error('Build directory is empty.') |
43 | 42 | } |
44 | 43 |
|
45 | | - console.log(files) |
| 44 | + // clear deploy repository (delete all files) |
| 45 | + await execa('git', ['rm', '-r', '*'], { cwd: path.deploy }) |
46 | 46 |
|
47 | | - // const dir = await glob(path.build + '/') |
48 | | - // console.log(dir) |
| 47 | + // copy build files to deploy repository |
| 48 | + await gulp |
| 49 | + .src(files, { cwd: path.build }) |
| 50 | + .pipe(gulp.dest(path.deploy)) |
49 | 51 |
|
50 | | - // console.log(process.argv) |
51 | | - // log.info('Check "build" directory' + color.cyan('YEAH!')) |
| 52 | + // wait for a while for the files to be completely copied |
| 53 | + await delay(1000) |
52 | 54 |
|
53 | | - // log.info(process.argv) |
54 | | - await gulp |
| 55 | + // process passed next version number |
| 56 | + const arg = process.argv[process.argv.length - 1] || '' |
| 57 | + const version = arg.substring(2) |
| 58 | + |
| 59 | + if (semver.valid(version) === null) { |
| 60 | + throw Error('Version is invalid.') |
| 61 | + } |
| 62 | + |
| 63 | + // commit files with next version |
| 64 | + await execa('git', ['add', '.'], { cwd: path.deploy }) |
| 65 | + await execa('git', ['commit', '-m', `chore(release): ${version}`], { cwd: path.deploy }) |
55 | 66 | }) |
| 67 | + |
| 68 | +gulp.task('deploy:push-content', async () => { |
| 69 | + // push content to remote rpository |
| 70 | + await execa('git', ['push', '-u', 'origin', 'master'], { cwd: path.deploy }) |
| 71 | +}) |
| 72 | + |
| 73 | +gulp.task('deploy', gulp.series( |
| 74 | + 'deploy:prepare-repository', |
| 75 | + 'deploy:prepare-content', |
| 76 | + 'deploy:push-content' |
| 77 | +)) |
0 commit comments