From 9f9f1c1aeb19fa45147bf1ca123e73a8dec85e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 6 Dec 2022 11:01:46 +0100 Subject: [PATCH] Add a Version utility func --- transpiler.go | 33 +++++++++++++++++++++++++++++++++ transpiler_test.go | 10 ++++++++++ 2 files changed, 43 insertions(+) diff --git a/transpiler.go b/transpiler.go index 879f0ee..f00f3f0 100644 --- a/transpiler.go +++ b/transpiler.go @@ -72,6 +72,39 @@ func Start(opts Options) (*Transpiler, error) { return t, nil } +// Version returns version information about the Dart Sass frameworks used +// in dartSassEmbeddedFilename. +func Version(dartSassEmbeddedFilename string) (DartSassVersion, error) { + var v DartSassVersion + bin, err := safeexec.LookPath(dartSassEmbeddedFilename) + if err != nil { + return v, err + } + + cmd := exec.Command(bin, "--version") + cmd.Stderr = os.Stderr + + out, err := cmd.Output() + if err != nil { + return v, err + } + + if err := json.Unmarshal(out, &v); err != nil { + return v, err + } + + return v, nil + +} + +type DartSassVersion struct { + ProtocolVersion string `json:"protocolVersion"` + CompilerVersion string `json:"compilerVersion"` + ImplementationVersion string `json:"implementationVersion"` + ImplementationName string `json:"implementationName"` + ID int `json:"id"` +} + // Transpiler controls transpiling of SCSS into CSS. type Transpiler struct { opts Options diff --git a/transpiler_test.go b/transpiler_test.go index b9c618f..990ea46 100644 --- a/transpiler_test.go +++ b/transpiler_test.go @@ -406,6 +406,16 @@ func TestHasScheme(t *testing.T) { c.Assert(hasScheme("foo"), qt.Equals, false) } +func TestVersion(t *testing.T) { + c := qt.New(t) + + version, err := Version(getSassEmbeddedFilename()) + c.Assert(err, qt.IsNil) + c.Assert(version, qt.Not(qt.Equals), "") + c.Assert(version.ProtocolVersion, qt.Equals, "1.1.0") + +} + func newTestTranspiler(c *qt.C, opts Options) (*Transpiler, func()) { opts.DartSassEmbeddedFilename = getSassEmbeddedFilename() transpiler, err := Start(opts)