time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

configdir.js (2564B)


      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 join = require( 'path' ).join;
     24 var ENV = require( '@stdlib/process/env' );
     25 var IS_WINDOWS = require( '@stdlib/assert/is-windows' );
     26 var PLATFORM = require( './../../platform' );
     27 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     28 var homedir = require( './../../homedir' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Returns a directory for user-specific configuration files.
     35 *
     36 * @param {string} [p] - path to append to a base directory
     37 * @throws {TypeError} first argument must be a string primitive
     38 * @returns {(string|null)} directory
     39 *
     40 * @example
     41 * var dir = configdir();
     42 * // e.g., returns '/Users/<username>/Library/Preferences'
     43 *
     44 * @example
     45 * var dir = configdir( 'appname/config' );
     46 * // e.g., returns '/Users/<username>/Library/Preferences/appname/config'
     47 */
     48 function configdir( p ) {
     49 	var append;
     50 	var home;
     51 	var dir;
     52 
     53 	if ( arguments.length ) {
     54 		if ( !isString( p ) ) {
     55 			throw new TypeError( 'invalid argument. Must provide a string primitive. Value: `' + p + '`.' );
     56 		}
     57 		append = p;
     58 	} else {
     59 		append = '';
     60 	}
     61 	if ( IS_WINDOWS ) {
     62 		// http://blogs.msdn.com/b/patricka/archive/2010/03/18/where-should-i-store-my-data-and-configuration-files-if-i-target-multiple-os-versions.aspx
     63 		// https://en.wikipedia.org/wiki/Environment_variable#Windows
     64 		dir = ENV[ 'LOCALAPPDATA' ] || ENV[ 'APPDATA' ];
     65 		return ( dir ) ? join( dir, append ) : null;
     66 	}
     67 	home = homedir();
     68 	if ( home === null ) {
     69 		return null;
     70 	}
     71 	if ( PLATFORM === 'darwin' ) {
     72 		// http://stackoverflow.com/questions/410013/where-do-osx-applications-typically-store-user-configuration-data
     73 		return join( home, 'Library', 'Preferences', append );
     74 	}
     75 	// http://www.pathname.com/fhs/
     76 	// http://www.pathname.com/fhs/pub/fhs-2.3.html
     77 	// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
     78 	dir = ENV[ 'XDG_CONFIG_HOME' ] || join( home, '.config' );
     79 	return join( dir, append );
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = configdir;