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: throw error when download samples to the folder twice #1073

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions packages/cli/src/cmds/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {

import activate from "../activate";
import * as constants from "../constants";
import { NotFoundInputedFolder, SampleAppDownloadFailed } from "../error";
import { NotFoundInputedFolder, SampleAppDownloadFailed, ProjectFolderExist } from "../error";
import { validateAndUpdateAnswers, visitInteractively } from "../question/question";
import { YargsCommand } from "../yargsCommand";
import {
Expand All @@ -39,7 +39,6 @@ import {
toYargsOptions,
} from "../utils";
import CliTelemetry from "../telemetry/cliTelemetry";
import { TelemetryClient } from "applicationinsights";
import {
TelemetryEvent,
TelemetryProperty,
Expand Down Expand Up @@ -180,12 +179,17 @@ class NewTemplete extends YargsCommand {
const templateName = args["template-name"] as string;
const template = constants.templates.find((t) => t.sampleAppName === templateName)!;

const sampleAppFolder = path.resolve(folder, template.sampleAppName);
if ((await fs.pathExists(sampleAppFolder)) && (await fs.readdir(sampleAppFolder)).length > 0) {
throw ProjectFolderExist(sampleAppFolder);
}

const result = await this.fetchCodeZip(template.sampleAppUrl);
await this.saveFilesRecursively(new AdmZip(result.data), template.sampleAppName, folder);
console.log(
colors.green(
`Downloaded the '${colors.yellow(template.sampleAppName)}' sample to '${colors.yellow(
path.join(folder, template.sampleAppName)
sampleAppFolder
)}'.`
)
);
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@ export function InquirerAnswerNotFound(data: string): UserError {
"InquirerAnswerNotFound"
);
}

export function ProjectFolderExist(path: string): UserError {
return returnUserError(
new Error(`Path ${path} alreay exists. Select a different folder.`),
constants.cliSource,
"ProjectFolderExist"
);
}
2 changes: 1 addition & 1 deletion packages/fx-core/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class CoreImpl implements Core {
const url = samples.data as string;
const sampleId = samples.id;

const sampleAppPath = path.join(folder, sampleId);
const sampleAppPath = path.resolve(folder, sampleId);
if (
(await fs.pathExists(sampleAppPath)) &&
(await fs.readdir(sampleAppPath)).length > 0
Expand Down