-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #40
- Loading branch information
Showing
4 changed files
with
203 additions
and
8 deletions.
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package nodejs | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/blang/semver" | ||
"github.com/cavaliercoder/grab" | ||
"github.com/markelog/archive" | ||
"github.com/markelog/cprf" | ||
|
||
"github.com/markelog/eclectica/io" | ||
"github.com/markelog/eclectica/variables" | ||
) | ||
|
||
var ( | ||
minimalForYarn, _ = semver.Make("4.0.0") | ||
yarnUrl = "https://yarnpkg.com/latest.tar.gz" | ||
) | ||
|
||
func (node Node) download(path string) (err error) { | ||
_, err = grab.Get(path, yarnUrl) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func (node Node) Yarn() (ok bool, err error) { | ||
version, _ := semver.Make(node.Version) | ||
ok = false | ||
|
||
path := variables.Path("node", node.Version) | ||
modules := filepath.Join(path, "lib/node_modules") | ||
dist := filepath.Join(modules, "yarn-archive") | ||
temp := filepath.Join(modules, "yarn-temp") | ||
from := filepath.Join(temp, "dist/") | ||
dest := filepath.Join(modules, "yarn") | ||
|
||
if version.LT(minimalForYarn) { | ||
return true, errors.New("\"" + node.Version + "\" version is not supported by yarn") | ||
} | ||
|
||
err = node.download(dist) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = archive.Extract(dist, temp) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = cprf.Copy(from+"/", dest) | ||
if err != nil { | ||
return | ||
} | ||
|
||
os.RemoveAll(dist) | ||
os.RemoveAll(temp) | ||
|
||
current := filepath.Join(modules, "yarn/bin/yarn.js") | ||
base := filepath.Join(path, "bin/yarn") | ||
|
||
err = io.Symlink(base, current) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return true, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package nodejs_test | ||
|
||
import ( | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/bouk/monkey" | ||
"github.com/cavaliercoder/grab" | ||
"github.com/markelog/archive" | ||
"github.com/markelog/cprf" | ||
|
||
"github.com/markelog/eclectica/io" | ||
. "github.com/markelog/eclectica/plugins/nodejs" | ||
) | ||
|
||
var _ = Describe("yarn", func() { | ||
var ( | ||
grabGet bool | ||
archiveExtract bool | ||
cprfCopy bool | ||
osRemovaAll bool | ||
ioSymlink bool | ||
) | ||
|
||
BeforeEach(func() { | ||
grabGet = false | ||
archiveExtract = false | ||
cprfCopy = false | ||
osRemovaAll = false | ||
ioSymlink = false | ||
|
||
monkey.Patch(grab.Get, func(path, url string) (*grab.Response, error) { | ||
grabGet = true | ||
return nil, nil | ||
}) | ||
|
||
monkey.Patch(archive.Extract, func(from, to string) error { | ||
archiveExtract = true | ||
return nil | ||
}) | ||
|
||
monkey.Patch(cprf.Copy, func(from, to string) error { | ||
cprfCopy = true | ||
return nil | ||
}) | ||
|
||
monkey.Patch(os.RemoveAll, func(path string) error { | ||
osRemovaAll = true | ||
return nil | ||
}) | ||
|
||
monkey.Patch(io.Symlink, func(from, to string) error { | ||
ioSymlink = true | ||
return nil | ||
}) | ||
}) | ||
|
||
AfterEach(func() { | ||
monkey.Unpatch(grab.Get) | ||
monkey.Unpatch(archive.Extract) | ||
monkey.Unpatch(cprf.Copy) | ||
monkey.Unpatch(os.RemoveAll) | ||
monkey.Unpatch(io.Symlink) | ||
}) | ||
|
||
It("should not try to install yarn for unsupported node version", func() { | ||
working, err := (&Node{Version: "0.10.0"}).Yarn() | ||
|
||
Expect(grabGet).To(Equal(false)) | ||
Expect(archiveExtract).To(Equal(false)) | ||
Expect(cprfCopy).To(Equal(false)) | ||
Expect(osRemovaAll).To(Equal(false)) | ||
Expect(ioSymlink).To(Equal(false)) | ||
|
||
Expect(working).To(Equal(true)) | ||
Expect(err.Error()).To(Equal("\"0.10.0\" version is not supported by yarn")) | ||
}) | ||
|
||
It("installs yarn", func() { | ||
working, err := (&Node{Version: "6.10.0"}).Yarn() | ||
|
||
Expect(grabGet).To(Equal(true)) | ||
Expect(archiveExtract).To(Equal(true)) | ||
Expect(cprfCopy).To(Equal(true)) | ||
Expect(osRemovaAll).To(Equal(true)) | ||
Expect(ioSymlink).To(Equal(true)) | ||
|
||
Expect(working).To(Equal(true)) | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) |