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

style: separate logs from subprocesses #68

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"dependencies": {
"@clack/core": "^0.3.4",
"@clack/prompts": "^0.7.0",
"ascii-box": "^1.1.2",
"gluegun": "5.1.6",
"is-git-dirty": "^2.0.2",
"look-it-up": "^2.1.0",
Expand Down
30 changes: 30 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import { print } from 'gluegun'

export const COLORS = {
bold: print.colors.bold,
cyan: print.colors.cyan,
green: print.colors.green,
yellow: print.colors.yellow,
gray: print.colors.gray,
red: print.colors.red,
inverse: print.colors.inverse,
dim: print.colors.dim,
strikethrough: print.colors.strikethrough,
}

export const S_STEP_WARNING = COLORS.yellow('▲')
export const S_STEP_ERROR = COLORS.red('■')
export const S_STEP_SUCCESS = COLORS.green('◇')
export const S_SELECT = COLORS.cyan('◆')
export const S_ACTION = COLORS.cyan('▼')
export const S_ACTION_BULLET = COLORS.cyan('►')
export const S_BAR = '│'
export const S_VBAR = '─'
export const S_UL = '╭'
export const S_DL = '╰'
export const S_UR = '╮'
export const S_DR = '╯'
export const S_BAR_END = '└'
export const S_RADIO_ACTIVE = '●'
export const S_RADIO_INACTIVE = '○'

export const HELP_FLAG = 'help'
export const PRESET_FLAG = 'preset'

Expand Down
3 changes: 2 additions & 1 deletion src/extensions/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createHash } from 'crypto'
import { createReadStream } from 'fs'
import { join, sep } from 'path'
import { expandDirectory } from '../utils/expandDirectory'
import { S_ACTION } from '../constants'

const prettyTree = require('pretty-file-tree')

Expand Down Expand Up @@ -119,7 +120,7 @@ module.exports = (toolbox: CycliToolbox) => {
}

toolbox.interactive.info(
'The following files have been added or modified:',
`${S_ACTION} The following files have been added or modified:`,
'cyan'
)
toolbox.interactive.vspace()
Expand Down
23 changes: 10 additions & 13 deletions src/extensions/furtherActions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { S_ACTION_BULLET } from '../constants'
import { CycliToolbox } from '../types'

const box = require('ascii-box').box

module.exports = (toolbox: CycliToolbox) => {
const furtherActions: string[] = []

Expand All @@ -12,17 +11,15 @@ module.exports = (toolbox: CycliToolbox) => {
const print = () => {
if (furtherActions.length > 0) {
toolbox.interactive.vspace()

toolbox.interactive.info(
`${box(
`=== What next?\n\n${furtherActions
.map((action) => `● ${action}`)
.join('\n\n')}`,
{ border: 'round', maxWidth: 90 }
)}
`,
'cyan'
)
toolbox.interactive.sectionHeader('What next?', { color: 'cyan' })
furtherActions.forEach((action, index) => {
if (index > 0) {
toolbox.interactive.vspace()
}
toolbox.interactive.info(`${S_ACTION_BULLET} ${action}`, 'cyan')
})
toolbox.interactive.sectionFooter({ color: 'cyan' })
toolbox.interactive.vspace()
}
}

Expand Down
149 changes: 119 additions & 30 deletions src/extensions/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,34 @@ import {
isCancel,
log as clackLog,
} from '@clack/prompts'
import { CycliToolbox } from '../types'
import { CycliToolbox, MessageColor } from '../types'
import { ConfirmPrompt } from '@clack/core'
import { spawn } from 'child_process'
import {
COLORS,
S_BAR,
S_BAR_END,
S_DL,
S_DR,
S_RADIO_ACTIVE,
S_RADIO_INACTIVE,
S_SELECT,
S_STEP_ERROR,
S_STEP_SUCCESS,
S_STEP_WARNING,
S_UL,
S_UR,
S_VBAR,
} from '../constants'

interface Spinner {
stop: () => void
}

const COLORS = {
cyan: print.colors.cyan,
green: print.colors.green,
yellow: print.colors.yellow,
gray: print.colors.gray,
red: print.colors.red,
inverse: print.colors.inverse,
dim: print.colors.dim,
strikethrough: print.colors.strikethrough,
}

type MessageColor = keyof typeof COLORS
const DEFAULT_HEADER_WIDTH = 80

module.exports = (toolbox: CycliToolbox) => {
const { cyan, yellow, gray, red, inverse, dim, strikethrough } = COLORS

const S_STEP_ERROR = yellow('▲')
const S_SUCCESS = cyan('◆')
const S_STEP_CANCEL = red('■')
const S_BAR = '│'
const S_BAR_END = '└'
const S_RADIO_ACTIVE = '●'
const S_RADIO_INACTIVE = '○'
const { bold, cyan, yellow, gray, inverse, dim, strikethrough } = COLORS

const confirm = async (
message: string,
Expand All @@ -46,11 +44,11 @@ module.exports = (toolbox: CycliToolbox) => {
const title = () => {
switch (type) {
case 'normal':
return `${gray(S_BAR)}\n${S_SUCCESS} ${message
return `${gray(S_BAR)}\n${S_SELECT} ${message
.split('\n')
.join(`\n${S_BAR} `)}\n`
case 'warning':
return `${gray(S_BAR)} \n${S_STEP_ERROR} ${yellow(
return `${gray(S_BAR)} \n${S_STEP_WARNING} ${yellow(
message.split('\n').join(`\n${S_BAR} `)
)}\n`
}
Expand All @@ -59,11 +57,11 @@ module.exports = (toolbox: CycliToolbox) => {
const titleSubmitted = () => {
switch (type) {
case 'normal':
return `${gray(S_BAR)} \n${S_SUCCESS} ${message
return `${gray(S_BAR)} \n${S_SELECT} ${message
.split('\n')
.join(`\n${gray(S_BAR)} `)}\n`
case 'warning':
return `${gray(S_BAR)} \n${S_STEP_ERROR} ${message
return `${gray(S_BAR)} \n${S_STEP_WARNING} ${message
.split('\n')
.map((line) => yellow(line))
.join(`\n${gray(S_BAR)} `)}\n`
Expand All @@ -73,7 +71,7 @@ module.exports = (toolbox: CycliToolbox) => {
const titleCancelled = () => {
switch (type) {
case 'normal':
return `${gray(S_BAR)} \n${S_STEP_CANCEL} ${message
return `${gray(S_BAR)} \n${S_STEP_ERROR} ${message
.split('\n')
.join(`\n${gray(S_BAR)} `)}\n`
case 'warning':
Expand Down Expand Up @@ -142,19 +140,19 @@ module.exports = (toolbox: CycliToolbox) => {
const vspace = () => info('')

const step = (message: string) => {
print.info(`${COLORS.green('✔')} ${message} `)
print.info(`${S_STEP_SUCCESS} ${message} `)
}

const error = (message: string) => {
print.error(` ${message} `)
print.error(`${S_STEP_ERROR} ${message} `)
}

const success = (message: string) => {
print.success(message)
}

const warning = (message: string) => {
print.warning(` ${message} `)
print.warning(`${S_STEP_WARNING} ${message} `)
}

const spin = (message: string): Spinner => {
Expand All @@ -169,6 +167,84 @@ module.exports = (toolbox: CycliToolbox) => {
clackOutro(message)
}

const sectionHeader = (
title: string,
{
width = DEFAULT_HEADER_WIDTH,
color,
}: { width?: number; color?: MessageColor } = {}
) => {
const headerMessage = `${S_UL}${S_VBAR.repeat(
Math.floor((width - title.length - 3) / 2)
)} ${bold(title)} ${S_VBAR.repeat(
Math.ceil((width - title.length - 3) / 2)
)}`
info(headerMessage, color)
info(`${S_DR}`, color)
}

const sectionFooter = ({
width = DEFAULT_HEADER_WIDTH,
color,
}: { width?: number; color?: MessageColor } = {}) => {
info(`${S_UR}\n${S_DL}${S_VBAR.repeat(width - 1)}`, color)
}

const spawnSubprocess = async (
processName: string,
command: string,
{ alwaysPrintStderr = false }: { alwaysPrintStderr?: boolean } = {}
): Promise<void> => {
return new Promise((resolve, reject) => {
vspace()
sectionHeader(`Running ${processName}...`)

const subprocess = spawn(command, {
stdio: ['inherit', 'inherit', alwaysPrintStderr ? 'inherit' : 'pipe'],
shell: true,
})

step(`Started ${processName}.`)

let stderr = ''

if (!alwaysPrintStderr) {
subprocess.stderr?.on('data', (data) => {
stderr += data.toString()
})
}

subprocess.on('close', (code) => {
if (code !== 0) {
error(`${processName} exited with code ${code}.`)

if (!alwaysPrintStderr && stderr) {
info(`\nstderr: ${stderr}`, 'red')
}
} else {
step(`Finished running ${processName}.`)
}

sectionFooter()
vspace()

if (code !== 0) {
reject(
new Error(
`Failed to execute ${command}. The subprocess exited with code ${code}.`
)
)
} else {
resolve()
}
})

subprocess.on('error', (err) => {
reject(err)
})
})
}

toolbox.interactive = {
confirm,
surveyStep,
Expand All @@ -182,6 +258,9 @@ module.exports = (toolbox: CycliToolbox) => {
spin,
intro,
outro,
sectionHeader,
sectionFooter,
spawnSubprocess,
}
}

Expand All @@ -199,5 +278,15 @@ export interface InteractiveExtension {
spin: (message: string) => Spinner
intro: (message: string) => void
outro: (message: string) => void
sectionHeader: (
title: string,
options?: { width?: number; color?: MessageColor }
) => void
sectionFooter: (options?: { width?: number; color?: MessageColor }) => void
spawnSubprocess: (
processName: string,
command: string,
options?: { alwaysPrintStderr?: boolean }
) => Promise<void>
}
}
8 changes: 5 additions & 3 deletions src/recipes/build-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ export const createReleaseBuildWorkflowsForExpo = async (
const existsIOsDir = toolbox.filesystem.exists('ios')

toolbox.print.info('⚙️ Running expo prebuild to setup app.json properly.')
await toolbox.system.spawn(`npx expo prebuild --${context.packageManager}`, {
stdio: 'inherit',
})
await toolbox.interactive.spawnSubprocess(
'Expo prebuild',
`npx expo prebuild --${context.packageManager}`,
{ alwaysPrintStderr: true }
)

if (platforms.includes('android')) {
await createReleaseBuildWorkflowAndroid(toolbox, context)
Expand Down
14 changes: 8 additions & 6 deletions src/recipes/eas-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ const execute =
'Detected eas.json, skipping EAS Build configuration.'
)
} else {
await toolbox.system.spawn('eas build:configure -p all', {
stdio: 'inherit',
})
await toolbox.interactive.spawnSubprocess(
'EAS Build configuration',
'eas build:configure -p all'
)

toolbox.interactive.step('Created default EAS Build configuration.')
}

await toolbox.system.spawn('eas update:configure', {
stdio: 'inherit',
})
await toolbox.interactive.spawnSubprocess(
'EAS Update configuration',
'eas update:configure'
)

await toolbox.workflows.generate(join('eas', 'eas-update.ejf'), context)

Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { ProjectContextExtension } from './extensions/projectContext'
import { ScriptsExtension } from './extensions/scripts'
import { WorkflowsExtension } from './extensions/workflows'
import { ProjectConfigExtension } from './extensions/projectConfig'
import { LOCK_FILE_TO_MANAGER } from './constants'
import { COLORS, LOCK_FILE_TO_MANAGER } from './constants'
import { DiffExtension } from './extensions/diff'
import { OptionsExtension } from './extensions/options'
import { FurtherActionsExtension } from './extensions/furtherActions'

export type MessageColor = keyof typeof COLORS

export interface PackageJson {
name: string
dependencies?: {
Expand Down
Loading