@@ -5,16 +5,19 @@ import Chalk from 'chalk';
55import prompts from 'prompts' ;
66import { $ } from 'execa' ;
77
8- import { fileURLToPath } from 'url' ;
9- import { dirname , join , relative } from 'path' ;
8+ import { fileURLToPath } from 'node:url' ;
9+ import { dirname , join , relative } from 'node:path' ;
10+ import * as path from 'node:path' ;
1011
1112import { exists , mkdirp , processTemplate , readFile , writeFile } from './files.js' ;
1213import { memoize } from './fp.js' ;
1314
14- const { blue, green, greenBright } = Chalk ;
15+ const { blue, green, greenBright, red , yellow } = Chalk ;
1516
1617const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
1718
19+ const $$ = $ ( { stderr : 'inherit' } ) ;
20+
1821/**
1922* Available filenames.
2023*
@@ -142,37 +145,56 @@ async function writeComponentFile(key: FileKey, options: GenerateElementOptions)
142145 }
143146}
144147
148+ async function getElementPackageJsonPath ( options : GenerateElementOptions ) {
149+ const abspath = getComponentAbsPath ( options ) ;
150+ const { root } = path . parse ( abspath ) ;
151+ let packageJsonPath ;
152+ let currentdir = abspath ;
153+ while ( currentdir !== root && ! packageJsonPath ) {
154+ const possible = join ( currentdir , 'package.json' ) ;
155+ if ( await exists ( possible ) ) {
156+ packageJsonPath = possible ;
157+ } else {
158+ currentdir = dirname ( currentdir ) ;
159+ }
160+ }
161+ return packageJsonPath ;
162+ }
163+
164+ export class PackageJSONError extends Error { }
165+
145166/**
146167 * Generate an Element
147168 */
148169export async function generateElement ( options : GenerateElementOptions ) : Promise < void > {
149- // ctrl-c
170+ const log = ( ...args : unknown [ ] ) => void ( ! options ?. silent && console . log ( ...args ) ) ;
171+ const start = performance . now ( ) ;
150172 if ( ! options || ! options . tagName ) {
173+ // ctrl-c
151174 return ;
152- }
153-
154- const log = ( ...args : unknown [ ] ) => ! options . silent && console . log ( ...args ) ;
155-
156- const $$ = $ ( { stderr : 'inherit' } ) ;
157-
158- const packageJsonPath = join ( options . directory , 'package.json' ) ;
159- // Quit if trying to scaffold an element in an uninitialized non-monorepo
160- if ( ! await exists ( packageJsonPath ) ) {
161- return console . log ( '‼️ No package.json found.' , '� Scaffold a repository first' ) ;
175+ } else if ( ! await exists ( join ( options . directory , 'package.json' ) ) ) {
176+ // Quit if trying to scaffold an element in an uninitialized non-monorepo
177+ throw new PackageJSONError ( '‼️ No package.json found. � Scaffold a repository first' ) ;
162178 } else if ( ! await shouldWriteToDir ( options ) ) {
163- return ;
179+ return log ( red `Skipping` , 'file write!' ) ;
164180 } else {
181+ const packageJsonPath = await getElementPackageJsonPath ( options ) ?? './**/package.json ./package.json' ;
182+ if ( ! await exists ( packageJsonPath ) ) {
183+ throw new PackageJSONError ( `Could not find package at ${ packageJsonPath } ` ) ;
184+ }
165185 log ( `\nCreating ${ green ( options . tagName ) } in ${ getComponentPathFromDirectoryOption ( options ) } \n` ) ;
166186 log ( blue `Writing` , 'files...' ) ;
167187 // $ mkdir -p /Users/alj/jazz-elements/elements/pf-jazz-hands
168188 await mkdirp ( getComponentAbsPath ( options ) ) ;
169189 for ( const key of Object . keys ( FileKey ) . sort ( ) as FileKey [ ] ) {
170190 await writeComponentFile ( key , options ) ;
171191 }
172- log ( blue `Linting` , ' package exports...' ) ;
192+ log ( blue `Linting` , ` ${ relative ( options . directory , packageJsonPath ) } for package exports...` ) ;
173193 await $$ `npx eslint ${ packageJsonPath } --fix` ;
174194 log ( blue `Analyzing` , 'elements...' ) ;
175195 await $$ `npm run analyze` ;
176- log ( `\n${ greenBright ( 'Done!' ) } ` ) ;
196+ const end = performance . now ( ) ;
197+ const seconds = ( end - start ) / 1000 ;
198+ log ( `\n${ greenBright `Done` } in ${ yellow ( seconds . toFixed ( 2 ) ) } seconds` ) ;
177199 }
178200}
0 commit comments