Created
September 8, 2022 09:17
-
-
Save rznewswav/63d48f90bd344cd3e55ef3fce75e9f1b 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
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