Skip to content

Commit

Permalink
fix when you create-amplify absolute path, raised error (#1071)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
magisystem0408 committed Mar 18, 2024
1 parent 5e12247 commit c6a65d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/odd-dolphins-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-amplify': patch
---

fix: when you do create-amplify absolute path, raised error and log.
23 changes: 23 additions & 0 deletions packages/create-amplify/src/get_project_root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'];
Expand Down
10 changes: 9 additions & 1 deletion packages/create-amplify/src/get_project_root.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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;
};

0 comments on commit c6a65d5

Please sign in to comment.