plugins/watcher/watcher.js
function readAndHashFiles(files) {
    const fileHashes = {};
    const fileContents = {};
    const promises = [];

    function readSingleFile(file) {
        return new Promise((resolve, reject) => {
            fs.readFile(file, (err, data) => {
                if (err) {
                    console.log(err);
                    reject(err);
                    return;
                }
                const hash = crypto.createHash('sha1');
                hash.update(data);
                const bufferHash = hash.digest();
                fileHashes[file] = bufferHash.toString('hex');
                if (file.endsWith('.js') && !file.endsWith('.test.js')) {
                    fileContents[file] = data.toString('utf8');
                }
                resolve();
            });
        });
    }

    files.forEach((file) => {
        promises.push(readSingleFile(file));
    });

    return new Promise((resolve, reject) => {
        Promise.all(promises)
            .then(() => {
                resolve({ files, fileContents, fileHashes });
            })
            .catch(reject);
    });
}
function readAndHashFiles(files) {
    const fileHashes = {};
    const fileContents = {};
    const promises = [];

    function readSingleFile(file) {
        return new Promise((resolve, reject) => {
            fs.readFile(file, (err, data) => {
                if (err) {
                    console.log(err);
                    reject(err);
                    return;
                }
                const hash = crypto.createHash('sha1');
                hash.update(data);
                const bufferHash = hash.digest();
                fileHashes[file] = bufferHash.toString('hex');
                if (file.endsWith('.js') && !file.endsWith('.test.js')) {
                    fileContents[file] = data.toString('utf8');
                }
                resolve();
            });
        });
    }

    files.forEach((file) => {
        promises.push(readSingleFile(file));
    });

    return new Promise((resolve, reject) => {
        Promise.all(promises)
            .then(() => {
                resolve({ files, fileContents, fileHashes });
            })
            .catch(reject);
    });
}
function readAndHashFiles ( files ) { const fileHashes = { } ; const fileContents = { } ; const promises = [ ] ; function readSingleFile ( file ) { return new Promise ( ( resolve , reject ) => { fs . readFile ( file , ( err , data ) => { if ( err ) { console . log ( err ) ; reject ( err ) ; return ; } const hash = crypto . createHash ( 'sha1' ) ; hash . update ( data ) ; const bufferHash = hash . digest ( ) ; fileHashes [ file ] = bufferHash . toString ( 'hex' ) ; if ( file . endsWith ( '.js' ) && ! file . endsWith ( '.test.js' ) ) { fileContents [ file ] = data . toString ( 'utf8' ) ; } resolve ( ) ; } ) ; } ) ; } files . forEach ( ( file ) => { promises . push ( readSingleFile ( file ) ) ; } ) ; return new Promise ( ( resolve , reject ) => { Promise . all ( promises ) . then ( ( ) => { resolve ( { files , fileContents , fileHashes } ) ; } ) . catch ( reject ) ; } ) ; }
Reads files from disk and computes hashes for them.
@param {Array} files - array with file paths
@returns {Promise<any>}
Reads files from disk and computes hashes for them .