Skip to content

Commit

Permalink
feat: check version on npmjs
Browse files Browse the repository at this point in the history
  • Loading branch information
vafanassieff committed May 28, 2022
1 parent a0954e1 commit 6baa272
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
12 changes: 3 additions & 9 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import 'zx/globals'
import console from 'node:console'
import fs from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import { URL } from 'node:url'

import { Command } from 'commander'

Expand All @@ -15,26 +12,23 @@ import list from '#src/base/list.js'
import printConfiguration from '#src/base/print-configuration.js'
import remove from '#src/base/remove.js'
import configure from '#src/provider/configure.js'
import versions from '#src/versions.js'

const program = new Command()

const provider = new Command('provider')

$.verbose = false

const { version } = await fs
.readFile(
path.join(new URL('.', import.meta.url).pathname, '../package.json')
)
.then(JSON.parse)
const { current } = await versions()

provider
.command('configure')
.description('Configure a provider')
.argument('<provider>', 'Provider to configure')
.action(configure)

program.name('email-alias').description('Manage email alias').version(version)
program.name('email-alias').description('Manage email alias').version(current)

program.command('list').description('List current alias').action(list)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vafanassieff/email-alias",
"version": "0.1.4",
"version": "0.1.5",
"description": "Create a email alias to keep you real email hidden !",
"main": "src/index.js",
"repository": {
Expand Down
21 changes: 21 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,24 @@ export const getProviders = async () => {
const files = await fs.readdir(providerFolder)
return files.map((p) => p.split('.js')[0])
}

/**
* Fetch an URL with options
*
* @param {string} url Url to fetch
* @param {object} options Fetch options
* @returns {Promise} Result of the fetch
*/
export const request = async (url, options) => {
const response = await fetch(url, options)

if (response.ok) {
return response.json()
} else {
const data = await response.json()

const error = data.message ? new Error(data.message) : new Error(data)

throw error
}
}
39 changes: 39 additions & 0 deletions src/versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import console from 'node:console'
import fs from 'node:fs/promises'
import path from 'node:path'
import { URL } from 'node:url'

import { request } from '#src/utils.js'

/**
* @typedef {object} Versions
* @property {string} latest Latest semver
* @property {string} current Insttaled semver
*/

/**
* Get the installed version and the latest remote version of email-alias
*
* @returns {Versions} latest abd current version
*/
export default async () => {
const { latest } = await request(
'https://registry.npmjs.org/-/package/@vafanassieff/email-alias/dist-tags'
)

const { version: current } = await fs
.readFile(
path.join(new URL('.', import.meta.url).pathname, '../package.json')
)
.then(JSON.parse)

if (current !== latest) {
console.log(`There is a new email-alias version !`)
console.log(`You are running the ${current} latest is ${latest}`)
console.log(
'To update the version run npm update -g @vafanassieff/email-alias'
)
}

return { current, latest }
}

0 comments on commit 6baa272

Please sign in to comment.