@@ -0,0 +1,9 @@ | |||
{ | |||
"root": true, | |||
"extends": [ | |||
"lxsmnsyc/typescript" | |||
], | |||
"parserOptions": { | |||
"project": "./tsconfig.eslint.json" | |||
} | |||
} |
@@ -0,0 +1,107 @@ | |||
# Logs | |||
logs | |||
*.log | |||
npm-debug.log* | |||
yarn-debug.log* | |||
yarn-error.log* | |||
lerna-debug.log* | |||
# Diagnostic reports (https://nodejs.org/api/report.html) | |||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | |||
# Runtime data | |||
pids | |||
*.pid | |||
*.seed | |||
*.pid.lock | |||
# Directory for instrumented libs generated by jscoverage/JSCover | |||
lib-cov | |||
# Coverage directory used by tools like istanbul | |||
coverage | |||
*.lcov | |||
# nyc test coverage | |||
.nyc_output | |||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | |||
.grunt | |||
# Bower dependency directory (https://bower.io/) | |||
bower_components | |||
# node-waf configuration | |||
.lock-wscript | |||
# Compiled binary addons (https://nodejs.org/api/addons.html) | |||
build/Release | |||
# Dependency directories | |||
node_modules/ | |||
jspm_packages/ | |||
# TypeScript v1 declaration files | |||
typings/ | |||
# TypeScript cache | |||
*.tsbuildinfo | |||
# Optional npm cache directory | |||
.npm | |||
# Optional eslint cache | |||
.eslintcache | |||
# Microbundle cache | |||
.rpt2_cache/ | |||
.rts2_cache_cjs/ | |||
.rts2_cache_es/ | |||
.rts2_cache_umd/ | |||
# Optional REPL history | |||
.node_repl_history | |||
# Output of 'npm pack' | |||
*.tgz | |||
# Yarn Integrity file | |||
.yarn-integrity | |||
# dotenv environment variables file | |||
.env | |||
.env.production | |||
.env.development | |||
# parcel-bundler cache (https://parceljs.org/) | |||
.cache | |||
# Next.js build output | |||
.next | |||
# Nuxt.js build / generate output | |||
.nuxt | |||
dist | |||
# Gatsby files | |||
.cache/ | |||
# Comment in the public line in if your project uses Gatsby and *not* Next.js | |||
# https://nextjs.org/blog/next-9-1#public-directory-support | |||
# public | |||
# vuepress build output | |||
.vuepress/dist | |||
# Serverless directories | |||
.serverless/ | |||
# FuseBox cache | |||
.fusebox/ | |||
# DynamoDB Local files | |||
.dynamodb/ | |||
# TernJS port file | |||
.tern-port | |||
.npmrc |
@@ -0,0 +1,7 @@ | |||
MIT License Copyright (c) 2022 TheoryOfNekomata <allan.crisostomo@outlook.com> | |||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |||
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. | |||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@@ -0,0 +1,5 @@ | |||
import { vi } from 'vitest'; | |||
export const generate = vi.fn(() => 'foo\n-italic'); | |||
export const getDefaultCards = vi.fn(() => ['foo\n-italic']); |
@@ -0,0 +1,5 @@ | |||
import {vi} from 'vitest'; | |||
export default { | |||
italic: vi.fn((s: string) => s), | |||
} |
@@ -0,0 +1,3 @@ | |||
import { vi } from 'vitest'; | |||
export default vi.fn(); |
@@ -0,0 +1,3 @@ | |||
import {vi} from 'vitest'; | |||
export const readFile = vi.fn(() => Promise.resolve(Buffer.from(''))); |
@@ -0,0 +1,3 @@ | |||
import { vi } from 'vitest'; | |||
export default vi.fn((s: string) => s); |
@@ -0,0 +1,97 @@ | |||
{ | |||
"name": "cli", | |||
"version": "0.0.0", | |||
"types": "dist/types/index.d.ts", | |||
"main": "dist/cjs/production/index.js", | |||
"module": "dist/esm/production/index.js", | |||
"bin": { | |||
"oblique": "dist/cjs/production/index.js" | |||
}, | |||
"exports": { | |||
".": { | |||
"development": { | |||
"require": "./dist/cjs/development/index.js", | |||
"import": "./dist/esm/development/index.js" | |||
}, | |||
"require": "./dist/cjs/production/index.js", | |||
"import": "./dist/esm/production/index.js", | |||
"types": "./dist/types/index.d.ts" | |||
}, | |||
"./dev": { | |||
"production": { | |||
"require": "./dist/cjs/production/index.js", | |||
"import": "./dist/esm/production/index.js" | |||
}, | |||
"require": "./dist/cjs/development/index.js", | |||
"import": "./dist/esm/development/index.js", | |||
"types": "./dist/types/index.d.ts" | |||
}, | |||
"./esm": { | |||
"development": "./dist/esm/development/index.js", | |||
"production": "./dist/esm/production/index.js", | |||
"default": "./dist/esm/production/index.js", | |||
"types": "./dist/types/index.d.ts" | |||
}, | |||
"./cjs": { | |||
"development": "./dist/cjs/development/index.js", | |||
"production": "./dist/cjs/production/index.js", | |||
"default": "./dist/cjs/production/index.js", | |||
"types": "./dist/types/index.d.ts" | |||
} | |||
}, | |||
"files": [ | |||
"dist", | |||
"src" | |||
], | |||
"engines": { | |||
"node": ">=10" | |||
}, | |||
"license": "MIT", | |||
"keywords": [ | |||
"pridepack" | |||
], | |||
"devDependencies": { | |||
"@types/node": "^17.0.13", | |||
"@types/wrap-ansi": "^8.0.1", | |||
"@types/yargs": "^17.0.10", | |||
"c8": "^7.11.0", | |||
"eslint": "^8.8.0", | |||
"eslint-config-lxsmnsyc": "^0.4.0", | |||
"pridepack": "1.1.0", | |||
"tslib": "^2.3.1", | |||
"typescript": "^4.5.4", | |||
"vitest": "^0.9.3" | |||
}, | |||
"scripts": { | |||
"prepublishOnly": "pridepack clean && pridepack build", | |||
"build": "pridepack build", | |||
"type-check": "pridepack check", | |||
"lint": "pridepack lint", | |||
"clean": "pridepack clean", | |||
"watch": "pridepack watch", | |||
"start": "node ./dist/cjs/production/index.js", | |||
"dev": "pridepack dev", | |||
"test": "vitest", | |||
"test:coverage": "vitest run --coverage" | |||
}, | |||
"private": false, | |||
"description": "CLI for Oblique Strategies.", | |||
"repository": { | |||
"url": "https://code.modal.sh/modal-soft/oblique-strategies-cli", | |||
"type": "git" | |||
}, | |||
"homepage": "https://code.modal.sh/modal-soft/oblique-strategies-cli", | |||
"bugs": { | |||
"url": "https://code.modal.sh/modal-soft/oblique-strategies-cli/issues" | |||
}, | |||
"author": "TheoryOfNekomata <allan.crisostomo@outlook.com>", | |||
"publishConfig": { | |||
"access": "public" | |||
}, | |||
"dependencies": { | |||
"chalk": "^5.0.1", | |||
"fetch-ponyfill": "^7.1.0", | |||
"wrap-ansi": "^8.0.1", | |||
"yargs": "^17.4.1" | |||
} | |||
} |
@@ -0,0 +1,3 @@ | |||
{ | |||
"target": "es2017" | |||
} |
@@ -0,0 +1,55 @@ | |||
import chalk from 'chalk'; | |||
import * as obliqueStrategiesCore from '@theoryofnekomata/oblique-strategies-core'; | |||
import { | |||
describe, | |||
it, | |||
expect, | |||
vi, beforeAll, afterAll, beforeEach, afterEach, | |||
} from 'vitest'; | |||
import { main } from '../app'; | |||
import * as presenters from '../presenters'; | |||
vi.mock('@theoryofnekomata/oblique-strategies-core'); | |||
vi.mock('chalk'); | |||
vi.mock('wrap-ansi'); | |||
vi.mock('../readers/file'); | |||
vi.mock('../readers/http'); | |||
vi.mock('../readers/https'); | |||
vi.mock('../readers/text'); | |||
vi.mock('../presenters'); | |||
describe('formatting', () => { | |||
let defaultConsole: typeof console; | |||
beforeAll(() => { | |||
defaultConsole = console; | |||
global.console = { | |||
log: () => { | |||
// noop | |||
}, | |||
} as unknown as typeof console; | |||
}); | |||
afterAll(() => { | |||
global.console = defaultConsole; | |||
}); | |||
beforeAll(() => { | |||
vi.mocked(presenters.plain).mockReturnValue(vi.fn()); | |||
}); | |||
afterAll(() => { | |||
vi.mocked(presenters.plain).mockReset(); | |||
}); | |||
it('formats the card when italic parts are found', async () => { | |||
vi.mocked(obliqueStrategiesCore.generate).mockReturnValueOnce('foo\n-italic'); | |||
await main({ | |||
presentation: 'plain', | |||
formatted: true, | |||
cards: ['default'], | |||
}); | |||
expect(chalk.italic).toBeCalled(); | |||
}); | |||
}); |
@@ -0,0 +1,57 @@ | |||
import { | |||
describe, | |||
it, | |||
expect, | |||
vi, beforeAll, afterAll, beforeEach, afterEach, SpyInstanceFn, | |||
} from 'vitest'; | |||
import { main, PRESENTATION } from '../app'; | |||
import * as presenters from '../presenters'; | |||
vi.mock('@theoryofnekomata/oblique-strategies-core'); | |||
vi.mock('chalk'); | |||
vi.mock('wrap-ansi'); | |||
vi.mock('../readers/file'); | |||
vi.mock('../readers/http'); | |||
vi.mock('../readers/https'); | |||
vi.mock('../readers/text'); | |||
vi.mock('../presenters'); | |||
describe('presenters', () => { | |||
let defaultConsole: typeof console; | |||
beforeAll(() => { | |||
defaultConsole = console; | |||
global.console = { | |||
log: () => { | |||
// noop | |||
}, | |||
} as unknown as typeof console; | |||
}); | |||
afterAll(() => { | |||
global.console = defaultConsole; | |||
}); | |||
describe.each(PRESENTATION)('%s', (presentation) => { | |||
let presenterFn: SpyInstanceFn<[], Promise<void>>; | |||
beforeEach(() => { | |||
presenterFn = vi.fn(() => Promise.resolve()); | |||
vi.mocked(presenters[presentation]).mockReturnValueOnce(presenterFn); | |||
}); | |||
afterEach(() => { | |||
vi.mocked(presenters[presentation]).mockReset(); | |||
}); | |||
it('displays the card', async () => { | |||
await main({ | |||
presentation, | |||
formatted: false, | |||
cards: ['default'], | |||
}); | |||
expect(presenters[presentation]).toBeCalled(); | |||
expect(presenterFn).toBeCalled(); | |||
}); | |||
}); | |||
}); |
@@ -0,0 +1,88 @@ | |||
import * as obliqueStrategiesCore from '@theoryofnekomata/oblique-strategies-core'; | |||
import { | |||
afterAll, afterEach, | |||
beforeAll, beforeEach, | |||
describe, | |||
expect, | |||
it, vi, | |||
} from 'vitest'; | |||
import * as file from '../readers/file'; | |||
import * as text from '../readers/text'; | |||
import * as http from '../readers/http'; | |||
import * as https from '../readers/https'; | |||
import { main } from '../app'; | |||
import * as presenters from '../presenters'; | |||
vi.mock('@theoryofnekomata/oblique-strategies-core'); | |||
vi.mock('chalk'); | |||
vi.mock('wrap-ansi'); | |||
vi.mock('../readers/file'); | |||
vi.mock('../readers/http'); | |||
vi.mock('../readers/https'); | |||
vi.mock('../readers/text'); | |||
vi.mock('../presenters'); | |||
describe('readers', () => { | |||
let defaultConsole: typeof console; | |||
beforeAll(() => { | |||
defaultConsole = console; | |||
global.console = { | |||
log: () => { | |||
// noop | |||
}, | |||
} as unknown as typeof console; | |||
}); | |||
afterAll(() => { | |||
global.console = defaultConsole; | |||
}); | |||
beforeAll(() => { | |||
vi.mocked(presenters.plain).mockReturnValue(vi.fn()); | |||
}); | |||
afterAll(() => { | |||
vi.mocked(presenters.plain).mockReset(); | |||
}); | |||
describe('default reader', () => { | |||
it('reads from default source', async () => { | |||
await main({ | |||
cards: ['default'], | |||
formatted: false, | |||
presentation: 'plain', | |||
}); | |||
expect(obliqueStrategiesCore.getDefaultCards).toBeCalled(); | |||
}); | |||
}); | |||
describe.each([ | |||
{ type: 'file', prefix: 'file:///', reader: file }, | |||
{ type: 'text', prefix: 'text:', reader: text }, | |||
{ type: 'http', prefix: 'http://', reader: http }, | |||
{ type: 'https', prefix: 'https://', reader: https }, | |||
])('%s', ({ reader, prefix }) => { | |||
it('reads from source', async () => { | |||
await main({ | |||
cards: [`${prefix}foo`], | |||
formatted: false, | |||
presentation: 'plain', | |||
}); | |||
expect(reader.read).toBeCalled(); | |||
}); | |||
}); | |||
describe('unknown reader', () => { | |||
it('throws an error', async () => { | |||
await expect(() => main({ | |||
cards: ['unknown'], | |||
formatted: false, | |||
presentation: 'plain', | |||
})) | |||
.rejects | |||
.toThrowError(); | |||
}); | |||
}); | |||
}); |
@@ -0,0 +1,67 @@ | |||
import { generate, getDefaultCards } from '@theoryofnekomata/oblique-strategies-core'; | |||
import * as file from './readers/file'; | |||
import * as http from './readers/http'; | |||
import * as https from './readers/https'; | |||
import * as text from './readers/text'; | |||
import * as presenters from './presenters'; | |||
export const PRESENTATION = ['plain', 'card', 'centered'] as const; | |||
export type Presentation = typeof PRESENTATION[number]; | |||
export type MainArgs = { | |||
cards: string[], | |||
formatted: boolean, | |||
presentation: Presentation, | |||
} | |||
const formatCard = async (card: string) => { | |||
const { default: chalk } = await import('chalk'); | |||
return card | |||
.split('\n') | |||
.map((line) => ( | |||
line.startsWith('-') | |||
? chalk.italic(line) | |||
: line | |||
)) | |||
.join('\n'); | |||
}; | |||
const loadCards = async (cards: string[]) => { | |||
const readers = [ | |||
text, | |||
file, | |||
https, | |||
http, | |||
]; | |||
const promises = cards.map(async (c) => { | |||
if (c === 'default') { | |||
return getDefaultCards(); | |||
} | |||
const reader = readers.find((m) => c.startsWith(m.prefix)); | |||
if (reader) { | |||
const { read, prefix } = reader; | |||
return read(c.slice(c.indexOf(prefix) + prefix.length)); | |||
} | |||
throw new Error(`Invalid source: ${c}`); | |||
}); | |||
const sources = await Promise.all(promises); | |||
return sources.flat(1); | |||
}; | |||
export const main = async (args: MainArgs) => { | |||
const { cards, presentation, formatted } = args; | |||
const loadedCards = await loadCards(cards); | |||
const generatedCard = generate(loadedCards); | |||
const cardContent = await ( | |||
formatted | |||
? formatCard(generatedCard) | |||
: Promise.resolve(generatedCard) | |||
); | |||
await presenters[presentation]()(cardContent); | |||
}; |
@@ -0,0 +1,57 @@ | |||
import yargs from 'yargs'; | |||
import { hideBin } from 'yargs/helpers'; | |||
import { | |||
main, | |||
MainArgs, | |||
Presentation, | |||
PRESENTATION, | |||
} from './app'; | |||
void main( | |||
yargs | |||
.option('cards', { | |||
alias: 'c', | |||
description: "List of source for generating cards. Can have 'default', a pipe-delimited string starting with 'text:' or a URL starting with file:/// or http[s]://", | |||
coerce: (c?: unknown) => { | |||
if (typeof c !== 'string') { | |||
return ['default']; | |||
} | |||
const theCards = c.split(';'); | |||
if (theCards.length < 1) { | |||
return ['default']; | |||
} | |||
// dedupe so we don't have to generate some cards more likely than others | |||
return theCards.filter((card, i, cards) => cards.indexOf(card) === i); | |||
}, | |||
default: 'default', | |||
}) | |||
.option('formatted', { | |||
alias: 'f', | |||
description: 'Flag when the output should be formatted.', | |||
coerce: (c?: unknown) => Boolean(c), | |||
default: false, | |||
}) | |||
.option('presentation', { | |||
alias: 'p', | |||
description: 'Selection of how the card should be presented.', | |||
choices: PRESENTATION, | |||
coerce: (c?: unknown): Presentation => { | |||
if (typeof c !== 'string') { | |||
return 'plain'; | |||
} | |||
const maybePresentation = c as Presentation; | |||
if (!PRESENTATION.includes(maybePresentation)) { | |||
return 'plain'; | |||
} | |||
return maybePresentation; | |||
}, | |||
default: 'plain', | |||
}) | |||
.help() | |||
.parse(hideBin(process.argv)) as MainArgs, | |||
); |
@@ -0,0 +1,4 @@ | |||
import { vi } from 'vitest'; | |||
export default vi.fn(); | |||
@@ -0,0 +1,3 @@ | |||
import { vi } from 'vitest'; | |||
export default vi.fn(); |
@@ -0,0 +1,7 @@ | |||
import { vi } from 'vitest'; | |||
export const plain = vi.fn(); | |||
export const card = vi.fn(); | |||
export const centered = vi.fn(); |
@@ -0,0 +1,3 @@ | |||
import { vi } from 'vitest'; | |||
export default vi.fn(); |
@@ -0,0 +1,39 @@ | |||
import wrapAnsi from 'wrap-ansi'; | |||
import { | |||
afterAll, | |||
beforeAll, | |||
describe, | |||
expect, | |||
it, SpyInstanceFn, | |||
vi, | |||
} from 'vitest'; | |||
import card from '../card'; | |||
vi.mock('wrap-ansi'); | |||
describe('card', () => { | |||
let log: SpyInstanceFn<[], void>; | |||
let defaultConsole: typeof console; | |||
beforeAll(() => { | |||
defaultConsole = console; | |||
log = vi.fn(); | |||
global.console = { | |||
log, | |||
} as unknown as typeof console; | |||
}); | |||
afterAll(() => { | |||
global.console = defaultConsole; | |||
}); | |||
it('calls wrap', async () => { | |||
await card()('foo'); | |||
expect(wrapAnsi).toBeCalled(); | |||
}); | |||
it('calls the console output function', async () => { | |||
await card()('foo'); | |||
expect(log).toBeCalled(); | |||
}); | |||
}); |
@@ -0,0 +1,50 @@ | |||
import wrapAnsi from 'wrap-ansi'; | |||
import { | |||
afterAll, | |||
beforeAll, | |||
describe, | |||
expect, | |||
it, SpyInstanceFn, | |||
vi, | |||
} from 'vitest'; | |||
import centered from '../centered'; | |||
vi.mock('wrap-ansi'); | |||
describe('centered', () => { | |||
let log: SpyInstanceFn<[], void>; | |||
let defaultConsole: typeof console; | |||
beforeAll(() => { | |||
defaultConsole = console; | |||
log = vi.fn(); | |||
global.console = { | |||
log, | |||
} as unknown as typeof console; | |||
}); | |||
afterAll(() => { | |||
global.console = defaultConsole; | |||
}); | |||
it('calls wrap and ensures console has an assumed width of 80 text columns', async () => { | |||
const defaultColumns = process.stdout.columns; | |||
process.stdout.columns = undefined as unknown as typeof process.stdout.columns; | |||
await centered()('foo\n-italic'); | |||
expect(wrapAnsi).toBeCalledWith('foo\n-italic', 80, { hard: true, trim: true }); | |||
process.stdout.columns = defaultColumns; | |||
}); | |||
it('calls wrap and respects current text column count', async () => { | |||
const defaultColumns = process.stdout.columns; | |||
process.stdout.columns = 420; | |||
await centered()('foo\n-italic'); | |||
expect(wrapAnsi).toBeCalledWith('foo\n-italic', 420, { hard: true, trim: true }); | |||
process.stdout.columns = defaultColumns; | |||
}); | |||
it('calls the console output function', async () => { | |||
await centered()('foo\n-italic'); | |||
expect(log).toBeCalled(); | |||
}); | |||
}); |
@@ -0,0 +1,31 @@ | |||
import { | |||
afterAll, | |||
beforeAll, | |||
describe, | |||
expect, | |||
it, SpyInstanceFn, | |||
vi, | |||
} from 'vitest'; | |||
import plain from '../plain'; | |||
describe('plain', () => { | |||
let log: SpyInstanceFn<[], void>; | |||
let defaultConsole: typeof console; | |||
beforeAll(() => { | |||
defaultConsole = console; | |||
log = vi.fn(); | |||
global.console = { | |||
log, | |||
} as unknown as typeof console; | |||
}); | |||
afterAll(() => { | |||
global.console = defaultConsole; | |||
}); | |||
it('calls the console output function', async () => { | |||
await plain()('foo'); | |||
expect(log).toBeCalled(); | |||
}); | |||
}); |
@@ -0,0 +1,95 @@ | |||
const DEFAULT_CARD_WIDTH = 32; | |||
const DEFAULT_CARD_HEIGHT = 8; | |||
type CardPresenterOptions = { | |||
width: number, | |||
height: number | |||
} | |||
const makeCardEdge = (width: number, symbols: string) => { | |||
const middlePart = Math.floor(symbols.length / 2); | |||
return ( | |||
new Array<string>(width) | |||
.fill(symbols.at(middlePart) as string) | |||
.map((s, i, ss) => { | |||
if (i < middlePart) { | |||
return symbols.at(i); | |||
} | |||
if (i >= (ss.length - 1) - middlePart) { | |||
return symbols.at(ss.length - i - 1); | |||
} | |||
return s; | |||
}) | |||
.join('') | |||
); | |||
}; | |||
const encloseInCard = async ( | |||
cardText: string, | |||
cardWidth: number, | |||
cardHeight: number, | |||
symbols: [string, string, string], | |||
) => { | |||
// can't import es modules naturally, we use dynamic imports here | |||
const { default: wrapAnsi } = await import('wrap-ansi'); | |||
const lineWidth = cardWidth - (symbols[1].length - 1); | |||
const text = wrapAnsi(cardText, lineWidth, { hard: true, trim: true }); | |||
const lines = text.split('\n').map((line) => { | |||
let centeredLine = line; | |||
while (centeredLine.length < lineWidth) { | |||
// add space after | |||
centeredLine = `${centeredLine}${symbols[1].at(3) as string}`; | |||
if (centeredLine.length < lineWidth) { | |||
// add space before | |||
centeredLine = `${symbols[1].at(1) as string}${centeredLine}`; | |||
} | |||
} | |||
return centeredLine.slice(0, lineWidth); | |||
}); | |||
const displayLines = [...lines]; | |||
while (displayLines.length < cardHeight - 2) { | |||
const blank = new Array(lineWidth).fill(symbols[1].at(1) as string).join(''); | |||
displayLines.push(blank); | |||
if (displayLines.length < cardHeight - 2) { | |||
displayLines.unshift(blank); | |||
} | |||
} | |||
const cardCenter = displayLines | |||
.map((line) => ( | |||
`${symbols[1].at(0) as string}${symbols[1].at(1) as string}${line}${symbols[1].at(3) as string}${symbols[1].at(4) as string}` | |||
)) | |||
.join('\n'); | |||
return [ | |||
makeCardEdge(cardWidth, symbols[0]), | |||
cardCenter, | |||
makeCardEdge(cardWidth, symbols[2]), | |||
].join('\n'); | |||
}; | |||
export default (options = {} as CardPresenterOptions) => async (card: string) => { | |||
const { | |||
width: cardWidthRaw = DEFAULT_CARD_WIDTH, | |||
height: cardHeightRaw = DEFAULT_CARD_HEIGHT, | |||
} = options; | |||
const cardWidth = Math.max(cardWidthRaw, DEFAULT_CARD_WIDTH); | |||
const cardHeight = Math.max(cardHeightRaw, DEFAULT_CARD_HEIGHT); | |||
const cardContents = await encloseInCard( | |||
card, | |||
cardWidth, | |||
cardHeight, | |||
[ | |||
' ,-. ', | |||
'| A |', | |||
" `-' ", | |||
], | |||
); | |||
const Console = console; | |||
Console.log(cardContents); | |||
}; |
@@ -0,0 +1,25 @@ | |||
export default () => async (card: string) => { | |||
// can't import es modules naturally, we use dynamic imports here | |||
const { default: wrapAnsi } = await import('wrap-ansi'); | |||
const Console = console; | |||
const lineWidth = process.stdout.columns ?? 80; | |||
const text = wrapAnsi(card, lineWidth, { hard: true, trim: true }); | |||
const lines = text.split('\n').map((line) => { | |||
let centeredLine = line; | |||
while (centeredLine.length < lineWidth) { | |||
// add space after | |||
centeredLine = `${centeredLine} `; | |||
if (centeredLine.length < lineWidth) { | |||
// add space before | |||
centeredLine = ` ${centeredLine}`; | |||
} | |||
} | |||
return centeredLine.slice(0, lineWidth); | |||
}); | |||
const displayLines = [...lines]; | |||
Console.log(); | |||
Console.log(displayLines.join('\n')); | |||
Console.log(); | |||
}; |
@@ -0,0 +1,3 @@ | |||
export { default as plain } from './plain'; | |||
export { default as card } from './card'; | |||
export { default as centered } from './centered'; |
@@ -0,0 +1,7 @@ | |||
export default () => (card: string) => new Promise<void>((resolve) => { | |||
const Console = console; | |||
Console.log(card); | |||
resolve(); | |||
}); |
@@ -0,0 +1,5 @@ | |||
import { vi } from 'vitest'; | |||
export const read = vi.fn(() => Promise.resolve(['foo', 'bar\n-baz\n-quux'])); | |||
export const prefix = 'file:///'; |
@@ -0,0 +1,5 @@ | |||
import { vi } from 'vitest'; | |||
export const read = vi.fn(() => Promise.resolve(['foo', 'bar\n-baz\n-quux'])); | |||
export const prefix = 'http://'; |
@@ -0,0 +1,5 @@ | |||
import { vi } from 'vitest'; | |||
export const read = vi.fn(() => Promise.resolve(['foo', 'bar\n-baz\n-quux'])); | |||
export const prefix = 'https://'; |
@@ -0,0 +1,5 @@ | |||
import { vi } from 'vitest'; | |||
export const read = vi.fn(() => Promise.resolve(['foo', 'bar\n-baz\n-quux'])); | |||
export const prefix = 'text:'; |
@@ -0,0 +1,21 @@ | |||
import { | |||
describe, | |||
expect, | |||
it, | |||
vi, | |||
} from 'vitest'; | |||
import { readFile } from 'fs/promises'; | |||
import { prefix, read } from '../file'; | |||
vi.mock('fs/promises'); | |||
describe('file', () => { | |||
it('reads from the local file system', async () => { | |||
await read('foo'); | |||
expect(readFile).toBeCalled(); | |||
}); | |||
it('exports a prefix', () => { | |||
expect(prefix).toBeTypeOf('string'); | |||
}); | |||
}); |
@@ -0,0 +1,75 @@ | |||
import { | |||
describe, | |||
expect, | |||
vi, | |||
it, | |||
} from 'vitest'; | |||
import fetchPonyfill from 'fetch-ponyfill'; | |||
import { read, prefix } from '../http'; | |||
vi.mock('fetch-ponyfill'); | |||
describe('http', () => { | |||
describe('read', () => { | |||
it('calls fetch', async () => { | |||
const fetch = vi.fn().mockResolvedValueOnce({ | |||
ok: true, | |||
text: () => Promise.resolve('foo'), | |||
}); | |||
vi.mocked(fetchPonyfill).mockReturnValueOnce({ | |||
fetch, | |||
} as unknown as ReturnType<typeof fetchPonyfill>); | |||
await read('example.com'); | |||
expect(fetch).toBeCalled(); | |||
}); | |||
it('considers response as text data', async () => { | |||
const text = vi.fn().mockResolvedValueOnce('foo'); | |||
const fetch = vi.fn().mockResolvedValueOnce({ | |||
ok: true, | |||
text, | |||
}); | |||
vi.mocked(fetchPonyfill).mockReturnValueOnce({ | |||
fetch, | |||
} as unknown as ReturnType<typeof fetchPonyfill>); | |||
await read('example.com'); | |||
expect(text).toBeCalled(); | |||
}); | |||
it('returns text data when request from fetch is successful', async () => { | |||
const text = vi.fn().mockResolvedValueOnce('foo'); | |||
const fetch = vi.fn().mockResolvedValueOnce({ | |||
ok: true, | |||
text, | |||
}); | |||
vi.mocked(fetchPonyfill).mockReturnValueOnce({ | |||
fetch, | |||
} as unknown as ReturnType<typeof fetchPonyfill>); | |||
const cards = await read('example.com'); | |||
expect(cards).toEqual(expect.arrayContaining([expect.any(String)])); | |||
}); | |||
it('throws when request from fetch is unsuccessful', async () => { | |||
const fetch = vi.fn().mockResolvedValueOnce({ | |||
ok: false, | |||
text: () => Promise.resolve('foo'), | |||
}); | |||
vi.mocked(fetchPonyfill).mockReturnValueOnce({ | |||
fetch, | |||
} as unknown as ReturnType<typeof fetchPonyfill>); | |||
await expect(() => read('example.com')).rejects.toThrowError(); | |||
}); | |||
}); | |||
it('exports a prefix', () => { | |||
expect(prefix).toBeTypeOf('string'); | |||
}); | |||
}); |
@@ -0,0 +1,75 @@ | |||
import { | |||
describe, | |||
expect, | |||
vi, | |||
it, | |||
} from 'vitest'; | |||
import fetchPonyfill from 'fetch-ponyfill'; | |||
import { read, prefix } from '../https'; | |||
vi.mock('fetch-ponyfill'); | |||
describe('https', () => { | |||
describe('read', () => { | |||
it('calls fetch', async () => { | |||
const fetch = vi.fn().mockResolvedValueOnce({ | |||
ok: true, | |||
text: () => Promise.resolve('foo'), | |||
}); | |||
vi.mocked(fetchPonyfill).mockReturnValueOnce({ | |||
fetch, | |||
} as unknown as ReturnType<typeof fetchPonyfill>); | |||
await read('example.com'); | |||
expect(fetch).toBeCalled(); | |||
}); | |||
it('considers response as text data', async () => { | |||
const text = vi.fn().mockResolvedValueOnce('foo'); | |||
const fetch = vi.fn().mockResolvedValueOnce({ | |||
ok: true, | |||
text, | |||
}); | |||
vi.mocked(fetchPonyfill).mockReturnValueOnce({ | |||
fetch, | |||
} as unknown as ReturnType<typeof fetchPonyfill>); | |||
await read('example.com'); | |||
expect(text).toBeCalled(); | |||
}); | |||
it('returns text data when request from fetch is successful', async () => { | |||
const text = vi.fn().mockResolvedValueOnce('foo'); | |||
const fetch = vi.fn().mockResolvedValueOnce({ | |||
ok: true, | |||
text, | |||
}); | |||
vi.mocked(fetchPonyfill).mockReturnValueOnce({ | |||
fetch, | |||
} as unknown as ReturnType<typeof fetchPonyfill>); | |||
const cards = await read('example.com'); | |||
expect(cards).toEqual(expect.arrayContaining([expect.any(String)])); | |||
}); | |||
it('throws when request from fetch is unsuccessful', async () => { | |||
const fetch = vi.fn().mockResolvedValueOnce({ | |||
ok: false, | |||
text: () => Promise.resolve('foo'), | |||
}); | |||
vi.mocked(fetchPonyfill).mockReturnValueOnce({ | |||
fetch, | |||
} as unknown as ReturnType<typeof fetchPonyfill>); | |||
await expect(() => read('example.com')).rejects.toThrowError(); | |||
}); | |||
}); | |||
it('exports a prefix', () => { | |||
expect(prefix).toBeTypeOf('string'); | |||
}); | |||
}); |
@@ -0,0 +1,13 @@ | |||
import { describe, expect, it } from 'vitest'; | |||
import { read, prefix } from '../text'; | |||
describe('text', () => { | |||
it('resolves to a string array', async () => { | |||
const cards = await read('hello|hi|foo'); | |||
expect(cards).toHaveLength(3); | |||
}); | |||
it('exports a prefix', () => { | |||
expect(prefix).toBeTypeOf('string'); | |||
}); | |||
}); |
@@ -0,0 +1,10 @@ | |||
import { readFile } from 'fs/promises'; | |||
// input is without the prefix | |||
export const read = async (input: string) => { | |||
const buffer = await readFile(input); | |||
const contents = buffer.toString('utf-8'); | |||
return contents.split('\n\n'); | |||
}; | |||
export const prefix = 'file:///' as const; |
@@ -0,0 +1,22 @@ | |||
import fetchPonyfill from 'fetch-ponyfill'; | |||
// input is without the prefix | |||
export const read = async (input: string) => { | |||
const { fetch } = fetchPonyfill({}); | |||
const response = await fetch(`http://${input}`, { | |||
method: 'GET', | |||
headers: { | |||
Accept: 'text/plain', | |||
}, | |||
}); | |||
const responseData = await response.text(); | |||
if (response.ok) { | |||
return responseData.split('\n\n'); | |||
} | |||
throw new Error(responseData); | |||
}; | |||
export const prefix = 'http://' as const; |
@@ -0,0 +1,22 @@ | |||
import fetchPonyfill from 'fetch-ponyfill'; | |||
// input is without the prefix | |||
export const read = async (input: string) => { | |||
const { fetch } = fetchPonyfill({}); | |||
const response = await fetch(`https://${input}`, { | |||
method: 'GET', | |||
headers: { | |||
Accept: 'text/plain', | |||
}, | |||
}); | |||
const responseData = await response.text(); | |||
if (response.ok) { | |||
return responseData.split('\n\n'); | |||
} | |||
throw new Error(responseData); | |||
}; | |||
export const prefix = 'https://' as const; |
@@ -0,0 +1,4 @@ | |||
// input is without the prefix | |||
export const read = (input: string) => Promise.resolve<string[]>(input.split('|')); | |||
export const prefix = 'text:'; |
@@ -0,0 +1,21 @@ | |||
{ | |||
"exclude": ["node_modules"], | |||
"include": ["src", "types", "test"], | |||
"compilerOptions": { | |||
"module": "ESNext", | |||
"lib": ["ESNext", "DOM"], | |||
"importHelpers": true, | |||
"declaration": true, | |||
"sourceMap": true, | |||
"rootDir": "./", | |||
"strict": true, | |||
"noUnusedLocals": true, | |||
"noUnusedParameters": true, | |||
"noImplicitReturns": true, | |||
"noFallthroughCasesInSwitch": true, | |||
"moduleResolution": "node", | |||
"jsx": "react", | |||
"esModuleInterop": true, | |||
"target": "ES2017" | |||
} | |||
} |
@@ -0,0 +1,21 @@ | |||
{ | |||
"exclude": ["node_modules"], | |||
"include": ["src", "types"], | |||
"compilerOptions": { | |||
"module": "ESNext", | |||
"lib": ["ESNext", "DOM"], | |||
"importHelpers": true, | |||
"declaration": true, | |||
"sourceMap": true, | |||
"rootDir": "./src", | |||
"strict": true, | |||
"noUnusedLocals": true, | |||
"noUnusedParameters": true, | |||
"noImplicitReturns": true, | |||
"noFallthroughCasesInSwitch": true, | |||
"moduleResolution": "node", | |||
"jsx": "react", | |||
"esModuleInterop": true, | |||
"target": "ES2017" | |||
} | |||
} |
@@ -0,0 +1,12 @@ | |||
import { defineConfig } from 'vitest/config' | |||
export default defineConfig({ | |||
test: { | |||
coverage: { | |||
exclude: [ | |||
'**/__mocks__/**', | |||
'**/__tests__/**', | |||
], | |||
}, | |||
}, | |||
}) |