Skip to content

Instantly share code, notes, and snippets.

@rznewswav
Created September 8, 2022 09:17
Show Gist options
  • Save rznewswav/63d48f90bd344cd3e55ef3fce75e9f1b to your computer and use it in GitHub Desktop.
Save rznewswav/63d48f90bd344cd3e55ef3fce75e9f1b to your computer and use it in GitHub Desktop.
import { mkdirSync, readFileSync, writeFileSync } from 'fs';
import glob from 'glob';
import path from 'path';
const globAsync = (pattern: string): Promise<string[]> =>
new Promise<string[]>((res, rej) =>
glob(pattern, (err, matches) => (err ? rej(err) : res(matches))),
);
const paths = await globAsync('src/**/*.ts');
for (const filepath of paths) {
const content = readFileSync(filepath).toString();
console.log(':: Entering', filepath);
const updated = content.replace(
/^import.*from '\.\/?.*';/gim,
function (substring) {
const newImport = substring.substring(0, substring.length - 2) + ".js';";
console.log(':: Replacing', substring, 'to', newImport);
return newImport;
},
);
const newFilePath = path.join('dist', filepath);
const directory = path.resolve(newFilePath, '..');
const baseName = path.basename(newFilePath);
console.log(':: New directory:', directory);
const newFile = path.join(directory, baseName);
console.log(':: Writing to:', newFile);
mkdirSync(directory, {
recursive: true,
});
writeFileSync(newFile, updated);
console.log(':: Done');
console.log('::');
console.log('::');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment