-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
176 additions
and
27 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as fs from 'fs'; | ||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import * as uuid from 'uuid/v4'; | ||
import { getRelativePath } from '../fileSystem/getRelativePath'; | ||
import { getInfoAboutPath } from '../fileInfo/getInfoAboutPath'; | ||
import { createVariableTemplate } from '../variableTemplate/createVariableTemplate'; | ||
import { createDocument } from '../fileSystem/createDocument'; | ||
import { addExtensionToRecommendations } from '../fileSystem/addExtensionToRecommendations'; | ||
|
||
const DIRECTORY_FOR_TEMPLATES = 'awesome-tree-templates'; | ||
|
||
function findWorkspacePath(searchFsPath:string): string | undefined { | ||
const { workspaceFolders } = vscode.workspace; | ||
if(!workspaceFolders){ | ||
return; | ||
} | ||
for (let i = 0; i < workspaceFolders.length; i++) { | ||
const {fsPath} = workspaceFolders[i].uri; | ||
if(searchFsPath.indexOf(fsPath) === 0){ | ||
return fsPath; | ||
} | ||
} | ||
} | ||
|
||
export function saveAsTemplate(file: vscode.Uri) { | ||
const workspacePath = findWorkspacePath(file.fsPath); | ||
if (workspacePath === undefined) { | ||
vscode.window.showWarningMessage(`Can't find workspace directory for file: '${file.fsPath}'`); | ||
return; | ||
} | ||
|
||
const relativePath = getRelativePath(file.fsPath); | ||
const templateId = uuid(); | ||
const templatePath = path.join(workspacePath, DIRECTORY_FOR_TEMPLATES, 'templates', `template-${templateId}.json` ); | ||
const templateDatabasePath = path.join(workspacePath, DIRECTORY_FOR_TEMPLATES, 'database-awesome.json' ); | ||
const infoAboutNewFile = getInfoAboutPath(relativePath); | ||
const lines = fs.readFileSync(file.fsPath).toString().split('\n'); | ||
const templateLines = lines.map(line => createVariableTemplate(line, [infoAboutNewFile])); | ||
|
||
const template = { | ||
baseFilePath: relativePath, | ||
pathsTemplate: [ | ||
createVariableTemplate(relativePath, [infoAboutNewFile]) | ||
], | ||
templateId | ||
}; | ||
|
||
let currentSavedTemplates: Array<any>; | ||
|
||
try { | ||
currentSavedTemplates = JSON.parse(fs.readFileSync(templateDatabasePath).toString()); | ||
} catch (error) { | ||
currentSavedTemplates = []; | ||
} | ||
|
||
currentSavedTemplates.push(template); | ||
|
||
Promise.all([ | ||
createDocument(templatePath, JSON.stringify(templateLines, null, 4)), | ||
createDocument(templateDatabasePath, JSON.stringify(currentSavedTemplates, null, 4)) | ||
]).then(() => { | ||
vscode.window.showInformationMessage('Saved success!'); | ||
addExtensionToRecommendations(workspacePath); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as process from 'child_process'; | ||
|
||
export function addExtensionToRecommendations(workspaceDirectory: string){ | ||
try { | ||
const extensionsPath = path.join(workspaceDirectory, '.vscode', 'extensions.json'); | ||
let fileData; | ||
if (fs.existsSync(extensionsPath)) { | ||
fileData = JSON.parse(fs.readFileSync(extensionsPath).toString()); | ||
if (!Array.isArray(fileData.recommendations)) { | ||
fileData.recommendations = []; | ||
} | ||
if (fileData.recommendations.includes('bajdzis.awesome-tree')) { | ||
return; | ||
} | ||
} else { | ||
fileData = { | ||
recommendations: [] | ||
}; | ||
} | ||
fileData.recommendations.push('bajdzis.awesome-tree'); | ||
|
||
fs.writeFileSync(extensionsPath, JSON.stringify(fileData, null, 2)); | ||
|
||
process.spawn('git add ".vscode/extensions.json" -f'); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as fs from 'fs'; | ||
import * as vscode from 'vscode'; | ||
import { ensureDirectoryExistence } from './ensureDirectoryExistence'; | ||
|
||
export 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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
export function ensureDirectoryExistence(filePath:string) { | ||
const dirname = path.dirname(filePath); | ||
if (fs.existsSync(dirname)) { | ||
return true; | ||
} | ||
ensureDirectoryExistence(dirname); | ||
fs.mkdirSync(dirname); | ||
} |