Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dariocravero/cf751c72917d314852fb73e950b9135f to your computer and use it in GitHub Desktop.
Save dariocravero/cf751c72917d314852fb73e950b9135f to your computer and use it in GitHub Desktop.
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