All files api.ts

87% Statements 87/100
58.33% Branches 7/12
50% Functions 5/10
87% Lines 87/100

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 1011x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x                 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x   4x   4x 4x 4x 4x 4x 1x 1x  
import Hexo from 'hexo';
import { join } from 'upath';
import * as cleaner from './clean';
import { Nullable } from './globals';
import { taskSafelink } from './gulp.safelink';
import { taskSeo } from './gulp.seo';
import * as pcopy from './post/copy';
import standaloneRunner from './post/standalone';
import { chain } from './utils/chain';
import noop from './utils/noop';
import scheduler from './utils/scheduler';
import { fetchConfig, getConfig, setConfig } from './_config';
 
class SBG {
  cwd: string;
  config = getConfig();
  setConfig = setConfig;
  getConfig = getConfig;
 
  /**
   * Static blog generator
   * @param cwd base folder
   */
  constructor(cwd: Nullable<string>, options?: Parameters<typeof setConfig>[0]) {
    if (!cwd) cwd = process.cwd();
    this.cwd = cwd;
    fetchConfig(cwd);
    options = Object.assign(this.config, options || {}, { cwd });
 
    this.config = setConfig(options);
    SBG.setApi(this);
    new scheduler();
  }
 
  static currentApI: SBG;
  static setApi(api: SBG) {
    this.currentApI = api;
  }
  static getApi() {
    return this.currentApI;
  }
 
  /**
   * run files ends with `standalone.js` inside source posts {@link standaloneRunner}
   * @returns
   */
  standalone = () => chain([{ callback: standaloneRunner }]);
 
  /**
   * Auto seo on public dir (_config_yml.public_dir) (run after generated)
   * @returns
   */
  seo = () => taskSeo(null, join(this.cwd, this.config.public_dir));
 
  /**
   * Copy all **src-post** to **source/_posts** (run before generate)
   * * see the method {@link pcopy.copyAllPosts}
   * @returns
   */
  copy = () => chain([{ callback: () => pcopy.copyAllPosts(undefined, this.config) }]);
 
  /**
   * Anonymize external links on public dir (_config_yml.public_dir) (run after generated)
   * @returns
   */
  safelink = () => taskSafelink(noop, join(this.cwd, getConfig().public_dir));
 
  /**
   * generate site with hexo
   */
  async generate() {
    const hexo = new Hexo(this.cwd);
    // hexo init
    await hexo.init().catch(noop);
    await hexo.load().catch(noop);
    // hexo generate
    await hexo.call('generate').catch(noop);
    await hexo.exit();
  }
 
  /**
   * clean cache, auto generated posts, etc
   * @see {@link cleaner.cleanDb}
   * @see {@link cleaner.cleanArchive}
   */
  async clean(opt?: 'all' | 'archive' | 'database' | 'post') {
    if (opt === 'all') {
      await cleaner.cleanDb().catch(console.log);
      await cleaner.cleanArchive().catch(console.log);
    } else if (opt === 'archive') {
      await cleaner.cleanArchive().catch(console.log);
    } else if (opt === 'post') {
      await cleaner.cleanGeneratedPosts().catch(console.log);
    } else {
      await cleaner.cleanDb().catch(console.log);
    }
  }
}
 
export default SBG;