feat(sync): add project mode support for sync extras and sync --all -p

cmdSyncExtras now parses mode args and auto-detects project mode via
projectConfigExists. Global logic is extracted into cmdSyncExtrasGlobal;
new cmdSyncExtrasProject handles project-scoped extras using
config.LoadProject and config.ExtrasSourceDirProject, with relative
target paths resolved against the project root.

sync --all in project mode now defers a cmdSyncExtras("-p", ...) call
instead of printing a warning, so extras are also synced.
This commit is contained in:
Willie
2026-03-12 16:45:02 +08:00
parent 18d54bd293
commit c02b918057
2 changed files with 131 additions and 3 deletions
+10 -2
View File
@@ -102,8 +102,16 @@ func cmdSync(args []string) error {
}
if mode == modeProject {
if hasAll && !jsonOutput {
ui.Warning("--all is not supported in project mode (extras are global only)")
if hasAll {
// Run project extras sync after project skills sync
defer func() {
fmt.Println()
if extrasErr := cmdSyncExtras(append([]string{"-p"}, rest...)); extrasErr != nil {
if !jsonOutput {
ui.Warning("Extras sync: %v", extrasErr)
}
}
}()
}
stats, results, err := cmdSyncProject(cwd, dryRun, force, jsonOutput)
stats.ProjectScope = true
+121 -1
View File
@@ -16,8 +16,31 @@ import (
func cmdSyncExtras(args []string) error {
start := time.Now()
dryRun, force, _ := parseSyncFlags(args)
mode, rest, err := parseModeArgs(args)
if err != nil {
return err
}
dryRun, force, _ := parseSyncFlags(rest)
cwd, _ := os.Getwd()
if mode == modeAuto {
if projectConfigExists(cwd) {
mode = modeProject
} else {
mode = modeGlobal
}
}
applyModeLabel(mode)
if mode == modeProject {
return cmdSyncExtrasProject(cwd, dryRun, force, start)
}
return cmdSyncExtrasGlobal(dryRun, force, start)
}
func cmdSyncExtrasGlobal(dryRun, force bool, start time.Time) error {
cfg, err := config.Load()
if err != nil {
return err
@@ -126,6 +149,103 @@ func cmdSyncExtras(args []string) error {
return nil
}
func cmdSyncExtrasProject(cwd string, dryRun, force bool, start time.Time) error {
projCfg, err := config.LoadProject(cwd)
if err != nil {
return err
}
if len(projCfg.Extras) == 0 {
ui.Info("No extras configured in project.")
ui.Info("Run 'skillshare extras init <name> --target <path> -p' to add one.")
return nil
}
if dryRun {
ui.Warning("Dry run mode - no changes will be made")
}
var totalSynced, totalSkipped, totalPruned, totalErrors int
for _, extra := range projCfg.Extras {
extraSource := config.ExtrasSourceDirProject(cwd, extra.Name)
ui.Header(capitalize(extra.Name))
if _, statErr := os.Stat(extraSource); os.IsNotExist(statErr) {
ui.Info("Source directory does not exist: %s", extraSource)
ui.Info("Create it to start syncing %s", extra.Name)
continue
}
for _, target := range extra.Targets {
mode := target.Mode
if mode == "" {
mode = "merge"
}
// Resolve relative paths against project root
targetPath := target.Path
if !filepath.IsAbs(targetPath) {
targetPath = filepath.Join(cwd, targetPath)
}
result, syncErr := sync.SyncExtra(extraSource, targetPath, mode, dryRun, force)
shortTarget := shortenPath(targetPath)
if syncErr != nil {
ui.Warning(" %s: %v", shortTarget, syncErr)
totalErrors++
continue
}
totalSynced += result.Synced
totalSkipped += result.Skipped
totalPruned += result.Pruned
totalErrors += len(result.Errors)
verb := syncVerb(mode)
if result.Synced > 0 {
parts := []string{fmt.Sprintf("%d files %s", result.Synced, verb)}
if result.Pruned > 0 {
parts = append(parts, fmt.Sprintf("%d pruned", result.Pruned))
}
ui.Success(" %s %s (%s)", shortTarget, strings.Join(parts, ", "), mode)
} else if result.Skipped > 0 {
ui.Warning(" %s %d files skipped (use --force to override)", shortTarget, result.Skipped)
} else {
ui.Success(" %s up to date (%s)", shortTarget, mode)
}
for _, e := range result.Errors {
ui.Warning(" %s", e)
}
}
}
status := "ok"
if totalErrors > 0 {
status = "partial"
}
e := oplog.NewEntry("sync-extras", status, time.Since(start))
e.Args = map[string]any{
"extras_count": len(projCfg.Extras),
"synced": totalSynced,
"skipped": totalSkipped,
"pruned": totalPruned,
"errors": totalErrors,
"dry_run": dryRun,
"force": force,
"scope": "project",
}
oplog.WriteWithLimit(config.ProjectConfigPath(cwd), oplog.OpsFile, e, logMaxEntries()) //nolint:errcheck
if totalErrors > 0 {
return fmt.Errorf("%d extras sync error(s)", totalErrors)
}
return nil
}
// syncVerb returns a user-facing verb for the given sync mode.
func syncVerb(mode string) string {
switch mode {