-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(copy): support advanced copy configuration with custom target paths #1711
Open
BQXBQX
wants to merge
6
commits into
umijs:master
Choose a base branch
from
BQXBQX:feature/enhanced-copy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a3ddec7
feat(copy): support advanced copy configuration with custom target paths
BQXBQX 464b49c
fix(copy): prevent path traversal in copy plugin
BQXBQX ffc0081
chore: Update `copy` config type in Mako bundler
BQXBQX 5d42e4f
docs: Update `copy` config type in documentation
BQXBQX f2c75e3
test(copy): add e2e tests for copy plugin from/to pattern
BQXBQX 459b3a0
fix(copy): improve path validation and cleanup for copy plugin
BQXBQX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use anyhow::{anyhow, Result}; | ||
use fs_extra; | ||
use glob::glob; | ||
use notify::event::{CreateKind, DataChange, ModifyKind, RenameMode}; | ||
|
@@ -11,6 +12,7 @@ use tracing::debug; | |
|
||
use crate::ast::file::win_path; | ||
use crate::compiler::Context; | ||
use crate::config::CopyConfig; | ||
use crate::plugin::Plugin; | ||
use crate::stats::StatsJsonMap; | ||
use crate::utils::tokio_runtime; | ||
|
@@ -29,8 +31,12 @@ impl CopyPlugin { | |
notify::Config::default(), | ||
) | ||
.unwrap(); | ||
for src in context.config.copy.iter() { | ||
let src = context.root.join(src); | ||
for config in context.config.copy.iter() { | ||
let src = match config { | ||
CopyConfig::Basic(src) => context.root.join(src), | ||
CopyConfig::Advanced { from, .. } => context.root.join(from), | ||
}; | ||
|
||
Comment on lines
+34
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 建议检查源路径是否存在 当前代码在处理 示例修改: for config in context.config.copy.iter() {
let src = match config {
CopyConfig::Basic(src) => context.root.join(src),
CopyConfig::Advanced { from, .. } => context.root.join(from),
};
+ if !src.exists() {
+ eprintln!("Source path {:?} does not exist", src);
+ continue;
+ }
if src.exists() {
debug!("watch {:?}", src);
// ...
}
}
|
||
if src.exists() { | ||
debug!("watch {:?}", src); | ||
let mode = if src.is_dir() { | ||
|
@@ -62,10 +68,31 @@ impl CopyPlugin { | |
fn copy(context: &Arc<Context>) -> Result<()> { | ||
debug!("copy"); | ||
let dest = context.config.output.path.as_path(); | ||
for src in context.config.copy.iter() { | ||
let src = context.root.join(src); | ||
debug!("copy {:?} to {:?}", src, dest); | ||
copy(src.as_path(), dest)?; | ||
for config in context.config.copy.iter() { | ||
match config { | ||
CopyConfig::Basic(src) => { | ||
let src = context.root.join(src); | ||
debug!("copy {:?} to {:?}", src, dest); | ||
copy(&src, dest)?; | ||
} | ||
|
||
CopyConfig::Advanced { from, to } => { | ||
let src = context.root.join(from); | ||
let target = dest.join(to.trim_start_matches("/")); | ||
|
||
let dest_path = dest.to_path_buf(); | ||
if !target.starts_with(&dest_path) { | ||
return Err(anyhow!("Invalid target path: {:?}", target)); | ||
} | ||
|
||
if !target.exists() { | ||
fs::create_dir_all(&target)?; | ||
} | ||
|
||
debug!("copy {:?} to {:?}", src, target); | ||
copy(&src, &target)?; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"copy": ["src/assets"] | ||
} | ||
"copy": [ | ||
"src/assets", | ||
{ | ||
"from": "src/assets", | ||
"to": "assets-from-to" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
为
CopyConfig
添加配置验证CopyConfig
中的from
和to
字段直接来自用户配置,可能包含非法或不安全的路径。建议在配置解析时添加路径验证,确保路径不存在注入风险并且指向合法的位置。