Skip to content

Commit

Permalink
Merge pull request #108 from zanerock/work-liquid-labs/cloudsite/107
Browse files Browse the repository at this point in the history
Rework user output and remove dependency on process.stdout
  • Loading branch information
zanerock authored Mar 25, 2024
2 parents b633285 + 992263c commit 02e7484
Show file tree
Hide file tree
Showing 28 changed files with 177 additions and 99 deletions.
2 changes: 1 addition & 1 deletion DEVELOPER_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ All the CLI dependencies are noted as peer dependencies. This is so that if the
See also the [Known limitations section in the README.md](./README.md#known-limitations).

- The current implementation deals exclusively with apex domains. It would make a lot of sense to support sub-domains as well.
- Reporting progress through `process.stdout` isn't ideal, especial for the _lib_ components. We think using something like the Winston logger might be useful.[^1]
- We want to move to a more flexible and consistent output protocol. All actions should support 'quiet'-ing intermediate output and a single, formatable output object.[^1]

[^1: We did a survey of "top javascript log libraries" and determined that Winston seemed to fit best. It had all the feataures we needed and is very well supported.]

Expand Down
98 changes: 83 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"devDependencies": {
"@liquid-labs/sdlc-resource-babel-and-rollup": "^1.0.0-alpha.8",
"@liquid-labs/sdlc-resource-eslint": "^1.0.0-alpha.11",
"@liquid-labs/sdlc-resource-jest": "^1.0.0-alpha.7"
"@liquid-labs/sdlc-resource-jest": "^1.0.0-alpha.7",
"magic-print": "^1.0.0-alpha.4"
},
"_comply": {
"orgKey": "@liquid-labs"
Expand All @@ -72,6 +73,7 @@
"command-line-documentation": "^1.0.0-alpha.3",
"json-to-plain-text": "^1.1.4",
"lodash": "^4.17.21",
"magic-print": "^1.0.0-alpha.4",
"question-and-answer": "^1.0.0-alpha.16"
}
}
3 changes: 3 additions & 0 deletions src/cli/cloudsite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isEqual from 'lodash/isEqual'

import { cliSpec, DB_PATH } from './constants'
import { checkReminders } from './lib/check-reminders'
import { configureLogger } from '../lib/shared/progress-logger'
import { handleCleanup } from './lib/handle-cleanup'
import { handleConfiguration } from './lib/handle-configuration'
import { handleCreate } from './lib/handle-create'
Expand Down Expand Up @@ -37,6 +38,8 @@ const cloudsite = async () => {
db = { account : { settings : {} }, sites : {}, toCleanup : {}, reminders : [] }
}

configureLogger(db?.account?.settings?.terminal)

checkReminders({ reminders : db.reminders })

const origDB = structuredClone(db)
Expand Down
15 changes: 7 additions & 8 deletions src/cli/lib/check-reminders.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { progressLogger } from '../../lib/shared/progress-logger'

const checkReminders = ({ reminders }) => {
const now = new Date().getTime()
const currentReminders = reminders.filter(({ remindAfter }) => {
Expand All @@ -6,18 +8,15 @@ const checkReminders = ({ reminders }) => {
})

if (currentReminders.length > 0) {
const columnWidth = process.stdout.columns || 40
const opener = '-- Reminder' + (currentReminders.lengith > 1 ? 's ' : ' ')
process.stdout.write(opener + '-'.repeat(columnWidth - opener.length))
const columnWidth = progressLogger.write.width
const opener = '-- <yellow>Reminder<rst>' + (currentReminders.lengith > 1 ? 's ' : ' ')
progressLogger.write(opener + '-'.repeat(columnWidth - opener.length + '<yellow>'.length + '<rst>'.length) + '\n')

currentReminders.forEach(({ todo }, i) => {
if (currentReminders.length > 1) {
process.stdout.write(i + '.')
}
process.stdout.write(todo + '\n')
progressLogger.write((currentReminders.length > 1 ? i + '.' : '') + todo + '\n')
})

process.stdout.write('-'.repeat(columnWidth) + '\n')
progressLogger.write('-'.repeat(columnWidth) + '\n')
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/cli/lib/configuration/handle-configuration-show.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import commandLineArgs from 'command-line-args'

import { formatOutput } from '../format-output'

import { cliSpec } from '../../constants'
import { progressLogger } from '../../../lib/shared/progress-logger'

const handleConfigurationShow = async ({ argv, db }) => {
const showConfigurationCLISpec = cliSpec
Expand All @@ -13,7 +12,7 @@ const handleConfigurationShow = async ({ argv, db }) => {
const { format } = showConfigurationOptions

const accountSettings = db.account.settings || {}
process.stdout.write(formatOutput({ format, output : accountSettings }))
progressLogger.write(accountSettings, '', { format })
}

export { handleConfigurationShow }
24 changes: 11 additions & 13 deletions src/cli/lib/configuration/test/handle-configuration-show.test.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { StringOut } from 'magic-print'

import { configureLogger } from '../../../../lib/shared/progress-logger'
import { handleConfigurationShow } from '../handle-configuration-show'

const expectedOutput = 'hi'
const settingsVal = 'hi'

jest.mock('node:fs/promises', () => ({
readFile : () => expectedOutput
readFile : () => settingsVal
}))

describe('handleConfigurationShow', () => {
let output
let origWrite
beforeAll(() => {
origWrite = process.stdout.write
process.stdout.write = (chunk) => { output = chunk }
})

afterAll(() => {
process.stdout.write = origWrite
let stringOut
beforeEach(() => {
stringOut = new StringOut()
configureLogger({ out : stringOut })
})

test('prints the file contents', async () => {
await handleConfigurationShow({ argv : [], db : { account : { settings : expectedOutput } } })
expect(output).toBe(expectedOutput + '\n')
await handleConfigurationShow({ argv : [], db : { account : { settings : settingsVal } } })
expect(stringOut.string).toBe(settingsVal + '\n')
})
})
17 changes: 0 additions & 17 deletions src/cli/lib/format-output.mjs

This file was deleted.

Loading

0 comments on commit 02e7484

Please sign in to comment.