Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Jan 19, 2024
1 parent 00f9feb commit 38f30c1
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 84 deletions.
6 changes: 3 additions & 3 deletions code/lib/cli/src/automigrate/helpers/eslintPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fse, { readFile, readJson, writeJson } from 'fs-extra';

import { dedent } from 'ts-dedent';
import detectIndent from 'detect-indent';
import { readConfig, writeConfig } from '@storybook/csf-tools';
import prompts from 'prompts';
import chalk from 'chalk';

import { readConfig, writeConfig } from '@storybook/csf-tools';
import type { JsPackageManager } from '@storybook/core-common';
import { paddedLog } from '../../helpers';
import { paddedLog } from '@storybook/core-common';

export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs', 'json'];
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml'];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, vi, it } from 'vitest';
import { getMigrationSummary } from './getMigrationSummary';
import { FixStatus } from '../types';
import type { InstallationMetadata } from '../../js-package-manager/types';
import type { InstallationMetadata } from '@storybook/core-common';

vi.mock('boxen', () => ({
default: vi.fn((str, { title = '' }) => `${title}\n\n${str.replace(/\x1b\[[0-9;]*[mG]/g, '')}`),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import chalk from 'chalk';
import boxen from 'boxen';
import dedent from 'ts-dedent';
import type { InstallationMetadata } from '@storybook/core-common';
import type { FixSummary } from '../types';
import { FixStatus } from '../types';
import type { InstallationMetadata } from '../../js-package-manager/types';
import { getDuplicatedDepsWarnings } from '../../doctor/getDuplicatedDepsWarnings';

export const messageDivider = '\n\n';
Expand Down
3 changes: 2 additions & 1 deletion code/lib/cli/src/automigrate/helpers/mainConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { readConfig, writeConfig as writeConfigFile } from '@storybook/csf-tools
import chalk from 'chalk';
import dedent from 'ts-dedent';
import path from 'path';
import type { getStorybookVersion, JsPackageManager } from '@storybook/core-common';
import type { JsPackageManager } from '@storybook/core-common';
import { getStorybookVersion } from '@storybook/core-common';

const logger = console;

Expand Down
4 changes: 2 additions & 2 deletions code/lib/cli/src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
unsupportedTemplate,
CoreBuilder,
} from './project_types';
import { commandLog, isNxProject } from './helpers';
import { isNxProject } from './helpers';
import type { JsPackageManager, PackageJsonWithMaybeDeps } from '@storybook/core-common';
import { HandledError } from './HandledError';
import { commandLog, HandledError } from '@storybook/core-common';

const viteConfigFiles = ['vite.config.ts', 'vite.config.js', 'vite.config.mjs'];
const webpackConfigFiles = ['webpack.config.js'];
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/src/doctor/getDuplicatedDepsWarnings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import { frameworkPackages, rendererPackages } from '@storybook/core-common';
import type { InstallationMetadata } from '@storybook/core-common';
import { hasMultipleVersions } from './hasMultipleVersions';
import type { InstallationMetadata } from '../js-package-manager/types';

export const messageDivider = '\n\n';

Expand Down
3 changes: 2 additions & 1 deletion code/lib/cli/src/generators/ANGULAR/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { join } from 'path';
import { paddedLog } from '@storybook/core-common';
import { baseGenerator } from '../baseGenerator';
import type { Generator } from '../types';
import { CoreBuilder } from '../../project_types';
import { AngularJSON, compoDocPreviewPrefix, promptForCompoDocs } from './helpers';
import { getCliDir } from '../../dirs';
import { paddedLog, copyTemplate } from '../../helpers';
import { copyTemplate } from '../../helpers';

const generator: Generator<{ projectName: string }> = async (
packageManager,
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/src/generators/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NpmOptions } from '../NpmOptions';
import type { SupportedLanguage, Builder, ProjectType } from '../project_types';
import type { JsPackageManager, PackageManagerName } from '../js-package-manager/JsPackageManager';
import type { JsPackageManager, PackageManagerName } from '@storybook/core-common';
import type { FrameworkPreviewParts } from './configure';

export type GeneratorOptions = {
Expand Down
64 changes: 1 addition & 63 deletions code/lib/cli/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
} from '@storybook/core-common';
import type { SupportedFrameworks, SupportedRenderers } from './project_types';
import { SupportedLanguage } from './project_types';
import {versions as storybookMonorepoPackages} from '@storybook/core-common';
import { versions as storybookMonorepoPackages } from '@storybook/core-common';

const logger = console;

Expand Down Expand Up @@ -46,68 +46,6 @@ export const writeFileAsJson = (jsonPath: string, content: unknown) => {
return true;
};

export const commandLog = (message: string) => {
process.stdout.write(chalk.cyan(' • ') + message);

// Need `void` to be able to use this function in a then of a Promise<void>
return (errorMessage?: string | void, errorInfo?: string) => {
if (errorMessage) {
process.stdout.write(`. ${chalk.red('✖')}\n`);
logger.error(`\n ${chalk.red(errorMessage)}`);

if (!errorInfo) {
return;
}

const newErrorInfo = errorInfo
.split('\n')
.map((line) => ` ${chalk.dim(line)}`)
.join('\n');
logger.error(`${newErrorInfo}\n`);
return;
}

process.stdout.write(`. ${chalk.green('✓')}\n`);
};
};

export function paddedLog(message: string) {
const newMessage = message
.split('\n')
.map((line) => ` ${line}`)
.join('\n');

logger.log(newMessage);
}

export function getChars(char: string, amount: number) {
let line = '';
for (let lc = 0; lc < amount; lc += 1) {
line += char;
}

return line;
}

export function codeLog(codeLines: string[], leftPadAmount?: number) {
let maxLength = 0;
const newLines = codeLines.map((line) => {
maxLength = line.length > maxLength ? line.length : maxLength;
return line;
});

const finalResult = newLines
.map((line) => {
const rightPadAmount = maxLength - line.length;
let newLine = line + getChars(' ', rightPadAmount);
newLine = getChars(' ', leftPadAmount || 2) + chalk.inverse(` ${newLine} `);
return newLine;
})
.join('\n');

logger.log(finalResult);
}

/**
* Detect if any babel dependencies need to be added to the project
* This is currently used by react-native generator
Expand Down
14 changes: 9 additions & 5 deletions code/lib/cli/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import prompts from 'prompts';
import { telemetry } from '@storybook/telemetry';
import { withTelemetry } from '@storybook/core-server';
import { NxProjectDetectedError } from '@storybook/core-events/server-errors';
import {
versions,
HandledError,
JsPackageManagerFactory,
commandLog,
codeLog,
paddedLog,
} from '@storybook/core-common';
import type { JsPackageManager } from '@storybook/core-common';

import dedent from 'ts-dedent';
import boxen from 'boxen';
import { lt, prerelease } from 'semver';
import type { Builder } from './project_types';
import { installableProjectTypes, ProjectType } from './project_types';
import { detect, isStorybookInstantiated, detectLanguage, detectPnp } from './detect';
import { commandLog, codeLog, paddedLog } from './helpers';
import angularGenerator from './generators/ANGULAR';
import emberGenerator from './generators/EMBER';
import reactGenerator from './generators/REACT';
Expand All @@ -28,13 +36,9 @@ import qwikGenerator from './generators/QWIK';
import svelteKitGenerator from './generators/SVELTEKIT';
import solidGenerator from './generators/SOLID';
import serverGenerator from './generators/SERVER';
import type { JsPackageManager } from '@storybook/core-common';
import { JsPackageManagerFactory } from '@storybook/core-common';
import type { NpmOptions } from './NpmOptions';
import type { CommandOptions, GeneratorOptions } from './generators/types';
import { HandledError } from './HandledError';
import { currentDirectoryIsEmpty, scaffoldNewProject } from './scaffold-new-project';
import { versions } from '@storybook/core-common';

const logger = console;

Expand Down
10 changes: 7 additions & 3 deletions code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import chalk from 'chalk';
import dedent from 'ts-dedent';
import boxen from 'boxen';
import type { PackageManagerName } from '@storybook/core-common';
import { JsPackageManagerFactory } from '@storybook/core-common';
import { coerceSemver, commandLog } from './helpers';
import {
JsPackageManagerFactory,
isCorePackage,
versions,
commandLog,
} from '@storybook/core-common';
import { coerceSemver } from './helpers';
import { automigrate } from './automigrate';
import { isCorePackage, versions } from '@storybook/core-common';

type Package = {
package: string;
Expand Down
2 changes: 2 additions & 0 deletions code/lib/core-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './utils/get-storybook-configuration';
export * from './utils/get-storybook-info';
export * from './utils/get-storybook-refs';
export * from './utils/glob-to-regexp';
export * from './utils/HandledError';
export * from './utils/handlebars';
export * from './utils/interpolate';
export * from './utils/interpret-files';
Expand All @@ -22,6 +23,7 @@ export * from './utils/load-custom-presets';
export * from './utils/load-main-config';
export * from './utils/load-manager-or-addons-file';
export * from './utils/load-preview-or-config-file';
export * from './utils/log';
export * from './utils/log-config';
export * from './utils/normalize-stories';
export * from './utils/paths';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import fs from 'fs';
import dedent from 'ts-dedent';
import { readFile, writeFile, readFileSync } from 'fs-extra';
import invariant from 'tiny-invariant';
import { commandLog } from '../../../cli/src/helpers';
import { commandLog } from '../utils/log';
import type { PackageJson, PackageJsonWithDepsAndDevDeps } from './PackageJson';
import storybookPackagesVersions from '../versions';
import type { InstallationMetadata } from './types';
import { HandledError } from '../../../cli/src/HandledError';
import { HandledError } from '../utils/HandledError';

const logger = console;

Expand Down
File renamed without changes.
65 changes: 65 additions & 0 deletions code/lib/core-common/src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import chalk from 'chalk';

const logger = console;

export const commandLog = (message: string) => {
process.stdout.write(chalk.cyan(' • ') + message);

// Need `void` to be able to use this function in a then of a Promise<void>
return (errorMessage?: string | void, errorInfo?: string) => {
if (errorMessage) {
process.stdout.write(`. ${chalk.red('✖')}\n`);
logger.error(`\n ${chalk.red(errorMessage)}`);

if (!errorInfo) {
return;
}

const newErrorInfo = errorInfo
.split('\n')
.map((line) => ` ${chalk.dim(line)}`)
.join('\n');
logger.error(`${newErrorInfo}\n`);
return;
}

process.stdout.write(`. ${chalk.green('✓')}\n`);
};
};

export function paddedLog(message: string) {
const newMessage = message
.split('\n')
.map((line) => ` ${line}`)
.join('\n');

logger.log(newMessage);
}

export function getChars(char: string, amount: number) {
let line = '';
for (let lc = 0; lc < amount; lc += 1) {
line += char;
}

return line;
}

export function codeLog(codeLines: string[], leftPadAmount?: number) {
let maxLength = 0;
const newLines = codeLines.map((line) => {
maxLength = line.length > maxLength ? line.length : maxLength;
return line;
});

const finalResult = newLines
.map((line) => {
const rightPadAmount = maxLength - line.length;
let newLine = line + getChars(' ', rightPadAmount);
newLine = getChars(' ', leftPadAmount || 2) + chalk.inverse(` ${newLine} `);
return newLine;
})
.join('\n');

logger.log(finalResult);
}

0 comments on commit 38f30c1

Please sign in to comment.