all files / lib/ file.js

100% Statements 53/53
100% Branches 10/10
100% Functions 21/21
100% Lines 52/52
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                                216× 216×   216× 33×   183×     216× 216×                                                                                                                                                                                                                                                                                 31× 31× 31× 31×                                                       14× 14× 14×                 175×                                       214×          
'use strict'
 
let assert = require('assert')
let debug = require('debug')('mako-file')
let extension = require('file-extension')
let path = require('path')
let utils = require('mako-utils')
let uuid = require('uuid')
let Vinyl = require('vinyl')
 
/**
 * Represents a file within a build tree.
 *
 * @class
 */
class File extends Vinyl {
  /**
   * Sets up the instance.
   *
   * @param {Object} params  The vinyl params for this file.
   * @param {Tree} tree      The parent build tree.
   */
  constructor (params, tree) {
    assert(params, 'params needed to initialize a file')
    debug('initialize with %s', params)
 
    if (typeof params === 'string') {
      super({ path: params })
    } else {
      super(params)
    }
 
    if (!this.id) this.id = File.id()
    this.tree = tree
  }
 
  /**
   * Check to see if this file has the given path currently. (or did at some
   * point in it's history)
   *
   * @param {String} path  The absolute path to search for.
   * @return {Boolean} has
   */
  hasPath (path) {
    return this.history.indexOf(path) > -1
  }
 
  /**
   * Check to see if the `child` file is a dependency of this file.
   *
   * @see Tree#hasDependency()
   * @param {String} child  The file ID of the dependency.
   * @return {Boolean} has
   */
  hasDependency (child) {
    return this.tree.hasDependency(this.id, child)
  }
 
  /**
   * Adds the `child` as a dependency of this file. Returns the new `File`
   * instance.
   *
   * @see Tree#addDependency()
   * @param {String} child  The file ID of the dependency.
   */
  addDependency (child) {
    this.tree.addDependency(this.id, child)
  }
 
  /**
   * Removes the `child` as a dependency of this file.
   *
   * @see Tree#removeDependency()
   * @param {String} child  The file ID of the dependency.
   */
  removeDependency (child) {
    this.tree.removeDependency(this.id, child)
  }
 
  /**
   * Find the dependencies of this file.
   *
   * @see Tree#dependencies()
   * @param {Object} options  The search criteria.
   * @return {Array} files
   */
  dependencies (options) {
    return this.tree.dependenciesOf(this.id, options)
  }
 
  /**
   * Check to see if the `parent` file is a dependant of this file.
   *
   * @see Tree#hasDependant()
   * @param {String} parent  The file ID of the dependant.
   * @return {Boolean} has
   */
  hasDependant (parent) {
    return this.tree.hasDependant(this.id, parent)
  }
 
  /**
   * Adds the `parent` as a dependant of this file. Returns the new `File`
   * instance.
   *
   * @see Tree#addDependant()
   * @param {String} parent  The file ID of the dependant.
   */
  addDependant (parent) {
    this.tree.addDependant(this.id, parent)
  }
 
  /**
   * Removes the `parent` as a dependant of this file.
   *
   * @see Tree#removeDependant()
   * @param {String} parent  The file ID of the dependant.
   */
  removeDependant (parent) {
    this.tree.removeDependant(this.id, parent)
  }
 
  /**
   * Find the dependants of this file.
   *
   * @see Tree#dependants()
   * @param {Object} options  The search criteria.
   * @return {Array} files
   */
  dependants (options) {
    return this.tree.dependantsOf(this.id, options)
  }
 
  /**
   * Used to reset a file prior to re-running the analyze phase.
   */
  reset () {
    this.history.splice(1)
    this.contents = null
  }
 
  /**
   * Retrieves the current type for the file.
   *
   * @return {String} type
   */
  get type () {
    return extension(this.basename)
  }
 
  /**
   * Set the type/extension for this file.
   *
   * @param {String} type  The type (without the leading dot)
   */
  set type (type) {
    this.extname = `.${type}`
  }
 
  /**
   * Gets the initial path for this file.
   *
   * @return {String} path
   */
  get initialPath () {
    return this.history[0]
  }
 
  /**
   * Determine the original file type for this file (as if no transformations
   * have been run)
   *
   * @return {String} type
   */
  get initialType () {
    return extension(path.basename(this.initialPath))
  }
 
  /**
   * Creates a clone of this file.
   *
   * @param {Tree} tree  The tree to attach the clone to.
   * @return {File}
   */
  clone (tree) {
    debug('cloning %s', utils.relative(this.path))
    let clone = super.clone()
    clone.tree = tree || this.tree
    return clone
  }
 
  /**
   * Performs a deep copy of this file, making the following alterations after
   * successfully cloning:
   *  - a new id is generated (so it can be added to a tree as a different file)
   *  - uses `newPath` to update the path (relative to the current path)
   *
   * Available `options` include:
   *  - resetPath: if enabled, it will reset file.history with only the new path
   *
   * @param {String} newPath  The relative path for this new file.
   * @param {Object} options  Additional options.
   * @return {File} copy
   */
  copy (newPath, options) {
    assert(newPath, 'a new path is required to copy a file')
    debug('copying %s', utils.relative(this.path))
    let absolute = path.resolve(this.dirname, newPath)
    let copy = this.clone()
    copy.id = File.id()
    if (options && options.resetPath) {
      copy.history = [ absolute ]
    } else {
      copy.path = absolute
    }
    return copy
  }
 
  /**
   * Returns a trimmed object that can be serialized as JSON. It strips the tree
   * link and includes all other properties, including the custom ones.
   *
   * @return {File} obj
   */
  toJSON () {
    let clone = this.clone()
    delete clone.tree
    return clone
  }
 
  /**
   * Allow for easier logging.
   *
   * @return {String} str
   */
  toString () {
    return this.inspect()
  }
 
  /**
   * Used to parse a string value into a usable file.
   *
   * @static
   * @param {String} input  The raw JSON string to parse.
   * @param {Tree} tree     The tree to associate this file with.
   * @return {File}
   */
  static fromObject (input, tree) {
    let props = Object.assign({}, input, { path: null })
    props.contents = props._contents
    delete props._contents
    return new File(props, tree)
  }
 
  /**
   * Used to generate IDs, exposed as a static method to allow customization.
   *
   * @return {String} id
   */
  static id () {
    return uuid.v4()
  }
}
 
// single export
module.exports = File