From e21b2c9cb6affa3fd3ebb6575493935873d00646 Mon Sep 17 00:00:00 2001 From: magisystem0408 <61937077+magisystem0408@users.noreply.github.com> Date: Mon, 26 Feb 2024 02:19:35 +0900 Subject: [PATCH 1/8] fix create-amplify with absolute path --- .changeset/odd-dolphins-jog.md | 5 +++ .../src/get_project_root.test.ts | 32 +++++++++++++++++++ .../create-amplify/src/get_project_root.ts | 12 ++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 .changeset/odd-dolphins-jog.md diff --git a/.changeset/odd-dolphins-jog.md b/.changeset/odd-dolphins-jog.md new file mode 100644 index 0000000000..c72f032420 --- /dev/null +++ b/.changeset/odd-dolphins-jog.md @@ -0,0 +1,5 @@ +--- +'create-amplify': patch +--- + +fix: when you do create-amplify absolute path, raised error and log. diff --git a/packages/create-amplify/src/get_project_root.test.ts b/packages/create-amplify/src/get_project_root.test.ts index ed269ccbbd..6a5632ce36 100644 --- a/packages/create-amplify/src/get_project_root.test.ts +++ b/packages/create-amplify/src/get_project_root.test.ts @@ -4,6 +4,7 @@ import fsp from 'fs/promises'; import path from 'path'; import { AmplifyPrompter } from '@aws-amplify/cli-core'; import { getProjectRoot } from './get_project_root.js'; +import { printer } from './printer.js'; const originalEnv = process.env; @@ -72,6 +73,37 @@ void describe('getProjectRoot', () => { assert.equal(projectRoot, path.resolve(userInput)); }); + void it('prints warning if creation of project root failed and path is absolute', async () => { + process.env.npm_config_yes = 'false'; + const userInput = 'some/absolute/path'; + mock.method(AmplifyPrompter, 'input', () => Promise.resolve(userInput)); + const expectedError = new Error(); + fsMkDirSyncMock.mock.mockImplementationOnce(() => + Promise.reject(expectedError) + ); + const printerMock = mock.method(printer, 'log'); + await assert.rejects( + () => getProjectRoot(), + (error: Error) => { + assert.strictEqual(error, expectedError); + return true; + } + ); + + assert.equal(fsMkDirSyncMock.mock.callCount(), 1); + assert.equal( + fsMkDirSyncMock.mock.calls[0].arguments[0], + path.resolve(userInput) + ); + assert.equal(printerMock.mock.callCount(), 3); + assert.equal( + printerMock.mock.calls[2].arguments[0], + `Failed to create directory at ${path.resolve( + userInput + )}. Ensure this is the correct path and you have write permissions to this location.` + ); + }); + void it('use default options if `yes`', async (ctx) => { process.env.npm_config_yes = 'false'; process.argv = ['node', 'test.js', '--yes']; diff --git a/packages/create-amplify/src/get_project_root.ts b/packages/create-amplify/src/get_project_root.ts index 2f21b57e0e..5cd4ea7bb2 100644 --- a/packages/create-amplify/src/get_project_root.ts +++ b/packages/create-amplify/src/get_project_root.ts @@ -37,7 +37,17 @@ export const getProjectRoot = async () => { LogLevel.DEBUG ); printer.log(`Creating directory ${projectRoot}`, LogLevel.DEBUG); - await fsp.mkdir(projectRoot, { recursive: true }); + try { + await fsp.mkdir(projectRoot, { recursive: true }); + } catch (err) { + if (path.isAbsolute(projectRoot)) { + printer.log( + `Failed to create directory at ${projectRoot}. Ensure this is the correct path and you have write permissions to this location.`, + LogLevel.ERROR + ); + } + throw err; + } } return projectRoot; }; From 653ecb366e31d3038747a9071262937aaff5c9f0 Mon Sep 17 00:00:00 2001 From: magisystem0408 <61937077+magisystem0408@users.noreply.github.com> Date: Wed, 28 Feb 2024 12:36:18 +0900 Subject: [PATCH 2/8] fix: remove condition --- packages/create-amplify/src/get_project_root.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/create-amplify/src/get_project_root.ts b/packages/create-amplify/src/get_project_root.ts index 5cd4ea7bb2..8f9e62c364 100644 --- a/packages/create-amplify/src/get_project_root.ts +++ b/packages/create-amplify/src/get_project_root.ts @@ -40,12 +40,10 @@ export const getProjectRoot = async () => { try { await fsp.mkdir(projectRoot, { recursive: true }); } catch (err) { - if (path.isAbsolute(projectRoot)) { - printer.log( - `Failed to create directory at ${projectRoot}. Ensure this is the correct path and you have write permissions to this location.`, - LogLevel.ERROR - ); - } + printer.log( + `Failed to create directory at ${projectRoot}. Ensure this is the correct path and you have write permissions to this location.`, + LogLevel.ERROR + ); throw err; } } From 4e070def49e77f1560468835a90fbc837cfe1358 Mon Sep 17 00:00:00 2001 From: magisystem0408 Date: Thu, 29 Feb 2024 02:37:33 +0900 Subject: [PATCH 3/8] feat: add AmplifyUserError and test case --- .../src/get_project_root.test.ts | 43 +++++++++++++------ .../create-amplify/src/get_project_root.ts | 10 ++--- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/packages/create-amplify/src/get_project_root.test.ts b/packages/create-amplify/src/get_project_root.test.ts index 6a5632ce36..c6e0fd5d82 100644 --- a/packages/create-amplify/src/get_project_root.test.ts +++ b/packages/create-amplify/src/get_project_root.test.ts @@ -4,7 +4,8 @@ import fsp from 'fs/promises'; import path from 'path'; import { AmplifyPrompter } from '@aws-amplify/cli-core'; import { getProjectRoot } from './get_project_root.js'; -import { printer } from './printer.js'; +import { AmplifyError, AmplifyUserError } from '@aws-amplify/platform-core'; +import util from 'util'; const originalEnv = process.env; @@ -77,31 +78,47 @@ void describe('getProjectRoot', () => { process.env.npm_config_yes = 'false'; const userInput = 'some/absolute/path'; mock.method(AmplifyPrompter, 'input', () => Promise.resolve(userInput)); - const expectedError = new Error(); + const expectedError = new AmplifyUserError( + 'MultipleSingletonResourcesError', + { + message: `Failed to create project directory`, + resolution: `Ensure that ${path.resolve( + userInput + )} is the correct path and you have write permissions to this location.`, + } + ); fsMkDirSyncMock.mock.mockImplementationOnce(() => Promise.reject(expectedError) ); - const printerMock = mock.method(printer, 'log'); await assert.rejects( () => getProjectRoot(), - (error: Error) => { - assert.strictEqual(error, expectedError); + (err: Error) => { + const sampleStderr = `some random stderr +before the actual error message +${util.inspect(err, { depth: null })} +and some after the error message`; + + const actual = AmplifyError.fromStderr(sampleStderr); + assert.deepStrictEqual(actual?.name, expectedError.name); + assert.deepStrictEqual( + actual?.classification, + expectedError.classification + ); + assert.deepStrictEqual(actual?.message, expectedError.message); + assert.deepStrictEqual(actual?.details, expectedError.details); + assert.deepStrictEqual(actual?.cause?.name, expectedError.cause?.name); + assert.deepStrictEqual( + actual?.cause?.message, + expectedError.cause?.message + ); return true; } ); - assert.equal(fsMkDirSyncMock.mock.callCount(), 1); assert.equal( fsMkDirSyncMock.mock.calls[0].arguments[0], path.resolve(userInput) ); - assert.equal(printerMock.mock.callCount(), 3); - assert.equal( - printerMock.mock.calls[2].arguments[0], - `Failed to create directory at ${path.resolve( - userInput - )}. Ensure this is the correct path and you have write permissions to this location.` - ); }); void it('use default options if `yes`', async (ctx) => { diff --git a/packages/create-amplify/src/get_project_root.ts b/packages/create-amplify/src/get_project_root.ts index 8f9e62c364..7085eb9847 100644 --- a/packages/create-amplify/src/get_project_root.ts +++ b/packages/create-amplify/src/get_project_root.ts @@ -3,6 +3,7 @@ import path from 'path'; import yargs from 'yargs'; import { AmplifyPrompter, LogLevel } from '@aws-amplify/cli-core'; import { printer } from './printer.js'; +import { AmplifyUserError } from '@aws-amplify/platform-core'; /** * Returns the project root directory. @@ -40,11 +41,10 @@ export const getProjectRoot = async () => { try { await fsp.mkdir(projectRoot, { recursive: true }); } catch (err) { - printer.log( - `Failed to create directory at ${projectRoot}. Ensure this is the correct path and you have write permissions to this location.`, - LogLevel.ERROR - ); - throw err; + throw new AmplifyUserError('MultipleSingletonResourcesError', { + message: `Failed to create project directory`, + resolution: `Ensure that ${projectRoot} is the correct path and you have write permissions to this location.`, + }); } } return projectRoot; From 130b5ed69ffc884755d3a153217819319aaef86c Mon Sep 17 00:00:00 2001 From: magisystem0408 Date: Thu, 29 Feb 2024 02:43:56 +0900 Subject: [PATCH 4/8] fix: delete resolve --- packages/create-amplify/src/get_project_root.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/create-amplify/src/get_project_root.test.ts b/packages/create-amplify/src/get_project_root.test.ts index c6e0fd5d82..8ef9b07e51 100644 --- a/packages/create-amplify/src/get_project_root.test.ts +++ b/packages/create-amplify/src/get_project_root.test.ts @@ -82,9 +82,7 @@ void describe('getProjectRoot', () => { 'MultipleSingletonResourcesError', { message: `Failed to create project directory`, - resolution: `Ensure that ${path.resolve( - userInput - )} is the correct path and you have write permissions to this location.`, + resolution: `Ensure that ${userInput} is the correct path and you have write permissions to this location.`, } ); fsMkDirSyncMock.mock.mockImplementationOnce(() => From 18edfb089607cedf6c97a85a57f047fe6053e6ca Mon Sep 17 00:00:00 2001 From: magisystem0408 Date: Thu, 29 Feb 2024 02:52:54 +0900 Subject: [PATCH 5/8] fix: change name --- packages/create-amplify/src/get_project_root.test.ts | 11 ++++------- packages/create-amplify/src/get_project_root.ts | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/create-amplify/src/get_project_root.test.ts b/packages/create-amplify/src/get_project_root.test.ts index 8ef9b07e51..6c145acff3 100644 --- a/packages/create-amplify/src/get_project_root.test.ts +++ b/packages/create-amplify/src/get_project_root.test.ts @@ -78,13 +78,10 @@ void describe('getProjectRoot', () => { process.env.npm_config_yes = 'false'; const userInput = 'some/absolute/path'; mock.method(AmplifyPrompter, 'input', () => Promise.resolve(userInput)); - const expectedError = new AmplifyUserError( - 'MultipleSingletonResourcesError', - { - message: `Failed to create project directory`, - resolution: `Ensure that ${userInput} is the correct path and you have write permissions to this location.`, - } - ); + const expectedError = new AmplifyUserError('ProjectDirectoryCreateError', { + message: `Failed to create project directory`, + resolution: `Ensure that ${userInput} is the correct path and you have write permissions to this location.`, + }); fsMkDirSyncMock.mock.mockImplementationOnce(() => Promise.reject(expectedError) ); diff --git a/packages/create-amplify/src/get_project_root.ts b/packages/create-amplify/src/get_project_root.ts index 7085eb9847..d89a68982b 100644 --- a/packages/create-amplify/src/get_project_root.ts +++ b/packages/create-amplify/src/get_project_root.ts @@ -41,7 +41,7 @@ export const getProjectRoot = async () => { try { await fsp.mkdir(projectRoot, { recursive: true }); } catch (err) { - throw new AmplifyUserError('MultipleSingletonResourcesError', { + throw new AmplifyUserError('ProjectDirectoryCreateError', { message: `Failed to create project directory`, resolution: `Ensure that ${projectRoot} is the correct path and you have write permissions to this location.`, }); From b7e13df12643711bec473a56b2fc496c8b7b4ec4 Mon Sep 17 00:00:00 2001 From: magisystem0408 Date: Thu, 29 Feb 2024 12:34:13 +0900 Subject: [PATCH 6/8] fix: refactoring error items --- packages/create-amplify/src/get_project_root.test.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/create-amplify/src/get_project_root.test.ts b/packages/create-amplify/src/get_project_root.test.ts index 6c145acff3..08d0dc34a0 100644 --- a/packages/create-amplify/src/get_project_root.test.ts +++ b/packages/create-amplify/src/get_project_root.test.ts @@ -95,17 +95,8 @@ and some after the error message`; const actual = AmplifyError.fromStderr(sampleStderr); assert.deepStrictEqual(actual?.name, expectedError.name); - assert.deepStrictEqual( - actual?.classification, - expectedError.classification - ); assert.deepStrictEqual(actual?.message, expectedError.message); - assert.deepStrictEqual(actual?.details, expectedError.details); - assert.deepStrictEqual(actual?.cause?.name, expectedError.cause?.name); - assert.deepStrictEqual( - actual?.cause?.message, - expectedError.cause?.message - ); + assert.deepStrictEqual(actual?.resolution, expectedError.resolution); return true; } ); From d7226b6a974c18e0362af236f848bc51c0e1bc70 Mon Sep 17 00:00:00 2001 From: magisystem0408 Date: Sat, 2 Mar 2024 02:54:21 +0900 Subject: [PATCH 7/8] fix: change simple reject --- .../src/get_project_root.test.ts | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/packages/create-amplify/src/get_project_root.test.ts b/packages/create-amplify/src/get_project_root.test.ts index 08d0dc34a0..7fa36ba16d 100644 --- a/packages/create-amplify/src/get_project_root.test.ts +++ b/packages/create-amplify/src/get_project_root.test.ts @@ -4,8 +4,7 @@ import fsp from 'fs/promises'; import path from 'path'; import { AmplifyPrompter } from '@aws-amplify/cli-core'; import { getProjectRoot } from './get_project_root.js'; -import { AmplifyError, AmplifyUserError } from '@aws-amplify/platform-core'; -import util from 'util'; +import { AmplifyUserError } from '@aws-amplify/platform-core'; const originalEnv = process.env; @@ -80,26 +79,15 @@ void describe('getProjectRoot', () => { mock.method(AmplifyPrompter, 'input', () => Promise.resolve(userInput)); const expectedError = new AmplifyUserError('ProjectDirectoryCreateError', { message: `Failed to create project directory`, - resolution: `Ensure that ${userInput} is the correct path and you have write permissions to this location.`, + resolution: `Ensure that ${path.resolve( + userInput + )} is the correct path and you have write permissions to this location.`, }); fsMkDirSyncMock.mock.mockImplementationOnce(() => Promise.reject(expectedError) ); - await assert.rejects( - () => getProjectRoot(), - (err: Error) => { - const sampleStderr = `some random stderr -before the actual error message -${util.inspect(err, { depth: null })} -and some after the error message`; - - const actual = AmplifyError.fromStderr(sampleStderr); - assert.deepStrictEqual(actual?.name, expectedError.name); - assert.deepStrictEqual(actual?.message, expectedError.message); - assert.deepStrictEqual(actual?.resolution, expectedError.resolution); - return true; - } - ); + + await assert.rejects(() => getProjectRoot(), expectedError); assert.equal(fsMkDirSyncMock.mock.callCount(), 1); assert.equal( fsMkDirSyncMock.mock.calls[0].arguments[0], From cccc41ef2374cd860ea27f936066b031a5930c61 Mon Sep 17 00:00:00 2001 From: magisystem0408 Date: Sat, 2 Mar 2024 03:09:10 +0900 Subject: [PATCH 8/8] fix: refactor --- packages/create-amplify/src/get_project_root.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create-amplify/src/get_project_root.test.ts b/packages/create-amplify/src/get_project_root.test.ts index 7fa36ba16d..6bbc1d9ee7 100644 --- a/packages/create-amplify/src/get_project_root.test.ts +++ b/packages/create-amplify/src/get_project_root.test.ts @@ -87,7 +87,7 @@ void describe('getProjectRoot', () => { Promise.reject(expectedError) ); - await assert.rejects(() => getProjectRoot(), expectedError); + await assert.rejects(getProjectRoot, expectedError); assert.equal(fsMkDirSyncMock.mock.callCount(), 1); assert.equal( fsMkDirSyncMock.mock.calls[0].arguments[0],