mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-11 13:56:15 -08:00
refactor(tooling): [MOD-PLUGIN-01] generate typed plugins with safe output handling
This commit is contained in:
1 parent
07d5bfef2e
commit
28c7abb2bb
32 files changed
+958
-161
No files matched your search
+6
-102
@@ -1,107 +1,11 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import process from 'node:process'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { createPlugin } from './cli.ts'
|
||||
|
||||
// Get the current file path
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
|
||||
// Get the plugin name from command line arguments
|
||||
const args = process.argv.slice(2)
|
||||
const pluginName = args[0]
|
||||
|
||||
if (!pluginName) {
|
||||
console.error('Please provide a plugin name, e.g., npm run create:plugin somePluginName')
|
||||
process.exit(1)
|
||||
try {
|
||||
createPlugin(fileURLToPath(new URL('../../', import.meta.url)), process.argv.slice(2))
|
||||
}
|
||||
|
||||
// Validate plugin name
|
||||
const pluginNameRegex = /^[a-z-]+$/
|
||||
if (!pluginNameRegex.test(pluginName)) {
|
||||
console.error('Invalid plugin name. Only lowercase letters and hyphens are allowed.')
|
||||
process.exit(1)
|
||||
catch (error) {
|
||||
console.error(error instanceof Error ? error.message : error)
|
||||
process.exitCode = 1
|
||||
}
|
||||
|
||||
// Convert plugin name to export name (PascalCase)
|
||||
const exportName = pluginName
|
||||
.split('-')
|
||||
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
||||
.join('')
|
||||
const exportNameFull = `artplayerPlugin${exportName}`
|
||||
|
||||
// Define paths
|
||||
const templateDir = path.join(__dirname, 'template')
|
||||
const destDir = path.join(__dirname, '../../packages', `artplayer-plugin-${pluginName}`)
|
||||
const exampleDir = path.join(__dirname, '../../docs/assets/example')
|
||||
const exampleFileName = `${pluginName.replace(/-/g, '.')}.js`
|
||||
const exampleFile = path.join(exampleDir, exampleFileName)
|
||||
|
||||
// Check if a plugin with the same name already exists
|
||||
if (fs.existsSync(destDir)) {
|
||||
console.error(`Plugin ${pluginName} already exists. Please choose another name.`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Create the destination directory
|
||||
fs.mkdirSync(destDir, { recursive: true })
|
||||
|
||||
// Read files from the template directory and copy them to the destination directory
|
||||
function copyTemplateFiles(templateDir, destDir) {
|
||||
const files = fs.readdirSync(templateDir)
|
||||
|
||||
files.forEach((file) => {
|
||||
const templateFilePath = path.join(templateDir, file)
|
||||
let destFilePath = path.join(destDir, file)
|
||||
|
||||
const stats = fs.statSync(templateFilePath)
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
fs.mkdirSync(destFilePath, { recursive: true })
|
||||
copyTemplateFiles(templateFilePath, destFilePath)
|
||||
}
|
||||
else {
|
||||
// Replace placeholders in the file name
|
||||
destFilePath = destFilePath.replace(/\{\{name\}\}/g, pluginName)
|
||||
|
||||
let content = fs.readFileSync(templateFilePath, 'utf8')
|
||||
|
||||
// Replace placeholders in the file content
|
||||
content = content.replace(/\{\{name\}\}/g, pluginName)
|
||||
content = content.replace(/\{\{export\}\}/g, exportNameFull)
|
||||
|
||||
fs.writeFileSync(destFilePath, content)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Create the example file
|
||||
function createExampleFile() {
|
||||
const exampleContent = `
|
||||
// npm i artplayer-plugin-${pluginName}
|
||||
// import ${exportNameFull} from 'artplayer-plugin-${pluginName}';
|
||||
|
||||
var art = new Artplayer({
|
||||
container: '.artplayer-app',
|
||||
url: '/assets/sample/video.mp4',
|
||||
plugins: [
|
||||
${exportNameFull}({
|
||||
//
|
||||
}),
|
||||
],
|
||||
});
|
||||
`.trim()
|
||||
|
||||
if (!fs.existsSync(exampleDir)) {
|
||||
fs.mkdirSync(exampleDir, { recursive: true })
|
||||
}
|
||||
|
||||
fs.writeFileSync(exampleFile, exampleContent)
|
||||
console.log(`Example file created at ${exampleFile}`)
|
||||
}
|
||||
|
||||
// Start copying template files and creating example file
|
||||
copyTemplateFiles(templateDir, destDir)
|
||||
createExampleFile()
|
||||
|
||||
console.log(`Plugin ${pluginName} has been successfully created at ${destDir}`)
|
||||
Reference in new issue
Block a user