File: detector.js

package info (click to toggle)
grunt 1.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 688 kB
  • sloc: javascript: 3,775; makefile: 4
file content (66 lines) | stat: -rw-r--r-- 1,489 bytes parent folder | download | duplicates (6)
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
/**
 * Temporary - The lord of tmp.
 * 
 * Author: Veselin Todorov <hi@vesln.com>
 * Licensed under the MIT License.
 */
 
/**
 * Detection stolen from NPM (https://github.com/isaacs/npm/)
 * 
 * Copyright 2009, 2010, 2011 Isaac Z. Schlueter (the "Author")
 * MIT License (https://github.com/isaacs/npm/blob/master/LICENSE)
 */ 

/**
 * Detector namespace.
 * 
 * @type {Object}
 */
var detector = module.exports;

var normalize = function(path) {
  var last = path[path.length - 1];
  
  if (detector.platform() !== "win32") {
    if (last !== '/') {
      path += '/';
    }
  } else {
    //This is fine b/c Windows will 
    //correctly resolve filepaths with additional slashes
    //and it is not correct to assume that on Windows the value
    //of path will be a string that terminates in '\'.
    //
    //See: http://stackoverflow.com/questions/4158597/extra-slashes-in-path-variable-of-file-copy-or-directory-createdirectory-met
    //
    path += '/';
  }
  
  return path;
}

/**
 * Returns tmp dir. Thank you npm.
 * 
 * @returns {String} tmp dir.
 */
detector.tmp = function() {
  var temp = process.env.TMPDIR
      || process.env.TMP
      || process.env.TEMP
      || (detector.platform() === "win32" ? "c:\\windows\\temp\\" : "/tmp/")
  
  return normalize(temp);
};

/**
 * Returns the platform. Allows Tests to verify all behaviors.
 *
 * @returns {String} platform.
 */
detector.platform = function() {
  return process.platform;
};

detector._normalize = normalize;