Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
phobos-ui
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ui
phobos-ui
Commits
00b80040
Commit
00b80040
authored
May 10, 2023
by
贾慧斌
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 转换脚本支持标签替换
parent
e276ec19
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
219 additions
and
97 deletions
+219
-97
uniappUtils.js
uniappUtils.js
+219
-97
No files found.
uniappUtils.js
View file @
00b80040
const
fs
=
require
(
'
fs
'
);
const
fs
=
require
(
"
fs
"
);
const
path
=
require
(
'
path
'
);
const
path
=
require
(
"
path
"
);
const
util
=
require
(
'
util
'
);
const
util
=
require
(
"
util
"
);
const
deleteDir
=
util
.
promisify
(
fs
.
rmdir
);
const
deleteDir
=
util
.
promisify
(
fs
.
rmdir
);
const
makeDir
=
util
.
promisify
(
fs
.
mkdir
);
const
makeDir
=
util
.
promisify
(
fs
.
mkdir
);
const
sourcePath
=
'
./source
'
;
// 源文件夹路径
const
sourcePath
=
"
./source
"
;
// 源文件夹路径
const
tempPath
=
'
./temp
'
;
// 临时文件夹路径
const
tempPath
=
"
./temp
"
;
// 临时文件夹路径
const
map
=
{
// 替换规则
const
basePath
=
"
./base
"
;
// 基础文件夹路径
'
\\
.txt$
'
:
'
_text
'
,
const
map
=
{
'
\\
.js$
'
:
'
_js
'
,
// 替换规则
"
\\
.txt$
"
:
"
_text
"
,
"
\\
.js$
"
:
"
_js
"
,
};
};
/**
/**
...
@@ -21,120 +23,240 @@ const map = { // 替换规则
...
@@ -21,120 +23,240 @@ const map = { // 替换规则
* @return {Promise}
* @return {Promise}
*/
*/
function
copyDir
(
srcDir
,
destDir
,
options
=
{})
{
function
copyDir
(
srcDir
,
destDir
,
options
=
{})
{
const
{
prefix
=
''
,
suffix
=
''
,
exclude
=
''
}
=
options
;
const
{
prefix
=
""
,
suffix
=
""
,
exclude
=
""
,
include
=
""
}
=
options
;
const
excludeDirs
=
exclude
?
exclude
.
split
(
'
,
'
)
:
[];
const
excludeDirs
=
exclude
?
exclude
.
split
(
"
,
"
)
:
[];
return
new
Promise
((
resolve
,
reject
)
=>
{
// 创建目标文件夹
fs
.
mkdir
(
destDir
,
{
recursive
:
true
},
(
err
)
=>
{
if
(
err
)
{
reject
(
err
);
}
else
{
// 读取原文件夹下的文件和子文件夹
fs
.
readdir
(
srcDir
,
{
withFileTypes
:
true
},
(
err
,
files
)
=>
{
if
(
err
)
{
reject
(
err
);
}
else
{
// 处理每个文件
Promise
.
all
(
files
.
map
((
file
)
=>
{
const
srcPath
=
path
.
join
(
srcDir
,
file
.
name
);
const
destPath
=
path
.
join
(
destDir
,
`
${
prefix
}${
file
.
name
}${
suffix
}
`
);
return
new
Promise
((
resolve
,
reject
)
=>
{
// 排除特定文件夹
// 创建目标文件夹
if
(
excludeDirs
.
includes
(
file
.
name
))
{
fs
.
mkdir
(
destDir
,
{
recursive
:
true
},
(
err
)
=>
{
return
Promise
.
resolve
();
if
(
err
)
{
}
reject
(
err
);
}
else
{
// 读取原文件夹下的文件和子文件夹
fs
.
readdir
(
srcDir
,
{
withFileTypes
:
true
},
(
err
,
files
)
=>
{
if
(
err
)
{
reject
(
err
);
}
else
{
// 处理每个文件
Promise
.
all
(
files
.
map
((
file
)
=>
{
const
srcPath
=
path
.
join
(
srcDir
,
file
.
name
);
const
destPath
=
path
.
join
(
destDir
,
`
${
prefix
}${
file
.
name
}${
suffix
}
`
);
// 排除特定文件夹
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
excludeDirs
.
includes
(
file
.
name
))
{
// 复制文件或递归复制子文件夹
return
Promise
.
resolve
();
if
(
file
.
isFile
())
{
}
const
readStream
=
fs
.
createReadStream
(
srcPath
);
readStream
.
on
(
"
error
"
,
reject
);
return
new
Promise
((
resolve
,
reject
)
=>
{
const
writeStream
=
fs
.
createWriteStream
(
destPath
);
// 复制文件或递归复制子文件夹
writeStream
.
on
(
"
error
"
,
reject
);
if
(
file
.
isFile
())
{
writeStream
.
on
(
"
finish
"
,
resolve
);
const
readStream
=
fs
.
createReadStream
(
srcPath
);
readStream
.
pipe
(
writeStream
);
readStream
.
on
(
'
error
'
,
reject
);
}
else
{
const
writeStream
=
fs
.
createWriteStream
(
destPath
);
copyDir
(
srcPath
,
destPath
,
options
).
then
(
resolve
,
reject
);
writeStream
.
on
(
'
error
'
,
reject
);
}
writeStream
.
on
(
'
finish
'
,
resolve
);
readStream
.
pipe
(
writeStream
);
}
else
{
copyDir
(
srcPath
,
destPath
,
options
).
then
(
resolve
,
reject
);
}
});
})).
then
(
resolve
,
reject
);
}
});
});
}
})
).
then
(
resolve
,
reject
);
}
});
});
}
});
});
});
}
}
// 替换文件中的文本
// 替换文件中的文本
const
replaceText
=
async
(
filePath
,
map
)
=>
{
const
replaceText
=
async
(
filePath
,
map
)
=>
{
let
content
=
await
util
.
promisify
(
fs
.
readFile
)(
filePath
,
{
encoding
:
'
utf8
'
});
let
content
=
await
util
.
promisify
(
fs
.
readFile
)(
filePath
,
{
encoding
:
"
utf8
"
});
for
(
const
[
regexStr
,
replacement
]
of
Object
.
entries
(
map
))
{
for
(
const
[
regexStr
,
replacement
]
of
Object
.
entries
(
map
))
{
const
regex
=
new
RegExp
(
regexStr
,
'
g
'
);
const
regex
=
new
RegExp
(
regexStr
,
"
g
"
);
content
=
content
.
replace
(
regex
,
replacement
);
content
=
content
.
replace
(
regex
,
replacement
);
}
}
await
util
.
promisify
(
fs
.
writeFile
)(
filePath
,
content
,
{
encoding
:
'
utf8
'
});
await
util
.
promisify
(
fs
.
writeFile
)(
filePath
,
content
,
{
encoding
:
"
utf8
"
});
};
};
// 替换路径下所有的文件
async
function
replaceFiles
(
dirPath
,
map
,
options
)
{
const
replaceFiles
=
async
(
folderPath
,
map
)
=>
{
const
files
=
await
readDir
(
dirPath
,
options
);
const
files
=
await
util
.
promisify
(
fs
.
readdir
)(
folderPath
);
for
(
const
file
of
files
)
{
for
(
let
i
=
0
;
i
<
files
.
length
;
i
++
)
{
const
filePath
=
path
.
join
(
dirPath
,
file
);
const
file
=
files
[
i
];
const
stats
=
await
stat
(
filePath
);
const
filePath
=
path
.
join
(
folderPath
,
file
);
if
(
stats
.
isDirectory
())
{
const
stat
=
await
util
.
promisify
(
fs
.
stat
)(
filePath
);
await
replaceFiles
(
filePath
,
map
,
options
);
if
(
stat
.
isDirectory
())
{
}
else
{
await
replaceFiles
(
filePath
,
map
);
await
replaceFile
(
filePath
,
map
);
}
else
{
await
replaceText
(
filePath
,
map
);
}
}
}
};
}
}
async
function
replaceFile
(
filePath
,
map
)
{
const
content
=
await
readFile
(
filePath
);
let
newContent
=
content
;
for
(
const
[
regex
,
replacement
]
of
map
)
{
newContent
=
newContent
.
replace
(
regex
,
replacement
);
}
if
(
newContent
!==
content
)
{
await
writeFile
(
filePath
,
newContent
);
}
}
async
function
readDir
(
dirPath
,
options
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
fs
.
readdir
(
dirPath
,
(
err
,
files
)
=>
{
if
(
err
)
{
reject
(
err
);
}
else
{
if
(
options
&&
options
.
extensions
)
{
files
=
files
.
filter
((
file
)
=>
{
const
extension
=
path
.
extname
(
file
).
toLowerCase
();
return
options
.
extensions
.
includes
(
extension
);
});
}
if
(
options
&&
options
.
exclude
)
{
files
=
files
.
filter
((
file
)
=>
!
options
.
exclude
.
test
(
file
));
}
resolve
(
files
);
}
});
});
}
async
function
stat
(
filePath
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
fs
.
stat
(
filePath
,
(
err
,
stats
)
=>
{
if
(
err
)
{
reject
(
err
);
}
else
{
resolve
(
stats
);
}
});
});
}
async
function
readFile
(
filePath
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
fs
.
readFile
(
filePath
,
"
utf8
"
,
(
err
,
content
)
=>
{
if
(
err
)
{
reject
(
err
);
}
else
{
resolve
(
content
);
}
});
});
}
async
function
writeFile
(
filePath
,
content
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
fs
.
writeFile
(
filePath
,
content
,
"
utf8
"
,
(
err
)
=>
{
if
(
err
)
{
reject
(
err
);
}
else
{
resolve
();
}
});
});
}
// 复制 temp 文件到 src
// 复制 temp 文件到 src
const
copyTempToSrc
=
async
(
tempPath
,
srcPath
)
=>
{
const
copyTempToSrc
=
async
(
tempPath
,
srcPath
)
=>
{
await
copyFolder
(
tempPath
,
srcPath
);
await
copyFolder
(
tempPath
,
srcPath
);
};
};
// 删除目录
// 删除目录
const
deleteFolder
=
async
(
folderPath
)
=>
{
const
deleteFolder
=
async
(
folderPath
)
=>
{
const
files
=
await
util
.
promisify
(
fs
.
readdir
)(
folderPath
);
const
files
=
await
util
.
promisify
(
fs
.
readdir
)(
folderPath
);
for
(
let
i
=
0
;
i
<
files
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
files
.
length
;
i
++
)
{
const
file
=
files
[
i
];
const
file
=
files
[
i
];
const
filePath
=
path
.
join
(
folderPath
,
file
);
const
filePath
=
path
.
join
(
folderPath
,
file
);
const
stat
=
await
util
.
promisify
(
fs
.
stat
)(
filePath
);
const
stat
=
await
util
.
promisify
(
fs
.
stat
)(
filePath
);
if
(
stat
.
isDirectory
())
{
if
(
stat
.
isDirectory
())
{
await
deleteFolder
(
filePath
);
await
deleteFolder
(
filePath
);
}
else
{
}
else
{
await
util
.
promisify
(
fs
.
unlink
)(
filePath
);
await
util
.
promisify
(
fs
.
unlink
)(
filePath
);
}
}
}
await
util
.
promisify
(
fs
.
rmdir
)(
folderPath
);
}
await
util
.
promisify
(
fs
.
rmdir
)(
folderPath
);
};
};
async
function
renameFolder
(
oldName
,
newName
)
{
async
function
renameFolder
(
oldName
,
newName
)
{
try
{
try
{
await
fs
.
promises
.
rename
(
oldName
,
newName
);
await
fs
.
promises
.
rename
(
oldName
,
newName
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Error:
'
,
error
);
console
.
error
(
"
Error:
"
,
error
);
}
}
}
}
const
replaceInFile
=
async
(
file
,
mapping
,
options
)
=>
{
try
{
let
content
=
await
fs
.
promises
.
readFile
(
file
,
"
utf8
"
);
Object
.
entries
(
mapping
).
forEach
(([
pattern
,
replacement
])
=>
{
content
=
content
.
replace
(
new
RegExp
(
pattern
,
"
g
"
),
replacement
);
});
await
fs
.
promises
.
writeFile
(
file
,
content
,
"
utf8
"
);
}
catch
(
err
)
{
console
.
error
(
`Error replacing in file
${
file
}
:
${
err
.
message
}
`
);
}
};
const
traverseDirectory
=
async
(
dir
,
mapping
,
options
)
=>
{
try
{
const
files
=
await
fs
.
promises
.
readdir
(
dir
);
for
(
const
file
of
files
)
{
const
filePath
=
path
.
join
(
dir
,
file
);
const
stats
=
await
fs
.
promises
.
lstat
(
filePath
);
if
(
stats
.
isDirectory
())
{
if
(
options
&&
options
.
excludeDirs
&&
options
.
excludeDirs
.
includes
(
file
))
{
continue
;
}
await
traverseDirectory
(
filePath
,
mapping
,
options
);
}
else
if
(
stats
.
isFile
())
{
const
ext
=
path
.
extname
(
file
);
if
(
options
&&
options
.
excludeFiles
&&
options
.
excludeFiles
.
includes
(
file
))
{
continue
;
}
if
(
options
&&
options
.
extensions
&&
!
options
.
extensions
.
includes
(
ext
))
{
continue
;
}
await
replaceInFile
(
filePath
,
mapping
,
options
);
}
}
}
catch
(
err
)
{
console
.
error
(
`Error traversing directory
${
dir
}
:
${
err
.
message
}
`
);
}
};
// 执行操作
// 执行操作
(
async
()
=>
{
(
async
()
=>
{
// 删除并重新创建 temp 目录
await
deleteFolder
(
tempPath
).
finally
(()
=>
{
await
deleteFolder
(
tempPath
).
finally
(()
=>
{
return
makeDir
(
tempPath
);
return
makeDir
(
tempPath
);
});
})
// home 页面调整
// 复制 source 下所有文件到 temp
// 复制 source 下所有文件到 temp
await
copyDir
(
sourcePath
,
tempPath
,{
exclude
:
'
App.vue
'
});
await
copyDir
(
`
${
sourcePath
}
/views/home/`
,
`
${
tempPath
}
/pages/home`
);
// 调整pages 目录
await
copyDir
(
`
${
sourcePath
}
/views/test/`
,
`
${
tempPath
}
/pages/test`
);
await
renameFolder
(
tempPath
+
'
/views
'
,
tempPath
+
'
/pages
'
)
// 标签操作
// copy temp 文件到 src
await
copyDir
(
tempPath
,
'
./src
'
);
const
mapping
=
{
// 删除 temp 目录
"
<div
"
:
"
<view
"
,
await
deleteFolder
(
tempPath
);
"
</div
"
:
"
</view
"
,
};
const
options
=
{
extensions
:
[
"
.vue
"
],
};
await
traverseDirectory
(
tempPath
,
mapping
,
options
);
await
copyDir
(
tempPath
,
"
./src
"
);
// 删除并重新创建 temp 目录
// await deleteFolder(tempPath).finally(() => {
// return makeDir(tempPath);
// });
// // 复制 source 下所有文件到 temp
// await copyDir(sourcePath, tempPath, { exclude: "App.vue,main.js" });
// // 调整pages 目录
// await renameFolder(tempPath + "/views", tempPath + "/pages");
// // copy temp 文件到 src
// await copyDir(tempPath, "./src");
// // 删除 temp 目录
// await deleteFolder(tempPath);
//
// // 复制base 到src
// await copyDir(basePath, "./src");
})();
})();
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment