Skip to content

Commit

Permalink
feat: create templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajdzis committed Sep 27, 2019
1 parent 2d363cc commit 699acb2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
7 changes: 7 additions & 0 deletions icons/awesome-template.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,26 @@
},
"main": "./out/extension.js",
"contributes": {
"commands": [],
"commands": [
{
"command": "extension.saveAsTemplate",
"title": "Save file as awesome template",
"category": "FILE",
"icon": {
"dark": "icons/awesome-template.svg",
"light": "icons/awesome-template.svg"
}
}
],
"menus": {
"editor/title": [
{
"command": "extension.saveAsTemplate",
"group": "navigation@1",
"when": "resourceScheme == file"
}
]
},
"configuration": {
"title": "Awesome Tree",
"properties": {
Expand Down
61 changes: 61 additions & 0 deletions src/commands/saveAsTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as fs from 'fs';
import * as vscode from 'vscode';
import * as path from 'path';
import { getRelativePath } from '../fileSystem/getRelativePath';
import { getInfoAboutPath } from '../fileInfo/getInfoAboutPath';
import { createVariableTemplate } from '../variableTemplate/createVariableTemplate';

const DIRECTORY_FOR_TEMPLATES = 'awesome-tree-templates';

function createDocument(filePath: string, content: string): Promise<vscode.TextDocument> {
return new Promise((resolve, reject) => {
ensureDirectoryExistence(filePath);
fs.writeFile(filePath, content, {}, async (err) => {
if(err){
return reject(err);
}
vscode.workspace.openTextDocument(filePath).then(resolve);
});
});
}

function ensureDirectoryExistence(filePath:string) {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}

export function saveAsTemplate(file: vscode.Uri){
console.log(file.fsPath);
const { workspaceFolders } = vscode.workspace;
if(!workspaceFolders){
return;
}
const relativePath = getRelativePath(file.fsPath);
const templatePath = path.join(workspaceFolders[0].uri.fsPath, DIRECTORY_FOR_TEMPLATES, path.dirname( relativePath ), 'files.json' );
const infoAboutNewFile = getInfoAboutPath(relativePath);
const lines = fs.readFileSync(file.fsPath).toString().split('\n');
const templateLines = lines.map(line => createVariableTemplate(line, [infoAboutNewFile]));

const template = {
path: relativePath,
pathTemplate: createVariableTemplate(relativePath, [infoAboutNewFile]),
template: templateLines
};

let currentSavedTemplates: Array<any>;

try {
currentSavedTemplates = JSON.parse(fs.readFileSync(templatePath).toString());
} catch (error) {
currentSavedTemplates = [];
}


currentSavedTemplates.push(template);

createDocument(templatePath, JSON.stringify(currentSavedTemplates, null, 4));
}
6 changes: 4 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { getRelativePath } from './fileSystem/getRelativePath';
import { getSiblingInfo, DirectoriesInfo } from './fileInfo/getSiblingInfo';
import { getPathTemplates } from './fileSystem/getPathTemplates';
import { getFilesContentAsTemplate } from './fileSystem/getFilesContentAsTemplate';
import { saveAsTemplate } from './commands/saveAsTemplate';

export function activate() {
export function activate(context: vscode.ExtensionContext) {
const settingProvider = vscode.workspace.getConfiguration('awesomeTree');
const fileSystemWatcher = vscode.workspace.createFileSystemWatcher('**/*',false, true, true);
const outputChannel = vscode.window.createOutputChannel('Awesome tree');


context.subscriptions.push(vscode.commands.registerCommand('extension.saveAsTemplate', saveAsTemplate));
outputChannel.appendLine('Listening for file changes started!');

fileSystemWatcher.onDidCreate(async(createdItemUri: vscode.Uri) => {
Expand Down
4 changes: 2 additions & 2 deletions src/extenstion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('extenstion', () => {

it('should start listen for create file', () => {
expect(vscode.workspace.createFileSystemWatcher).not.toHaveBeenCalled();
activate();
activate({} as vscode.ExtensionContext);

expect(vscode.workspace.createFileSystemWatcher).toHaveBeenCalled();
expect(mockWatcher.onDidCreate).toHaveBeenCalled();
Expand All @@ -47,7 +47,7 @@ describe('extenstion', () => {
});
});

activate();
activate({} as vscode.ExtensionContext);
});

});

0 comments on commit 699acb2

Please sign in to comment.