Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save thanh4890/0c5c63c981be0b8fee9219693e3f3afc to your computer and use it in GitHub Desktop.

Select an option

Save thanh4890/0c5c63c981be0b8fee9219693e3f3afc 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