11import { invariant , singleDebounce } from '@zenstackhq/common-helpers' ;
22import { ZModelLanguageMetaData } from '@zenstackhq/language' ;
3- import { isPlugin , LiteralExpr , Plugin , type AbstractDeclaration , type Model } from '@zenstackhq/language/ast' ;
3+ import { isPlugin , type AbstractDeclaration , type Model } from '@zenstackhq/language/ast' ;
44import { getLiteral , getLiteralArray } from '@zenstackhq/language/utils' ;
55import { type CliPlugin } from '@zenstackhq/sdk' ;
66import { watch } from 'chokidar' ;
77import colors from 'colors' ;
8- import { createJiti } from 'jiti' ;
9- import fs from 'node:fs' ;
108import path from 'node:path' ;
11- import { pathToFileURL } from 'node:url' ;
129import ora , { type Ora } from 'ora' ;
1310import semver from 'semver' ;
1411import { CliError } from '../cli-error' ;
1512import * as corePlugins from '../plugins' ;
1613import {
1714 getOutputPath ,
15+ getPluginProvider ,
1816 getSchemaFile ,
1917 getZenStackPackages ,
18+ loadPluginModule ,
2019 loadSchemaDocument ,
2120 startUsageTipsFetch ,
2221} from './action-utils' ;
@@ -258,14 +257,7 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string,
258257 }
259258}
260259
261- function getPluginProvider ( plugin : Plugin ) {
262- const providerField = plugin . fields . find ( ( f ) => f . name === 'provider' ) ;
263- invariant ( providerField , `Plugin ${ plugin . name } does not have a provider field` ) ;
264- const provider = ( providerField . value as LiteralExpr ) . value as string ;
265- return provider ;
266- }
267-
268- function getPluginOptions ( plugin : Plugin ) : Record < string , unknown > {
260+ function getPluginOptions ( plugin : Parameters < typeof getPluginProvider > [ 0 ] ) : Record < string , unknown > {
269261 const result : Record < string , unknown > = { } ;
270262 for ( const field of plugin . fields ) {
271263 if ( field . name === 'provider' ) {
@@ -281,72 +273,6 @@ function getPluginOptions(plugin: Plugin): Record<string, unknown> {
281273 return result ;
282274}
283275
284- async function loadPluginModule ( provider : string , basePath : string ) {
285- let moduleSpec = provider ;
286- if ( moduleSpec . startsWith ( '.' ) ) {
287- // relative to schema's path
288- moduleSpec = path . resolve ( basePath , moduleSpec ) ;
289- }
290-
291- const importAsEsm = async ( spec : string ) => {
292- try {
293- const result = ( await import ( spec ) ) . default as CliPlugin ;
294- return result ;
295- } catch ( err ) {
296- throw new CliError ( `Failed to load plugin module from ${ spec } : ${ ( err as Error ) . message } ` ) ;
297- }
298- } ;
299-
300- const jiti = createJiti ( pathToFileURL ( basePath ) . toString ( ) ) ;
301- const importAsTs = async ( spec : string ) => {
302- try {
303- const result = ( await jiti . import ( spec , { default : true } ) ) as CliPlugin ;
304- return result ;
305- } catch ( err ) {
306- throw new CliError ( `Failed to load plugin module from ${ spec } : ${ ( err as Error ) . message } ` ) ;
307- }
308- } ;
309-
310- const esmSuffixes = [ '.js' , '.mjs' ] ;
311- const tsSuffixes = [ '.ts' , '.mts' ] ;
312-
313- if ( fs . existsSync ( moduleSpec ) && fs . statSync ( moduleSpec ) . isFile ( ) ) {
314- // try provider as ESM file
315- if ( esmSuffixes . some ( ( suffix ) => moduleSpec . endsWith ( suffix ) ) ) {
316- return await importAsEsm ( pathToFileURL ( moduleSpec ) . toString ( ) ) ;
317- }
318-
319- // try provider as TS file
320- if ( tsSuffixes . some ( ( suffix ) => moduleSpec . endsWith ( suffix ) ) ) {
321- return await importAsTs ( moduleSpec ) ;
322- }
323- }
324-
325- // try ESM index files in provider directory
326- for ( const suffix of esmSuffixes ) {
327- const indexPath = path . join ( moduleSpec , `index${ suffix } ` ) ;
328- if ( fs . existsSync ( indexPath ) ) {
329- return await importAsEsm ( pathToFileURL ( indexPath ) . toString ( ) ) ;
330- }
331- }
332-
333- // try TS index files in provider directory
334- for ( const suffix of tsSuffixes ) {
335- const indexPath = path . join ( moduleSpec , `index${ suffix } ` ) ;
336- if ( fs . existsSync ( indexPath ) ) {
337- return await importAsTs ( indexPath ) ;
338- }
339- }
340-
341- // last resort, try to import as esm directly
342- try {
343- return ( await import ( moduleSpec ) ) . default as CliPlugin ;
344- } catch {
345- // plugin may not export a generator so we simply ignore the error here
346- return undefined ;
347- }
348- }
349-
350276async function checkForMismatchedPackages ( projectPath : string ) {
351277 const packages = await getZenStackPackages ( projectPath ) ;
352278 if ( ! packages . length ) {
0 commit comments