-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathwriter.ts
More file actions
107 lines (87 loc) · 3.74 KB
/
Copy pathwriter.ts
File metadata and controls
107 lines (87 loc) · 3.74 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (C) 2016 Patrick Maué
//
// This file is part of vscode-journal.
//
// vscode-journal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// vscode-journal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with vscode-journal. If not, see <http://www.gnu.org/licenses/>.
//
'use strict';
import * as vscode from 'vscode';
import { DocumentOpener, IConfiguration, IFileSystem, IInject, ILogger } from '../../shared/model/index';
/**
* Anything which modifies the text documents goes here.
*
*/
export class Writer {
constructor(
private config: IConfiguration,
private logger: ILogger,
private inject: IInject,
private fs: IFileSystem,
private openDocument: DocumentOpener,
) { }
public async saveDocument(doc: vscode.TextDocument): Promise<vscode.TextDocument> {
await doc.save();
return doc;
}
/**
* Adds the given content at the start of text document
*/
public async writeHeader(doc: vscode.TextDocument, content: string): Promise<vscode.TextDocument> {
return this.inject.injectString(doc, content, new vscode.Position(0, 0));
}
/**
* Creates and saves a new file (with configured content) for a journal entry and returns the associated TextDocument
*
* @param {string} path
* @param {Date} date
* @returns {Promise<vscode.TextDocument>}
* @memberof Writer
*/
public async createEntryForPath(path: string, date: Date): Promise<vscode.TextDocument> {
this.logger.trace("Entering createEntryForPath() in ext/writer.ts for path: ", path);
const tpl = await this.config.getEntryTemplate(date);
const content = tpl.value || "";
return this.createSaveLoadTextDocument(path, content);
}
/**
* Creates and saves a new file (with configured content) for a weekly entry and returns the associated TextDocument
*
* @param {string} path
* @param {Number} week
* @returns {Promise<vscode.TextDocument>}
* @memberof Writer
*/
public async createWeeklyForPath(path: string, week: Number): Promise<vscode.TextDocument> {
this.logger.trace("Entering createWeeklyForPath() in ext/writer.ts for path: ", path);
const tpl = await this.config.getWeeklyTemplate(week);
const content = tpl.value || "";
return this.createSaveLoadTextDocument(path, content);
}
/**
* Creates a new file, adds the given content, saves it and opens it.
*
* @param {string} path The path in of the new file
* @param {string} content The preconfigured content of the new file
* @returns {Promise<vscode.TextDocument>} The new document associated with the file
*/
public async createSaveLoadTextDocument(path: string, content: string): Promise<vscode.TextDocument> {
this.logger.trace("Entering createSaveLoadTextDocument() in ext/writer.ts for path: ", path);
const fileUri = vscode.Uri.file(path);
const encoder = new TextEncoder();
await this.fs.writeFile(path, encoder.encode(content));
const doc = await this.openDocument(path);
this.logger.debug("Opened new file with name: ", doc.fileName);
return doc;
}
}