Skip to content

Commit

Permalink
refactor: move project config scan to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Sep 5, 2018
1 parent d982b66 commit db8e8ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion garden-cli/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const getLogLevelFromArg = (level: string) => {
export const MOCK_CONFIG: GardenConfig = {
version: "0",
dirname: "/",
path: "/",
path: process.cwd(),
project: {
name: "mock-project",
defaultEnvironment: "local",
Expand Down
18 changes: 17 additions & 1 deletion garden-cli/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { join, relative, basename } from "path"
import { join, relative, basename, sep, resolve } from "path"
import {
findByName,
getNames,
Expand Down Expand Up @@ -135,3 +135,19 @@ export async function loadConfig(projectRoot: string, path: string): Promise<Gar
project,
}
}

export async function findProjectConfig(path: string): Promise<GardenConfig | undefined> {
let config: GardenConfig | undefined

let sepCount = path.split(sep).length - 1
for (let i = 0; i < sepCount; i++) {
config = await loadConfig(path, path)
if (!config || !config.project) {
path = resolve(path, "..")
} else if (config.project) {
return config
}
}

return config
}
26 changes: 10 additions & 16 deletions garden-cli/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Bluebird = require("bluebird")
import {
join,
parse,
relative,
resolve,
Expand Down Expand Up @@ -95,6 +94,7 @@ import {
configSchema,
GardenConfig,
loadConfig,
findProjectConfig,
} from "./config/base"
import { Task } from "./tasks/base"
import { LocalConfigStore } from "./config-store"
Expand Down Expand Up @@ -197,8 +197,6 @@ export class Garden {

static async factory(currentDirectory: string, { env, config, logger, plugins = [] }: ContextOpts = {}) {
let parsedConfig: GardenConfig
let initialDirectory = currentDirectory || []
let projectRoot: string

if (config) {
parsedConfig = <GardenConfig>validate(config, configSchema, { context: "root configuration" })
Expand All @@ -210,23 +208,19 @@ export class Garden {
})
}
} else {
let sepCount = (currentDirectory.match(new RegExp(sep, "g")) || []).length
for (let i = 0; i < sepCount; i++) {
config = await loadConfig(currentDirectory, currentDirectory)
if (!config || !config.project) {
currentDirectory = join(currentDirectory, "..")
} else if (config.project) {
break
}
}
config = await findProjectConfig(currentDirectory)

if (!config || !config.project) {
throw new ConfigurationError(`Not a project directory (or any of the parent directories): ${initialDirectory}`,
{ initialDirectory })
throw new ConfigurationError(
`Not a project directory (or any of the parent directories): ${currentDirectory}`,
{ currentDirectory },
)
}

parsedConfig = await resolveTemplateStrings(config!, new ProjectConfigContext())
}

projectRoot = currentDirectory
parsedConfig = await resolveTemplateStrings(config!, new ProjectConfigContext())
const projectRoot = parsedConfig.path

const {
defaultEnvironment,
Expand Down

0 comments on commit db8e8ed

Please sign in to comment.