javascript - Node - go through folder of JS files and extract certain bits -


i'm maintaining document of tests spreadsheet - have 3 columns:

  • filename
  • text it block - mocha test it('does thing', function(){ ... })
  • a description - i'm writing.

however don't idea of time consuming maintain.

is possible automate this, i'm thinking npm package exists. run through folder, filenames , each js gather information after defined string regex. example text after //description , it( - this:

//description: text here it('does thing', function(){ ... })

any appreciated.

thanks.

use esprima ast, filter out test blocks want, generate source code escodegen

here code:

const fs = require('fs') const path = require('path') const esprima = require('esprima') const escodegen = require('escodegen')   function walksync (dir) {   return fs.statsync(dir).isdirectory()     ? array.prototype.concat(...fs.readdirsync(dir).map(f => walksync(path.join(dir, f))))     : dir }  const result = walksync('./test').map(path => {   const ast = esprima.parse(fs.readfilesync(path, 'utf8'))   ast.body = ast.body.filter(elem => {     return elem.type === 'expressionstatement'       && elem.expression && elem.expression.callee       && elem.expression.callee.name === 'it'   })   const code = escodegen.generate(ast)   return {     path,     code   } })  console.log(result) 

what test folder structure like: enter image description here

what test case like: enter image description here

what execution result like: enter image description here


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -