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 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 | 3x 3x 3x 3x 23x 23x 23x 23x 23x 23x 1x 7x 6x 6x 11x 8x 8x 4x 3x 3x 3x 1x 6x 6x 1x 1x 2x 2x 1x 1x 6x 6x 1x 1x 5x 5x 1x 1x 4x 4x 1x 1x 7x 7x 7x 1x 7x | import fs from 'fs'; import path from 'path'; import { getLogger } from './logger'; /** * This is the util class for the worker. * It handles the path operation, the load and save of the packages (json) and provides a simple way of checking the passed cli args. */ export class Utils { /** * Path to the package.json file. * * @private */ private readonly packageJsonPath: string; /** * Path to the selected environment json file. * * @private */ private readonly environmentJsonPath: string; /** * Path to the backup file which is made of the original package.json. * * @private */ private readonly backupJsonPath: string; /** * The logger. * * @private */ private readonly logger = getLogger('utils'); /** * The class constructor. * * @param parsedArgs The parsed arguments passed via cli */ constructor(private parsedArgs: any) { const rootFolder = process.cwd(); this.packageJsonPath = path.join(rootFolder, 'package.json'); this.environmentJsonPath = path.join( rootFolder, this.parsedArgs.env_dir, `${this.parsedArgs.environment}.json`, ); this.backupJsonPath = path.join(rootFolder, this.parsedArgs.env_dir, this.parsedArgs.backup_name); } /** * Get the selected environment name. * * @returns The environment name */ public getEnvironment(): string { return this.parsedArgs.environment; } /** * Checks if the environment should be resetted. Means: If the original package.json should be restored. * * @returns True / False */ public isResetEnvironment(): boolean { return this.parsedArgs.environment.toLowerCase() == '__reset__'; } /** * Checks if the user wishes to replace the original package.json with the environmental one instead of merging it. * * @returns True / False */ public isReplace(): boolean { return this.parsedArgs.replace; } /** * Checks if the user wishes to include a marker in the package.json to identify the change by the Node Package Builder. * * @returns True / False */ public isIncludeEnvironment(): boolean { return this.parsedArgs.include_environment; } /** * Checks if the user wished only a dry run. Means: No changes are made; just display the changes which would have been made in the console. * * @returns True / False */ public isDryRun(): boolean { return this.parsedArgs.dry_run; } /** * Checks for verbose level 1. * * @returns True / False */ public isVerboseL1(): boolean { return this.parsedArgs.verbose >= 1; } /** * Checks for verbose level 2. * * @returns True / False */ public isVerboseL2(): boolean { return this.parsedArgs.verbose >= 2; } /** * Checks if a backup of the package.json exists. * * @returns True / False */ public isBackupExisting(): boolean { return fs.existsSync(this.backupJsonPath); } /** * Replace the generated package.json with the original one. */ public restoreBackup() { try { fs.copyFileSync(this.backupJsonPath, this.packageJsonPath); fs.unlinkSync(this.backupJsonPath); } catch (e) { this.logError(`Could not find backup file '${this.backupJsonPath}'!`, e); } } /** * Loads the package.json if possible. If not: Exits the application with exit code 1. * * @returns The package.json content as string */ public loadPackageJson(): string { try { return fs.readFileSync(this.packageJsonPath, 'utf-8'); } catch (e) { this.logError( 'Could not load "package.json". Ensure you\'re running this command fron the root of your project.', e, ); throw new Error('exit'); } } /** * Loads the backup package.json if possible. If not: Exits the application with exit code 1. * * @returns The original package.json content as string */ public loadBackupJson(): string { try { return fs.readFileSync(this.backupJsonPath, 'utf-8'); } catch (e) { this.logError('Could not load backup file ".package.json"...', e); throw new Error('exit'); } } /** * Loads the selected environment json file if possible. If not: Exits the application with exit code 1. * * @returns The environment content as string */ public loadEnvironmentJson(): string { try { return fs.readFileSync(this.environmentJsonPath, 'utf-8'); } catch (e) { this.logError(`Could not load environment file "${this.environmentJsonPath}"...`, e); throw new Error('exit'); } } /** * Do the backup of the original package.json. */ public backupPackageJson() { try { fs.copyFileSync(this.packageJsonPath, this.backupJsonPath); } catch (e) { this.logError('Could not make backup of "package.json"...', e); throw new Error('exit'); } } /** * Saves the new (merged) package.json. * * @param mergedPackage The content for the new package.json */ public savePackageJson(mergedPackage: any) { try { fs.writeFileSync(this.packageJsonPath, JSON.stringify(mergedPackage, null, 2), 'utf-8'); } catch (e) { this.logError('Could not save new "package.json"...', e); throw new Error('exit'); } } /** * Logs errors. The output is dependent of the verbose level. The higher the more will be written to the console. * * @param msg The message to display * @param e The origin error object * * @private */ private logError(msg: string, e: any) { this.logger.error(msg); if (this.isVerboseL1()) this.logger.debug('Source error message:', e.message); if (this.isVerboseL2()) this.logger.debug('Source error:', e); } /** * Used for debugging purposes. (Prints the parsed cli parameters to console.) */ public verboseParameters() { for (const key of Object.keys(this.parsedArgs)) { this.logger.info(`Key: ${key} ==> Value: ${this.parsedArgs[key]}`); } } } |