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 when you create-amplify absolute path, raised error #1071

Merged
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;
};