Skip to content
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(cpm)!: switch to golang to speed up execution #85

Merged
merged 7 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-countries-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mheob/check-package-manager': major
---

switch to golang to speed up the execution
29 changes: 29 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release Go project

on:
push:
tags:
- "@mheob/check-package-manager@*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ">=1.19"

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v3
with:
version: latest
args: release --rm-dist
workdir: ./packages/check-package-manager
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# Jetbrains
.idea

# dependencies
node_modules
.pnp
Expand Down
16 changes: 0 additions & 16 deletions packages/check-package-manager/.eslintrc

This file was deleted.

27 changes: 27 additions & 0 deletions packages/check-package-manager/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
run:
timeout: 5m
modules-download-mode: readonly
go: "1.19"

linters:
enable:
- errcheck
- goimports
- gosimple
- govet
- ineffassign
- nakedret
- revive
- staticcheck
- typecheck
- unused

linters-settings:
nakedret:
# Aggressively disallow naked returns
max-func-lines: 3

issues:
exclude-use-default: false
max-issues-per-linter: 0
max-same-issues: 0
8 changes: 8 additions & 0 deletions packages/check-package-manager/.goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
builds:
- binary: check-package-manager
goos:
- windows
- darwin
- linux
goarch:
- amd64
1 change: 1 addition & 0 deletions packages/check-package-manager/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO
2 changes: 0 additions & 2 deletions packages/check-package-manager/bin/cli.js

This file was deleted.

50 changes: 50 additions & 0 deletions packages/check-package-manager/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Package cmd holds the root cobra command
package cmd

import (
"fmt"
"os"

pm "check-package-manager/packageManager"
"check-package-manager/utils"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "check-package-manager",
Version: "1.0.0",
Short: "A simple check of the usage of the correct package manager",
Long: `Check if the correct package manager is used.
Available are 'NPM', 'Yarn' and 'PNPM'.`,
Run: func(cmd *cobra.Command, args []string) {
chosenPackageManager := pm.GetSelectedPackageManager(args)
errors := pm.GetCheckLockFilesErrors(chosenPackageManager)
hasErrors := len(errors) > 0

if hasErrors {
utils.PrintInvalidMessage()

for _, err := range errors {
fmt.Println(err)
}
os.Exit(1)
}

utils.PrintValidMessage()
},
}

// Execute executes the root command
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
rootCmd.Flags().BoolP(pm.NPM.Name, pm.NPM.ShortFlag, false, pm.NPM.Description)
rootCmd.Flags().BoolP(pm.PNPM.Name, pm.PNPM.ShortFlag, false, pm.PNPM.Description)
rootCmd.Flags().BoolP(pm.YARN.Name, pm.YARN.ShortFlag, false, pm.YARN.Description)
}
10 changes: 10 additions & 0 deletions packages/check-package-manager/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module check-package-manager

go 1.19

require github.com/spf13/cobra v1.6.1

require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
14 changes: 14 additions & 0 deletions packages/check-package-manager/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 8 additions & 0 deletions packages/check-package-manager/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Package main provides the entry point for the CLI.
package main

import "check-package-manager/cmd"

func main() {
cmd.Execute()
}
28 changes: 12 additions & 16 deletions packages/check-package-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,23 @@
},
"license": "MIT",
"author": "Alexander Böhm <[email protected]>",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"check-package-manager": "./bin/cli.js"
},
"files": [
"bin",
"dist"
],
"scripts": {
"build": "tsc",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"lint": "eslint **/*.ts --fix",
"sort-package-json": "pnpm dlx sort-package-json"
"postinstall": "is-ci || go-npm install",
"sort-package-json": "pnpm dlx sort-package-json",
"preuninstall": "go-npm uninstall"
},
"dependencies": {
"@go-task/go-npm": "^0.1.15"
},
"devDependencies": {
"@mheob/eslint-config": "workspace:*",
"@mheob/tsconfig": "workspace:*",
"eslint": "^8.26.0"
"is-ci": "^3.0.1"
},
"publishConfig": {
"access": "public"
},
"goBinary": {
"name": "check-package-manager",
"path": "./bin",
"url": "https://github.com/mheob/config/releases/download/%40mheob%2Fcheck-package-manager%40{{version}}/check-package-manager_{{version}}_{{platform}}_{{arch}}.tar.gz"
}
}
42 changes: 42 additions & 0 deletions packages/check-package-manager/packageManager/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Package packageManager holds the package manager related functions
package packageManager

import "check-package-manager/utils"

// GetSelectedPackageManager returns the selected package manager; default is PNPM
func GetSelectedPackageManager(args []string) PackageManager {
if len(args) == 0 {
return PNPM
}

switch args[0] {
case NPM.Name:
return NPM
case YARN.Name:
return YARN
default:
return PNPM
}
}

// GetCheckLockFilesErrors returns a list of errors if the lock files are not correct
func GetCheckLockFilesErrors(selectedPackageManager PackageManager) []string {
var errors []string

for _, packageManager := range []PackageManager{NPM, PNPM, YARN} {
if packageManager.Name == selectedPackageManager.Name {
if !utils.FileExists(packageManager.LockFile) {
errors = append(errors, utils.GetMissingLockFileMessage(packageManager.LockFile))
}
} else {
if utils.FileExists(packageManager.LockFile) {
errors = append(
errors,
utils.GetInvalidFileMessage(packageManager.LockFile, selectedPackageManager.LockFile),
)
}
}
}

return errors
}
33 changes: 33 additions & 0 deletions packages/check-package-manager/packageManager/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package packageManager

// PackageManager structure of possible package manager
type PackageManager struct {
Name string
Description string
ShortFlag string
LockFile string
}

// NPM Package Manager
var NPM = PackageManager{
Name: "npm",
Description: "Use NPM as package manager",
ShortFlag: "n",
LockFile: "package-lock.json",
}

// PNPM Package Manager
var PNPM = PackageManager{
Name: "pnpm",
Description: "Use PNPM as package manager",
ShortFlag: "p",
LockFile: "pnpm-lock.yaml",
}

// YARN Package Manager
var YARN = PackageManager{
Name: "yarn",
Description: "Use YARN as package manager",
ShortFlag: "y",
LockFile: "yarn.lock",
}
39 changes: 0 additions & 39 deletions packages/check-package-manager/src/check.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/check-package-manager/src/index.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/check-package-manager/src/messages.ts

This file was deleted.

Loading