Skip to content

Commit

Permalink
feat: use templates when generating content
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajdzis committed Oct 6, 2019
1 parent 5206770 commit c95e17b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/commands/saveAsTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { createVariableTemplate } from '../variableTemplate/createVariableTempla
import { createDocument } from '../fileSystem/createDocument';
import { addExtensionToRecommendations } from '../fileSystem/addExtensionToRecommendations';

const DIRECTORY_FOR_TEMPLATES = 'awesome-tree-templates';
export const DIRECTORY_FOR_TEMPLATES = 'awesome-tree-templates';

function findWorkspacePath(searchFsPath:string): string | undefined {
export function findWorkspacePath(searchFsPath:string): string | undefined {
const { workspaceFolders } = vscode.workspace;
if(!workspaceFolders){
return;
Expand Down
24 changes: 23 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getPathTemplates } from './fileSystem/getPathTemplates';
import { getFilesContentAsTemplate } from './fileSystem/getFilesContentAsTemplate';
import { saveAsTemplate } from './commands/saveAsTemplate';
import { createDocument } from './fileSystem/createDocument';
import { getMatchingTemplate } from './savedTemplates/getMatchingTemplate';

export function activate(context: vscode.ExtensionContext) {
const settingProvider = vscode.workspace.getConfiguration('awesomeTree');
Expand Down Expand Up @@ -58,16 +59,37 @@ export function activate(context: vscode.ExtensionContext) {

uniquePathFiles.forEach(async (filePathTemplate) => {
const filePath: string = path.join(createdItemUri.fsPath, renderVariableTemplate(filePathTemplate, [infoAboutNewDirectory]));
const content = createFileContent(filePathTemplate, infoAboutSiblingDirectories, [infoAboutNewDirectory]);
const savedTemplate = getMatchingTemplate(filePath);

let content: string;
if (savedTemplate === null) {
content = createFileContent(filePathTemplate, infoAboutSiblingDirectories, [infoAboutNewDirectory]);
} else {
content = savedTemplate.map(line =>
renderVariableTemplate(line, [infoAboutNewDirectory])
).join('\n');
}

const textDocument = await createDocument(filePath, content);

vscode.window.showTextDocument(textDocument);
});


} else if(isEmptyFile(createdItemUri, outputChannel)) {
const savedTemplate = getMatchingTemplate(createdItemUri.fsPath);
const relativePath = getRelativePath(createdItemUri.fsPath);
const infoAboutNewFile = getInfoAboutPath(relativePath);
if (savedTemplate!==null) {

const content = savedTemplate.map(line =>
renderVariableTemplate(line, [infoAboutNewFile])
).join('\n');

createDocument(createdItemUri.fsPath, content);
return;
}

const parentDir = path.dirname(createdItemUri.fsPath);

const fileToSkip = path.basename(createdItemUri.fsPath);
Expand Down
54 changes: 54 additions & 0 deletions src/savedTemplates/getMatchingTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

import * as fs from 'fs';
import * as path from 'path';
import { DIRECTORY_FOR_TEMPLATES, findWorkspacePath } from '../commands/saveAsTemplate';
import { getRelativePath } from '../fileSystem/getRelativePath';
import { getInfoAboutPath } from '../fileInfo/getInfoAboutPath';
import { createVariableTemplate } from '../variableTemplate/createVariableTemplate';
import { compareVariableTemplate } from '../variableTemplate/compareVariableTemplate';

export function getMatchingTemplate (fileFsPath: string): null | string[] {

const workspacePath = findWorkspacePath(fileFsPath);
if (workspacePath === undefined) {
return null;
}
const relativePath = getRelativePath(fileFsPath);
const infoAboutNewFile = getInfoAboutPath(relativePath);
const templatePath = createVariableTemplate(relativePath, [infoAboutNewFile]);
const availableTemplates = getTemplatesDatabase(workspacePath);

for (let i = 0; i < availableTemplates.length; i++) {
const templateInfo = availableTemplates[i];
for (let j = 0; j < templateInfo.pathsTemplate.length; j++) {
const pathTemplate = templateInfo.pathsTemplate[j];
if(compareVariableTemplate(pathTemplate, templatePath)){
const templateId = templateInfo.templateId;
const templatePath = path.join(workspacePath, DIRECTORY_FOR_TEMPLATES, 'templates', `template-${templateId}.json` );
try {
return JSON.parse(fs.readFileSync(templatePath).toString()) as string[];
} catch {
return null;
}
}
}
}

return null;
}

interface TemplateInfo {
baseFilePath: string;
pathsTemplate: string[];
templateId: string;
}

function getTemplatesDatabase(workspacePath: string): TemplateInfo[] {

const templateDatabasePath = path.join(workspacePath, DIRECTORY_FOR_TEMPLATES, 'database-awesome.json' );
try {
return JSON.parse(fs.readFileSync(templateDatabasePath).toString()) as TemplateInfo[];
} catch {
return [];
}
}

0 comments on commit c95e17b

Please sign in to comment.