bfg-common 1.1.26 → 1.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const path = require('path')
|
|
4
|
+
import {
|
|
5
|
+
replaceFileContent,
|
|
6
|
+
removeNonImageFiles,
|
|
7
|
+
removeImagesFromContent,
|
|
8
|
+
updateImagesPathAndCustomComponents,
|
|
9
|
+
} from './utils/methods'
|
|
10
|
+
|
|
11
|
+
// const npmDev = (): void => {
|
|
12
|
+
// const contentPath = path.join(
|
|
13
|
+
// __dirname.replace('/node_modules/bfg-common/modules/fixContentBuild', ''),
|
|
14
|
+
// 'content',
|
|
15
|
+
// 'help'
|
|
16
|
+
// )
|
|
17
|
+
// // Заменяем кастомные тэги
|
|
18
|
+
// updateCustomComponents(contentPath)
|
|
19
|
+
// }
|
|
20
|
+
|
|
21
|
+
const npmGenerate = (): void => {
|
|
22
|
+
// Правим путь к файлу
|
|
23
|
+
const filePath = path.join(
|
|
24
|
+
__dirname.replace('/bfg-common/modules/fixContentBuild', ''),
|
|
25
|
+
'@nuxt',
|
|
26
|
+
'content',
|
|
27
|
+
'dist',
|
|
28
|
+
'runtime',
|
|
29
|
+
'legacy',
|
|
30
|
+
'composables',
|
|
31
|
+
'query.mjs'
|
|
32
|
+
)
|
|
33
|
+
const searchString = '/query/${hash(params)}'
|
|
34
|
+
const replacementString = 'cache'
|
|
35
|
+
replaceFileContent(filePath, searchString, replacementString)
|
|
36
|
+
// End block
|
|
37
|
+
|
|
38
|
+
// Переносим весь контент и удаляем все файлы кроме картинок
|
|
39
|
+
const contentPath = path.join(
|
|
40
|
+
__dirname.replace('/node_modules/bfg-common/modules/fixContentBuild', ''),
|
|
41
|
+
'content',
|
|
42
|
+
'help'
|
|
43
|
+
)
|
|
44
|
+
if (!fs.existsSync(contentPath)) return
|
|
45
|
+
|
|
46
|
+
const publicPath = path.join(
|
|
47
|
+
__dirname.replace('/node_modules/bfg-common/modules/fixContentBuild', ''),
|
|
48
|
+
'public',
|
|
49
|
+
'help',
|
|
50
|
+
'images'
|
|
51
|
+
)
|
|
52
|
+
fs.cp(contentPath, publicPath, { recursive: true }, () => {
|
|
53
|
+
// Удаляем все файлы кроме картинок
|
|
54
|
+
console.log('removeNonImageFiles start');
|
|
55
|
+
removeNonImageFiles(publicPath)
|
|
56
|
+
console.log('removeNonImageFiles end');
|
|
57
|
+
|
|
58
|
+
// Удаляем картинки из папки content
|
|
59
|
+
console.log('removeImagesFromContent start');
|
|
60
|
+
removeImagesFromContent(contentPath)
|
|
61
|
+
console.log('removeImagesFromContent end');
|
|
62
|
+
|
|
63
|
+
// Правим путь к картинкам и заменяем кастомные тэги
|
|
64
|
+
console.log('updateImagesPathAndCustomComponents start');
|
|
65
|
+
updateImagesPathAndCustomComponents(contentPath)
|
|
66
|
+
console.log('updateImagesPathAndCustomComponents end');
|
|
67
|
+
})
|
|
68
|
+
// End block
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
export default defineNuxtModule(() => {
|
|
73
|
+
switch (process.env.npm_command) {
|
|
74
|
+
case 'run-script':
|
|
75
|
+
// npmDev()
|
|
76
|
+
break
|
|
77
|
+
case 'exec':
|
|
78
|
+
npmGenerate()
|
|
79
|
+
break
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
// export default defineNuxtModule({
|
|
84
|
+
// meta: {
|
|
85
|
+
// name: 'fix-content-build',
|
|
86
|
+
// configKey: 'fixContentBuild',
|
|
87
|
+
// },
|
|
88
|
+
// setup() {
|
|
89
|
+
// switch (process.env.npm_command) {
|
|
90
|
+
// case 'run-script':
|
|
91
|
+
// // npmDev()
|
|
92
|
+
// break
|
|
93
|
+
// case 'exec':
|
|
94
|
+
// npmGenerate()
|
|
95
|
+
// break
|
|
96
|
+
// }
|
|
97
|
+
// },
|
|
98
|
+
// })
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
|
|
4
|
+
const isImage = (fileName: string) => {
|
|
5
|
+
const imageExtensions = ['.png', '.jpeg', '.jpg']
|
|
6
|
+
return imageExtensions.some(
|
|
7
|
+
(ext) => path.extname(fileName).toLowerCase() === ext
|
|
8
|
+
)
|
|
9
|
+
}
|
|
10
|
+
const isMdFile = (fileName: string) => {
|
|
11
|
+
return path.extname(fileName).toLowerCase() === '.md'
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const removeNonImageFiles = (dirPath: string) => {
|
|
15
|
+
fs.readdirSync(dirPath).forEach((file: string) => {
|
|
16
|
+
const filePath = path.join(dirPath, file)
|
|
17
|
+
const isDir = fs.lstatSync(filePath).isDirectory()
|
|
18
|
+
|
|
19
|
+
if (isDir) {
|
|
20
|
+
removeNonImageFiles(filePath)
|
|
21
|
+
} else {
|
|
22
|
+
if (!isImage(file)) {
|
|
23
|
+
fs.unlinkSync(filePath)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const removeImagesFromContent = (dirPath: string) => {
|
|
30
|
+
fs.readdirSync(dirPath).forEach((file: string) => {
|
|
31
|
+
const filePath = path.join(dirPath, file)
|
|
32
|
+
const isDir = fs.lstatSync(filePath).isDirectory()
|
|
33
|
+
|
|
34
|
+
if (isDir) {
|
|
35
|
+
removeImagesFromContent(filePath)
|
|
36
|
+
} else {
|
|
37
|
+
if (isImage(file)) {
|
|
38
|
+
fs.unlinkSync(filePath)
|
|
39
|
+
} else if (isMdFile(file) && filePath.includes('_index.md')) {
|
|
40
|
+
// Удаляем префикс "_" у индексных файлов
|
|
41
|
+
const filePathWidthOutPrefix = filePath.replace('_index.md', 'index.md')
|
|
42
|
+
fs.rename(filePath, filePathWidthOutPrefix, (err: any) => {
|
|
43
|
+
if (err) {
|
|
44
|
+
console.error(`Error renaming file: ${err}`)
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const updateImagesPathAndCustomComponents = (dirPath: string) => {
|
|
53
|
+
fs.readdirSync(dirPath).forEach((file: string) => {
|
|
54
|
+
const filePath = path.join(dirPath, file)
|
|
55
|
+
const isDir = fs.lstatSync(filePath).isDirectory()
|
|
56
|
+
|
|
57
|
+
if (isDir) {
|
|
58
|
+
updateImagesPathAndCustomComponents(filePath)
|
|
59
|
+
} else {
|
|
60
|
+
if (isMdFile(file)) {
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
// Правим путь к картинкам
|
|
63
|
+
const imagePath = filePath
|
|
64
|
+
.replaceAll('\\', '/')
|
|
65
|
+
.match(/\/content.+\//)[0]
|
|
66
|
+
.replace('/content/help', '/help/images')
|
|
67
|
+
replaceFileContent(
|
|
68
|
+
filePath,
|
|
69
|
+
/!\[(.*)\]\((.*?)\)/g,
|
|
70
|
+
``
|
|
71
|
+
).then(() => {
|
|
72
|
+
// Заменяем кастомные тэги
|
|
73
|
+
replaceFileContent(
|
|
74
|
+
filePath,
|
|
75
|
+
/\{\{ *< *hint *(info) *> *\}\}|\{\{ *< *\/hint *> *\}\}/g,
|
|
76
|
+
(_match: any, hintInfo: string) => {
|
|
77
|
+
return hintInfo ? '\n::hint\n' : '\n::\n'
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const replaceFileContent = async (
|
|
87
|
+
filePath: string,
|
|
88
|
+
searchString: string | RegExp,
|
|
89
|
+
replacementString: any
|
|
90
|
+
): Promise<void> => {
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
fs.readFile(filePath, 'utf8', (err: any, data: string) => {
|
|
93
|
+
if (err) {
|
|
94
|
+
console.log('replaceFileContent reject 1');
|
|
95
|
+
reject()
|
|
96
|
+
return console.log(err)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (data.match(replacementString)) {
|
|
100
|
+
console.log('replaceFileContent, resolve 1');
|
|
101
|
+
resolve()
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const updatedContent = data.replace(searchString, replacementString)
|
|
106
|
+
|
|
107
|
+
fs.writeFile(filePath, updatedContent, 'utf8', (err: any) => {
|
|
108
|
+
if (err) {
|
|
109
|
+
console.log('replaceFileContent reject 2');
|
|
110
|
+
reject()
|
|
111
|
+
return console.log(err)
|
|
112
|
+
}
|
|
113
|
+
console.log('replaceFileContent, resolve 2');
|
|
114
|
+
resolve()
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
}
|