Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

load all plugins from ts #311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"klaw": "^2.1.1",
"log-chopper": "^1.0.2",
"moment": "^2.20.1",
"read-pkg": "^3.0.0",
"rwlockfile": "^2.0.17",
"semver": "5.4.1",
"string-similarity": "^1.2.0",
Expand All @@ -38,8 +39,8 @@
"devDependencies": {
"@cli-engine/command": "^12.1.0",
"@cli-engine/config": "^5.1.0",
"@cli-engine/util": "^1.0.13",
"@heroku-cli/command": "^7.0.12",
"@cli-engine/util": "^1.1.2",
"@heroku-cli/command": "^7.0.13",
"@heroku-cli/tslint": "^1.1.2",
"@types/ansi-styles": "^2.0.30",
"@types/chalk": "^2.2.0",
Expand All @@ -64,6 +65,7 @@
"tar-fs": "^1.16.0",
"ts-jest": "^22.0.1",
"ts-node": "4.1.0",
"tsconfig-paths": "^2.7.3",
"tslint": "^5.8.0",
"typescript": "2.6.2"
},
Expand Down
2 changes: 2 additions & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HTTP } from 'http-call'
import * as klaw from 'klaw'
import * as moment from 'moment'
import semver = require('semver')
// import readPkg = require('read-pkg')

import command = require('./command')
import help = require('./commands/help')
Expand All @@ -29,6 +30,7 @@ export default {
get assync(): typeof assync.default { return fetch('assync').default },
get filesize(): any { return fetch('filesize') },
get globby(): typeof globby { return fetch('globby') },
get readPkg(): any { return fetch('read-pkg') },

// local
get Help(): typeof help.default { return fetch('./commands/help').default },
Expand Down
7 changes: 1 addition & 6 deletions src/plugins/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import { Plugin } from './plugin'
export class Builtin extends Plugin {
constructor(config: Config) {
const root = path.join(__dirname, '../..')
const pjson = require(path.join(root, 'package.json'))
super({ config, type: 'builtin', root, pjson })
}

public get commandsDir() {
return path.join(__dirname, '..', 'commands')
super({ config, type: 'builtin', root })
}

protected async commandIDsFromDir(): Promise<string[]> {
Expand Down
9 changes: 3 additions & 6 deletions src/plugins/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Config from '../config'
import deps from '../deps'

import { PluginManifest } from './manifest'
import { IPluginOptions, IPluginPJSON, Plugin, PluginType } from './plugin'
import { IPluginOptions, Plugin, PluginType } from './plugin'

function touch(f: string) {
fs.utimesSync(f, new Date(), new Date())
Expand All @@ -18,10 +18,6 @@ function pjsonPath(root: string) {
return path.join(root, 'package.json')
}

function linkPJSON(root: string): Promise<IPluginPJSON> {
return deps.file.readJSON(pjsonPath(root))
}

export interface IManifestPlugin {
name: string
root: string
Expand Down Expand Up @@ -186,10 +182,11 @@ export class LinkPlugins {

private async loadPlugin(root: string, refresh = false) {
if (!await deps.file.exists(root)) return
const pkg = await deps.readPkg(root)
let p = new LinkPlugin({
config: this.config,
root,
pjson: await linkPJSON(root),
pjson: pkg.pkg,
type: 'link',
})
await p.refresh(refresh)
Expand Down
55 changes: 1 addition & 54 deletions src/plugins/main.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,10 @@
import * as path from 'path'

import { ILoadResult } from '../command'
import Config from '../config'
import deps from '../deps'

import { Plugin } from './plugin'

interface TSConfig {
compilerOptions: {
rootDir?: string
outDir?: string
}
}

export class MainPlugin extends Plugin {
protected skipCache: boolean
private tsconfig?: TSConfig
private _commandsDir: string

constructor(config: Config) {
const root = config.root!
const pjson = require(`${root}/package.json`)
super({ config, type: 'main', root, pjson })
}

public async load(): Promise<ILoadResult> {
if (this.result) return this.result
this.tsconfig = await this.fetchTSConfig()
this._commandsDir = this.config.commandsDir!
if (this.tsconfig) {
this.debug('tsconfig.json found, skipping cache for main commands')
this.skipCache = true
let { rootDir, outDir } = this.tsconfig.compilerOptions
if (rootDir && outDir) {
try {
this.debug('using ts files for main commands')
require('ts-node/register')
const lib = path.join(this.config.root!, outDir)
const src = path.join(this.config.root!, rootDir)
const relative = path.relative(lib, this.config.commandsDir!)
this._commandsDir = path.join(src, relative)
} catch (err) {
this.debug(err)
}
}
}
return super.load()
}

protected get commandsDir() {
return this._commandsDir
}

private async fetchTSConfig(): Promise<TSConfig | undefined> {
try {
const tsconfig = await deps.file.readJSON(path.join(this.root, 'tsconfig.json'))
return tsconfig.compilerOptions && tsconfig
} catch (err) {
if (err.code !== 'ENOENT') throw err
}
super({ config, type: 'main', root })
}
}
53 changes: 43 additions & 10 deletions src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,37 @@ export interface IPluginModule {
export interface IPluginOptions {
config: Config
root: string
pjson: IPluginPJSON
pjson?: IPluginPJSON
type: PluginType
}

interface TSConfig {
compilerOptions: {
rootDir?: string
outDir?: string
}
}

export abstract class Plugin implements ICommandManager {
public type: PluginType
public name: string
public version: string
public tag?: string
public pjson: IPluginPJSON
public root: string
public commandsDir?: string
protected config: Config
protected debug: any
protected lock: RWLockfile
protected result: ILoadResult
protected skipCache?: boolean
private _module: Promise<IPluginModule>
private cache: PluginManifest
private tsconfig?: TSConfig

constructor(opts: IPluginOptions) {
this.config = opts.config
this.root = opts.root
this.pjson = opts.pjson
this.pjson = opts.pjson || deps.readPkg.sync(this.root)
if (!this.pjson['cli-engine']) this.pjson['cli-engine'] = {}
this.name = this.name || this.pjson.name
this.version = this.version || this.pjson.version
Expand All @@ -76,6 +84,8 @@ export abstract class Plugin implements ICommandManager {
@rwlockfile('lock', 'read')
public async load(): Promise<ILoadResult> {
if (this.result) return this.result
this.tsconfig = await this.fetchTSConfig()
this.commandsDir = this.fetchCommandsDir()
this.result = {
commands: await this.commands(),
topics: await this.topics(),
Expand Down Expand Up @@ -114,7 +124,7 @@ export abstract class Plugin implements ICommandManager {
}

protected async commands(): Promise<ICommandInfo[]> {
const cache: ICommandInfo[] = await this.cacheFetch('commands', async () => {
const cache: ICommandInfo[] = await this.cache.fetch('commands', async () => {
this.debug('fetching commands')
const commands = await deps
.assync<any>([this.commandsFromModule(), this.commandsFromDir()])
Expand Down Expand Up @@ -148,7 +158,7 @@ export abstract class Plugin implements ICommandManager {
}

protected async topics(): Promise<ITopic[]> {
const cache: ITopic[] = await this.cacheFetch('topics', async () => {
const cache: ITopic[] = await this.cache.fetch('topics', async () => {
this.debug('fetching topics')
const m = await this.fetchModule()
if (!m) return []
Expand All @@ -159,9 +169,27 @@ export abstract class Plugin implements ICommandManager {
return cache
}

protected get commandsDir(): string | undefined {
let d = this.pjson['cli-engine'].commands
if (d) return path.join(this.root, d)
protected fetchCommandsDir(): string | undefined {
let commandsDir = this.pjson['cli-engine'].commands
if (!commandsDir) return
commandsDir = path.join(this.root, commandsDir)
if (this.tsconfig) {
this.debug('tsconfig.json found, skipping cache for main commands')
let { rootDir, outDir } = this.tsconfig.compilerOptions
if (rootDir && outDir) {
try {
this.debug('using ts files')
require('ts-node').register()
const lib = path.join(this.root, outDir)
const src = path.join(this.root, rootDir)
const relative = path.relative(lib, commandsDir)
commandsDir = path.join(src, relative)
} catch (err) {
this.debug(err)
}
}
}
return commandsDir
}

protected async commandIDsFromDir(): Promise<string[]> {
Expand Down Expand Up @@ -256,8 +284,13 @@ export abstract class Plugin implements ICommandManager {
})())
}

private cacheFetch<T>(key: string, fn: () => Promise<T>) {
return this.skipCache ? fn() : this.cache.fetch(key, fn)
private async fetchTSConfig(): Promise<TSConfig | undefined> {
try {
const tsconfig = await deps.file.readJSON(path.join(this.root, 'tsconfig.json'))
return tsconfig.compilerOptions && tsconfig
} catch (err) {
if (err.code !== 'ENOENT') throw err
}
}

private convertConfig(config: Config): Partial<Config> {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./lib",
"rootDir": "./src",
"rootDirs": ["./src"],
"strict": true,
"target": "es2016"
},
Expand Down
Loading