-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* chore: extension pattern * chore: RUNNER_THEME_CSS extension hook * chore: ext add and ext rm * chore: some doc on extenstions
- Loading branch information
1 parent
a9cecb0
commit 22f8591
Showing
8 changed files
with
221 additions
and
2 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
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,17 @@ | ||
## Known Extensions | ||
|
||
> Please make a pull request to add your extension to this list | ||
- [mterm-ext-red](#mterm-ext-red) | ||
- more... | ||
|
||
|
||
### mterm-ext-red | ||
|
||
```bash | ||
ext add mterm-red | ||
``` | ||
Make your terminal (mterm) red 10 seconds - | ||
|
||
![red terminal](https://github.com/mterm-io/mterm-ext-red/blob/HEAD/info.png?raw=true) | ||
|
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Workspace } from './workspace' | ||
import { join } from 'path' | ||
import { pathExists, readJson } from 'fs-extra' | ||
import { log } from '../logger' | ||
|
||
export enum ExtensionHook { | ||
RUNNER_THEME_CSS = 'RUNNER_THEME_CSS' | ||
} | ||
|
||
export type ExtensionHookCallback = (workspace?: Workspace) => string | ||
export type ExtensionHookResolution = string | ExtensionHookCallback | ||
export class Extensions { | ||
public extensionHooks: Map<ExtensionHook, Array<ExtensionHookResolution>> = new Map< | ||
ExtensionHook, | ||
Array<ExtensionHookResolution> | ||
>() | ||
public extensionList: string[] = [] | ||
constructor(private workspace: Workspace) {} | ||
|
||
async run(hook: ExtensionHook): Promise<string> { | ||
const resolutions = this.extensionHooks.get(hook) || [] | ||
|
||
let result = '' | ||
for (const resolution of resolutions) { | ||
if (typeof resolution === 'string') { | ||
result += resolution | ||
} else { | ||
result += resolution(this.workspace) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
async load(): Promise<void> { | ||
this.extensionList = [] | ||
this.extensionHooks.clear() | ||
|
||
const start = Date.now() | ||
const packageJson = join(this.workspace.folder, 'package.json') | ||
const isPackageJsonExists = await pathExists(packageJson) | ||
if (!isPackageJsonExists) { | ||
log('No package.json found in workspace') | ||
return | ||
} | ||
const packageJsonData = await readJson(packageJson) | ||
const packages: string[] = [] | ||
|
||
if (packageJsonData.dependencies) { | ||
packages.push(...Object.keys(packageJsonData.dependencies)) | ||
} | ||
if (packageJsonData.devDependencies) { | ||
packages.push(...Object.keys(packageJsonData.devDependencies)) | ||
} | ||
|
||
const folder = this.workspace.folder | ||
const ext = this.extensionHooks | ||
const list = this.extensionList | ||
async function scan(packageName: string): Promise<void> { | ||
log(`Scanning package: ${packageName}..`) | ||
|
||
const mtermExtPath = join(folder, 'node_modules', packageName, 'mterm.js') | ||
|
||
const isMtermExtensionExists = await pathExists(mtermExtPath) | ||
if (!isMtermExtensionExists) { | ||
return | ||
} | ||
|
||
log(`Loading package: ${packageName}`) | ||
const mtermExt = require(mtermExtPath) | ||
|
||
const hooks = Object.keys(ExtensionHook) | ||
|
||
log(`Mapping hooks for ${packageName} = ${hooks}`) | ||
|
||
list.push(packageName) | ||
|
||
for (const hook of hooks) { | ||
const hookKey = hook as ExtensionHook | ||
let resolutions: ExtensionHookResolution[] = [] | ||
if (ext.has(ExtensionHook[hookKey])) { | ||
resolutions = ext.get(ExtensionHook[hookKey]) as ExtensionHookResolution[] | ||
} | ||
|
||
resolutions.push(mtermExt[hookKey]) | ||
|
||
ext.set(hookKey, resolutions) | ||
} | ||
} | ||
|
||
const promiseList = packages.map(scan) | ||
|
||
await Promise.all(promiseList) | ||
|
||
log('Extensions loaded in ' + (Date.now() - start) + 'ms') | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ExecuteContext } from '../execute-context' | ||
|
||
async function ext(context: ExecuteContext, task?: string): Promise<void> { | ||
if (!task) { | ||
context.out( | ||
`ext list: | ||
${context.workspace.extensions.extensionList.length > 0 ? '-' : ''}` + | ||
context.workspace.extensions.extensionList.join('\n-') | ||
) | ||
context.finish(0) | ||
return | ||
} | ||
if (task === 'load') { | ||
await context.workspace.extensions.load() | ||
context.out('Extensions loaded\n') | ||
context.finish(0) | ||
return | ||
} | ||
|
||
if (task === 'add') { | ||
const extName = (context.prompt.args[1] || '').trim() | ||
if (!extName) { | ||
context.out('No extension name provided\n\n:ext add EXT_NAME', true) | ||
context.finish(1) | ||
return | ||
} | ||
|
||
context.out(`Installing ${extName}..\n`) | ||
|
||
await context.runTask(process.env, context.workspace.folder, `npm install ${extName}`) | ||
|
||
await context.workspace.extensions.load() | ||
|
||
context.out('Done\n') | ||
} else if (task === 'remove' || task === 'rm' || task === 'delete') { | ||
const extName = (context.prompt.args[1] || '').trim() | ||
if (!extName) { | ||
context.out('No extension name provided\n\n:ext rm EXT_NAME', true) | ||
context.finish(1) | ||
return | ||
} | ||
|
||
context.out(`Removing ${extName}..\n`) | ||
|
||
await context.runTask(process.env, context.workspace.folder, `npm rm ${extName}`) | ||
|
||
await context.workspace.extensions.load() | ||
|
||
context.out('Done\n') | ||
} else { | ||
context.out( | ||
'Unknown command\n\nTry :ext {add,remove} EXT_NAME or :ext to list current extensions', | ||
true | ||
) | ||
context.finish(1) | ||
return | ||
} | ||
} | ||
|
||
export default { | ||
command: ':ext', | ||
alias: [':extension', 'ext', 'extension'], | ||
task: ext | ||
} |
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