Skip to content

Commit

Permalink
fix: create file if file does not exist. closes #2274
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Feb 23, 2018
1 parent 30dd02b commit adbdf92
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/cmd_line/commands/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,28 @@ export class FileCommand extends node.CommandBase {
return;
}

let currentFilePath = vscode.window.activeTextEditor!.document.uri.path;
let editorFilePath = vscode.window.activeTextEditor!.document.uri.path;
this._arguments.name = <string>untildify(this._arguments.name);
let newFilePath = path.isAbsolute(this._arguments.name)
let filePath = path.isAbsolute(this._arguments.name)
? this._arguments.name
: path.join(path.dirname(currentFilePath), this._arguments.name);

if (newFilePath !== currentFilePath) {
const newFileDoesntExist = !await this.fileExists(newFilePath);
const newFileHasNoExtname = path.extname(newFilePath) === '';
if (newFileDoesntExist && newFileHasNoExtname) {
const pathWithExtname = newFilePath + path.extname(currentFilePath);
if (await this.fileExists(pathWithExtname)) {
newFilePath = pathWithExtname;
: path.join(path.dirname(editorFilePath), this._arguments.name);

if (filePath !== editorFilePath) {
if (!fs.existsSync(filePath)) {
// if file does not exist and does not have an extension
// try to find it with the same extension
if (path.extname(filePath) === '') {
const pathWithExt = filePath + path.extname(editorFilePath);
if (fs.existsSync(pathWithExt)) {
filePath = pathWithExt;
}
}

// create file
fs.closeSync(fs.openSync(filePath, 'w'));
}

let folder = vscode.Uri.file(newFilePath);
let folder = vscode.Uri.file(filePath);
await vscode.commands.executeCommand(
'vscode.open',
folder,
Expand All @@ -121,12 +126,4 @@ export class FileCommand extends node.CommandBase {
}
}
}

protected fileExists(filePath: string) {
return new Promise<boolean>((resolve, reject) => {
fs.stat(filePath, async (error, stat) => {
resolve(!error);
});
});
}
}

0 comments on commit adbdf92

Please sign in to comment.