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 testit('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)
Comments
Post a Comment