Skip to content

Commit

Permalink
fix: default cwd to process.cwd()
Browse files Browse the repository at this point in the history
The jsdoc comment already mentioned that `cwd` defaults to
`process.cwd()`.
  • Loading branch information
ZauberNerd committed Jul 17, 2024
1 parent fa86688 commit edc3f50
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
26 changes: 13 additions & 13 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ export interface LocalExecConfig {

/**
* The working directory to run the command in.
* Defaults to process.pwd().
* If copyBeforeRun is set to true it will copy the working directory to an asset directory and take that as the base to run.
*
* @default process.cwd()
*/
readonly cwd: string;
readonly cwd?: string;

/**
* If set to true, the working directory will be copied to an asset directory.
Expand All @@ -53,13 +54,15 @@ export class LocalExec extends Resource {
constructor(scope: Construct, id: string, config: LocalExecConfig) {
super(scope, id, config);

const cwd = config.cwd ?? process.cwd();

const workingDir =
config.copyBeforeRun === true
? new TerraformAsset(this, "workingDir", {
path: config.cwd,
path: cwd,
type: AssetType.DIRECTORY,
}).path
: config.cwd;
: cwd;

this.cwd = workingDir;
this.command = config.command;
Expand Down
25 changes: 25 additions & 0 deletions test/local-exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,29 @@ describe("LocalExec", () => {
expect(fs.existsSync(path.resolve(workingDir, "test.txt"))).toBe(true);
expect(fs.existsSync(path.resolve(__dirname, "test.txt"))).toBe(false);
});

describe("default cwd", () => {
let oldCwd = process.cwd();

beforeEach(() => {
oldCwd = process.cwd();
process.chdir(testdir);
});

afterEach(() => {
process.chdir(oldCwd);
});

test("defaults cwd to process.cwd()", () => {
// Create a file in the working directory
apply((stack) => {
new LocalExec(stack, "test", {
command: "cp origin.txt test.txt",
copyBeforeRun: false,
});
});

expect(fs.existsSync(path.resolve(testdir, "test.txt"))).toBe(true);
});
});
});

0 comments on commit edc3f50

Please sign in to comment.