Files
skillshare/internal/check/local_check.go
T
Willie 1a1eeee844 fix(check): compare project-relative local sources and record install layout (#337)
* fix(check): compare project-relative local sources and record install layout

Project installs record a local source as typed (./vendor/skill), relative
to the project root, so check skipped them. Resolve them against the project
root in both the CLI and the dashboard.

The copied shape of a local install was inferred from its file hashes, so a
CLI install whose source held only SKILL.md looked like a dashboard
collection-root install: a child skill added at the source later was neither
reported by check nor copied by update. Record the layout (skill-file or
directory) at install time and keep the hash heuristic only for entries
written before the field existed.

* fix(install): let a recorded layout decide the update shape without hashes

An entry whose hashing failed at install time still knows what was copied;
returning false there would turn a SKILL.md-only update into a full copy.
2026-10-02 20:56:55 +08:00

63 lines
2.0 KiB
Go

package check
import (
"maps"
"os"
"path/filepath"
"strings"
"skillshare/internal/install"
)
// LocalSourceStatus compares the files at a local install's recorded source
// path with the hashes recorded at install/update time. A relative source
// resolves against baseDir: project mode records sources as typed, relative to
// the project root. Pass "" when relative sources have no known base.
//
// Returns "update_available" when the source changed, "error" with a message
// when the source cannot be read, and "local" when there is nothing to
// compare against (not a local install, no recorded hashes, or a relative
// source without a base directory).
func LocalSourceStatus(entry *install.MetadataEntry, baseDir string) (status, message string) {
if entry == nil || entry.Type != "local" || len(entry.FileHashes) == 0 {
return "local", ""
}
source, err := install.ParseSource(entry.Source)
if err != nil || source.Type != install.SourceTypeLocalPath {
return "local", ""
}
path := source.Path
// ParseSource resolves a relative source against the current cwd, which
// need not be the directory the install ran in.
if !filepath.IsAbs(entry.Source) && !strings.HasPrefix(entry.Source, "~") {
if baseDir == "" {
return "local", ""
}
path = filepath.Join(baseDir, entry.Source)
}
if info, err := os.Stat(path); err != nil || !info.IsDir() {
return "error", "local source not found: " + path
}
hashes, err := install.ComputeFileHashes(path)
if err != nil {
return "error", "cannot read local source: " + err.Error()
}
if maps.Equal(installedFiles(entry, hashes), entry.FileHashes) {
return "up_to_date", ""
}
return "update_available", ""
}
// installedFiles narrows the source hashes to the files an install copies.
func installedFiles(entry *install.MetadataEntry, source map[string]string) map[string]string {
if !install.CopiesSkillFileOnly(entry, source) {
return source
}
if hash, ok := source["SKILL.md"]; ok {
return map[string]string{"SKILL.md": hash}
}
return map[string]string{}
}