feat!: remove pointer files

This commit is contained in:
Elliot DeNolf
2024-04-12 11:31:35 -04:00
parent 94c0095b3b
commit 4f566b088c
4 changed files with 3 additions and 113 deletions

View File

@@ -22,7 +22,7 @@
} }
}, },
"scripts": { "scripts": {
"build": "pnpm copyfiles && pnpm build:swc && pnpm build:types && tsx ../../scripts/exportPointerFiles.ts ../packages/payload dist/exports", "build": "pnpm copyfiles && pnpm build:swc && pnpm build:types",
"build:swc": "swc ./src -d ./dist --config-file .swcrc", "build:swc": "swc ./src -d ./dist --config-file .swcrc",
"build:types": "tsc --emitDeclarationOnly --outDir dist", "build:types": "tsc --emitDeclarationOnly --outDir dist",
"build:watch": "nodemon --watch 'src/**' --ext 'ts,tsx' --exec \"pnpm build:tsc\"", "build:watch": "nodemon --watch 'src/**' --ext 'ts,tsx' --exec \"pnpm build:tsc\"",

View File

@@ -13,7 +13,7 @@
"directory": "packages/plugin-cloud-storage" "directory": "packages/plugin-cloud-storage"
}, },
"scripts": { "scripts": {
"build": "pnpm build:swc && pnpm build:types && tsx ../../scripts/exportPointerFiles.ts ../packages/plugin-cloud-storage dist/exports", "build": "pnpm build:swc && pnpm build:types",
"build:swc": "swc ./src -d ./dist --config-file .swcrc", "build:swc": "swc ./src -d ./dist --config-file .swcrc",
"build:types": "tsc --emitDeclarationOnly --outDir dist", "build:types": "tsc --emitDeclarationOnly --outDir dist",
"clean": "rimraf {dist,*.tsbuildinfo}", "clean": "rimraf {dist,*.tsbuildinfo}",

View File

@@ -14,7 +14,7 @@
"types": "./src/index.ts", "types": "./src/index.ts",
"type": "module", "type": "module",
"scripts": { "scripts": {
"build": "pnpm copyfiles && pnpm build:swc && pnpm build:types && tsx ../../scripts/exportPointerFiles.ts ../packages/richtext-lexical dist/exports", "build": "pnpm copyfiles && pnpm build:swc && pnpm build:types",
"build:swc": "swc ./src -d ./dist --config-file .swcrc", "build:swc": "swc ./src -d ./dist --config-file .swcrc",
"build:types": "tsc --emitDeclarationOnly --outDir dist", "build:types": "tsc --emitDeclarationOnly --outDir dist",
"build:clean": "find . \\( -type d \\( -name build -o -name dist -o -name .cache \\) -o -type f -name tsconfig.tsbuildinfo \\) -exec rm -rf {} + && pnpm build", "build:clean": "find . \\( -type d \\( -name build -o -name dist -o -name .cache \\) -o -type f -name tsconfig.tsbuildinfo \\) -exec rm -rf {} + && pnpm build",

View File

@@ -1,110 +0,0 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const [baseDirRelativePath] = process.argv.slice(2)
const [sourceDirRelativePath] = process.argv.slice(3)
// Base directory
const baseDir = path.resolve(__dirname, baseDirRelativePath)
const sourceDir = path.join(baseDir, sourceDirRelativePath)
const targetDir = baseDir
// Helper function to read directories recursively and exclude .map files
function getFiles(dir: string): string[] {
const subDirs = fs.readdirSync(dir, { withFileTypes: true })
const files = subDirs.map((dirEntry) => {
const res = path.resolve(dir, dirEntry.name)
if (dirEntry.isDirectory()) {
return getFiles(res)
} else {
// Exclude .map files
return res.endsWith('.map') ? [] : res
}
})
return Array.prototype.concat(...files)
}
function fixImports(fileExtension: string, content: string, depth: number): string {
const parentDirReference = '../'.repeat(depth + 1) // +1 to account for the original reference
const replacementPrefix = (depth === 0 ? './' : '../'.repeat(depth)) + 'dist/'
if (fileExtension === '.scss') {
// Adjust paths in @import statements for SCSS
content = content.replace(
new RegExp(`(@import\\s+['"])${parentDirReference}`, 'gs'),
`$1${replacementPrefix}`,
)
return content
}
// Adjust paths in require statements
content = content.replace(
new RegExp(`(require\\()(['"])${parentDirReference}(.*?)\\2`, 'gs'),
`$1$2${replacementPrefix}$3$2`,
)
content = content.replace(
new RegExp(`(require\\()(['"])\\.\\/${parentDirReference}(.*?)\\2`, 'gs'),
`$1$2${replacementPrefix}$3$2`,
)
// Adjust paths in import and export from statements
content = content.replace(
new RegExp(`(from\\s+['"])${parentDirReference}(.*?)\\1`, 'gs'),
`$1${replacementPrefix}$2$1`,
)
content = content.replace(
new RegExp(`(from\\s+['"])\\.\\/${parentDirReference}(.*?)\\1`, 'gs'),
`$1${replacementPrefix}$2$1`,
)
// Adjust paths in simpler export statements
content = content.replace(
new RegExp(`(export.*?\\s+from\\s+['"])${parentDirReference}(.*?['"])`, 'gs'),
`$1${replacementPrefix}$2`,
)
content = content.replace(
new RegExp(`(export.*?\\s+from\\s+['"])\\.\\/${parentDirReference}(.*?['"])`, 'gs'),
`$1${replacementPrefix}$2`,
)
return content
}
const calculateDepth = (pathy: string): number => {
const parts = pathy.split(path.sep)
// Find the index of "exports"
const exportsIndex = parts.indexOf('exports')
if (exportsIndex === -1) {
return -1 // "exports" directory not found in the path
}
// Calculate the depth by subtracting the number of parts up to and including "exports" from the total number of parts.
// Subtract 1 more to not count the file itself.
return parts.length - exportsIndex - 2
}
// Move files and adjust paths
getFiles(sourceDir).forEach((filePath) => {
const fileContent = fs.readFileSync(filePath, 'utf-8')
const relativePath = path.relative(sourceDir, filePath)
const targetPath = path.join(targetDir, relativePath)
// Calculate the depth to correctly adjust imports
const depth = calculateDepth(filePath)
// Create any non-existent directories
const dir = path.dirname(targetPath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
fs.writeFileSync(targetPath, fixImports(path.extname(filePath), fileContent, depth), 'utf-8')
})
console.log('Export pointer files moved and paths adjusted successfully.')