private confirmPath()

in app/lib/outputs.ts [79:126]


	private confirmPath(outPath: string, confirmRelative: boolean = false): Promise<string> {
		let absPath = path.resolve(outPath);
		return fsUtils
			.exists(absPath)
			.then(exists => {
				if (!exists && (!confirmRelative || path.isAbsolute(outPath))) {
					return Promise.resolve(absPath);
				}
				if (exists && this.overwriteSetting === FileOverwriteOption.Throw) {
					throw new Error("Cannot overwrite existing file " + this.outputPath);
				}
				if (
					(exists && this.overwriteSetting === FileOverwriteOption.Overwrite) ||
					this.overwriteSetting === FileOverwriteOption.Append
				) {
					return Promise.resolve(absPath);
				}
				let prompt = exists
					? "Warning: " + absPath + " will be overwritten. Continue? (y/n or provide another file name.)"
					: "Write to " + absPath + "? (y/n or provide another file name.)";
				return qread.read("overwrite", prompt).then((result: string) => {
					let lcResult = result.toLowerCase();
					if (["y", "yes"].indexOf(lcResult) >= 0) {
						return Promise.resolve(result);
					} else if (["n", "no"].indexOf(lcResult) >= 0) {
						throw new Error("Operation canceled by user.");
					} else {
						return this.confirmPath(result, true);
					}
				});
			})
			.then(confirmedPath => {
				return fsUtils.fileAccess(confirmedPath, fsUtils.W_OK).then(access => {
					if (access) {
						return confirmedPath;
					} else {
						if (this.overwriteSetting === FileOverwriteOption.Throw) {
							throw new Error("Cannot write to file " + this.outputPath + " (access denied).");
						}
						return qread
							.read("filename", "No write access to file " + confirmedPath + ". Provide a new file name.")
							.then((result: string) => {
								return this.confirmPath(result, true);
							});
					}
				});
			});
	}