-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add support for export plugins in cli
- Loading branch information
Showing
5 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Debug from 'debug'; | ||
import path from 'path'; | ||
import { ExportPluginCtor, ExportPluginModule } from '../lib/types'; | ||
import { Utils } from '../lib/utils'; | ||
|
||
const info = Debug('sol-merger:info'); | ||
const error = Debug('sol-merger:error'); | ||
|
||
export class PluginLoadError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} | ||
|
||
export class PluginsLoader { | ||
#pluginPaths: string[]; | ||
#npmRoot: string; | ||
|
||
constructor(pluginPaths: string[], npmRoot: string) { | ||
this.#pluginPaths = pluginPaths; | ||
this.#npmRoot = npmRoot; | ||
} | ||
|
||
async getPlugins(): Promise<ExportPluginCtor[]> { | ||
const result: ExportPluginCtor[] = []; | ||
for (const pluginPath of this.#pluginPaths) { | ||
const fullPluginPath = this.getPluginPath(pluginPath); | ||
if (!fullPluginPath) { | ||
continue; | ||
} | ||
try { | ||
const plugin = await this.tryLoadPlugin(fullPluginPath); | ||
result.push(plugin); | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
error(e.message); | ||
} else { | ||
error(e); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private getPluginPath(pluginPath: string): string | null { | ||
if (!this.#npmRoot && !Utils.isRelative(pluginPath)) { | ||
info( | ||
`[PluginsLoader] SKIP: Unable to load plugin '${pluginPath}' because npmRoot is not found.`, | ||
); | ||
return null; | ||
} | ||
|
||
if (Utils.isRelative(pluginPath)) { | ||
return path.join(process.cwd(), pluginPath); | ||
} | ||
return path.join(this.#npmRoot, pluginPath); | ||
} | ||
|
||
private async tryLoadPlugin(modulePath: string): Promise<ExportPluginCtor> { | ||
const resolvedPath = path.resolve(modulePath); | ||
const pluginModule: Partial<ExportPluginModule> = await import( | ||
resolvedPath | ||
).catch((e) => { | ||
throw new PluginLoadError(`Can not import plugin ${resolvedPath}`); | ||
}); | ||
if (pluginModule.ExportPlugin) { | ||
return pluginModule.ExportPlugin; | ||
} | ||
throw new PluginLoadError( | ||
`Plugin ${resolvedPath} does not have exported member ExportPlugin`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
import { exec } from 'child_process'; | ||
import Debug from 'debug'; | ||
import path from 'path'; | ||
|
||
const error = Debug('sol-merger:error'); | ||
|
||
export class Utils { | ||
static isRelative(file: string) { | ||
return file.startsWith('.'); | ||
} | ||
|
||
static async getNodeModulesPath(file: string): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
exec('npm root', { cwd: path.dirname(file) }, (err, stdout) => { | ||
if (err) { | ||
error( | ||
'Unable to find npm root directory. Make sure contract is inside npm package.', | ||
); | ||
return reject(err); | ||
} | ||
resolve(stdout.trim()); | ||
}); | ||
}); | ||
} | ||
} |