Skip to content

Commit 7f71913

Browse files
author
Kamil Sobol
authored
fix: create dir if not exists when configuring profile (#883)
1 parent 5678ab4 commit 7f71913

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

.changeset/angry-avocados-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-cli': patch
3+
---
4+
5+
Create directory if does not exists when configuring profile

packages/cli/src/commands/configure/profile_controller.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ void describe('profile controller', () => {
214214
`${expectedCredentialText}${expectedCredentialText2}${expectedCredentialText3}`
215215
);
216216
});
217+
218+
void it('creates directory if does not exist', async () => {
219+
// delete directory
220+
await fs.rm(testDir, { recursive: true, force: true });
221+
222+
// assert that this doesn't throw
223+
await profileController.appendAWSFiles({
224+
profile: testProfile,
225+
region: testRegion,
226+
accessKeyId: testAccessKeyId,
227+
secretAccessKey: testSecretAccessKey,
228+
});
229+
230+
const data = await loadSharedConfigFiles({
231+
ignoreCache: true,
232+
});
233+
234+
assert.ok(data);
235+
});
217236
});
218237

219238
void describe('profile exists', () => {

packages/cli/src/commands/configure/profile_controller.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
import { fromIni } from '@aws-sdk/credential-providers';
77
import { EOL } from 'os';
88
import fs from 'fs/promises';
9-
import { join } from 'path';
9+
import path from 'path';
10+
import { existsSync } from 'fs';
1011

1112
/**
1213
* Options for the profile configuration.
@@ -58,7 +59,12 @@ export class ProfileController {
5859

5960
private appendAWSConfigFile = async (options: ConfigProfileOptions) => {
6061
const filePath =
61-
process.env.AWS_CONFIG_FILE ?? join(getHomeDir(), '.aws', 'config');
62+
process.env.AWS_CONFIG_FILE ?? path.join(getHomeDir(), '.aws', 'config');
63+
64+
const dirName = path.dirname(filePath);
65+
if (!existsSync(dirName)) {
66+
await fs.mkdir(dirName, { recursive: true });
67+
}
6268

6369
const fileEndsWithEOL = await this.isFileEndsWithEOL(filePath);
6470
let configData = fileEndsWithEOL ? '' : EOL;
@@ -86,7 +92,12 @@ export class ProfileController {
8692
) => {
8793
const filePath =
8894
process.env.AWS_SHARED_CREDENTIALS_FILE ??
89-
join(getHomeDir(), '.aws', 'credentials');
95+
path.join(getHomeDir(), '.aws', 'credentials');
96+
97+
const dirName = path.dirname(filePath);
98+
if (!existsSync(dirName)) {
99+
await fs.mkdir(dirName, { recursive: true });
100+
}
90101

91102
const fileEndsWithEOL = await this.isFileEndsWithEOL(filePath);
92103
let credentialData = fileEndsWithEOL ? '' : EOL;

0 commit comments

Comments
 (0)