Skip to content

Commit

Permalink
migrate config file to new location (#109)
Browse files Browse the repository at this point in the history
* fix author & version: 0.0.1 (#99)

* go back

* 0.0.1

* write tests for migrations, refactor

* finish writing config tests

* finish writing config tests

* migrate config file to new location

* add mkdirp for saving config to new folders

* Update src/config/index.ts

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* Update src/config/index.ts

Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>

* Improves README for self-surface. (#100)

* merge in dev

---------

Co-authored-by: Frankie <frankie.diamond@gmail.com>
Co-authored-by: Nayyir Jutha <nayyir.jutha@gmail.com>
Co-authored-by: Johnny <9611008+johnnymatthews@users.noreply.github.com>
  • Loading branch information
4 people authored Jun 24, 2024
1 parent 47ae2ca commit 4f22c85
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"cli-progress": "^3.12.0",
"commander": "^12.0.0",
"debug": "^4.3.4",
"env-paths": "^3.0.0",
"inquirer": "8.0.0",
"mkdirp": "^3.0.1",
"typescript": "^4.8.4",
Expand Down
25 changes: 20 additions & 5 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { readFile, writeFile, rm } from 'node:fs/promises'
import { readFileSync } from 'node:fs'
import { readFile, writeFile } from 'node:fs/promises'
import { mkdirp } from 'mkdirp'
import { join, dirname } from 'path'
import envPaths from 'env-paths'


import allMigrations from './migrations'
const CONFIG_PATH = `${process.env.HOME}/.entropy-cli.config`

const paths = envPaths('entropy-cryptography', { suffix: '' })
const CONFIG_PATH = join(paths.config, 'entropy-cli.json')
const OLD_CONFIG_PATH = join(process.env.HOME, '.entropy-cli.config')

export const VERSION = 'migration-version'

Expand All @@ -25,13 +32,19 @@ function hasRunMigration (config: any, version: number) {
return Number(currentVersion) >= Number(version)
}

export async function init (configPath = CONFIG_PATH) {
export async function init (configPath = CONFIG_PATH, oldConfigPath = OLD_CONFIG_PATH) {
const currentConfig = await get(configPath)
.catch(async (err) => {
if (err && err.code !== 'ENOENT') throw err

// TODO: when we do the migration of location, do it in here
return {}
const oldConfig = await get(oldConfigPath).catch(noop) // drop errors
if (oldConfig) {
// move the config
await set(oldConfig, configPath)
await rm(oldConfigPath)
return oldConfig
}
else return {}
})

const newConfig = migrateData(allMigrations, currentConfig)
Expand All @@ -40,6 +53,7 @@ export async function init (configPath = CONFIG_PATH) {
await set(newConfig, configPath)
}
}
function noop () {}

export async function get (configPath = CONFIG_PATH) {
const configBuffer = await readFile(configPath)
Expand All @@ -52,5 +66,6 @@ export function getSync (configPath = CONFIG_PATH) {
}

export async function set (config = {}, configPath = CONFIG_PATH) {
await mkdirp(dirname(configPath))
await writeFile(configPath, JSON.stringify(config))
}
33 changes: 31 additions & 2 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { migrateData, init, get, set } from '../src/config'
// used to ensure unique test ids
let id = Date.now()
const makeTmpPath = () => `/tmp/entropy-cli-${id++}.json`
const fakeOldConfigPath = '/tmp/fake-old-config.json'

test('config/migrations', async t => {
migrations.forEach(({ migrate, version }, i) => {
Expand Down Expand Up @@ -95,7 +96,7 @@ test('config - init', async t => {

let config
{
await init(configPath)
await init(configPath, fakeOldConfigPath)
const expected = migrateData(migrations)
config = await get(configPath)
t.deepEqual(config, expected, 'init empty state')
Expand All @@ -108,7 +109,7 @@ test('config - init', async t => {
manualAddition: 'boop'
}
await set(newConfig, configPath)
await init(configPath)
await init(configPath, fakeOldConfigPath)
config = await get(configPath)
t.deepEqual(config, newConfig, 'init does not over-write manual changes')
}
Expand All @@ -117,3 +118,31 @@ test('config - init', async t => {

t.end()
})

test('config - init (migration)', async t => {
const configPath = makeTmpPath().replace('/tmp', '/tmp/some-folder')
const oldConfigPath = makeTmpPath()

// old setup
await init(oldConfigPath, '/tmp/fake-old-config-path')

// customisation (to prove move done)
let config = await get(oldConfigPath)
const newConfig = {
...config,
manualAddition: 'boop'
}
await set(newConfig, oldConfigPath)


// init with new path
await init(configPath, oldConfigPath)
config = await get(configPath)
t.deepEqual(config, newConfig, 'init migrates data to new location')

await get(oldConfigPath)
.then(() => t.fail('old config should be empty'))
.catch(() => t.pass('old config should be empty'))

t.end()
})
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,11 @@ emoji-regex@^9.2.2:
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-9.2.2.tgz#840c8803b0d8047f4ff0cf963176b32d4ef3ed72"
integrity sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==

env-paths@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-3.0.0.tgz#2f1e89c2f6dbd3408e1b1711dd82d62e317f58da"
integrity sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==

es-abstract@^1.22.1, es-abstract@^1.22.3, es-abstract@^1.23.0:
version "1.23.3"
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.23.3.tgz#8f0c5a35cd215312573c5a27c87dfd6c881a0aa0"
Expand Down

0 comments on commit 4f22c85

Please sign in to comment.