all files / lib/ index.js

96.31% Statements 339/352
97.8% Branches 89/91
98.41% Functions 62/63
88.18% Lines 97/110
36 statements, 3 functions, 27 branches Ignored     
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452                                                                                                                         388×                                         40×           40×                                                           40× 16×                               41× 41×       40× 40×                                                                                                                                                                                                                                                                                                                                                                                                                                                                
import Promise from 'bluebird';
import values from 'lodash/values';
import each from 'lodash/each';
import map from 'lodash/map';
import isNil from 'lodash/isNil';
import moment from 'moment';
import fs from 'fs-extra';
Promise.promisifyAll(fs);
import rimraf from 'rimraf';
import glob from 'glob';
import log from './log';
import * as CONSTANTS from './constants';
import Config, {
  EVENTS,
} from './config';
import Url from './url';
import Render from './render';
import Plugin from './plugin/index';
import Theme from './theme';
import {
  createCollection,
} from './collection';
import File from './file';
 
export default class Yarn {
  constructor(rootPath) {
    /**
     * Expose config object on instance.
     * @type {Config}
     */
    this.config = new Config();
 
    Iif (isNil(rootPath)) {
      rootPath = this.config.findLocalDir();
    }
 
    // Update root path.
    this.config.setRoot(rootPath);
 
    /**
     * All files found in our source path.
     * Key is the full path to the file, value is the actual File object.
     * @type {Object.<string, File>}
     */
    this.files = Object.create(null);
 
    /**
     * Mapping of Collection IDs to the instance.
     * @type {Object.<string, Collection>}
     */
    this.collections = Object.create(null);
 
    /**
     * Class responsible for handling themes.
     * @type {Theme}
     */
    this.theme = new Theme();
    this.theme.setGetConfig(this.getConfig);
 
    /**
     * Site wide data available in all templates.
     * @type {Object.<string, Object>}
     */
    this.data = Object.create(null);
 
    /**
     * Expose collections.
     * @type {Object}
     */
    this.data.collections = Object.create(null);
 
    // Subscribe to when config is updated.
    this.config.on(EVENTS.CONFIG_UPDATED, this.configUpdated.bind(this));
 
    // Kick off leading config update.
    this.configUpdated();
  }
 
  /**
   * Returns our config instance.
   * @return {Object}
   */
  getConfig = () => {
    return this.config;
  };
 
  configUpdated() {
    Url.setSlugOptions(this.config.get('slug'));
 
    this.theme.update();
 
    // Configure template engine.
    Render.configureTemplate({
      paths: this.theme.config.path.templates
    });
 
    // Configure markdown engine.
    Render.configureMarkdown(this.config.get('remarkable'));
 
    /**
     * Expose site data from config file.
     * @type {Object}
     */
    this.data.site = this.config.get('site');
 
    // Update our collection configs.
    map(this.config.get('collections'), (collectionConfig, collectionName) => {
      let instance = createCollection(
        collectionName,
        collectionConfig,
        this.getConfig
      );
 
      this.collections[instance.id] = instance;
    });
  }
 
  loadPlugins() {
    const id = log.startActivity('Loading plugins.\t');
 
    // Built-in plugins.
    Plugin.loadFromPackageJson(
      Url.pathFromRoot('./'),
      this.config.get('plugins')
    );
    [
      this.theme.config.path.plugins, // Active theme plug-ins
      this.config.get('path.plugins') // Site plug-ins
    ].forEach(path => Plugin.loadFromDirectory(path));
 
    log.endActivity(id);
  }
 
  async readFiles() {
    const id = log.startActivity('Reading files.\t\t');
 
    const configPathSource = this.config.get('path.source');
 
    // Create an array of patterns that we should ignore when reading the source
    // files of the Yarn site from disk.
    // This primarily includes the '_config.yml' file as well as every path
    // directory that isn't our source path, primarily '_site', '_plugins',
    // and '_themes'.
    let ignorePatterns = values(CONSTANTS.YAML).concat(
      values(this.config.get('path')).filter(path => path !== configPathSource)
    );
 
    // Ignore package.json file as well.
    ignorePatterns.push('package.json');
 
    // Ignore StaticCollection paths as those are not processed at all.
    each(this.collections, collection => {
      if (collection.static) {
        ignorePatterns.push(collection.path);
      }
    });
 
    // Read all files from disk and get their file paths.
    let files = await Promise.fromCallback(cb => {
      glob(configPathSource + '/**/*', {
        // Do not match directories, only files.
        nodir: true,
        // Array of glob patterns to exclude from matching.
        ignore: ignorePatterns.map(path => `**/${path}/**`).concat(
          `${configPathSource}/node_modules/**`
        ),
        // Follow symlinks.
        follow: true
      }, cb);
    });
 
    files.map(filePath => {
      let sourceFile = new File(filePath, this.getConfig);
      this.files[sourceFile.id] = sourceFile;
    });
 
    // Populate every collection with its files.
    each(this.collections, collection => {
      collection.populate(this.files, this.collections);
 
      // Add collection data to our global data object.
      this.data.collections[collection.name] = collection.data;
    });
 
    log.endActivity(id);
 
    return Promise.resolve();
  }
 
  /**
   * Read theme files, processing any files that it needs to so that we get
   * the final path to the ready to write files.
   * @return {Proise} Promise object.
   */
  async readTheme() {
    let promise;
 
    // Reading theme files.
    const id = log.startActivity('Reading theme files.\t');
    try {
      promise = await this.theme.read();
    } catch (e) {
      log.error(e);
    }
    log.endActivity(id);
 
    // Expose theme data globally.
    this.data.theme = this.theme.data;
 
    return promise;
  }
 
  async loadState() {
    await this.readTheme();
 
    this.loadPlugins();
 
    await this.readFiles();
  }
 
  /**
   * Given a File we will write its contents and all related CollectionPages
   * that it exists to the destination directory.
   * @param {File} file File object.
   * @return {Promise} Promise containing all file system write promises.
   */
  async writeFile(file) {
    let promises = [];
 
    // For every collectionId the File belongs to rewrite that File within
    // the collection along with every CollectionPage.
    file.collectionIds.forEach(collectionId => {
      let collection = this.collections[collectionId];
      promises.push(collection.writeFile(file, this.data));
 
      collection.pages.forEach(page => {
        if (file.pageIds.has(page.id)) {
          promises.push(collection.writePage(page, this.data));
        }
      });
    });
 
    let promise;
    try {
      promise = await Promise.all(promises);
    } catch (e) {
      log.error(e);
    }
 
    return promise;
  }
 
  /**
   * Notify instance that a file changed so we can then update and re-write
   * the compiled file and associated collection page files.
   * @param {string} filePath File path.
   * @return {Promise} Promise object.
   */
  async fileChanged(filePath) {
    let file = this.files[filePath];
 
    if (isNil(file)) {
      return;
    }
 
    // Update our in memory File state with data from the file system.
    file.updateDataFromFileSystem();
 
    return this.writeFile(file);
  }
 
  /**
   * Notify instance that a file was added to the project so we can update and
   * re-write the compiled file and associated collection page files.
   * @param {string} filePath File path.
   * @return {Promise} Promise object resolving with created File object if one
   *   is created, or false if none is created.
   */
  async fileAdded(filePath) {
    let file = this.files[filePath];
 
    // Return early if File already exists.
    if (!isNil(file)) {
      return false;
    }
 
    file = new File(filePath, this.getConfig);
    this.files[file.id] = file;
 
    let shouldWriteFile = false;
 
    // Populate every collection with its files.
    each(this.collections, collection => {
      let fileAdded;
      // @TODO: support adding files to static collections.
      if (collection.addFile) {
        fileAdded = collection.addFile(file);
      }
 
      if (fileAdded) {
        shouldWriteFile = true;
        collection.createCollectionPages();
      }
    });
 
    // Write changes to file system.
    if (shouldWriteFile) {
      await this.writeFile(file);
    }
 
    return file;
  }
 
  /**
   * Notify instance that a file was removed from the project so we can update
   * and re-write the destination folder without the file and its associated
   * pages.
   * @param {string} filePath File path.
   * @return {Promise} Promise object resolving with true if the File was
   *   removed, false if it wasn't.
   */
  async fileRemoved(filePath) {
    let file = this.files[filePath];
 
    // Return early if File does not exist.
    if (isNil(file)) {
      return false;
    }
 
    // Remove File.
    delete this.files[file.id];
 
    let promises = [];
 
    // Delete file.
    promises.push(fs.removeAsync(file.destination));
 
    // For every collectionId the File belongs to rewrite that File within
    // the collection along with every CollectionPage.
    file.collectionIds.forEach(collectionId => E{
      let collection = this.collections[collectionId];
 
      // @TODO: support removing static files.
      if (collection.removeFile) {
        collection.removeFile(file);
      }
 
      // Update collection pages.
      collection.createCollectionPages();
 
      // Write all CollectionPages to disk.
      collection.pages.forEach(collectionPage => {
        promises.push(collection.writePage(collectionPage, this.data));
      });
    });
 
    try {
      await Promise.all(promises);
    } catch (e) {
      log.error(e);
    }
 
    return true;
  }
 
  /**
   * Removes configured destination directory and all files contained.
   * @return {Promise} Promise object.
   */
  async cleanDestination() {
    let promise;
 
    const id = log.startActivity(
      'Cleaning destination.\t'
    );
    try {
      promise = await Promise.fromCallback(cb => {
        rimraf(this.config.get('path.destination'), cb);
      });
    } catch (e) {
      log.error(e);
    }
 
    log.endActivity(id);
 
    return promise;
  }
 
  /**
   * Writes Theme files and assets to file system.
   * @return {Promise} Promise object.
   */
  async writeThemeFiles() {
    let promise;
 
    // Write theme static files.
    const id = log.startActivity('Writing theme files.\t');
    try {
      promise = await this.theme.write();
    } catch (e) {
      log.error(e);
    }
    log.endActivity(id);
 
    return promise;
  }
 
  /**
   * Builds the yarn site in its entirety.
   * @return {Promise} Promise of when we're done building.
   */
  async build() {
    let promise;
 
    if (this.config.get('clean_destination')) {
      promise = await this.cleanDestination();
    }
 
    this.data.yarn = Yarn.getYarnData();
 
    await this.writeThemeFiles();
 
    const id = log.startActivity('Writing files.\t\t');
    try {
      promise = await Promise.all(map(this.collections, collection => {
        return collection.write(this.data);
      }));
    } catch (e) {
      log.error(e.stack);
    }
    log.endActivity(id);
 
    return promise;
  }
 
  /**
   * Get information about the Yarn installation from its package.json.
   * @return {Object}
   */
  static getYarnData() {
    let packageJson = {};
    try {
      packageJson = require(Url.pathFromRoot('./package.json'));
    } catch (e) { /* swallow */ }
 
    return {
      version: packageJson.version,
      time: moment((new Date()).getTime()).format()
    };
  }
}