Skip to content

Instantly share code, notes, and snippets.

@sapphi-red
Last active March 16, 2022 03:32
Show Gist options
  • Save sapphi-red/fe3f4f8a74f43bdb0db025dc955d5026 to your computer and use it in GitHub Desktop.
Save sapphi-red/fe3f4f8a74f43bdb0db025dc955d5026 to your computer and use it in GitHub Desktop.
rename use/* to composables/use*
import fs from 'node:fs/promises'
import path from 'node:path'
import { appendUse } from './utils'
export const convert = async (p: string): Promise<string[]> => {
const content = await fs.readFile(p, 'utf-8')
let replacedContent = content.replace(
/\/use\/(\w+)/g,
(_m, p1) => `/composables/${appendUse(p1)}`
)
if (path.basename(path.dirname(p)) === 'use') {
replacedContent = replacedContent.replace(
/(?<!\.)\.\/(\w+)/g,
(_m, p1) => `./${appendUse(p1)}`
)
}
await fs.writeFile(p, replacedContent, 'utf-8')
return []
}
import klaw from 'klaw'
import path from 'node:path'
import { convert } from './convert'
import { move } from './move'
const root = process.cwd()
const ignores = [
'.git',
'node_modules',
'dist',
'coverage'
]
const toRelative = (p: string) => path.relative(root, p)
const main = async () => {
const list = klaw(root, {
filter: p => !ignores.includes(path.basename(p)),
depthLimit: -1
})
const warningList: Array<{ file: string, messages: string[] }> = []
for await (const file of list) {
if (file.stats.isDirectory()) continue
if (!['.ts', '.vue'].includes(path.extname(file.path))) continue
console.log(`Processing: ${toRelative(file.path)}`)
const warnings = await convert(file.path)
if (warnings.length > 0) {
warningList.push({
file: file.path,
messages: warnings
})
}
}
await move(root)
console.log('\n\nFinished')
console.log('Warnings:')
for (const w of warningList) {
console.log(` ${toRelative(w.file)}`)
for (const m of w.messages) {
console.log(` ${m}`)
}
}
}
main()
import fs from 'node:fs/promises'
import path from 'node:path'
import { appendUse } from './utils'
const moveUseAndRename = async (beforePath: string) => {
const afterPath = path.resolve(beforePath, '../composables')
let stat: Awaited<ReturnType<typeof fs.stat>> | undefined
try {
stat = await fs.stat(afterPath)
} catch { /* not found */ }
if (stat) {
if (!stat.isDirectory()) {
throw new Error('non directory "composables" exists')
}
const fileCount = (await fs.readdir(afterPath)).length
if (fileCount > 0) {
throw new Error('directory "composables" exists')
}
await fs.rmdir(afterPath)
}
await fs.rename(beforePath, afterPath)
const files = await fs.readdir(afterPath)
const promises = files
.filter(p => path.extname(p) === '.ts')
.map(p => fs.rename(
path.resolve(afterPath, p),
path.resolve(afterPath, p.replace(/^(\w+).ts$/, (_, p1) => `${appendUse(p1)}.ts`))
))
await Promise.all(promises)
}
const doForDir = async (p: string) => {
const files = await fs.readdir(p)
for (const file of files) {
const absolutePath = path.resolve(p, file)
try {
const stat = await fs.stat(absolutePath)
if (!stat.isDirectory()) {
continue
}
} catch {
continue // not found
}
if (file === 'use') {
await moveUseAndRename(absolutePath)
} else if (file !== 'node_modules') {
await doForDir(absolutePath)
}
}
}
export const move = async (root: string) => {
await doForDir(root)
}
{
"name": "m",
"private": true,
"dependencies": {
"klaw": "^4.0.1"
},
"devDependencies": {
"@types/klaw": "^3.0.3"
}
}
{
"compilerOptions": {
"module": "esnext",
"lib": ["esnext"],
"target": "esnext",
"declaration": true,
"sourceMap": true,
"rootDir": ".",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"importsNotUsedAsValues": "error",
"moduleResolution": "node",
"baseUrl": ".",
"esModuleInterop": true,
"skipLibCheck": true
}
}
export const appendUse = (f: string) =>
`use${f[0]!.toUpperCase()}${f.slice(1)}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment