All files gulp.deploy.ts

16.84% Statements 48/285
100% Branches 0/0
0% Functions 0/8
16.84% Lines 48/285

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 2861x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                       1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x 1x 1x                                                                                         1x                                                                   1x                                                                                                                         1x       1x                                                                                                                                                         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x  
import ansiColors from 'ansi-colors';
import Bluebird from 'bluebird';
import { existsSync } from 'fs';
import { default as gitHelper } from 'git-command-helper';
import { spawnAsync } from 'git-command-helper/dist/spawn';
import gulp, { TaskFunctionCallback } from 'gulp';
import moment from 'moment-timezone';
import { join, toUnix } from 'upath';
import './clean';
import './gulp.safelink';
import Logger from './utils/logger';
import { deployConfig, getConfig } from './_config';
 
/**
 * copy generated files (_config_yml.public_dir) to deploy dir (run after generated)
 * @returns
 */
export function copyGen() {
  const { deployDir } = deployConfig();
  const publicDir = join(process.cwd(), getConfig().public_dir);
  return gulp
    .src(['**/**', '!**/.git*', '!**/tmp/**', '!**/node_modules/**'], {
      cwd: publicDir,
      dot: true
    })
    .pipe(gulp.dest(deployDir))
    .on('error', console.trace);
}
 
/**
 * asynchronous copy generated files (_config_yml.public_dir) to deploy dir (run after generated)
 * @returns
 */
export function asyncCopyGen() {
  return new Bluebird(function (resolve) {
    copyGen().once('end', function () {
      resolve(null);
    });
  });
}
 
// copy public to .deploy_git
gulp.task('deploy:copy', copyGen);
 
/**
 * git pull on deploy dir
 * @param done
 */
async function pull(done: TaskFunctionCallback) {
  const config = getConfig();
  const cwd = config.deploy.deployDir;
  const gh = config.deploy.github;
  const doPull = async (cwd: string) => {
    try {
      await spawnAsync('git', ['config', 'pull.rebase', 'false'], {
        cwd
      });
    } catch (e) {
      // Logger.log(e.message, sub.root);
    }

    try {
      Logger.log('pulling', cwd);
      await spawnAsync('git', ['pull', '-X', 'theirs'], {
        cwd,
        stdio: 'pipe'
      });
    } catch (e) {
      Logger.log('cannot pull', cwd);
    }
  };

  const clone = async () => {
    if (!existsSync(cwd)) {
      await spawnAsync('git', [...'clone -b master --single-branch'.split(' '), config.deploy.repo, '.deploy_git'], {
        cwd: __dirname
      });
    }
  };

  await clone();
  doPull(cwd);
  if (gh) {
    const submodules = gh.submodule.get();
    for (let i = 0; i < submodules.length; i++) {
      const sub = submodules[i];

      doPull(sub.root);
    }
  }
  done();
}
 
function status(done?: gulp.TaskFunctionCallback) {
  const { github } = deployConfig();
  if (!github) return;
  github.status().then((statuses) => {
    statuses.map((item) => {
      let str = '';

      switch (item.changes) {
        case 'deleted':
          str += ansiColors.red(item.changes);
          break;
        case 'added':
          str += ansiColors.green(item.changes);
          break;
        case 'modified':
          str += ansiColors.yellow(item.changes);
          break;
        case 'untracked':
          str += ansiColors.grey(item.changes);
          break;
        default:
          str += item.changes;
          break;
      }

      str += ' ';
      str += item.path;
      Logger.log(str);
    });

    if (typeof done === 'function') done();
  });
}
 
function commit() {
  const { github } = deployConfig();
  if (!github) return Promise.resolve(null);
  const now = moment().tz(getConfig().timezone).format('LLL');
  const commitRoot = function () {
    return new Promise((resolve) => {
      github.status().then((changes) => {
        Logger.log('changes', changes.length, toUnix(github.cwd).replace(toUnix(process.cwd()), ''));
        if (changes.length > 0) {
          github.add('-A').then(() => {
            github.commit('update site ' + now).then(() => {
              resolve(null);
            });
          });
        } else {
          resolve(null);
        }
      });
    });
  };
  const commitSubmodule = function () {
    return new Promise((resolve) => {
      if (github.submodule.hasSubmodule()) {
        const info = github.submodule.get();
        const commitSubmoduleChild = async (sub: (typeof info)[number]) => {
          const submodule = new gitHelper(sub.root);
          const items = await submodule.status();
          if (items.length > 0) {
            await submodule.add('-A');
            await submodule.commit(`update ${sub.path} ${now}`, 'am');
          }
        };
        const iterate = function () {
          return new Promise((resolveIt) => {
            // resolve directly when submodule items no made changes
            if (info.length === 0) return resolveIt(null);
            commitSubmoduleChild(info[0])
              .catch(noop)
              .finally(() => {
                info.shift();
                // re-iterate when submodule items not committed
                if (info.length > 0) return iterate();
                // resolves all
                resolveIt(null);
              });
          });
        };
        iterate().then(() => {
          resolve(null);
        });
      }
    });
  };

  return new Promise((resolve) => {
    commitSubmodule()
      .then(commitRoot)
      .then(() => resolve(null));
  });
}
 
function noop() {
  //
}
 
function push() {
  const { github } = deployConfig();
  if (!github) {
    return;
  }
  const submodules = github.submodule.hasSubmodule() ? github.submodule.get() : [];
  const pushSubmodule = function (submodule: (typeof submodules)[number]) {
    const { url, branch, root } = submodule;
    if (!submodule.github) {
      submodule.github = new gitHelper(root);
    }
    const { github } = submodule;
    return new Promise((resolvePush) => {
      const setR = () => github.setremote(url);
      const setB = () => github.setbranch(branch);
      Promise.all([setR(), setB()]).then(() => {
        github.canPush().then((allowed) => {
          Logger.log(workspace(root), 'can push', allowed);
          if (allowed) {
            // push then resolve
            github.push(false, { stdio: 'pipe' }).then(resolvePush);
          } else {
            github.status().then((changes) => {
              if (changes.length > 0) {
                Logger.log('submodule', workspace(root), 'changes not staged');
                // resolve changes not staged
                resolvePush(null);
              } else {
                github.isUpToDate().then((updated) => {
                  Logger.log('submodule', workspace(root), 'updated', updated);
                  // resolve is up to date
                  resolvePush(null);
                });
              }
            });
          }
        });
      });
    });
  };
  const iterateSubmodule = function () {
    return new Promise((resolve) => {
      if (submodules.length === 0) {
        resolve(null);
      } else {
        const submodule = submodules.shift();
        if (submodule) {
          pushSubmodule(submodule).then(() => {
            if (submodules.length > 0) {
              iterateSubmodule().then(resolve);
            } else {
              resolve(null);
            }
          });
        } else {
          resolve(null);
        }
      }
    });
  };
  return new Promise((resolve) => {
    iterateSubmodule()
      .then(function () {
        return new Promise((resolvePush) => {
          github.canPush().then((allowed) => {
            Logger.log(workspace(github.cwd), 'can push', allowed);
            if (allowed) {
              github.push().then(resolvePush);
            }
          });
        });
      })
      .then(resolve);
    //pushSubmodule(submodules[1]).then(resolve);
  });
}
 
gulp.task('deploy:push', push);
gulp.task('deploy:status', status);
gulp.task('deploy:commit', commit);
gulp.task('deploy:pull', pull);
 
/**
 * get relative path from workspace
 * @param str
 * @returns
 */
function workspace(str: string) {
  return toUnix(str).replace(toUnix(process.cwd()), '');
}
 
// deploy
gulp.task('deploy', gulp.series('deploy:pull', 'deploy:copy', 'safelink', 'deploy:commit', 'deploy:push'));