From 1f6c13a595e6a09c5fda02c485b3680259a33ea2 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Wed, 9 Dec 2020 00:07:03 +0100 Subject: [PATCH] fix locking issue --- package.json | 2 +- src/index.ts | 18 ++++++++---------- src/lock.ts | 7 +++++++ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index b317233..b2f0093 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "private": true, - "version": "0.26", + "version": "0.27", "scripts": { "build": "tsc", "dev": "tsc -w", diff --git a/src/index.ts b/src/index.ts index 16ee3e2..ab94c8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import colors from 'colors' import { program } from 'commander' import { setCIMode } from 'clitastic' -import { unlock, readLock, writeLock } from './lock' +import { unlock, readLock, writeLock, lock } from './lock' import { Config } from './types' import { init } from './config' import { version } from '../package.json' @@ -118,21 +118,19 @@ async function main() { try { if (requireConfig) { config = init(configFile) - const lock = readLock() - if (lock.running) throw new Error('An instance of autorestic is already running for this config file'.red) - - writeLock({ - ...lock, - running: true, - }) + const { running } = readLock() + if (running) { + console.log('An instance of autorestic is already running for this config file'.red) + process.exit(1) + } + lock() } - await queue() - if (error) process.exit(1) } catch (e) { console.error(e.message) } finally { if (requireConfig) unlock() } + if (error) process.exit(1) } main() diff --git a/src/lock.ts b/src/lock.ts index 55ded75..db35bfa 100644 --- a/src/lock.ts +++ b/src/lock.ts @@ -30,3 +30,10 @@ export const unlock = () => { running: false, }) } + +export const lock = () => { + writeLock({ + ...readLock(), + running: true, + }) +}