Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): run cdk from inner directories #5772

Merged
merged 11 commits into from
Jan 13, 2020
38 changes: 37 additions & 1 deletion packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'source-map-support/register';

import * as cxapi from '@aws-cdk/cx-api';
import * as colors from 'colors/safe';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as yargs from 'yargs';

Expand All @@ -17,7 +18,7 @@ import { availableInitLanguages, cliInit, printAvailableTemplates } from '../lib
import { data, debug, error, print, setVerbose, success } from '../lib/logging';
import { PluginHost } from '../lib/plugin';
import { serializeStructure } from '../lib/serialize';
import { Configuration, Settings } from '../lib/settings';
import { Configuration, PROJECT_CONFIG, Settings } from '../lib/settings';
import * as version from '../lib/version';

// tslint:disable:no-shadowed-variable max-line-length
Expand Down Expand Up @@ -103,6 +104,16 @@ async function initCommandLine() {
if (argv.verbose) {
setVerbose();
}

const projectRootDirectory = await lookupProjectRoot();

if (projectRootDirectory) {
// We change to the project root directory to maintian the behavior prior to (1.21.0) this feature, where
// cdk commands had to be executed from the project root.
// Note that if we cannot locate the project root, then we proceed anyway because the user might have the "app" configured in some other way.
process.chdir(projectRootDirectory);
}

debug('CDK toolkit version:', version.DISPLAY_VERSION);
debug('Command line arguments:', argv);

Expand Down Expand Up @@ -382,6 +393,31 @@ async function initCommandLine() {
}
}

async function lookupProjectRoot(): Promise<string | undefined> {

async function ascend(directory: string): Promise<string | undefined> {

const filePath = path.join(directory, PROJECT_CONFIG);

if (await fs.pathExists(filePath)) {
return directory;
}

const parentDir = path.dirname(directory);

if (parentDir === directory) {
// We reached the file system root, give up.
return undefined;
}

return ascend(parentDir);

}

return ascend(path.resolve(process.cwd()));

}

initCommandLine()
.then(value => {
if (value == null) { return; }
Expand Down
12 changes: 12 additions & 0 deletions packages/aws-cdk/test/integ/cli/test-cdk-from-inner-directory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -euo pipefail
scriptdir=$(cd $(dirname $0) && pwd)
source ${scriptdir}/common.bash
# ----------------------------------------------------------

setup

pushd docker
cdk diff ${STACK_NAME_PREFIX}-test-1

echo "✅ success"
26 changes: 26 additions & 0 deletions packages/aws-cdk/test/integ/cli/test-cdk-from-outer-directory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -euo pipefail
scriptdir=$(cd $(dirname $0) && pwd)
source ${scriptdir}/common.bash
# ----------------------------------------------------------

setup

temp_dir=$(mktemp -d)

function cleanup() {
rm -rf ${temp_dir}
}

trap cleanup EXIT INT

pushd ${temp_dir}
set +e
output="$(cdk diff ${STACK_NAME_PREFIX}-test-1 2>&1)"
set -e

if [[ "${output}" != *"--app is required"* ]]; then
fail "unexpected output when running 'cdk diff' from outer directory: ${output}"
fi

echo "✅ success"