From c6a65d5092b5fc517bcb899bc20c5032696f4b65 Mon Sep 17 00:00:00 2001 From: magisystem0408 <61937077+magisystem0408@users.noreply.github.com> Date: Tue, 19 Mar 2024 04:59:05 +0900 Subject: [PATCH] fix when you create-amplify absolute path, raised error (#1071) * fix create-amplify with absolute path * fix: remove condition * feat: add AmplifyUserError and test case * fix: delete resolve * fix: change name * fix: refactoring error items * fix: change simple reject * fix: refactor --- .changeset/odd-dolphins-jog.md | 5 ++++ .../src/get_project_root.test.ts | 23 +++++++++++++++++++ .../create-amplify/src/get_project_root.ts | 10 +++++++- 3 files changed, 37 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..6bbc1d9ee7 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 { AmplifyUserError } from '@aws-amplify/platform-core'; const originalEnv = process.env; @@ -72,6 +73,28 @@ 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 AmplifyUserError('ProjectDirectoryCreateError', { + 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) + ); + + await assert.rejects(getProjectRoot, expectedError); + assert.equal(fsMkDirSyncMock.mock.callCount(), 1); + assert.equal( + fsMkDirSyncMock.mock.calls[0].arguments[0], + path.resolve(userInput) + ); + }); + 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 bc4bc1441f..63f0e0339c 100644 --- a/packages/create-amplify/src/get_project_root.ts +++ b/packages/create-amplify/src/get_project_root.ts @@ -1,6 +1,7 @@ import fsp from 'fs/promises'; import path from 'path'; import yargs from 'yargs'; +import { AmplifyUserError } from '@aws-amplify/platform-core'; import { AmplifyPrompter, LogLevel, printer } from '@aws-amplify/cli-core'; /** @@ -36,7 +37,14 @@ 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) { + 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.`, + }); + } } return projectRoot; };