bfg-common 1.0.84 → 1.0.86
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,116 @@
|
|
|
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
|
+
updateImagesPath,
|
|
9
|
+
} from './utils/methods'
|
|
10
|
+
|
|
11
|
+
export default defineNuxtModule({
|
|
12
|
+
meta: {
|
|
13
|
+
name: 'fix-content-build',
|
|
14
|
+
configKey: 'fixContentBuild'
|
|
15
|
+
},
|
|
16
|
+
setup () {
|
|
17
|
+
if (
|
|
18
|
+
process.env.NODE_ENV === 'development' ||
|
|
19
|
+
process.env.npm_command !== 'exec'
|
|
20
|
+
)
|
|
21
|
+
return
|
|
22
|
+
|
|
23
|
+
// Правим путь к файлу
|
|
24
|
+
const filePath = path.join(
|
|
25
|
+
__dirname.replace('/modules/fixContentBuild', ''),
|
|
26
|
+
'node_modules',
|
|
27
|
+
'@nuxt',
|
|
28
|
+
'content',
|
|
29
|
+
'dist',
|
|
30
|
+
'runtime',
|
|
31
|
+
'legacy',
|
|
32
|
+
'composables',
|
|
33
|
+
'query.mjs'
|
|
34
|
+
)
|
|
35
|
+
const searchString = '/query/${hash(params)}'
|
|
36
|
+
const replacementString = 'cache'
|
|
37
|
+
replaceFileContent(filePath, searchString, replacementString)
|
|
38
|
+
// End block
|
|
39
|
+
|
|
40
|
+
// Переносим весь контент и удаляем все файлы кроме картинок
|
|
41
|
+
const contentPath = path.join(
|
|
42
|
+
__dirname.replace('/modules/fixContentBuild', ''),
|
|
43
|
+
'content',
|
|
44
|
+
'help'
|
|
45
|
+
)
|
|
46
|
+
if (!fs.existsSync(contentPath)) return;
|
|
47
|
+
|
|
48
|
+
const publicPath = path.join(
|
|
49
|
+
__dirname.replace('/modules/fixContentBuild', ''),
|
|
50
|
+
'public',
|
|
51
|
+
'help',
|
|
52
|
+
'images'
|
|
53
|
+
)
|
|
54
|
+
fs.cp(contentPath, publicPath, { recursive: true }, () => {
|
|
55
|
+
// Удаляем все файлы кроме картинок
|
|
56
|
+
removeNonImageFiles(publicPath)
|
|
57
|
+
|
|
58
|
+
// Удаляем картинки из папки content
|
|
59
|
+
removeImagesFromContent(contentPath)
|
|
60
|
+
})
|
|
61
|
+
// End block
|
|
62
|
+
|
|
63
|
+
// Правим путь к картинкам
|
|
64
|
+
updateImagesPath(contentPath)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
// export default defineNuxtModule(() => {
|
|
68
|
+
// if (
|
|
69
|
+
// process.env.NODE_ENV === 'development' ||
|
|
70
|
+
// process.env.npm_command !== 'exec'
|
|
71
|
+
// )
|
|
72
|
+
// return
|
|
73
|
+
//
|
|
74
|
+
// // Правим путь к файлу
|
|
75
|
+
// const filePath = path.join(
|
|
76
|
+
// __dirname.replace('/modules/fixContentBuild', ''),
|
|
77
|
+
// 'node_modules',
|
|
78
|
+
// '@nuxt',
|
|
79
|
+
// 'content',
|
|
80
|
+
// 'dist',
|
|
81
|
+
// 'runtime',
|
|
82
|
+
// 'legacy',
|
|
83
|
+
// 'composables',
|
|
84
|
+
// 'query.mjs'
|
|
85
|
+
// )
|
|
86
|
+
// const searchString = '/query/${hash(params)}'
|
|
87
|
+
// const replacementString = 'cache'
|
|
88
|
+
// replaceFileContent(filePath, searchString, replacementString)
|
|
89
|
+
// // End block
|
|
90
|
+
//
|
|
91
|
+
// // Переносим весь контент и удаляем все файлы кроме картинок
|
|
92
|
+
// const contentPath = path.join(
|
|
93
|
+
// __dirname.replace('/modules/fixContentBuild', ''),
|
|
94
|
+
// 'content',
|
|
95
|
+
// 'help'
|
|
96
|
+
// )
|
|
97
|
+
// if (!fs.existsSync(contentPath)) return;
|
|
98
|
+
//
|
|
99
|
+
// const publicPath = path.join(
|
|
100
|
+
// __dirname.replace('/modules/fixContentBuild', ''),
|
|
101
|
+
// 'public',
|
|
102
|
+
// 'help',
|
|
103
|
+
// 'images'
|
|
104
|
+
// )
|
|
105
|
+
// fs.cp(contentPath, publicPath, { recursive: true }, () => {
|
|
106
|
+
// // Удаляем все файлы кроме картинок
|
|
107
|
+
// removeNonImageFiles(publicPath)
|
|
108
|
+
//
|
|
109
|
+
// // Удаляем картинки из папки content
|
|
110
|
+
// removeImagesFromContent(contentPath)
|
|
111
|
+
// })
|
|
112
|
+
// // End block
|
|
113
|
+
//
|
|
114
|
+
// // Правим путь к картинкам
|
|
115
|
+
// updateImagesPath(contentPath)
|
|
116
|
+
// })
|
|
@@ -0,0 +1,91 @@
|
|
|
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 updateImagesPath = (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
|
+
updateImagesPath(filePath)
|
|
59
|
+
} else {
|
|
60
|
+
if (isMdFile(file)) {
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
const imagePath = filePath.replaceAll('\\', '/')
|
|
63
|
+
.match(/\/content.+\//)[0]
|
|
64
|
+
.replace('\/content\/help', '\/help\/images')
|
|
65
|
+
replaceFileContent(filePath, /!\[(.*)\]\((.*?)\)/g, ``)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const replaceFileContent = (
|
|
72
|
+
filePath: string,
|
|
73
|
+
searchString: string | RegExp,
|
|
74
|
+
replacementString: string
|
|
75
|
+
) => {
|
|
76
|
+
fs.readFile(filePath, 'utf8', (err: any, data: string) => {
|
|
77
|
+
if (err) {
|
|
78
|
+
return console.log(err)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (data.match(replacementString)) return
|
|
82
|
+
|
|
83
|
+
const updatedContent = data.replace(searchString, replacementString)
|
|
84
|
+
|
|
85
|
+
fs.writeFile(filePath, updatedContent, 'utf8', (err: any) => {
|
|
86
|
+
if (err) {
|
|
87
|
+
return console.log(err)
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
}
|