Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 7x 9x 4x 12x 13x 7x 7x 7x 6x 7x 7x 4x 1x 1x 1x 1x 2x 2x 1x 2x 2x 2x 2x 1x 1x | const yaml = require( 'js-yaml' ); const fs = require( 'fs' ); const path = require( 'path' ); const ConfigFile = require( './Config-file' ); const ESLintrc = require( './ESLintrc' ); /** * * @param {string} type */ function validateTypeName( type ) { const validTypes = [ 'appsscript', 'clasp', 'eslintrc', 'package' ]; if ( !validTypes.includes( type ) ) throw Error( `Type name ${type} is not one of accepted type names: ${validTypes.reduce( ( prev, cur ) => prev + '\n' + cur )}` ); } /** * * @param {string} ext */ function validateExtension( ext ) { const validExtensions = [ '', '.js', '.json', '.yml', '.yaml' ]; if ( !validExtensions.includes( ext ) ) throw Error( `File extension ${ext} is not one of accepted values: ${validExtensions.reduce( ( prev, cur ) => prev + '\n' + cur )}` ); } /** * @param {string} input input * @returns {string} Sanitized value * @example let str = '.eslintrc.json'; sanitizeTypeName(str) // => 'eslintrc's */ function formatTypeName( input ) { if ( !( typeof input === 'string' ) ) throw Error( 'Input must be string' ); let output = input.toLowerCase(); const ext = path.extname( input ); if ( ext ) { output = output.slice( 0, input.length - ext.length ); } if ( input[ 0 ] === '.' ) output = output.slice( 1 ); validateTypeName( output ); validateExtension( ext ); return output; } /** * @export * @description * @class Config */ class Config extends ConfigFile { // eslint-disable-line no-unused-vars /** * Creates an instance of Config. * @param {string} type * @memberof Config */ constructor( type ) { super(); this.formatTypeName = formatTypeName; this.type = this.formatTypeName( type ); } /** * * @param {Object} [object] A base configuration object to start from. * @returns * @memberof Config */ create( object ) { let base; if ( object ) base = object; else if ( this.importedObject ) base = this.importedObject; else base = null; return { // 'appsscript': obj => new AppsScript(obj), // 'clasp' : obj => new Clasp( obj ), 'eslintrc': obj => new ESLintrc( obj ) // 'package' : obj => new Package( obj ) }[ this.type ]( base ); } /** * Imports the config file from filepath and converts it to an object * @param {string} filepath The absolute path */ importFile( filepath ) { const fpath = checkExists( filepath ); if ( !fpath ) throw Error( 'File not found.' ); let extension = path.ext( fpath ); if ( extension === '' ) extension = '.json'; if ( extension === '.yml' ) extension = '.yaml'; validateExtension( extension ); const loaders = { // eslint-disable-next-line global-require '.js' : p => require( p ), '.json' : p => JSON.parse( fs.readFileSync( p, 'utf8' ) ), '.yaml' : p => yaml.safeLoad( fs.readFileSync( p, 'utf8' ) ) }; const load = loaders[ extension ]; const object = load( fpath ); this.importedObject = object; } } /** * Verifies a path * @param {string} string * @returns */ function normalizePath( string ) { Iif ( !path.isAbsolute( string ) ) { return path.resolve( process.cwd(), string ); } return string; } /** * @param {string} filepath * @returns {boolean} */ function checkExists( filepath ) { const fpath = normalizePath( filepath ); if ( fs.existsSync( fpath ) ) return fpath; return null; } // ANCHOR module.exports module.exports = Config; |