Skip to content

Commit

Permalink
feat(cli): improve versioning (#212)
Browse files Browse the repository at this point in the history
* feat(cli): improve versionning

* chore: add changesets
  • Loading branch information
QuiiBz authored Oct 25, 2022
1 parent 763ac50 commit 5fe9e73
Show file tree
Hide file tree
Showing 9 changed files with 426 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-cooks-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/cli': patch
---

Notify for new release
5 changes: 5 additions & 0 deletions .changeset/quiet-ligers-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/cli': patch
---

Show correct version with --version
13 changes: 7 additions & 6 deletions packages/cli/npm/getBinary.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Binary } = require('binary-install');
const os = require('node:os');
import { createRequire } from 'node:module';
import { Binary } from 'binary-install';
import os from 'node:os';

function getPlatform() {
const type = os.type();
Expand Down Expand Up @@ -36,16 +37,16 @@ function getPlatform() {
throw new Error(`Unsupported platform: ${type} ${arch}`);
}

function getBinary() {
export function getBinary() {
// Prevent exiting with code 1
process.exit = () => { };

const { platform, name } = getPlatform();
const { name: packageName, version } = require('../package.json');
const customRequire = createRequire(import.meta.url);

const { name: packageName, version } = customRequire('../package.json');

const url = `https://github.com/lagonapp/lagon/releases/download/${packageName}@${version}/${platform}.tar.gz`;

return new Binary(name, url);
}

module.exports = getBinary;
5 changes: 3 additions & 2 deletions packages/cli/npm/install.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node

try {
const getBinary = require('./getBinary');
getBinary().install();
import('./getBinary.js').then(({ getBinary }) => {
getBinary().install();
});
} catch (error) {
console.error(error);
}
10 changes: 9 additions & 1 deletion packages/cli/npm/run.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#!/usr/bin/env node

const getBinary = require('./getBinary');
import { createRequire } from 'node:module';
import updateNotifier from 'update-notifier';
import { getBinary } from './getBinary.js';

const customRequire = createRequire(import.meta.url);
const pkg = customRequire('../package.json');

updateNotifier({ pkg }).notify();

const binary = getBinary();

// Try to install the binary before executing the CLI
Expand Down
8 changes: 0 additions & 8 deletions packages/cli/npm/uninstall.js

This file was deleted.

10 changes: 7 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@lagon/cli",
"version": "0.3.1",
"description": "CLI for Lagon",
"type": "module",
"files": [
"npm"
],
Expand All @@ -11,10 +12,13 @@
"scripts": {
"build": "cargo build",
"lint": "cargo clippy -- -Dwarnings --no-deps",
"postinstall": "node npm/install.js",
"preuninstall": "node npm/uninstall.js"
"postinstall": "node npm/install.js"
},
"dependencies": {
"binary-install": "^1.0.6"
"binary-install": "^1.0.6",
"update-notifier": "^6.0.2"
},
"devDependencies": {
"@types/update-notifier": "^6.0.1"
}
}
20 changes: 19 additions & 1 deletion packages/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use serde::Deserialize;

use crate::utils::error;

mod commands;
mod utils;

static PACKAGE_JSON: &str = include_str!("../package.json");

#[derive(Deserialize)]
struct PackageJson {
version: String,
}

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, arg_required_else_help = true)]
#[command(author, about, long_about = None, arg_required_else_help = true)]
struct Cli {
#[clap(subcommand)]
command: Option<Commands>,
/// Print version information
#[clap(short, long)]
version: bool,
}

#[derive(Subcommand, Debug)]
Expand Down Expand Up @@ -103,5 +114,12 @@ async fn main() {
} {
println!("{}", error(&err.to_string()));
}
} else {
match serde_json::from_str(PACKAGE_JSON) {
Ok(PackageJson { version }) => {
println!("{}", version);
}
_ => println!("{}", error("Couldn't extract version from package.json")),
}
}
}
Loading

0 comments on commit 5fe9e73

Please sign in to comment.