Skip to content
Draft
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
44 changes: 44 additions & 0 deletions checkVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { version } = require('./package.json');
const execa = require('execa')
const picocolors = require('picocolors')
const path = require('path');

const CACHE_FILE = path.join(__dirname, 'version_cache.json')

async function checkVersion () {
try {
// Check if the cache file exists
if (!fs.existsSync(CACHE_FILE)) {
// Create the cache file if it doesn't exist
await createCacheFile()
}

const cache = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8'))
const { version: cacheVersion, timestamp } = cache
let version = cacheVersion
// cache for 7 day
if (Date.now() - timestamp < 7 * 24 * 60 * 60 * 1000) {
return
}

const latest = execa.sync('npm', ['view', 'OwlTing/cz', 'version']).stdout
await updateCacheFile(latest)
if (cacheVersion !== latest) {
console.log(picocolors.gray(`🎉 owlting_cz v${latest} is available! please check the documentation for more information. (https://github.com/OwlTing/cz)`))
}
} catch (error) {
// do nothing if failed
}
}

async function createCacheFile () {
const initialCacheData = { version: '', timestamp: 0 }
fs.writeFileSync(CACHE_FILE, JSON.stringify(initialCacheData))
}

async function updateCacheFile (version) {
const cacheData = { version, timestamp: Date.now() }
fs.writeFileSync(CACHE_FILE, JSON.stringify(cacheData))
}

module.exports = checkVersion
3 changes: 3 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const types = require('./types')
const projects = require('./projects')
const picocolors = require('picocolors')
const fs = require('fs')
const checkVersion = require('./checkVersion')

checkVersion()

let defaultProjectValue = ''
try {
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": "owlting_cz",
"version": "2.2.0",
"version": "2.3.0",
"description": "create-commit for OwlTing",
"dependencies": {
"execa": "^5.1.1",
Expand Down
Loading