import { basename , dirname , extname } from 'path' ; import { codeFrameColumns } from '@babel/code-frame' ; import debug from 'debug' ; import * as chalk from 'chalk' ; import pkg from '../package.json' ; import type { Debugger } from 'debug' ; import type { File } from './transform' ; const c = new chalk . Instance ( { level : 3 , } ) ; function getPackageNameWithoutNamespace ( ) { return pkg . name . split ( '/' ) [ 1 ] ; } function niceName ( name ) { const niceName = basename ( name , extname ( name ) ) ; if ( niceName !== 'index' ) { return niceName ; } return basename ( dirname ( name ) ) ; } export function createDebug ( filename ) { return debug ( `${ getPackageNameWithoutNamespace ( ) }:${ niceName ( filename ) }` ) ; } type LogOptions = { file ? : File ; loc ? : { start : { line : number ; column : number ; } ; end : { line : number ; column : number ; } ; } | null ; } ; function createFrame ( { file , loc } = { } ) { if ( ! file || ! loc ) { return '' ; } loc . start . column ++ ; loc . end . column ++ ; const frame = codeFrameColumns ( file . source , loc , { highlightCode : true , forceColor : true , } ) ; return `\nat ${ file . path }:\n${ frame }` ; } export function info ( msg , options ? ) { console . info ( `${ c . cyan ( 'INFO:' ) } ${ msg }${ createFrame ( options ) }` ) ; } export function warn ( msg , options ? ) { console . warn ( `${ c . yellow ( 'WARN:' ) } ${ msg }${ createFrame ( options ) }` ) ; }