Skip to content

Commit

Permalink
Improves display of preferences list command (#9292)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
  • Loading branch information
natemoo-re and sarah11918 authored Dec 4, 2023
1 parent fd9ffe2 commit 5428b3d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-squids-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improves display for `astro preferences list` command
66 changes: 53 additions & 13 deletions packages/astro/src/cli/preferences/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import type yargs from 'yargs-parser';
import type { AstroSettings } from '../../@types/astro.js';

import { bold } from 'kleur/colors';
import { bgGreen, black, bold, dim } from 'kleur/colors';
import { fileURLToPath } from 'node:url';

import dlv from 'dlv';
import { resolveConfig } from '../../core/config/config.js';
import { createSettings } from '../../core/config/settings.js';
import * as msg from '../../core/messages.js';
import { DEFAULT_PREFERENCES } from '../../preferences/defaults.js';
import { coerce, isValidKey, type PreferenceKey } from '../../preferences/index.js';
import { coerce, isValidKey, type PreferenceKey, type PreferenceLocation } from '../../preferences/index.js';
import { createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js';
// @ts-expect-error flattie types are mispackaged
import { flattie } from 'flattie';
Expand Down Expand Up @@ -211,12 +211,52 @@ async function resetPreference(
}

async function listPreferences(settings: AstroSettings, { location, json }: SubcommandOptions) {
const store = await settings.preferences.getAll({ location });
if (json) {
console.log(JSON.stringify(store, null, 2));
const resolved = await settings.preferences.getAll();
console.log(JSON.stringify(resolved, null, 2));
return 0;
}
prettyPrint(store);
const { global, project, defaults } = await settings.preferences.list({ location });
const flatProject = flattie(project);
const flatGlobal = flattie(global);
const flatUser = Object.assign({}, flatGlobal, flatProject);
for (let key of Object.keys(flatUser)) {
if (!isValidKey(key)) {
delete flatUser[key];
continue;
}
}

const flatDefault = flattie(defaults);
const userKeys = Object.keys(flatUser);

if (userKeys.length > 0) {
const badge = bgGreen(black(` Your Preferences `));
const table = formatTable(flatUser, ['Preference', 'Value']);

console.log(['', badge, table].join('\n'));
} else {
const badge = bgGreen(black(` Your Preferences `));
const message = dim('No preferences set');
console.log(['', badge, '', message].join('\n'));
}
const flatUnset = Object.assign({}, flatDefault);
for (const key of userKeys) {
delete flatUnset[key];
}
const unsetKeys = Object.keys(flatUnset);

if (unsetKeys.length > 0) {
const badge = bgGreen(black(` Default Preferences `));
const table = formatTable(flatUnset, ['Preference', 'Value']);

console.log(['', badge, table].join('\n'));
} else {
const badge = bgGreen(black(` Default Preferences `));
const message = dim('All preferences have been set');
console.log(['', badge, '', message].join('\n'));
}

return 0;
}

Expand Down Expand Up @@ -256,19 +296,19 @@ function formatTable(
b: string | number | boolean,
style: (value: string | number | boolean) => string = (v) => v.toString()
): string {
return `${chars.v} ${style(a)} ${space(colALength - a.length - 2)} ${chars.v} ${style(
return `${dim(chars.v)} ${style(a)} ${space(colALength - a.length - 2)} ${dim(chars.v)} ${style(
b
)} ${space(colBLength - b.toString().length - 3)} ${chars.v}`;
)} ${space(colBLength - b.toString().length - 3)} ${dim(chars.v)}`;
}
const top = `${chars.topLeft}${chars.h.repeat(colALength + 1)}${chars.hBottom}${chars.h.repeat(
const top = dim(`${chars.topLeft}${chars.h.repeat(colALength + 1)}${chars.hBottom}${chars.h.repeat(
colBLength
)}${chars.topRight}`;
const bottom = `${chars.bottomLeft}${chars.h.repeat(colALength + 1)}${chars.hTop}${chars.h.repeat(
)}${chars.topRight}`);
const bottom = dim(`${chars.bottomLeft}${chars.h.repeat(colALength + 1)}${chars.hTop}${chars.h.repeat(
colBLength
)}${chars.bottomRight}`;
const divider = `${chars.vRightThick}${chars.hThick.repeat(colALength + 1)}${
)}${chars.bottomRight}`);
const divider = dim(`${chars.vRightThick}${chars.hThick.repeat(colALength + 1)}${
chars.hThickCross
}${chars.hThick.repeat(colBLength)}${chars.vLeftThick}`;
}${chars.hThick.repeat(colBLength)}${chars.vLeftThick}`);
const rows: string[] = [top, formatRow(-1, colA, colB, bold), divider];
let i = 0;
for (const [key, value] of Object.entries(object)) {
Expand Down
28 changes: 21 additions & 7 deletions packages/astro/src/preferences/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ export type GetDotKey<
K extends string,
> = K extends `${infer U}.${infer Rest}` ? GetDotKey<T[U], Rest> : T[K];

export type PreferenceLocation = 'global' | 'project';
export interface PreferenceOptions {
location?: 'global' | 'project';
location?: PreferenceLocation;
}

type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>;
} : T;

export type PreferenceKey = DotKeys<Preferences>;
export interface PreferenceList extends Record<PreferenceLocation, DeepPartial<Preferences>> {
defaults: Preferences;
}

export interface AstroPreferences {
get<Key extends PreferenceKey>(
Expand All @@ -38,7 +46,8 @@ export interface AstroPreferences {
value: GetDotKey<Preferences, Key>,
opts?: PreferenceOptions
): Promise<void>;
getAll(opts?: PreferenceOptions): Promise<Record<string, any>>;
getAll(): Promise<Preferences>;
list(opts?: PreferenceOptions): Promise<PreferenceList>;
}

export function isValidKey(key: string): key is PreferenceKey {
Expand All @@ -62,7 +71,7 @@ export function coerce(key: string, value: unknown) {
export default function createPreferences(config: AstroConfig): AstroPreferences {
const global = new PreferenceStore(getGlobalPreferenceDir());
const project = new PreferenceStore(fileURLToPath(new URL('./.astro/', config.root)));
const stores = { global, project };
const stores: Record<PreferenceLocation, PreferenceStore> = { global, project };

return {
async get(key, { location } = {}) {
Expand All @@ -72,10 +81,15 @@ export default function createPreferences(config: AstroConfig): AstroPreferences
async set(key, value, { location = 'project' } = {}) {
stores[location].set(key, value);
},
async getAll({ location } = {}) {
if (!location)
return Object.assign({}, stores['global'].getAll(), stores['project'].getAll());
return stores[location].getAll();
async getAll() {
return Object.assign({}, DEFAULT_PREFERENCES, stores['global'].getAll(), stores['project'].getAll());
},
async list() {
return {
global: stores['global'].getAll(),
project: stores['project'].getAll(),
defaults: DEFAULT_PREFERENCES,
};
},
};
}
Expand Down

0 comments on commit 5428b3d

Please sign in to comment.