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

fix: sandbox client config options #643

Merged
merged 8 commits into from Nov 15, 2023
5 changes: 5 additions & 0 deletions .changeset/dry-ladybugs-film.md
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

client config generation respects sandbox format option
5 changes: 5 additions & 0 deletions .changeset/strong-plums-explain.md
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

Change sandbox out-dir to config-out-dir and client config generation respects config-out-dir option
34 changes: 33 additions & 1 deletion packages/cli/src/commands/sandbox/sandbox_command.test.ts
Expand Up @@ -105,7 +105,7 @@ void describe('sandbox command', () => {
assert.match(output, /--dir-to-watch/);
assert.match(output, /--exclude/);
assert.match(output, /--format/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just chatted with the PMs and they suggested changing this to config-format for consistency. If you want to roll that into this PR you could, or do a follow up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add that to this PR. What is your opinion on e2e testing config-format and config-out-dir? I can follow up with another PR when I address #403 or should it be a part of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to config-format in ece9635

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with e2e tests going in a follow up PR. When you do get to that, I suggest adding this as an additional assertion on the existing sandbox test rather than a whole new test.

assert.match(output, /--out-dir/);
assert.match(output, /--config-out-dir/);
assert.equal(mockHandleProfile.mock.callCount(), 0);
});

Expand Down Expand Up @@ -251,4 +251,36 @@ void describe('sandbox command', () => {
sandboxProfile
);
});

void it('fails if invalid config-out-dir is provided', async () => {
mock.method(fs, 'lstatSync', () => {
return {
isFile: () => false,
isDir: () => false,
};
});
const output = await commandRunner.runCommand(
'sandbox --config-out-dir nonExistentDir'
);
assert.match(output, /--config-out-dir nonExistentDir does not exist/);
});

void it('fails if a file is provided for config-out-dir', async (contextual) => {
mock.method(fs, 'lstatSync', () => {
return {
isFile: () => true,
isDir: () => false,
};
});
contextual.mock.method(fs, 'statSync', () => ({
isDirectory: () => false,
}));
const output = await commandRunner.runCommand(
'sandbox --config-out-dir existentFile'
);
assert.match(
output,
/--config-out-dir existentFile is not a valid directory/
);
});
});
40 changes: 22 additions & 18 deletions packages/cli/src/commands/sandbox/sandbox_command.ts
Expand Up @@ -20,7 +20,7 @@ type SandboxCommandOptionsCamelCase = {
exclude: string[] | undefined;
name: string | undefined;
format: ClientConfigFormat | undefined;
outDir: string | undefined;
configOutDir: string | undefined;
profile: string | undefined;
};

Expand Down Expand Up @@ -81,7 +81,9 @@ export class SandboxCommand

// attaching event handlers
const clientConfigLifecycleHandler = new ClientConfigLifecycleHandler(
this.clientConfigGeneratorAdapter
this.clientConfigGeneratorAdapter,
args['config-out-dir'],
args.format
);
const eventHandlers = this.sandboxEventHandlerCreator?.({
sandboxName: this.sandboxName,
Expand All @@ -94,7 +96,7 @@ export class SandboxCommand
}
const watchExclusions = args.exclude ?? [];
const clientConfigWritePath = await getClientConfigPath(
args['out-dir'],
args['config-out-dir'],
args.format
);
watchExclusions.push(clientConfigWritePath);
Expand Down Expand Up @@ -143,7 +145,7 @@ export class SandboxCommand
choices: Object.values(ClientConfigFormat),
global: false,
})
.option('out-dir', {
.option('config-out-dir', {
describe:
'A path to directory where config is written. If not provided defaults to current process working directory.',
type: 'string',
Expand All @@ -157,20 +159,10 @@ export class SandboxCommand
})
.check((argv) => {
if (argv['dir-to-watch']) {
// make sure it's a real directory
let stats;
try {
stats = fs.statSync(argv['dir-to-watch'], {});
} catch (e) {
throw new Error(
`--dir-to-watch ${argv['dir-to-watch']} does not exist`
);
}
if (!stats.isDirectory()) {
throw new Error(
`--dir-to-watch ${argv['dir-to-watch']} is not a valid directory`
);
}
this.validateDirectory('dir-to-watch', argv['dir-to-watch']);
}
if (argv['config-out-dir']) {
this.validateDirectory('config-out-dir', argv['config-out-dir']);
}
if (argv.name) {
const projectNameRegex = /^[a-zA-Z0-9-]{1,15}$/;
Expand Down Expand Up @@ -201,4 +193,16 @@ export class SandboxCommand
await this.sandboxFactory.getInstance()
).delete({ name: this.sandboxName });
};

validateDirectory = (option: string, dir: string) => {
let stats;
try {
stats = fs.statSync(dir, {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that check might be supporting async functions.
Can you give it a try and convert this to async?

Ref: yargs/yargs#1872

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not aware of this, good to know and done in 4cb5b72.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any file system access if possible should be made async so that event loop is not blocked.

} catch (e) {
throw new Error(`--${option} ${dir} does not exist`);
}
if (!stats.isDirectory()) {
throw new Error(`--${option} ${dir} is not a valid directory`);
}
};
}