Forked from dariocravero/from-relative-to-absolute-imports.js
Created
December 14, 2024 18:42
-
-
Save thanh4890/0c5c63c981be0b8fee9219693e3f3afc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let glob = require('fast-glob'); | |
| let fs = require('fs').promises; | |
| let path = require('path'); | |
| let RELATIVE_IMPORT_ABOVE_FILE = /^import .+? '((\.\.\/)+)/; | |
| let isRelativeImportAboveFile = line => RELATIVE_IMPORT_ABOVE_FILE.test(line); | |
| let asAbsoluteImport = line => | |
| line.replace(line.match(RELATIVE_IMPORT_ABOVE_FILE)[1], ''); | |
| async function run() { | |
| let files = await glob(path.join(process.cwd(), 'src', '**/*.js')); | |
| await Promise.all( | |
| files.map(async file => { | |
| let source = await fs.readFile(file, 'utf8'); | |
| source = source | |
| .split('\n') | |
| .map(line => | |
| isRelativeImportAboveFile(line) ? asAbsoluteImport(line) : line | |
| ) | |
| .join('\n'); | |
| await fs.writeFile(file, source, 'utf8'); | |
| }) | |
| ); | |
| } | |
| run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment