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 | 1x 1x 24x 3x 36x 36x 14x 35x 35x 24x 24x 21x 14x 16x 1x | 'use strict'; const fs = require( 'fs' ); const path = require( 'path' ); /** * @ignore * @param {string} name * @throws {Error} If name cannot be used for a file. */ function validateName( name ) { if ( // Empty name === '' || // Only dots /^\.+$/.test( name ) || // Traversal on different operating systems /[/?<>\\:*|":]/.test( name ) ) { throw new Error( `Invalid file name: "${name}"` ); } } /** * Represents a directory and an (optional) prefix for files and * subdirectories created within it. * * @class */ class Writer { /** * The specified directory will be created if needed. * Any parent directories must exist beforehand. * * @param {string} dir File path * @param {string} prefix * @throws {Error} If directory can't be created. */ constructor( dir, prefix = '' ) { // Create as needed try { fs.accessSync( dir, fs.constants.W_OK ); } catch ( err ) { fs.mkdirSync( dir ); } /** * @private * @property {string} */ this.dir = dir; /** * @private * @property {string} */ this.namePrefix = prefix; } /** * Get the file path for a resource in this writer's directory. * * @param {string} name File name * @return {string} File path */ getPath( name ) { const segment = this.namePrefix + name; validateName( segment ); return path.join( this.dir, segment ); } /** * Create a Writer object for the same directory, with * an added prefix for any files and subdirectories. * * @param {string} prefix * @return {Writer} */ prefix( prefix ) { return new Writer( this.dir, this.namePrefix + prefix ); } /** * Create a Writer object for a subdirectory of the current one. * * @param {string} name * @return {Writer} */ child( name ) { return new Writer( this.getPath( name ) ); } } module.exports = Writer; |