Skip to content

Commit d79627c

Browse files
authored
fix: prevent init if local files belong to other app (#13954)
1 parent 33c10fd commit d79627c

File tree

1 file changed

+14
-1
lines changed
  • packages/amplify-cli/src/commands

1 file changed

+14
-1
lines changed

packages/amplify-cli/src/commands/init.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { $TSContext, LocalEnvInfo } from '@aws-amplify/amplify-cli-core';
1+
import { $TSContext, AmplifyError, LocalEnvInfo, stateManager } from '@aws-amplify/amplify-cli-core';
22
import { constructInputParams } from '../amplify-service-helper';
33
import { Context } from '../domain/context';
44
import { raisePostEnvAddEvent } from '../execution-manager';
@@ -10,6 +10,7 @@ import { initProviders } from '../init-steps/s2-initProviders';
1010
import { scaffoldProjectHeadless } from '../init-steps/s8-scaffoldHeadless';
1111
import { onHeadlessSuccess, onSuccess } from '../init-steps/s9-onSuccess';
1212
import { checkForNestedProject } from './helpers/projectUtils';
13+
import { getAmplifyAppId } from '../extensions/amplify-helpers/get-amplify-appId';
1314

1415
const constructExeInfo = (context: $TSContext): void => {
1516
context.exeInfo = {
@@ -32,6 +33,18 @@ export const run = async (context: $TSContext): Promise<void> => {
3233
constructExeInfo(context);
3334
checkForNestedProject();
3435

36+
const projectPath = process.cwd();
37+
if (stateManager.metaFileExists(projectPath)) {
38+
const inputAppId = context.exeInfo?.inputParams?.amplify?.appId;
39+
const appId = getAmplifyAppId();
40+
if (inputAppId && appId && inputAppId !== appId) {
41+
throw new AmplifyError('InvalidAmplifyAppIdError', {
42+
message: `Amplify appId mismatch.`,
43+
resolution: `You are currently working in the amplify project with Id ${appId}`,
44+
});
45+
}
46+
}
47+
3548
const steps = runStrategy(!!context?.parameters?.options?.quickstart);
3649
for (const step of steps) {
3750
await step(context);

0 commit comments

Comments
 (0)