Skip to content

Instantly share code, notes, and snippets.

@rchod
Last active April 3, 2020 21:31
Show Gist options
  • Save rchod/b9c985913b5e3381d2fe26b498bbf728 to your computer and use it in GitHub Desktop.
Save rchod/b9c985913b5e3381d2fe26b498bbf728 to your computer and use it in GitHub Desktop.
// to be run from current directory (e.g app/src/ )
const fs = require('fs');
const path = require('path');
const REGEX = /beforeEach\(async\((.+?)\)\.compileComponents\(\);\n\s+}\)\);$/gms;
function readDirR(dir) {
return fs.statSync(dir).isDirectory()
? Array.prototype.concat(
...fs.readdirSync(dir).map(f => readDirR(path.join(dir, f))),
)
: dir;
}
readDirR(__dirname).forEach(filePath => {
if (filePath.endsWith('spec.ts')) {
fs.readFile(filePath, { encoding: 'utf-8' }, function(err, data) {
if (!err && data.match(REGEX)) {
data = data.replace(
/import { async } from \'@angular\/core\/testing\';/gm,
'',
);
// maybe there's a better way to do this but this works for now
data = data.replace(/{\sasync,/gm, '{');
data = data.replace(/,\sasync\s}/gm, '}');
data = data.replace(/,\sasync\s?,/gm, ',');
data = data.replace(/\sasync,\n/gm, '');
data =
`import { configureTestSuite } from 'ng-bullet';\n` +
data.replace(REGEX, 'configureTestSuite($1)})');
data = data.replace(
/beforeEach\(async\((.+})\)\);$/gms,
'beforeEach($1);',
);
fs.writeFile(filePath, data, function(err) {
console.log(filePath, 'The file was saved!');
});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment