|
| 1 | +import { promises as fs } from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +const PACKAGE_FOLDER = process.env.PACKAGE_FOLDER || 'package'; |
| 5 | +const TARGETS = ['components', 'config', 'src', 'stores']; |
| 6 | +const BASE_DIR = path.join(process.cwd(), PACKAGE_FOLDER); |
| 7 | + |
| 8 | +const getFolder = async (folder, depth = 0) => { |
| 9 | + const contents = await fs.readdir(path.join(process.cwd(), PACKAGE_FOLDER, folder), { |
| 10 | + withFileTypes: true |
| 11 | + }); |
| 12 | + |
| 13 | + return { |
| 14 | + depth, |
| 15 | + folders: await Promise.all( |
| 16 | + contents |
| 17 | + .filter((f) => f.isDirectory()) |
| 18 | + .map((dir) => getFolder(`${folder}/${dir.name}`, depth + 1)) |
| 19 | + ), |
| 20 | + files: contents.filter((f) => !f.isDirectory()).map(({ name }) => `${folder}/${name}`) |
| 21 | + }; |
| 22 | +}; |
| 23 | + |
| 24 | +const transformFolder = async ({ folders, files, depth }) => { |
| 25 | + await Promise.all( |
| 26 | + files.map(async (f) => { |
| 27 | + const filepath = path.join(BASE_DIR, f); |
| 28 | + const contents = await fs.readFile(filepath, 'utf-8'); |
| 29 | + const base = depth ? [...Array(depth + 1).fill('..')].join('/') : './'; |
| 30 | + const updated = contents.replace(/from '\$lib(.*)'/g, `from '${base}$1'`); |
| 31 | + await Promise.all( |
| 32 | + [ |
| 33 | + updated === contents ? null : fs.writeFile(filepath, updated), |
| 34 | + ...folders.map(transformFolder) |
| 35 | + ].filter(Boolean) |
| 36 | + ); |
| 37 | + }) |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +Promise.all(TARGETS.map((t) => getFolder(t).then(transformFolder))); |
0 commit comments