-
Notifications
You must be signed in to change notification settings - Fork 8
/
ReadmeWriter.ts
64 lines (49 loc) · 1.69 KB
/
ReadmeWriter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
export class ReadmeWriter {
constructor(private context: vscode.ExtensionContext) {}
async insertReadme() {
const items: string[] = ['Bot', 'Hackathon', 'Minimal', 'Standard'];
const selectedItem = await vscode.window.showQuickPick(items, {
placeHolder: 'Select readme pattern that you want'
});
if (selectedItem) {
this.createFile(selectedItem);
}
}
private createFile(selectedItem: string) {
const tempPath = this.context.asAbsolutePath(path.join('templates', `${selectedItem}.md`));
const buffer = fs.readFileSync(tempPath);
const folders = vscode.workspace.workspaceFolders;
let content = '';
console.log(`folders:${JSON.stringify(folders)}`);
console.log(`selectedItem:${selectedItem}`);
if (folders) {
const url = folders[0].uri;
const filePath = `${url.fsPath}\\README.md`;
console.log(`url:${filePath}`);
content = this.replaceContent(buffer.toString());
fs.writeFileSync(filePath, content);
}
}
getProjectTitle() {
const folders = vscode.workspace.workspaceFolders;
let projectTitle = 'Project Title';
if (folders) {
const url = folders[0].uri;
const pkgUrl = `${url.fsPath}\\package.json`;
if (fs.existsSync(pkgUrl)) {
const packageBuff = fs.readFileSync(pkgUrl);
const pkg = JSON.parse(packageBuff.toString());
projectTitle = pkg.name;
}
}
return projectTitle;
}
replaceContent(content: string) {
const projectTitle = this.getProjectTitle();
content = content.replace(/@Project Title@/gi, projectTitle);
return content;
}
}