From 77cecfcc5e2d9e7c8e3452fa8dedff7166907275 Mon Sep 17 00:00:00 2001 From: Netanel Basal Date: Tue, 10 Sep 2019 10:16:40 +0300 Subject: [PATCH] feat(lib): add migration script --- migration/run.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 migration/run.js diff --git a/migration/run.js b/migration/run.js new file mode 100644 index 0000000..6f762c8 --- /dev/null +++ b/migration/run.js @@ -0,0 +1,22 @@ +const hasUntilDestroy = /import\s*{\s*[^}]*untilDestroyed[^}]*}\s*from\s*("|')ngx-take-until-destroy\1(?=[^]*untilDestroyed\(\w*\)[^]*)/; +const catchImport = /import\s*{\s*[^}]*untilDestroyed[^}]*}\s*from\s*("|')ngx-take-until-destroy['|"];/; + +const glob = require('glob'); +const fs = require('fs'); + +const base = `src/app`; + +glob(`${base}/**/*.ts`, {}, function (er, files) { + files.forEach((path) => { + fs.readFile(path, 'utf8', function (err, text) { + if (hasUntilDestroy.test(text)) { + console.log(`Replaced ${path}`); + const result = text.replace(/((?:@\w*\([^]*\)[\n\s\r\t]*)(?=([\n\s\r\t]*export[\s\r\t]*class))\2)/, '@UntilDestroy()\n$1') + .replace(catchImport, `import { untilDestroyed, UntilDestroy } from '@ngneat/until-destroy';`); + fs.writeFile(path, result, 'utf8', function (err) { + if (err) return console.log(err); + }); + } + }); + }); +});