Skip to content

Commit

Permalink
fix: 调整项目结构
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Jan 28, 2021
1 parent 488a15a commit b209824
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 60 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,3 @@ dist

# TernJS port file
.tern-port

# dist
packages/**/lib/
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
{
"name": "@xus/cli-shared-utils",
"name": "@xus/cli-plugin-command-help",
"version": "0.0.1",
"description": "xus cli-shared-utils",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"description": "xus cli plugin command-help",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"keywords": [
"cli-shared-utils"
"cli",
"rollup"
],
"author": "guo.xu <xuguo@outlook.it>",
"homepage": "https://github.com/xus-code/bundle-tools/tree/master/packages/cli#readme",
"license": "MIT",
"files": [
"lib"
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/xus-code/bundle-tools.git"
},
"scripts": {
"dev:utils": "tsc --watch",
"build:utils": "tsc"
"dev:chelp": "tsc --watch",
"build:chelp": "tsc"
},
"bugs": {
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"joi": "^17.3.0"
"@xus/cli": "^0.0.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Commands, Command } from './../../types'
import { IPluginAPI } from '../../PluginAPI'
import { info } from '@xus/cli-shared-utils'
import type { Command, Commands, IPluginAPI, Args } from '@xus/cli'
import { info } from '@xus/cli'
import chalk from 'chalk'

export default function (api: IPluginAPI): void {
api.registerCommand('help', (args) => {
api.registerCommand('help', (args: Args) => {
console.log(`cmd `, args)
const commandName = args._[0]
if (!commandName) {
// log all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"outDir": "./lib",
"outDir": "./dist"
},
"extends": "../../tsconfig.json",
"include": ["./src/**/*"]
}
}
3 changes: 3 additions & 0 deletions packages/cli/bin/xus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist/xus')
16 changes: 10 additions & 6 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
"cli",
"bundle"
],
"author": "guo.xu <xuguo@outlook.it>",
"homepage": "https://github.com/xus-code/bundle-tools/tree/master/packages/cli#readme",
"license": "MIT",
"bin": {
"xus-cli": "./lib/index.js"
"xus": "bin/xus.js"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"lib"
"dist",
"bin"
],
"author": "guo.xu <xuguo@outlook.it>",
"homepage": "https://github.com/xus-code/bundle-tools/tree/master/packages/cli#readme",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/xus-code/bundle-tools.git"
Expand All @@ -27,8 +30,9 @@
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"@xus/cli-shared-utils": "^0.0.1",
"@xus/cli-plugin-command-help": "^0.0.1",
"chalk": "^4.1.0",
"joi": "^17.3.0",
"minimist": "^1.2.5"
},
"devDependencies": {
Expand Down
35 changes: 28 additions & 7 deletions packages/cli/src/Cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// types
import { Commands, Args, RawArgs, Plugin } from './types'
import { error } from '@xus/cli-shared-utils'
import { Commands, Args, RawArgs, Plugin, PluginApply } from './types'
import { error, loadModule } from './utils'
import ConfigManager, { IConfigManager } from './manager/ConfigManager'
import PathManager, { IPathManager } from './manager/PathManager'
import EnvManager, { IEnvManager } from './manager/EnvManager'
import PluginAPI from './PluginAPI'
import { BuiltInPlugins } from './builtInPlugins'

class Cli {
private initialized = false
Expand All @@ -20,14 +19,14 @@ class Cli {
this.PathManager = new PathManager(context)
this.EnvManager = new EnvManager(this.PathManager)
this.ConfigManager = new ConfigManager(this.PathManager)

this.plugins = this.resolvePlugins()
}

// 启动 cli
async setupCli(): Promise<void> {
if (this.initialized) return
this.initialized = true
// load plugin
this.plugins = await this.resolvePlugins()
// 1. load config
await this.ConfigManager.loadUserConfig()
// 2. apply plugins
Expand All @@ -37,9 +36,31 @@ class Cli {
})
}

resolvePlugins(): Plugin[] {
async resolvePlugins(): Promise<Plugin[]> {
async function toPlugin(pluginPkgName: string) {
const [err, apply] = await loadModule<PluginApply>(pluginPkgName)
const id = pluginPkgName.replace(/^@xus\/cli-plugin-/, 'built-in:')
if (err) {
error(`
plugin [${id}] load error
`)
return null
}
return {
id,
apply
}
}
const builtInPlugins = []
const builtInMap = ['@xus/cli-plugin-command-help']
for (const pkgName in builtInMap) {
const plugin = await toPlugin(pkgName)
if (plugin) {
builtInPlugins.push(plugin)
}
}
// TODO: user install plugin
return BuiltInPlugins
return builtInPlugins
}

async run(commandName: string, args: Args, rawArgs: RawArgs): Promise<any> {
Expand Down
8 changes: 0 additions & 8 deletions packages/cli/src/builtInPlugins/index.ts

This file was deleted.

18 changes: 3 additions & 15 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
#!/usr/bin/env node
import minimist from 'minimist'
import Cli from './Cli'
import { EnvEnum } from './utils/constants'

// 1. init cli
const cli = new Cli(process.env[EnvEnum.context] || process.cwd())
// 2. get args
const rawArgs = process.argv.slice(2)
const args = minimist(rawArgs)
const commandName = args._[0]
// 3. run commander
cli.run(commandName, args, rawArgs).catch(() => {
process.exit(1)
})
export * from './types'
export { IPluginAPI } from './PluginAPI'
export * from './utils'
2 changes: 1 addition & 1 deletion packages/cli/src/manager/ConfigManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProjectConfig } from '../types'
import { loadModule } from '@xus/cli-shared-utils'
import { loadModule } from '../utils'
import { IPathManager } from './PathManager'

class ConfigManager {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/manager/EnvManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEnvName } from '@xus/cli-shared-utils'
import { createEnvName } from '../utils'
import { IPathManager } from './PathManager'

class EnvManager {
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/types/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { ProjectConfig } from './config'
import { Args, RawArgs } from './args'
import { IPluginAPI } from '../PluginAPI'

export type PluginApply = (api: IPluginAPI, projectOps?: ProjectConfig) => void
export interface Plugin {
id: string
apply: (api: IPluginAPI, projectOps: ProjectConfig) => void
apply: PluginApply
}

export type CommandFn = (args: Args, rawArgs?: RawArgs) => any | Promise<any>
export type CommandFn = (args: Args, rawArgs: RawArgs) => any | Promise<any>
export interface CommandOps {
desc: string
usage: string
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './logger'
export * from './module'
export * from './validator'
export * from './env'
export * from './constants'
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions packages/cli/src/xus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// export types
import minimist from 'minimist'
import Cli from './Cli'
import { EnvEnum } from './utils/constants'
// 1. init cli
const cli = new Cli(process.env[EnvEnum.context] || process.cwd())
// 2. get args
const rawArgs = process.argv.slice(2)
const args = minimist(rawArgs)
const commandName = args._[0]
// 3. run commander
cli.run(commandName, args, rawArgs).catch(() => {
process.exit(1)
})
4 changes: 2 additions & 2 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"outDir": "./lib",
"outDir": "./dist"
},
"extends": "../../tsconfig.json",
"include": ["./src/**/*"]
}
}

0 comments on commit b209824

Please sign in to comment.