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

added fallbacks for destination directory #87

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

* Adds ability to use the "New File from Template" command from command palette or from shortcut

## [3.0.0 - 2019-01-02]

* Adds ability to provide dynamic template replacements at the time of file creation
Expand Down
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publisher": "teamchilla",
"license": "MIT",
"engines": {
"vscode": "^1.26.0"
"vscode": "^1.34.0"
},
"icon": "icon_128_128.png",
"galleryBanner": {
Expand Down Expand Up @@ -66,18 +66,21 @@
"lint": "node ./node_modules/tslint/bin/tslint \"src/**/*.ts\""
},
"devDependencies": {
"@types/fs-extra": "^5.0.2",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.0",
"mocha": "^5.2.0",
"tslint": "^5.1.0",
"typescript": "^2.9.2",
"vscode": "^1.1.21",
"@types/fs-extra": "^8.0.1",
"@types/glob": "^7.1.1",
"@types/lodash": "^4.14.149",
"@types/mocha": "^7.0.1",
"@types/node": "^13.7.0",
"dir-compare": "^2.2.0",
"glob": "^7.1.6",
"mocha": "^7.0.1",
"tslint": "^6.0.0",
"typescript": "^3.7.5",
"vscode": "^1.1.36",
"vscode-test": "^1.2.3"
},
"dependencies": {
"dir-compare": "^1.4.0",
"fs-extra": "^7.0.0",
"fs-extra": "^8.1.0",
"handlebars": "^4.1.2",
"lodash": "^4.17.13"
}
Expand Down
22 changes: 21 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,27 @@ export function activate(context: vscode.ExtensionContext) {

const disposable = vscode.commands.registerCommand("extension.blueprint", async (e: vscode.Uri) => {

let directoryPath = (e && e.fsPath) ? e.fsPath : vscode.workspace.rootPath;
// get target dir path from args, when command is executed from context menu
let directoryPath = (e && e.fsPath) ? e.fsPath : null;

// otherwise use hacky clipboard trick to get currently selected item it treeview
// see this issue for vscode api implementation
// https://github.com/microsoft/vscode/issues/3553
if (!directoryPath) {
const tempClipboardValue = await vscode.env.clipboard.readText();
await vscode.commands.executeCommand('copyFilePath');
directoryPath = await vscode.env.clipboard.readText();
await vscode.env.clipboard.writeText(tempClipboardValue);
}
// otherwise place stuff in the first workspace root (if it's open)
if (!directoryPath) {
directoryPath = vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders[0].uri.fsPath : null;
}
// do no proceed if there is still no directory path
if (!directoryPath) {
vscode.window.showInformationMessage("Directory for new files not found.")
return;
}

if (!(await fs.stat(directoryPath)).isDirectory()) {
directoryPath = path.dirname(directoryPath);
Expand Down
6 changes: 3 additions & 3 deletions src/fileCreator/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function replaceTemplateContent(

const template = handlebars.compile(rawContent);

const dynamicTemplateInputMap = Object.keys(dynamicTemplateValues).reduce((prev, value) => {
const dynamicTemplateInputMap = Object.keys(dynamicTemplateValues).reduce<{ [key: string]: string }>((prev, value) => {
prev[value] = dynamicTemplateValues[value].userInput;
return prev;
}, {});
Expand Down Expand Up @@ -76,10 +76,10 @@ export function replaceStringUsingTransforms(stringToReplace: string, name: stri
return result;
}

function escapeRegExp(str): string {
function escapeRegExp(str: string): string {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

function replaceAll(str, find, replace): string {
function replaceAll(str: string, find: string, replace: string): string {
return str.replace(new RegExp(escapeRegExp(find), "g"), replace);
}
5 changes: 4 additions & 1 deletion src/inputs/getSelectedTemplatePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export function expandFolderPath(folderPath: string): string {
const subPath = normalizedPath.substring(1, normalizedPath.length);
result = path.join(home, subPath);
} else {
result = path.resolve(workspace.rootPath, folderPath);
if (!workspace.workspaceFolders) {
throw new Error("No open folder found in workspace")
}
result = path.resolve(workspace.workspaceFolders[0].uri.fsPath, folderPath);
}

return result;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"module": "commonjs",
"target": "es6",
"outDir": "out",
"strict": true,
"lib": [
"es6"
],
Expand Down
Loading