Skip to content

Commit

Permalink
Merge pull request #2 from Icaruk:dev
Browse files Browse the repository at this point in the history
3.0.0
  • Loading branch information
Icaruk authored Apr 15, 2023
2 parents c7f6f1a + 1d2f035 commit c519487
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 73 deletions.
2 changes: 0 additions & 2 deletions .env.example

This file was deleted.

21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@ CLI tool written in Go to review and update your NPM dependencies, easy and fast

![](https://i.imgur.com/8AUJFVb.png)


# Features

- Update each package one by one
- Check version update before updating it: patch, minor or major
- Review what's new on each package before updating it
- 🔍 **Easily identify the update type** for each package, whether it's a patch, minor, or major update.
- 📃 Review the **release notes** for each package to see "what's new" before deciding whether to update.
- 🦘 Selectively **skip** updates for specific packages.
- 🛡️ **Back up** your `package.json` file before updating, ensuring you always have a fallback option if something goes wrong.


# Usage

Go where your `package.json` is located and run:

```bash
up-npm [flags]
up-npm
```

or

```bash
up-npm [flags]
```

| Flag | Description |
|--------------------- |-----------------------------------------------|
| -d, --dev | Update dev dependencies |
| -d, --dev | Include dev dependencies |
| -f, --filter `string` | Filter dependencies by package name |
| -h, --help | Display help information for up-npm |
| -v, --version | Display the version number for up-npm |
Expand Down Expand Up @@ -51,8 +56,8 @@ npm-up -f lint
# Build yourself

- Prerequisites:
- [Go 1.20](https://go.dev/doc/install)
- [Node 18](https://nodejs.org/en/download)
- [Go 1.20+](https://go.dev/doc/install)
- [Node 18+](https://nodejs.org/en/download)
- [Taskfile](https://taskfile.dev)
- Then run:
```bash
Expand Down
10 changes: 5 additions & 5 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

version: '3'

dotenv: [".env"]

vars:
DIST_FOLDER: dist
APPNAME: $APP_NAME
VERSION: $VERSION
DIST_FOLDER: "dist"
APPNAME: "up-npm"
VERSION:
sh: '{{if eq OS "windows"}}more version{{else}}cat version{{end}}'

tasks:
build:
cmds:
- echo "Building version {{.VERSION}}"
- CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -trimpath -tags=netgo -o {{.DIST_FOLDER}}/{{.APPNAME}}-{{.VERSION}}-windows-amd64.exe
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -trimpath -tags=netgo -o {{.DIST_FOLDER}}/{{.APPNAME}}-{{.VERSION}}-darwin-amd64
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -trimpath -tags=netgo -o {{.DIST_FOLDER}}/{{.APPNAME}}-{{.VERSION}}-linux-amd64
Expand Down
26 changes: 20 additions & 6 deletions cmd/updater/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ package updater
import (
"fmt"
"os"
"path/filepath"

"github.com/icaruk/updatenpm/pkg/updater"
"github.com/spf13/cobra"
)

var version = "2.3.1"
var dev bool

var rootCmd = &cobra.Command{
Use: "up-npm",
Version: version,
Short: "Updates npm depeendencies",
Long: `up-npm is a easy way to keep your npm depeendencies up to date.`,
Use: "up-npm",
Short: "Updates npm depeendencies",
Long: `up-npm is a easy way to keep your npm depeendencies up to date.`,
RunE: func(cmd *cobra.Command, args []string) error {
devFlag, err := cmd.Flags().GetBool("dev")
if err != nil {
Expand All @@ -33,8 +32,23 @@ var rootCmd = &cobra.Command{
}

func init() {
rootCmd.Flags().BoolVarP(&dev, "dev", "d", false, "Update dev dependencies")
rootCmd.Flags().BoolVarP(&dev, "dev", "d", false, "Include dev dependencies")
rootCmd.Flags().StringP("filter", "f", "", "Filter dependencies by package name")

binaryPath, err := os.Executable()
if err != nil {
fmt.Println(err)
}

localVersionPath := filepath.Join(filepath.Dir(binaryPath), "../version")

localVersion := "0.0.0"
localVersionByte, err := os.ReadFile(localVersionPath)
if err == nil {
localVersion = string(localVersionByte)
}

rootCmd.Version = string(localVersion)
}

func Execute() {
Expand Down
3 changes: 0 additions & 3 deletions hashes.sha256

This file was deleted.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "up-npm",
"version": "2.3.2",
"version": "3.0.0",
"author": "Icaruk",
"scripts": {
"postinstall": "node ./scripts/setup.js"
Expand All @@ -26,11 +26,11 @@
"package"
],
"files": [
"dist",
"scripts",
"up-npm"
"bin",
"version"
],
"bin": {
"up-npm": "up-npm"
"up-npm": "bin/up-npm"
}
}
175 changes: 130 additions & 45 deletions scripts/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,139 @@ const path = require("path");

const platform = os.platform();

const distPath = path.join(__dirname, "../dist");
const scriptsPath = path.join(__dirname, "../scripts");
const binPath = path.join(__dirname, "..");
const currentVersionPath = "version";
const binPath = path.join(__dirname, "../bin");

const appName = "up-npm";
const version = "2.3.1";

let distFilename = "";
let isWindows = false;


switch (platform) {
case "win32":
distFilename = `${appName}-${version}-windows-amd64.exe`;
isWindows = true;
break;
case "darwin":
distFilename = `${appName}-${version}-darwin-amd64`;
break;
case "linux":
distFilename = `${appName}-${version}-linux-amd64`;
break;
default:
console.error("Unsupported platform:", platform);
process.exit(1);
}

const suffix = isWindows ? ".exe" : "";
const binFilename = `${appName}${suffix}`;

const source = path.join(distPath, distFilename);
const destination = path.join(binPath, binFilename);

if (!fs.existsSync(source)) {
console.error(`${source} does not exist`);
process.exit(1);
};


const emptyBinPath = path.join(binPath, appName);
if (fs.existsSync(emptyBinPath)) fs.rmSync(emptyBinPath)
fs.copyFileSync(source, destination);

if (!isWindows) {
// "chmod +rwx up-npm"
fs.chmodSync(destination, 0o700);
}
/**
* Compare two semantic version strings and return 1 if the first version is
* greater, -1 if the second version is greater, or 0 if they are equal.
*
* @param {string} versionA The first version string to compare.
* @param {string} versionB The second version string to compare.
* @returns {-1 | 0 | 1} 1 if versionA > versionB, -1 if versionB > versionA, or 0 if they are equal.
*/
function compareSemver(versionA, versionB) {
const arr1 = versionA.split(".").map(n => parseInt(n));
const arr2 = versionB.split(".").map(n => parseInt(n));

for (let i = 0; i < arr1.length; i++) {
if (arr1[i] > arr2[i]) {
return 1;
} else if (arr2[i] > arr1[i]) {
return -1;
}
}

return 0;
};

async function downloadBinary() {
try {
// Get current version
let currentVersion = "0.0.0";

if (fs.existsSync(currentVersionPath)) {
currentVersion = fs.readFileSync("version").toString();
console.log( `Current version: ${currentVersion}` )
};

console.log( "Fetching latest version..." )
const responseLatestVersion = await fetch("https://api.github.com/repos/Icaruk/up-npm/releases/latest");
const json = await responseLatestVersion.json();

const latestVersion = json.tag_name;
console.log( "OK. Latest version found: ", latestVersion );


const semverCompareResult = compareSemver(latestVersion, currentVersion);
// 1 must upgrade
// 0 up to date
// -1 (impossible) local version is greater than remote one

if (semverCompareResult === 0) {
console.log( "You are already up-to-date!" );
process.exit(1);
};

let remoteBinaryName = "";
let localBinaryName = appName;
let isWindows = false;
let platformName = "";
let archName = "amd64";


switch (platform) {
case "win32":
platformName = `windows`;
isWindows = true;
break;
case "darwin":
platformName = `darwin`;
break;
case "linux":
platformName = `linux`;
break;
default:
console.error("Unsupported platform:", platform);
process.exit(1);
};

remoteBinaryName = `${appName}-${latestVersion}-${platformName}-${archName}`;

if (isWindows) {
remoteBinaryName += ".exe";
localBinaryName += ".exe";
};

const assets = json.assets ?? [];

// https://regex101.com/r/rfH9Fe/1
const regex = new RegExp(`(${platformName}-${archName})(\.exe)?$`, "gi")
const foundAsset = assets.find( _asset => regex.test(_asset.name) );

if (!foundAsset) {
console.log(`Can't find binary asset for ${remoteBinaryName}`);
process.exit(1);
};

const size = foundAsset.size ?? 0;
const sizeMB = +(size / (1024 * 1024)).toFixed(2);
console.log( `Found binary '${remoteBinaryName}' (${sizeMB} MB)` );

console.log( `Downloading ${remoteBinaryName}...` );
const downloadUrl = foundAsset.browser_download_url;
const responseDownload = await fetch(downloadUrl);
console.log( "OK" )

const data = await responseDownload.arrayBuffer();
const bufferView = new Uint8Array(data);

// Delete all contents of folder or create if it doesn't exists
if (fs.existsSync(binPath)) {
for (const _file of fs.readdirSync(binPath)) {
fs.unlinkSync(path.join(binPath, _file));
}
} else {
fs.mkdirSync(binPath);
}

fs.writeFileSync(path.join(binPath, localBinaryName), bufferView);

if (!isWindows) {
// "chmod +rwx up-npm"
fs.chmodSync(destination, 0o700);
}

fs.writeFileSync("version", latestVersion);

} catch (err) {
console.error(err)
};
};


// Cleanup
fs.rmdirSync(distPath, { recursive: true });
fs.rmdirSync(scriptsPath, { recursive: true });
downloadBinary();
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0.0
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0.0

0 comments on commit c519487

Please sign in to comment.