tmpdir.js (1692B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); 24 var ENV = require( '@stdlib/process/env' ); 25 26 27 // VARIABLES // 28 29 // Match a trailing slash... 30 var RE; 31 if ( IS_WINDOWS ) { 32 RE = /[^:]\\$/; 33 } else { 34 RE = /.\/$/; 35 } 36 37 38 // MAIN // 39 40 /** 41 * Returns the directory for storing temporary files. 42 * 43 * ## Notes 44 * 45 * - Introduced in io.js and Node v4. See the [ES5][1] and [ES6][2]. 46 * 47 * [1]: https://github.com/nodejs/node/blob/9cd44bb2b683446531306bbcff8739fc3526d02c/lib/os.js#L31 48 * [2]: https://github.com/nodejs/node/blob/5e5ec2cd1ee875ec5c837b5e0721d73628f12053/lib/os.js#L40 49 * 50 * @returns {string} directory for temporary files 51 * 52 * @example 53 * var dir = tmpdir(); 54 * // e.g., returns '/path/to/temporary/files/directory' 55 */ 56 function tmpdir() { 57 var tmp; 58 if ( IS_WINDOWS ) { 59 tmp = ENV.TEMP || 60 ENV.TMP || 61 (ENV.SystemRoot || ENV.windir || '') + '\\temp'; 62 } else { 63 tmp = ENV.TMPDIR || 64 ENV.TMP || 65 ENV.TEMP || 66 '/tmp'; 67 } 68 if ( RE.test( tmp ) ) { 69 tmp = tmp.slice( 0, -1 ); 70 } 71 return tmp; 72 } 73 74 75 // EXPORTS // 76 77 module.exports = tmpdir;