Skip to content

Commit

Permalink
refactor: merge autoreload command into dev command
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Apr 25, 2018
1 parent bde56fa commit 3c78c36
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 244 deletions.
60 changes: 0 additions & 60 deletions src/commands/auto-reload.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class DeployCommand extends Command<typeof deployArgs, typeof deployOpts>

const modules = Array.from(new Set(values(services).map(s => s.module)))

const result = ctx.processModules(modules, watch, async (module) => {
const result = await ctx.processModules(modules, watch, async (module) => {
const servicesToDeploy = values(await module.getServices()).filter(s => !!services[s.name])
for (const service of servicesToDeploy) {
await ctx.addTask(new DeployTask(ctx, service, force, forceBuild))
Expand Down
52 changes: 40 additions & 12 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
*/

import { PluginContext } from "../plugin-context"
import { BuildTask } from "../tasks/build"
import { Task } from "../types/task"
import { Command } from "./base"
import { join } from "path"
import { STATIC_DIR } from "../constants"
import { spawnSync } from "child_process"
import chalk from "chalk"
import { sleep } from "../util"
import Bluebird = require("bluebird")
import { values } from "lodash"
import moment = require("moment")

const imgcatPath = join(__dirname, "..", "..", "bin", "imgcat")
const imgcatPath = join(STATIC_DIR, "imgcat")
const bannerPath = join(STATIC_DIR, "garden-banner-1-half.png")

export class DevCommand extends Command {
Expand All @@ -26,24 +30,48 @@ export class DevCommand extends Command {
spawnSync(imgcatPath, [bannerPath], {
stdio: "inherit",
})
console.log()
} catch (_) {
// the above fails for terminals other than iTerm2. just ignore the error and move on.
}

// console.log(chalk.bold(` garden - dev\n`))
console.log(chalk.gray.italic(` Good afternoon, Jon! Let's get your environment wired up...\n`))
ctx.log.info(chalk.gray.italic(`\nGood ${getGreetingTime()}! Let's get your environment wired up...\n`))

await ctx.configureEnvironment()
await ctx.deployServices({})

ctx.log.info({ msg: "" })
const watchEntry = ctx.log.info({ emoji: "koala", msg: `Waiting for code changes...` })
const modules = values(await ctx.getModules())

while (true) {
// TODO: actually do stuff
await sleep(10000)
watchEntry.setState({ emoji: "koala", msg: `Waiting for code changes...` })
if (modules.length === 0) {
if (modules.length === 0) {
ctx.log.info({ msg: "No modules found in project." })
}
ctx.log.info({ msg: "Aborting..." })
return
}

await ctx.processModules(modules, true, async (module) => {
const testTasks: Task[] = await module.getTestTasks({})
const deployTasks = await module.getDeployTasks({})
const tasks = testTasks.concat(deployTasks)

if (tasks.length === 0) {
await ctx.addTask(new BuildTask(ctx, module, false))
} else {
await Bluebird.map(tasks, ctx.addTask)
}
})
}
}

function getGreetingTime() {
const m = moment()

const currentHour = parseFloat(m.format("HH"))

if ( currentHour >= 17 ) {
return "evening"
} else if ( currentHour >= 12 ) {
return "afternoon"
} else {
return "morning"
}
}
20 changes: 8 additions & 12 deletions src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import { PluginContext } from "../plugin-context"
import { BooleanParameter, Command, ParameterValues, StringParameter } from "./base"
import { values } from "lodash"
import { TestTask } from "../tasks/test"
import chalk from "chalk"
import { TaskResults } from "../task-graph"
import Bluebird = require("bluebird")

export const testArgs = {
module: new StringParameter({
Expand Down Expand Up @@ -51,23 +51,19 @@ export class TestCommand extends Command<typeof testArgs, typeof testOpts> {

await ctx.configureEnvironment()

const results = await ctx.processModules(modules, opts.watch, async (module) => {
const config = await module.getConfig()
const group = opts.group
const force = opts.force
const forceBuild = opts["force-build"]

for (const testName of Object.keys(config.test)) {
if (opts.group && testName !== opts.group) {
continue
}
const testSpec = config.test[testName]
const task = new TestTask(ctx, module, testName, testSpec, opts.force, opts["force-build"])
await ctx.addTask(task)
}
const results = await ctx.processModules(modules, opts.watch, async (module) => {
const tasks = await module.getTestTasks({ group, force, forceBuild })
await Bluebird.map(tasks, ctx.addTask)
})

const failed = values(results).filter(r => !!r.error).length

if (failed) {
ctx.log.error({ emoji: "warning", msg: `${failed} tests runs failed! See log output above.\n` })
ctx.log.error({ emoji: "warning", msg: `${failed} test runs failed! See log output above.\n` })
} else {
ctx.log.info("")
ctx.log.info({ emoji: "heavy_check_mark", msg: chalk.green(` All tests passing!\n`) })
Expand Down
38 changes: 37 additions & 1 deletion src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
import * as Joi from "joi"
import { ServiceMap } from "../garden"
import { PluginContext } from "../plugin-context"
import { DeployTask } from "../tasks/deploy"
import { TestTask } from "../tasks/test"
import { identifierRegex, joiIdentifier, joiVariables, PrimitiveMap } from "./common"
import { ConfigurationError } from "../exceptions"
import Bluebird = require("bluebird")
import {
extend,
keys,
set,
values,
} from "lodash"
import { ServiceConfig } from "./service"
import {
Service,
ServiceConfig,
} from "./service"
import { resolveTemplateStrings, TemplateStringContext } from "../template-string"
import { Memoize } from "typescript-memoize"
import { TreeVersion } from "../vcs/base"
Expand Down Expand Up @@ -68,6 +74,10 @@ export interface ModuleConfig<T extends ServiceConfig = ServiceConfig> {
variables: PrimitiveMap
}

interface ModuleConstructor<T extends ModuleConfig = ModuleConfig> {
new (ctx: PluginContext, config: T): Module<T>
}

export class Module<T extends ModuleConfig = ModuleConfig> {
public name: string
public type: string
Expand Down Expand Up @@ -153,6 +163,32 @@ export class Module<T extends ModuleConfig = ModuleConfig> {
const serviceNames = keys(this.services || {})
return this.ctx.getServices(serviceNames)
}

async getDeployTasks(
{ force = false, forceBuild = false }: { force?: boolean, forceBuild?: boolean },
): Promise<DeployTask<Service<Module<T>>>[]> {
const services = await this.getServices()
const module = this

return values(services).map(s => new DeployTask(module.ctx, s, force, forceBuild))
}

async getTestTasks(
{ group, force = false, forceBuild = false }: { group?: string, force?: boolean, forceBuild?: boolean },
) {
const tasks: TestTask<Module<T>>[] = []
const config = await this.getConfig()

for (const testName of Object.keys(config.test)) {
if (group && testName !== group) {
continue
}
const testSpec = config.test[testName]
tasks.push(new TestTask<Module<T>>(this.ctx, this, testName, testSpec, force, forceBuild))
}

return tasks
}
}

export type ModuleConfigType<T extends Module> = T["_ConfigType"]
Expand Down
File renamed without changes.
Loading

0 comments on commit 3c78c36

Please sign in to comment.