samedi 14 mars 2020

Search a text between many files in Node.js

Using jest with node, I want to check if one of my files has a bad resolved GIT conflict. I did a code that works in my machine:

const cmd = `find . -not \\( -path ./node_modules -prune \\) -not \\( -path ./.next -prune \\) -not \\( -path ./.git -prune \\) -type f -print0 | xargs -0 grep -li "<<<< HEAD"`

describe('Git conflicts', () => {
  test('All conflicts should be resolved', () => {
    const result = require('child_process')
      .execSync(cmd)
      .toString('utf8')
    expect(result).toBe('./server/__tests__/check-git-conflicts.test.js\n')
  })
})

However, as I said, it works in my machine... But it doesn't work in different environments because I'm executing directly a UNIX command.

In order to do it more robust. I want to change that command:

find . -not \\( -path ./node_modules -prune \\) -not \\( -path ./.next -prune \\) -not \\( -path ./.git -prune \\) -type f -print0 | xargs -0 grep -li "<<<< HEAD

To write it in just node.js code to be more compatible with environments. I tried to use fs.readFile, but I failed in my try...

Does anybody know an efficient way in Node.js to search a text in many files?

Aucun commentaire:

Enregistrer un commentaire