@@ -12,28 +12,31 @@ const PLUGIN_CACHE_DIR = '/Library/Caches/codify/plugins'
1212
1313export class PluginResolver {
1414
15- static async resolve ( name : string , version : string ) : Promise < Plugin > {
16- await PluginResolver . checkAndCreateCacheDirIfNotExists ( )
15+ static async getOrDownload ( name : string , versionOrPath ?: string ) : Promise < Plugin > {
16+ await PluginResolver . createPluginDirIfNotExists ( )
17+
18+ // Default versions to latest
19+ versionOrPath = versionOrPath ?? 'latest' ;
1720
1821 let directoryStat ;
1922 try {
20- directoryStat = await fs . stat ( version ) ;
23+ directoryStat = await fs . stat ( versionOrPath ) ;
2124 } catch {
2225 }
2326
2427 // For easier development. A direct js file can be specified for the plugin.
2528 if ( directoryStat && directoryStat . isFile ( ) ) {
26- return PluginResolver . resolvePluginFs ( name , version )
29+ return PluginResolver . getFromFileSystem ( name , versionOrPath )
2730 }
2831
2932 if ( name === 'default' ) {
30- return PluginResolver . resolvePluginDefault ( name , version )
33+ return PluginResolver . downloadDefaultPlugin ( name , versionOrPath )
3134 }
3235
3336 throw new Error ( 'Non-default plugins are not currently supported' ) ;
3437 }
3538
36- static async resolveExisting ( exclude : string [ ] ) : Promise < Plugin [ ] > {
39+ static async getAllExisting ( exclude : string [ ] ) : Promise < Plugin [ ] > {
3740 let files ;
3841 try {
3942 files = await fs . readdir ( PLUGIN_CACHE_DIR ) ;
@@ -57,7 +60,7 @@ export class PluginResolver {
5760 }
5861 }
5962
60- private static async resolvePluginFs ( name : string , filePath : string ) : Promise < Plugin > {
63+ private static async getFromFileSystem ( name : string , filePath : string ) : Promise < Plugin > {
6164 const fileExtension = filePath . slice ( filePath . lastIndexOf ( '.' ) )
6265 if ( fileExtension !== '.js' && fileExtension !== '.ts' ) {
6366 throw new Error ( `Only .js and .ts plugins are support currently. Can't resolve ${ filePath } ` ) ;
@@ -70,7 +73,7 @@ export class PluginResolver {
7073 )
7174 }
7275
73- private static async resolvePluginDefault ( name : string , version : string ) : Promise < Plugin > {
76+ private static async downloadDefaultPlugin ( name : string , version : string ) : Promise < Plugin > {
7477 const { body } = await fetch ( DEFAULT_PLUGIN_URL )
7578 if ( ! body ) {
7679 throw new Error ( 'Un-able to fetch the default plugin (body not found). Exiting' ) ;
@@ -89,7 +92,7 @@ export class PluginResolver {
8992 )
9093 }
9194
92- private static async checkAndCreateCacheDirIfNotExists ( ) {
95+ private static async createPluginDirIfNotExists ( ) {
9396 let pluginDirStat = null ;
9497 try {
9598 pluginDirStat = await fs . stat ( PluginResolver . getCacheDir ( ) )
0 commit comments