|
| 1 | +const gulp = require('gulp'); |
| 2 | +const sri = require('gulp-sri'); |
| 3 | +const branch = require('git-branch'); |
| 4 | +const pkgVersion = require('./package.json').version; |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +// S3 CLIENT CONFIG |
| 8 | +const s3 = require('gulp-s3-upload')({ useIAM:true }); |
| 9 | + |
| 10 | +// DEPLOYMENT CONFIGURATION OPTIONS |
| 11 | +const source = ['dist/*.js', 'dist/*.map', 'dist/*.json']; // source for deploy |
| 12 | +const sourceSRI = ['dist/*.js', 'dist/*.map']; // source for sri generation |
| 13 | +const Bucket = 'static.filestackapi.com' // upload bucked |
| 14 | +const ACL = 'public-read'; // upload acl |
| 15 | +const deployPath = 'filestack-react'; // upload path |
| 16 | +const deploymentBranch = 'master'; // branch for upload production version |
| 17 | +const cacheControll = { // cache controll for each version |
| 18 | + latest: 1, |
| 19 | + version: 30, |
| 20 | + beta: 0, |
| 21 | +}; |
| 22 | + |
| 23 | +// HELPERS |
| 24 | +const getDeployPath = (inputPath, version) => `${deployPath}/${version}/${path.basename(inputPath)}`; |
| 25 | +const getMajorVersion = (version) => version.split('.')[0]; |
| 26 | + |
| 27 | +// GENERATE SRI TAG |
| 28 | +gulp.task('sri', () => { |
| 29 | + return gulp.src(sourceSRI) |
| 30 | + .pipe(sri({ |
| 31 | + fileName: 'dist/manifest.json', |
| 32 | + transform: (o) => { |
| 33 | + let newOb = {}; |
| 34 | + for (const el in o) { |
| 35 | + newOb[el.replace('dist/', '')] = { sri: o[el] }; |
| 36 | + }; |
| 37 | + |
| 38 | + return newOb; |
| 39 | + } |
| 40 | + })) |
| 41 | + .pipe(gulp.dest('.')); |
| 42 | +}); |
| 43 | + |
| 44 | +// DEPLOYMENTS |
| 45 | +gulp.task('publish:beta', () => { |
| 46 | + return gulp.src(source) |
| 47 | + .pipe(s3({ |
| 48 | + Bucket, |
| 49 | + ACL, |
| 50 | + CacheControl: `max-age=${cacheControll.beta * 86400}`, |
| 51 | + keyTransform: (path) => getDeployPath(path, 'beta'), |
| 52 | + })); |
| 53 | +}); |
| 54 | + |
| 55 | +gulp.task('publish:latest', () => { |
| 56 | + return gulp.src(source) |
| 57 | + .pipe(s3({ |
| 58 | + Bucket, |
| 59 | + ACL, |
| 60 | + CacheControl: `max-age=${cacheControll.latest * 86400}`, |
| 61 | + keyTransform: (path) => getDeployPath(path, `${getMajorVersion(pkgVersion)}.x.x`), |
| 62 | + })); |
| 63 | +}); |
| 64 | + |
| 65 | +gulp.task('publish', gulp.series(() => { |
| 66 | + const currentBranch = branch.sync(); |
| 67 | + console.info(`Current branch is "${currentBranch}"`) |
| 68 | + |
| 69 | + if (currentBranch !== deploymentBranch) { |
| 70 | + return gulp.start('publish:beta'); |
| 71 | + } |
| 72 | + |
| 73 | + return gulp.src(source) |
| 74 | + .pipe(s3({ |
| 75 | + Bucket, |
| 76 | + ACL, |
| 77 | + CacheControl: `max-age=${cacheControll.version * 86400}`, |
| 78 | + keyTransform: (path) => getDeployPath(path, pkgVersion), |
| 79 | + })); |
| 80 | +}, () => { |
| 81 | + const currentBranch = branch.sync(); |
| 82 | + if (currentBranch !== deploymentBranch) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + return gulp.start('publish:latest'); |
| 87 | +})); |
0 commit comments