mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-27 01:56:15 -04:00
feat: externalized tool configuration
This commit is contained in:
parent
c3adfe30ec
commit
690bd099ef
31 changed files with 387 additions and 300 deletions
85
buildModules/tool-config/index.ts
Normal file
85
buildModules/tool-config/index.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
import {readdirSync, readFileSync} from 'fs'
|
||||
import path, {join} from 'path'
|
||||
import {Module} from '@nuxt/types'
|
||||
import {NuxtRouteConfig} from '@nuxt/types/config/router'
|
||||
import YAML from 'yaml'
|
||||
import {capitalise} from '../../utils/string'
|
||||
|
||||
const toolDirName = 'tools'
|
||||
const rootDir = join(__dirname, '..', '..')
|
||||
const toolsDir = join(rootDir, toolDirName)
|
||||
|
||||
interface toolConfigModuleOptions {
|
||||
}
|
||||
|
||||
function getTools() {
|
||||
const categories = readdirSync(toolsDir)
|
||||
const toolList: { [key: string]: any[] } = {}
|
||||
|
||||
for (const category of categories) {
|
||||
const categoryDir = join(toolsDir, category)
|
||||
const categoryFormatted = capitalise(category)
|
||||
|
||||
toolList[categoryFormatted] = readdirSync(categoryDir).map((toolFileName) => {
|
||||
const toolPath = join(categoryDir, toolFileName)
|
||||
const contentMatch = readFileSync(toolPath, 'utf8').match(/<tool(\s[^>\s]*)*>([\S\s.]*?)<\/tool>/)
|
||||
|
||||
return contentMatch
|
||||
? {
|
||||
...YAML.parse(contentMatch[2]),
|
||||
componentPath: join(toolDirName, category, toolFileName)
|
||||
}
|
||||
: null
|
||||
}).filter(v => v !== null)
|
||||
}
|
||||
|
||||
return toolList
|
||||
}
|
||||
|
||||
const toolConfigModule: Module<toolConfigModuleOptions> = function () {
|
||||
const {nuxt, extendBuild, addPlugin} = this
|
||||
const toolList = getTools()
|
||||
const toolListFlat = Object.values(toolList).flat()
|
||||
|
||||
nuxt.hook('build:extendRoutes', (routes: NuxtRouteConfig[]) => {
|
||||
toolListFlat.forEach((toolConfig) => {
|
||||
const {path = '', title, componentPath} = toolConfig
|
||||
const name = title.toLowerCase().split(/\s/).join('-').replace(/\.vue$/, '')
|
||||
|
||||
const newRoute: NuxtRouteConfig = {
|
||||
name,
|
||||
path,
|
||||
component: join(rootDir, componentPath),
|
||||
chunkName: componentPath.replace(/\.vue$/, '')
|
||||
}
|
||||
|
||||
routes.push(newRoute)
|
||||
})
|
||||
|
||||
nuxt.options.publicRuntimeConfig.toolList = toolList
|
||||
})
|
||||
|
||||
extendBuild((config) => {
|
||||
if (!config.module) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('Failed to register the tool-config module.')
|
||||
return
|
||||
}
|
||||
|
||||
config.module.rules.push({
|
||||
resourceQuery: /blockType=tool/,
|
||||
loader: require.resolve('./loader.js')
|
||||
})
|
||||
})
|
||||
|
||||
addPlugin({
|
||||
src: path.resolve(__dirname, 'plugin.ts'),
|
||||
fileName: 'tool-config/plugin.ts',
|
||||
options: {
|
||||
toolList,
|
||||
toolListFlat
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default toolConfigModule
|
13
buildModules/tool-config/loader.js
Normal file
13
buildModules/tool-config/loader.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const YAML = require('yaml')
|
||||
|
||||
const loader = function (source, map) {
|
||||
this.callback(
|
||||
null,
|
||||
`export default function (Component) {
|
||||
Component.options.__toolConfig = ${JSON.stringify(YAML.parse(source))}
|
||||
}`,
|
||||
map
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = loader
|
16
buildModules/tool-config/plugin.ts
Normal file
16
buildModules/tool-config/plugin.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
// @ts-nocheck
|
||||
import {Plugin} from '@nuxt/types'
|
||||
import type {ToolRouteConfig} from '~/types/ToolConfig';
|
||||
|
||||
declare module 'vue/types/vue' {
|
||||
interface Vue {
|
||||
$toolListFlat: ToolRouteConfig[]
|
||||
$toolList: { [key: string]: ToolRouteConfig[] }
|
||||
}
|
||||
}
|
||||
|
||||
const plugin: Plugin = (_, inject) => {
|
||||
inject('toolListFlat', <%= serialize(options.toolListFlat) %>)
|
||||
inject('toolList', <%= serialize(options.toolList) %>)
|
||||
}
|
||||
export default plugin
|
Loading…
Add table
Add a link
Reference in a new issue