diff --git a/githubREADME.md b/githubREADME.md index 155f419..a1fc065 100644 --- a/githubREADME.md +++ b/githubREADME.md @@ -74,14 +74,14 @@ npm unlink bl [command] ``` -| Command | Description | -| --------------------- | ------------------------------------ | -| `pick` | checkout git branch from select list | -| `create` | create new git branch | -| `delete` | delete git branch from select list | -| `-v --version` | show package version | -| `-h --help` | show help menu | -| `[command] -h --help` | show help menu for specific command | +| Command | Description | +| --------------------- | ------------------------------------------------------ | +| `pick` | checkout git branch from select list with fuzzy search | +| `create` | create new git branch | +| `delete` | delete git branch from select list with fuzzy search | +| `-v --version` | show package version | +| `-h --help` | show help menu | +| `[command] -h --help` | show help menu for specific command | ## Demo diff --git a/npmREADME.md b/npmREADME.md index c4e0a04..12e79be 100644 --- a/npmREADME.md +++ b/npmREADME.md @@ -53,14 +53,14 @@ npm install -g @thimlohsedev/branch_lightyear bl [command] ``` -| Command | Description | -| --------------------- | ------------------------------------ | -| `pick` | checkout git branch from select list | -| `create` | create new git branch | -| `delete` | delete git branch from select list | -| `-v --version` | show package version | -| `-h --help` | show help menu | -| `[command] -h --help` | show help menu for specific command | +| Command | Description | +| --------------------- | ------------------------------------------------------ | +| `pick` | checkout git branch from select list with fuzzy search | +| `create` | create new git branch | +| `delete` | delete git branch from select list with fuzzy search | +| `-v --version` | show package version | +| `-h --help` | show help menu | +| `[command] -h --help` | show help menu for specific command | ## Demo diff --git a/package-lock.json b/package-lock.json index fc8e084..d84e5a3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@thimlohsedev/branch_lightyear", - "version": "1.8.0", + "version": "1.9.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -143,6 +143,12 @@ "chalk": "*" } }, + "@types/configstore": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@types/configstore/-/configstore-4.0.0.tgz", + "integrity": "sha512-SvCBBPzOIe/3Tu7jTl2Q8NjITjLmq9m7obzjSyb8PXWWZ31xVK6w4T6v8fOx+lrgQnqk3Yxc00LDolFsSakKCA==", + "dev": true + }, "@types/inquirer": { "version": "7.3.1", "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-7.3.1.tgz", @@ -166,15 +172,15 @@ "dev": true }, "@types/node": { - "version": "14.11.5", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.5.tgz", - "integrity": "sha512-jVFzDV6NTbrLMxm4xDSIW/gKnk8rQLF9wAzLWIOg+5nU6ACrIMndeBdXci0FGtqJbP9tQvm6V39eshc96TO2wQ==", + "version": "14.11.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.8.tgz", + "integrity": "sha512-KPcKqKm5UKDkaYPTuXSx8wEP7vE9GnuaXIZKijwRYcePpZFDVuy2a57LarFKiORbHOuTOOwYzxVxcUzsh2P2Pw==", "dev": true }, "@types/ramda": { - "version": "0.27.20", - "resolved": "https://registry.npmjs.org/@types/ramda/-/ramda-0.27.20.tgz", - "integrity": "sha512-Oxi5JMT6YIiMLP0gir8Eiy22f1CfbKLGGisu6tDrLCl5gHQd0EeUTUCfy7zdWfV19WEU/ccUVBfKOh2B1c2DbQ==", + "version": "0.27.23", + "resolved": "https://registry.npmjs.org/@types/ramda/-/ramda-0.27.23.tgz", + "integrity": "sha512-gcg91dmYVCt1c7tPr76iON1HF3YA09D77A7T5z/KgVY4SSlM7o2bnek1HQZ/JhLlbeUSmRcKMJ2f2D8d97qQMw==", "dev": true, "requires": { "ts-toolbelt": "^6.3.3" @@ -202,6 +208,14 @@ "regexpp": "^3.0.0", "semver": "^7.3.2", "tsutils": "^3.17.1" + }, + "dependencies": { + "semver": { + "version": "7.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", + "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==", + "dev": true + } } }, "@typescript-eslint/experimental-utils": { @@ -260,6 +274,14 @@ "lodash": "^4.17.15", "semver": "^7.3.2", "tsutils": "^3.17.1" + }, + "dependencies": { + "semver": { + "version": "7.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", + "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==", + "dev": true + } } }, "@typescript-eslint/visitor-keys": { @@ -455,6 +477,19 @@ "typedarray": "^0.0.6" } }, + "configstore": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/configstore/-/configstore-5.0.1.tgz", + "integrity": "sha512-aMKprgk5YhBNyH25hj8wGt2+D52Sw1DRRIzqBwLp2Ya9mFmY8KPvvtvmna8SxVR9JMZ4kzMD68N22vlaRpkeFA==", + "requires": { + "dot-prop": "^5.2.0", + "graceful-fs": "^4.1.2", + "make-dir": "^3.0.0", + "unique-string": "^2.0.0", + "write-file-atomic": "^3.0.0", + "xdg-basedir": "^4.0.0" + } + }, "core-util-is": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", @@ -472,6 +507,11 @@ "which": "^2.0.1" } }, + "crypto-random-string": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz", + "integrity": "sha512-v1plID3y9r/lPhviJ1wrXpLeyUIGAZ2SHNYTEapm7/8A9nLPoyvVp3RK/EPFqn5kEznyWgYZNsRtYYIWbuG8KA==" + }, "debug": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/debug/-/debug-4.2.0.tgz", @@ -519,6 +559,14 @@ "esutils": "^2.0.2" } }, + "dot-prop": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-5.3.0.tgz", + "integrity": "sha512-QM8q3zDe58hqUqjraQOmzZ1LIH9SWQJTlEKCH4kJ2oQvLZk7RbQXvtDM2XEq3fwkV9CCvvH4LA0AV+ogFsBM2Q==", + "requires": { + "is-obj": "^2.0.0" + } + }, "emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", @@ -539,9 +587,9 @@ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, "eslint": { - "version": "7.10.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-7.10.0.tgz", - "integrity": "sha512-BDVffmqWl7JJXqCjAK6lWtcQThZB/aP1HXSH1JKwGwv0LQEdvpR7qzNrUT487RM39B5goWuboFad5ovMBmD8yA==", + "version": "7.11.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-7.11.0.tgz", + "integrity": "sha512-G9+qtYVCHaDi1ZuWzBsOWo2wSwd70TXnU6UHA3cTYHp7gCTXZcpggWFoUVAMRarg68qtPoNfFbzPh+VdOgmwmw==", "dev": true, "requires": { "@babel/code-frame": "^7.0.0", @@ -554,7 +602,7 @@ "enquirer": "^2.3.5", "eslint-scope": "^5.1.1", "eslint-utils": "^2.1.0", - "eslint-visitor-keys": "^1.3.0", + "eslint-visitor-keys": "^2.0.0", "espree": "^7.3.0", "esquery": "^1.2.0", "esutils": "^2.0.2", @@ -583,17 +631,17 @@ "v8-compile-cache": "^2.0.3" }, "dependencies": { - "eslint-visitor-keys": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-1.3.0.tgz", - "integrity": "sha512-6J72N8UNa462wa/KFODt/PJ3IU60SDpC3QXC1Hjc1BXXpfL2C9R5+AU7jhe0F6GREqVMh4Juu+NY7xn+6dipUQ==", - "dev": true - }, "ignore": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz", "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true + }, + "semver": { + "version": "7.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", + "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==", + "dev": true } } }, @@ -882,6 +930,11 @@ "slash": "^3.0.0" } }, + "graceful-fs": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz", + "integrity": "sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==" + }, "has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", @@ -914,8 +967,7 @@ "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", - "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=", - "dev": true + "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=" }, "inflight": { "version": "1.0.6", @@ -996,6 +1048,16 @@ "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "dev": true }, + "is-obj": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz", + "integrity": "sha512-drqDG3cbczxxEJRoOXcOjtdp1J/lyp1mNn0xaznRs8+muBhgQcrnbspox5X5fOw0HnMnbfDzvnEMEtqDEJEo8w==" + }, + "is-typedarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", + "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -1069,6 +1131,14 @@ "yallist": "^2.1.2" } }, + "make-dir": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", + "integrity": "sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==", + "requires": { + "semver": "^6.0.0" + } + }, "make-error": { "version": "1.3.6", "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", @@ -1404,10 +1474,9 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, "semver": { - "version": "7.3.2", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", - "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ==", - "dev": true + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", + "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==" }, "shebang-command": { "version": "2.0.0", @@ -1692,12 +1761,28 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", "dev": true }, + "typedarray-to-buffer": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz", + "integrity": "sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==", + "requires": { + "is-typedarray": "^1.0.0" + } + }, "typescript": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.3.tgz", "integrity": "sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==", "dev": true }, + "unique-string": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/unique-string/-/unique-string-2.0.0.tgz", + "integrity": "sha512-uNaeirEPvpZWSgzwsPGtU2zVSTrn/8L5q/IexZmH0eH6SA73CmAA5U4GwORTxQAZs95TAXLNqeLoPPNO5gZfWg==", + "requires": { + "crypto-random-string": "^2.0.0" + } + }, "uri-js": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.0.tgz", @@ -1757,6 +1842,22 @@ "mkdirp": "^0.5.1" } }, + "write-file-atomic": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-3.0.3.tgz", + "integrity": "sha512-AvHcyZ5JnSfq3ioSyjrBkH9yW4m7Ayk8/9My/DD9onKeu/94fwrMocemO2QAJFAlnnDN+ZDS+ZjAR5ua1/PV/Q==", + "requires": { + "imurmurhash": "^0.1.4", + "is-typedarray": "^1.0.0", + "signal-exit": "^3.0.2", + "typedarray-to-buffer": "^3.1.5" + } + }, + "xdg-basedir": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz", + "integrity": "sha512-PSNhEJDejZYV7h50BohL09Er9VaIefr2LMAf3OEmpCkjOi34eYyQYAXUTjEQtZJTKcF0E2UKTh+osDLsgNim9Q==" + }, "yallist": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", diff --git a/package.json b/package.json index 6e62666..4f772d5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@thimlohsedev/branch_lightyear", - "version": "1.8.0", - "description": "The comfort of Git in an IDE right in the terminal", + "version": "1.9.0", + "description": "The comfort of some pretty sleek Git commands right in the terminal", "bin": { "@thimlohsedev/bl": "bin/branch_lightyear", "bl": "bin/branch_lightyear" @@ -58,6 +58,7 @@ "license": "MIT", "dependencies": { "chalk": "^4.1.0", + "configstore": "^5.0.1", "esm": "^3.2.25", "fuzzy": "^0.1.3", "inquirer": "^7.3.3", @@ -73,6 +74,7 @@ "@types/minimist": "^1.2.0", "@types/node": "^14.11.2", "@types/ramda": "^0.27.19", + "@types/configstore": "^4.0.0", "@typescript-eslint/eslint-plugin": "^4.3.0", "@typescript-eslint/parser": "^4.3.0", "eslint": "^7.10.0", diff --git a/src/commands/helpHandler.ts b/src/commands/helpHandler.ts index 89bc340..e2ae144 100644 --- a/src/commands/helpHandler.ts +++ b/src/commands/helpHandler.ts @@ -22,7 +22,10 @@ const helpDescription: Record = { pick: ` bl pick - -r, --remote ....... checkout remote branch with fuzzy search functionality + -r, --remote ....... checkout remote branch with fuzzy search functionality + (Note: will fetch from cache or fetch and automatically save most recent list in cache for 5 minutes) + -f --fresh ......... pick branch from fresh remote + (Note: will overwrite stored list of remote branches in cache with new list [remote only]) `, create: ` bl create diff --git a/src/commands/pickHandler.ts b/src/commands/pickHandler.ts index f2455c1..7e577b5 100644 --- a/src/commands/pickHandler.ts +++ b/src/commands/pickHandler.ts @@ -10,5 +10,5 @@ export default async (argv: ParsedArgs): Promise => { if (!argv.r) { return await pickLocalBranch() } - return await pickRemoteBranch() + return await pickRemoteBranch({ freshFetch: !!argv.f }) } diff --git a/src/entry/cli.ts b/src/entry/cli.ts index e235bee..a0fbd7d 100644 --- a/src/entry/cli.ts +++ b/src/entry/cli.ts @@ -6,13 +6,14 @@ import { ErrorHandler } from '../handlers' const parseArgs = (rawArgs: string[]) => minimist(rawArgs.slice(2), { string: ['pick', 'delete', 'create'], - boolean: ['version', 'help', 'multiple', 'remote', 'upstream'], + boolean: ['version', 'help', 'multiple', 'remote', 'upstream', 'fresh'], alias: { v: 'version', h: 'help', m: 'multiple', r: 'remote', u: 'upstream', + f: 'fresh', }, }) diff --git a/src/helpers/branchListPromptPromise.ts b/src/helpers/branchListPromptPromise.ts index e7ad017..a3118a9 100644 --- a/src/helpers/branchListPromptPromise.ts +++ b/src/helpers/branchListPromptPromise.ts @@ -16,6 +16,7 @@ interface BranchListPromptParams { multipleChoice?: boolean fetchRemote?: boolean withSearch?: boolean + freshFetch?: boolean } export default async function ({ message, @@ -23,8 +24,9 @@ export default async function ({ multipleChoice = false, fetchRemote = false, withSearch = false, + freshFetch = false, }: BranchListPromptParams): Promise { - const branchList = await getBranchList({ fetchRemote }) + const branchList = await getBranchList({ fetchRemote, freshFetch }) let questions: Question[] = [] diff --git a/src/helpers/getBranchList.ts b/src/helpers/getBranchList.ts index 428ed51..4162a8a 100644 --- a/src/helpers/getBranchList.ts +++ b/src/helpers/getBranchList.ts @@ -1,5 +1,9 @@ import util from 'util' import { anyPass, isNil, isEmpty } from 'ramda' +import configStoreApi, { + ConfigStoreApi, + remoteBranchesKey, +} from '../persistantLocalStore' import { NoBranchError } from '../errors' import childProcess from 'child_process' const exec = util.promisify(childProcess.exec) @@ -11,6 +15,7 @@ interface FormatBranchesStringOptions { } interface GetBranchListParams { fetchRemote?: boolean + freshFetch?: boolean } interface FilterNewBranchesParams { @@ -23,6 +28,15 @@ const gitListLocalBranchesScript = `git branch` const gitListRemoteBranchesScript = `git fetch --prune && git branch -r` /* Util functions */ + +const shouldFetchFromLocalStore = ( + configStore: ConfigStoreApi, + freshFetch?: boolean +): boolean => + !freshFetch && + configStore.hasSavedRemoteBranches() && + !configStore.isLocalStorageStale() + const filterNewOnRemote = (parameters: FilterNewBranchesParams) => { const remote = new Set(parameters.remoteBranches) const local = new Set(parameters.localBranches) @@ -33,7 +47,7 @@ const filterNewOnRemote = (parameters: FilterNewBranchesParams) => { const formatBranchesString = (options: FormatBranchesStringOptions) => { const { localBranchesString, remoteBranchesString } = options - const localBranches = localBranchesString + let localBranches = localBranchesString .split('\n') .filter((branch) => !branch.includes('*')) .map((branch) => branch.trim()) @@ -41,19 +55,16 @@ const formatBranchesString = (options: FormatBranchesStringOptions) => { if (!remoteBranchesString) { return localBranches } - if (remoteBranchesString) { - const localBranches = localBranchesString - .split('\n') - .map((branch) => branch.replace('*', '')) - .map((branch) => branch.trim()) - const remoteBranches = remoteBranchesString - .split('\n') - .filter((branch) => !branch.includes('HEAD')) - .map((branch) => branch.split('origin/')[1]) - return filterNewOnRemote({ localBranches, remoteBranches }) - } - return + localBranches = localBranchesString + .split('\n') + .map((branch) => branch.replace('*', '')) + .map((branch) => branch.trim()) + const remoteBranches = remoteBranchesString + .split('\n') + .filter((branch) => !branch.includes('HEAD')) + .map((branch) => branch.split('origin/')[1]) + return filterNewOnRemote({ localBranches, remoteBranches }) } export default async function ( @@ -66,9 +77,14 @@ export default async function ( )?.stdout.trim() if (options?.fetchRemote) { - remoteBranches = ( - await exec(gitListRemoteBranchesScript) - )?.stdout.trim() + if (shouldFetchFromLocalStore(configStoreApi, options.freshFetch)) { + remoteBranches = configStoreApi.get(remoteBranchesKey) + } else { + remoteBranches = ( + await exec(gitListRemoteBranchesScript) + )?.stdout.trim() + configStoreApi.store(remoteBranchesKey, remoteBranches) + } branchList = formatBranchesString({ localBranchesString: localBranches, remoteBranchesString: remoteBranches, diff --git a/src/helpers/pickRemoteBranch.ts b/src/helpers/pickRemoteBranch.ts index 3f2bc8c..438d43c 100644 --- a/src/helpers/pickRemoteBranch.ts +++ b/src/helpers/pickRemoteBranch.ts @@ -4,14 +4,20 @@ import branchListPromptPromise from './branchListPromptPromise' import { PickBranchError } from '../errors' import childProcess from 'child_process' const exec = util.promisify(childProcess.exec) - -export default async function (): Promise { +/* Interfaces */ +interface PickRemoteBranchOptions { + freshFetch?: boolean +} +export default async function ( + options: PickRemoteBranchOptions +): Promise { let spinner = ora() try { const { branch } = await branchListPromptPromise({ message: 'Which new (remote) branch would you like to check out?', fetchRemote: true, withSearch: true, + freshFetch: options.freshFetch, }) if (branch) { diff --git a/src/persistantLocalStore/index.ts b/src/persistantLocalStore/index.ts new file mode 100644 index 0000000..4782fd2 --- /dev/null +++ b/src/persistantLocalStore/index.ts @@ -0,0 +1,52 @@ +import ConfigStore from 'configstore' + +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { name } = require('../../package.json') +const configStore = new ConfigStore(name) + +const remoteBranchesKey = 'remoteBranches' +const createdAtKey = 'createdAt' +const timeUntilLocalStorageIsStaleInMinutes = 5 +const millisecondsPerSecond = 1000 +const secondsPerMinute = 60 +const convertToMillisecondsFromMinutesMultipler = + millisecondsPerSecond * secondsPerMinute +const timeUntilLocalStorageIsStaleInMilliseconds = + timeUntilLocalStorageIsStaleInMinutes * + convertToMillisecondsFromMinutesMultipler + +interface ConfigStoreApi { + isLocalStorageStale(): boolean + hasSavedRemoteBranches(): boolean + store(key: string, input?: string): boolean + get(key: string): string +} +const configStoreApi: ConfigStoreApi = { + isLocalStorageStale: (): boolean => { + const createdAt = configStore.get(createdAtKey) || 0 + const currentTimeInMs = Date.now() + const approximateTimeElapsedInMillisecondsSinceCreated = Math.floor( + currentTimeInMs - createdAt + ) + return ( + approximateTimeElapsedInMillisecondsSinceCreated > + timeUntilLocalStorageIsStaleInMilliseconds + ) + }, + hasSavedRemoteBranches: (): boolean => configStore.has(remoteBranchesKey), + store: (key: string, input?: string): boolean => { + if (!input) return false + const preSize = configStore.size + configStore.set(key, input) + configStore.set(createdAtKey, Date.now()) + const postSize = configStore.size + return postSize > preSize + }, + get: (key: string): string => { + const branchList = configStore.get(key) + return branchList + }, +} + +export default configStoreApi +export { ConfigStoreApi, remoteBranchesKey } diff --git a/src/types/index.ts b/src/types/index.d.ts similarity index 100% rename from src/types/index.ts rename to src/types/index.d.ts