Skip to content

Commit

Permalink
[CLOSES #227][CLI] Add misk command to spit out multibindings
Browse files Browse the repository at this point in the history
  • Loading branch information
adrw committed May 9, 2019
1 parent 296f5da commit 78233b2
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 29 deletions.
128 changes: 128 additions & 0 deletions packages/@misk/cli/src/commands/misk.ts
@@ -0,0 +1,128 @@
import * as fs from "fs-extra"
import {
logDebug,
handleCommand,
parseArgs,
makePath,
Files,
IMiskTabJSON
} from "../utils"
export const command = "misk"
export const desc = "multibindings to add tab to a Misk Admin Dashboard"
export const handlerFn = async (...args: any) => {
logDebug(command, desc)
const { dir } = parseArgs(...args)
const miskTab: IMiskTabJSON = fs.readJSONSync(makePath(dir, Files.miskTab))
const dashboardTabBinding = `
multibind<DashboardTab, AdminDashboardTab>().toInstance(DashboardTab(
name = "${miskTab.name}",
slug = "${miskTab.slug}",
url_path_prefix = "/_admin/${miskTab.slug}/",
category = "{add menu category to group the tab}",
roles = setOf("optional list", "of authenticated roles",
"who can see the tab")
))
`
const webTabResourceModuleBinding = `
install(WebTabResourceModule(
environment = environment,
slug = "${miskTab.slug}",
web_proxy_url = "http://localhost:${miskTab.port}/"
))
`
const multibindingsMessage = `
How to Add Tab ${miskTab.slug} to your Misk Service
===
Add the following multibindings to a KAbstractModule in a Misk service.
If you have many tabs, it may make sense to create a dedicated
{Service Name}AdminDashboardModule.kt
Note that there are areas in the generated multibindings below that you will
need to fill out or customize to your specific use case.
The generated bindings assume you want to add your tab to the default Misk
Admin Dashboard available at /_admin/ in a Misk service.
Dashboard Tab Binding
===
The Dashboard Tab multibinding adds a menu link for your tab to the Misk Admin
Dashboard.
It is organized by link category and can also carry authentication permissions
so that tabs only show up to appropriately authenticated users.
Dashboard Tab Binding for ${miskTab.slug}:
--------------------------------------------------------------------------------
${dashboardTabBinding}
--------------------------------------------------------------------------------
Web Tab Resource Module Binding
===
The Web Tab Resource Module multibinding tells Misk where to find the compiled
tab code. It lives in two places:
- In Classpath/JAR path ${miskTab.output_path} after running $ miskweb build
- Served from Webpack-Dev-Server over localhost on port ${
miskTab.port
} when running $ miskweb start
Web Tab Resource Module Binding for ${miskTab.slug}:
--------------------------------------------------------------------------------
${webTabResourceModuleBinding}
--------------------------------------------------------------------------------
AdminDashboardTestingModule
===
To run your Misk Admin Dashboard in DEVELOPMENT or TESTING environments, you'll
need to add the AdminDashboardTestingModule to your primary service
applicationModules() function.
It will look something like below.
--------------------------------------------------------------------------------
DinoService.kt
--------------------------------------------------------------------------------
fun main(args: Array<String>) {
ServiceBuilder.getMiskApplication(::applicationModules).run(args)
}
fun applicationModules(serviceBuilder: ServiceBuilder<DinoConfig>):
List<KAbstractModule> {
val modules = mutableListOf(
ConfigModule.create("dino", serviceBuilder.config),
DinoAccessModule(),
DinoActionModule(),
DinoAdminDashboardModule(serviceBuilder.env)
DinoPersistenceModule(serviceBuilder.config),
EnvironmentModule(serviceBuilder.env)
)
when (serviceBuilder.env) {
Environment.PRODUCTION, Environment.STAGING -> {
modules.addAll(listOf(
JurassicRealModule(serviceBuilder.env,
serviceBuilder.config.jurassic)
))
}
Environment.DEVELOPMENT, Environment.TESTING -> {
modules.addAll(listOf(
JurassicTestingModule(serviceBuilder.env,
serviceBuilder.config.jurassic),
AdminDashboardTestingModule(serviceBuilder.env)
))
}
}
return modules
}
--------------------------------------------------------------------------------
Done!
Boot your service and your tab will be at:
http://localhost:8080/_admin/${miskTab.slug}/
`
console.log(multibindingsMessage)
}
export const handler = async (yargs: any) =>
handleCommand(yargs, handlerFn, ["e", "each"])
41 changes: 27 additions & 14 deletions packages/@misk/cli/src/utils/index.ts
Expand Up @@ -10,20 +10,20 @@ export * from "./generate"
export * from "./migrate"

export interface IMiskTabJSON {
name: string
output_path: string
port: number
rawGitginore: string
rawPackageJson: any
rawTsconfig: any
rawTslint: any
rawWebpackConfig: any
relative_path_prefix: string
slug: string
useWebpackExternals: boolean
version: MiskVersion
zipOnBuild: boolean
___DeprecatedKeys: string
name: string // name of tab in Title Case
output_path: string // output path for Webpack build
port: number // port for Webpack-Dev-Server
rawGitginore: string // prebuild permanent add to .gitignore file
rawPackageJson: any // prebuild permanent add/override to package.json file
rawTsconfig: any // prebuild permanent add/override to tsconfig.json file
rawTslint: any // prebuild permanent add/override to tslint.json file
rawWebpackConfig: any // prebuild permanent add to webpack.config.js file
relative_path_prefix: string // override default URL for tab: /_tab/{slug}/
slug: string // unique slug used in URL path
useWebpackExternals: boolean // turn off/on thin build by including externals in Webpack build
version: MiskVersion // Misk Web release version or keyword (alpha, latest)
zipOnBuild: boolean // zip relevant source code of tab into {slug}.tgz after each build
___DeprecatedKeys: string // divider to the miskTab.json to improve maintainability
}

export const defaultMiskTabJson: IMiskTabJSON = {
Expand Down Expand Up @@ -105,6 +105,19 @@ export const execute = (cmd: string, ...args: any) => {
}
}

export const generateMiskTabJson = (dir: string, fieldsToSet?: any) => {
const miskTab = fs.readJSONSync(makePath(dir, Files.miskTab))
fs.writeJsonSync(
makePath(dir, Files.miskTab),
{
...defaultMiskTabJson,
...miskTab,
...fieldsToSet
},
JsonOptions
)
}

export const npmRunScript = (cmd: string, prebuild: boolean = false) =>
`${prebuild ? "miskweb prebuild && " : ""}npm run-script ${cmd}`

Expand Down
20 changes: 5 additions & 15 deletions packages/@misk/cli/src/utils/migrate.ts
Expand Up @@ -12,7 +12,7 @@ import {
remove,
makePath,
parseArgs,
defaultMiskTabJson
generateMiskTabJson
} from "../utils"
import { MiskVersion } from "./changelog"

Expand All @@ -24,19 +24,6 @@ const moveOldBuildFile = async (dir: string, filename: Files) => {
}
}

export const generateMiskTabJson = (dir: string, fieldsToSet?: any) => {
const miskTab = fs.readJSONSync(makePath(dir, Files.miskTab))
fs.writeJsonSync(
makePath(dir, Files.miskTab),
{
...defaultMiskTabJson,
...miskTab,
...fieldsToSet
},
JsonOptions
)
}

export const migrateBuildFiles = (...args: any) => {
const { dir } = parseArgs(...args)
logDebug(tag, "", dir)
Expand Down Expand Up @@ -90,7 +77,10 @@ export const migrateBuildFiles = (...args: any) => {
// move all build files to an .old-build-files folder
logDebug(tag, `Stashing old build files in ${makePath(dir, Files.old)}`)
fs.mkdirp(makePath(dir, Files.old))
fs.copy(makePath(dir, Files.package), makePath(dir, Files.old, Files.package))
fs.copy(
makePath(dir, Files.package),
makePath(dir, Files.old, Files.package)
)
moveOldBuildFile(dir, Files.gitignore)
moveOldBuildFile(dir, Files.prettier)
moveOldBuildFile(dir, Files.tsconfig)
Expand Down

0 comments on commit 78233b2

Please sign in to comment.