Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[legacy-framework] Move display package to nextjs fork #2989

Merged
merged 16 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/custom-server/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import blitz from "blitz/custom-server"
import {createServer} from "http"
import {parse} from "url"
import {log} from "@blitzjs/display"
import {log} from "next/dist/server/lib/logging"

const {PORT = "3000"} = process.env
const dev = process.env.NODE_ENV !== "production"
Expand Down
2 changes: 2 additions & 0 deletions nextjs/packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"caniuse-lite": "^1.0.30001228",
"chalk": "2.4.2",
"chokidar": "3.5.1",
"console-table-printer": "^2.7.5",
"constants-browserify": "1.0.0",
"cookie-session": "^1.4.0",
"cross-spawn": "7.0.3",
Expand Down Expand Up @@ -123,6 +124,7 @@
"react-is": "17.0.2",
"react-query": "3.21.1",
"react-refresh": "0.8.3",
"readline": "1.3.0",
"resolve-from": "^5.0.0",
"secure-password": "4.0.0",
"stream-browserify": "3.0.0",
Expand Down
115 changes: 115 additions & 0 deletions nextjs/packages/next/server/lib/logging.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ISettingsParam, Logger } from 'tslog'
import { loadConfigAtRuntime, LogLevel } from '../config-shared'
import c from 'chalk'
import { Table } from 'console-table-printer'
import ora from 'next/dist/compiled/ora'
import readline from 'readline'

// eslint-disable-next-line
declare module globalThis {
Expand Down Expand Up @@ -65,3 +69,114 @@ export const baseLogger = (options?: ISettingsParam): Logger => {

return globalThis._blitz_baseLogger
}

export const table = Table
export const chalk = c

// const blitzTrueBrandColor = '6700AB'
const blitzBrightBrandColor = '8a3df0'

// Using bright brand color so it's better for dark terminals
const brandColor = blitzBrightBrandColor

const withBrand = (str: string) => {
return c.hex(brandColor).bold(str)
}

const withCaret = (str: string) => {
return `${c.gray('>')} ${str}`
}

const withCheck = (str: string) => {
return `${c.green('✔')} ${str}`
}

const withProgress = (str: string) => {
return withCaret(str)
}

/**
* Logs a branded purple message to stdout.
*
* @param {string} msg
*/
const branded = (msg: string) => {
console.log(c.hex(brandColor).bold(msg))
}

/**
* Clears the line and optionally log a message to stdout.
*
* @param {string} msg
*/
const clearLine = (msg?: string) => {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
msg && process.stdout.write(msg)
}

const clearConsole = () => {
if (process.platform === 'win32') {
process.stdout.write('\x1B[2J\x1B[0f')
} else {
process.stdout.write('\x1B[2J\x1B[3J\x1B[H')
}
}

/**
* Logs a progress message to stdout.
*
* @param {string} msg
*/
const progress = (msg: string) => {
console.log(withProgress(msg))
}

const spinner = (str: string) => {
return ora({
text: str,
color: 'blue',
spinner: {
interval: 120,
frames: ['◢', '◣', '◤', '◥'],
},
})
}

/**
* Logs a green success message to stdout.
*
* @param {string} msg
*/
const success = (msg: string) => {
console.log(withCheck(c.green(msg)))
}

/**
* Colorizes a variable for display.
*
* @param {string} val
*/
const variable = (val: any) => {
return c.cyan.bold(`${val}`)
}

/**
* If the DEBUG env var is set this will write to the console
* @param str msg
*/
const debug = require('debug')('blitz')

export const log = {
withBrand,
withCaret,
branded,
clearLine,
clearConsole,
progress,
spinner,
success,
variable,
debug,
Table,
}
14 changes: 6 additions & 8 deletions nextjs/packages/next/stdlib-server/auth-sessions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Must import this type from 'blitz'
// import { log } from '@blitzjs/display'
import { baseLogger } from '../server/lib/logging'
import { fromBase64, toBase64 } from 'b64-lite'
import cookie from 'next/dist/compiled/cookie'
import fs from 'fs'
Expand Down Expand Up @@ -95,8 +94,9 @@ const defaultConfig: SessionConfig = {
} catch (error) {
// Session doesn't exist in DB for some reason, so create it
if (error.code === 'P2016') {
// log.warning("Could not update session because it's not in the DB")
console.log("Could not update session because it's not in the DB")
baseLogger({ displayDateTime: false }).warn(
"Could not update session because it's not in the DB"
)
} else {
throw error
}
Expand Down Expand Up @@ -693,8 +693,7 @@ async function getSessionKernel(
persistedSession.antiCSRFToken !== antiCSRFToken
) {
if (!antiCSRFToken) {
// log.warning(
console.log(
baseLogger({ displayDateTime: false }).warn(
`This request is missing the ${HEADER_CSRF} header. You can learn about adding this here: https://blitzjs.com/docs/session-management#manual-api-requests`
)
}
Expand Down Expand Up @@ -770,8 +769,7 @@ async function getSessionKernel(

if (enableCsrfProtection && payload.antiCSRFToken !== antiCSRFToken) {
if (!antiCSRFToken) {
// log.warning(
console.log(
baseLogger({ displayDateTime: false }).warn(
`This request is missing the ${HEADER_CSRF} header. You can learn about adding this here: https://blitzjs.com/docs/session-management#manual-api-requests`
)
}
Expand Down
19 changes: 18 additions & 1 deletion nextjs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6474,7 +6474,7 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@1.0.30001274, caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001093, caniuse-lite@^1.0.30001165, caniuse-lite@^1.0.30001202, caniuse-lite@^1.0.30001219, caniuse-lite@^1.0.30001228:
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001093, caniuse-lite@^1.0.30001165, caniuse-lite@^1.0.30001202, caniuse-lite@^1.0.30001219, caniuse-lite@^1.0.30001228:
version "1.0.30001274"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001274.tgz#26ca36204d15b17601ba6fc35dbdad950a647cc7"
integrity sha512-+Nkvv0fHyhISkiMIjnyjmf5YJcQ1IQHZN6U9TLUMroWR38FNwpsC51Gb68yueafX1V6ifOisInSgP9WJFS13ew==
Expand Down Expand Up @@ -7156,6 +7156,13 @@ console-control-strings@^1.0.0, console-control-strings@~1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/console-control-strings/-/console-control-strings-1.1.0.tgz#3d7cf4464db6446ea644bf4b39507f9851008e8e"

console-table-printer@^2.7.5:
version "2.10.0"
resolved "https://registry.yarnpkg.com/console-table-printer/-/console-table-printer-2.10.0.tgz#4a6875b95071b36542d1c55c6d9dcc6206e1e9f0"
integrity sha512-7pTsysaJs1+R+OO4cCtJbl+Lr4piHYIhi7/V1qHbOg/uiYgq2yUINFgvXZtVHqm9qpW0+Uk190qkGcKvzdunvg==
dependencies:
simple-wcswidth "^1.0.1"

constant-case@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/constant-case/-/constant-case-2.0.0.tgz#4175764d389d3fa9c8ecd29186ed6005243b6a46"
Expand Down Expand Up @@ -16554,6 +16561,11 @@ readjson@^2.0.1:
jju "^1.4.0"
try-catch "^3.0.0"

[email protected]:
version "1.3.0"
resolved "https://registry.yarnpkg.com/readline/-/readline-1.3.0.tgz#c580d77ef2cfc8752b132498060dc9793a7ac01c"
integrity sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw=

[email protected]:
version "0.18.5"
resolved "https://registry.yarnpkg.com/recast/-/recast-0.18.5.tgz#9d5adbc07983a3c8145f3034812374a493e0fe4d"
Expand Down Expand Up @@ -17672,6 +17684,11 @@ simple-swizzle@^0.2.2:
dependencies:
is-arrayish "^0.3.1"

simple-wcswidth@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/simple-wcswidth/-/simple-wcswidth-1.0.1.tgz#8ab18ac0ae342f9d9b629604e54d2aa1ecb018b2"
integrity sha512-xMO/8eNREtaROt7tJvWJqHBDTMFN4eiQ5I4JRMuilwfnFcV5W9u7RUkueNkdw0jPqGMX36iCywelS5yilTuOxg==

sinon@^1.17.6:
version "1.17.7"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-1.17.7.tgz#4542a4f49ba0c45c05eb2e9dd9d203e2b8efe0bf"
Expand Down
1 change: 0 additions & 1 deletion packages/blitz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
},
"dependencies": {
"@blitzjs/cli": "0.43.0",
"@blitzjs/display": "0.43.0",
"@blitzjs/generator": "0.43.0",
"@blitzjs/server": "0.43.0",
"@testing-library/jest-dom": "5.11.9",
Expand Down
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"/lib"
],
"dependencies": {
"@blitzjs/display": "0.43.0",
"@blitzjs/generator": "0.43.0",
"@blitzjs/installer": "0.43.0",
"@blitzjs/server": "0.43.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {log} from "@blitzjs/display"
import {Command, flags} from "@oclif/command"
import {log} from "next/dist/server/lib/logging"
import {getPackageJson} from "../utils/get-package-json"
import {runPrisma} from "./prisma"

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Console extends Command {
async run() {
process.env.CLI_COMMAND_CONSOLE = "true"
this.parse(Console)
const {log} = require("@blitzjs/display")
const {log} = require("next/dist/server/lib/logging")
const chalk = require("chalk")
log.branded("You have entered the Blitz console")
console.log(chalk.yellow("Tips: - Exit by typing .exit or pressing Ctrl-D"))
Expand Down
11 changes: 6 additions & 5 deletions packages/cli/src/commands/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {log} from "@blitzjs/display"
import {Command, flags} from "@oclif/command"
import {baseLogger} from "next/dist/server/lib/logging"
import {baseLogger, log} from "next/dist/server/lib/logging"

export function getDbName(connectionString: string): string {
const dbUrlParts: string[] = connectionString!.split("/")
Expand All @@ -25,7 +24,7 @@ async function runSeed(seedBasePath: string) {
throw new Error(`Couldn't find default export from ${seedBasePath}`)
}
} catch (err) {
log.error(`Couldn't import default from ${seedBasePath}`)
baseLogger({displayDateTime: false}).error(`Couldn't import default from ${seedBasePath}`)
throw err
}
spinner.succeed()
Expand All @@ -35,7 +34,9 @@ async function runSeed(seedBasePath: string) {
seeds && (await seeds())
} catch (err) {
baseLogger().prettyError(err as any)
log.error(`Couldn't run imported function, are you sure it's a function?`)
baseLogger({displayDateTime: false}).error(
`Couldn't run imported function, are you sure it's a function?`,
)
throw err
}

Expand Down Expand Up @@ -86,7 +87,7 @@ ${require("chalk").bold("seed")} Generates seeded data in database via Prisma.
try {
return await runSeed(flags.file)
} catch (err) {
log.error("Could not seed database:")
baseLogger({displayDateTime: false}).error("Could not seed database:")
baseLogger().prettyError(err as any)
process.exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Dev extends Command {
blitzConfig.cli?.clearConsoleOnBlitzDev !== false &&
!process.env.BLITZ_TEST_ENVIRONMENT
) {
const {log} = await import("@blitzjs/display")
const {log} = await import("next/dist/server/lib/logging")
log.clearConsole()
}

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {log} from "@blitzjs/display"
import {
capitalize,
FormGenerator,
Expand All @@ -16,6 +15,7 @@ import {
} from "@blitzjs/generator"
import {flags} from "@oclif/command"
import chalk from "chalk"
import {baseLogger} from "next/dist/server/lib/logging"
import {Command} from "../command"
import {PromptAbortedError} from "../errors/prompt-aborted"

Expand Down Expand Up @@ -180,7 +180,7 @@ export class Generate extends Command {
async handleNoContext(message: string): Promise<void> {
const shouldCreateNewRoot = await this.genericConfirmPrompt(message)
if (!shouldCreateNewRoot) {
require("@blitzjs/display").log.error(
baseLogger({displayDateTime: false}).error(
"Could not determine proper location for files. Aborting.",
)
this.exit(0)
Expand Down Expand Up @@ -263,7 +263,7 @@ export class Generate extends Command {
} catch (err) {
if (err instanceof PromptAbortedError) this.exit(0)

log.error(err as any)
baseLogger({displayDateTime: false}).error(err)
this.exit(1)
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {log} from "@blitzjs/display"
import type {RecipeCLIArgs, RecipeCLIFlags, RecipeExecutor} from "@blitzjs/installer"
import {flags} from "@oclif/command"
import {bootstrap} from "global-agent"
import {baseLogger, log} from "next/dist/server/lib/logging"
import {join, resolve} from "path"
import {Stream} from "stream"
import {promisify} from "util"
Expand All @@ -25,7 +25,7 @@ const pipeline = promisify(Stream.pipeline)
async function got(url: string) {
return require("got")(url).catch((e: any) => {
if (e.response.statusCode === 403) {
log.error(e.response.body)
baseLogger({displayDateTime: false}).error(e.response.body)
} else {
return e
}
Expand Down Expand Up @@ -295,7 +295,7 @@ export class Install extends Command {

if (!(await isUrlValid(packageJsonPath))) {
debug("Url is invalid for ", packageJsonPath)
log.error(`Could not find recipe "${args.recipe}"\n`)
baseLogger({displayDateTime: false}).error(`Could not find recipe "${args.recipe}"\n`)
console.log(`${chalk.bold("Please provide one of the following:")}

1. The name of a recipe to install (e.g. "tailwind")
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {log} from "@blitzjs/display"
import type {AppGeneratorOptions} from "@blitzjs/generator"
import {getLatestVersion} from "@blitzjs/generator"
import {flags} from "@oclif/command"
import chalk from "chalk"
import spawn from "cross-spawn"
import hasbin from "hasbin"
import {log} from "next/dist/server/lib/logging"
import {lt} from "semver"
const debug = require("debug")("blitz:new")

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {table as Table} from "@blitzjs/display"
import {ServerConfig} from "@blitzjs/server"
import {Command, flags} from "@oclif/command"
import {table as Table} from "next/dist/server/lib/logging"
import {newline} from "next/dist/server/lib/logging"

export class Routes extends Command {
Expand Down
4 changes: 0 additions & 4 deletions packages/display/.gitignore

This file was deleted.

Loading