Skip to content

Commit 36d977c

Browse files
committed
Refactor markdown service
1 parent be4119a commit 36d977c

File tree

4 files changed

+160
-137
lines changed

4 files changed

+160
-137
lines changed

tools/office-cmdlet-updater/helpers/di.container.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const MailNotificationService = require('../services/mail.notification.service')
88
const MarkdownController = require('../controllers/markdown.controller');
99
const db = require('../db')();
1010
const CmdletDependenciesService = require('../services/cmdlet.dependencies.service');
11+
const FsService = require('../services/fs.service');
1112

1213
module.exports = () => {
1314
const container = awilix.createContainer({
@@ -30,6 +31,7 @@ module.exports = () => {
3031
cmdletDependenciesService: awilix
3132
.asClass(CmdletDependenciesService)
3233
.singleton(),
34+
fsService: awilix.asClass(FsService).singleton(),
3335
markdownService: awilix.asClass(MarkdownService).singleton()
3436
});
3537

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
const path = require('path');
2+
const fs = require('fs-extra');
3+
const shortId = require('shortid');
4+
5+
class FsService {
6+
constructor(config, logStoreService) {
7+
this.config = config;
8+
this.logStoreService = logStoreService;
9+
10+
this.getTempFolderPath = this.getTempFolderPath.bind(this);
11+
this.removeTempFolders = this.removeTempFolders.bind(this);
12+
}
13+
14+
// region getModuleFiles
15+
16+
async getModuleFiles({ path, metaTags }) {
17+
const { ignoreFiles } = this.config.get('platyPS');
18+
19+
const mdFiles = await this._getMdFiles(path);
20+
21+
return mdFiles.filter(
22+
(fileName) =>
23+
!this._isFileIgnore({ fileName, ignoreFiles }) &&
24+
this._isContainsTag({ filePath: fileName, metaTags })
25+
);
26+
}
27+
28+
async _getMdFiles(folderPath) {
29+
const mdExt = '.md';
30+
31+
const allFiles = await this._getFolderFiles(folderPath);
32+
33+
return allFiles.filter((file) => file.endsWith(mdExt));
34+
}
35+
36+
async _getFolderFiles(folderPath) {
37+
const files = await fs.readdir(folderPath);
38+
39+
return await files.reduce(async (promiseResult, filePath) => {
40+
const result = await promiseResult;
41+
const absolute = path.resolve(folderPath, filePath);
42+
43+
const fileStat = await fs.stat(absolute);
44+
45+
if (fileStat.isDirectory()) {
46+
const subDirFiles = await this._getFolderFiles(absolute);
47+
48+
return [...result, ...subDirFiles];
49+
}
50+
51+
return [...result, absolute];
52+
}, []);
53+
}
54+
55+
_isFileIgnore({ fileName, ignoreFiles }) {
56+
const ignoreAbsolutePathsArr = ignoreFiles.map((f) => path.resolve(f));
57+
58+
const absoluteFilePath = path.resolve(fileName);
59+
60+
return ignoreAbsolutePathsArr.includes(absoluteFilePath);
61+
}
62+
63+
_isContainsTag({ filePath, metaTags }) {
64+
if (!metaTags.length) {
65+
return true;
66+
}
67+
68+
const metaTagRegex = /(?<=applicable: ).+/gmu;
69+
70+
const groups = fs
71+
.readFileSync(filePath, 'utf8')
72+
.toString()
73+
.match(metaTagRegex);
74+
75+
if (!groups) {
76+
return false;
77+
}
78+
79+
for (const metaTag of metaTags) {
80+
if (groups[0].indexOf(metaTag) !== -1) {
81+
return true;
82+
}
83+
}
84+
85+
return false;
86+
}
87+
88+
// endregion
89+
90+
getTempFolderPath({ name, path }) {
91+
let tempFolders = this.logStoreService.getAllTempFolders();
92+
93+
if (!tempFolders.has(name)) {
94+
const tempFolderPath = `${path}\\${shortId()}`;
95+
96+
this.logStoreService.addTempFolder(tempFolderPath, name);
97+
98+
tempFolders = this.logStoreService.getAllTempFolders();
99+
}
100+
101+
return tempFolders.get(name);
102+
}
103+
104+
async copyMdInTempFolder(srcFilePath, tempFolderPath) {
105+
const fileName = path.basename(srcFilePath);
106+
const distFilePath = `${tempFolderPath}\\${fileName}`;
107+
108+
await fs.ensureDir(tempFolderPath);
109+
110+
await fs.copy(srcFilePath, distFilePath);
111+
112+
return distFilePath;
113+
}
114+
115+
async getFileContent(logFilePath) {
116+
await fs.ensureFile(logFilePath);
117+
118+
return (await fs.readFile(logFilePath)).toString();
119+
}
120+
121+
async removeTempFolders() {
122+
const { getAllTempFolders } = this.logStoreService;
123+
124+
const tempFolders = [...getAllTempFolders().values()];
125+
126+
const tempFoldersPath = tempFolders.map((path) => path[0]);
127+
128+
for (const path of tempFoldersPath) {
129+
if (fs.pathExists(path)) {
130+
fs.remove(path);
131+
}
132+
}
133+
}
134+
}
135+
136+
module.exports = FsService;

tools/office-cmdlet-updater/services/log.store.service.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const moment = require('moment');
55
class LogStoreService {
66
constructor(db) {
77
this.db = db;
8+
9+
this.getAllTempFolders = this.getAllTempFolders.bind(this);
810
}
911

1012
addLog(log, name) {

tools/office-cmdlet-updater/services/markdown.service.js

Lines changed: 20 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
const fs = require('fs-extra');
2-
const path = require('path');
31
const Queue = require('better-queue');
42
const of = require('await-of').default;
53
const shortId = require('shortid');
6-
const { markdownErrors } = require('../constants/errors');
74

85
class MarkdownService {
96
constructor(
107
powerShellService,
118
logStoreService,
129
logParseService,
1310
cmdletDependenciesService,
11+
fsService,
1412
config
1513
) {
1614
this.logStoreService = logStoreService;
17-
this.powerShellService = powerShellService;
15+
this.pss = powerShellService;
1816
this.logParseService = logParseService;
1917
this.config = config;
2018
this.cds = cmdletDependenciesService;
19+
this.fsService = fsService;
2120

2221
this.processQueue = this.processQueue.bind(this);
23-
this.copyMdInTempFolder = this.copyMdInTempFolder.bind(this);
24-
this.getLogFileContent = this.getLogFileContent.bind(this);
2522
this.queueFinishHandler = this.queueFinishHandler.bind(this);
2623
this.updateMd = this.updateMd.bind(this);
2724
this.addMdFilesInQueue = this.addMdFilesInQueue.bind(this);
@@ -39,42 +36,7 @@ class MarkdownService {
3936
}
4037

4138
async addMdFilesInQueue(doc) {
42-
const { ignoreFiles } = this.config.get('platyPS');
43-
const ignoreAbsolutePathsArr = ignoreFiles.map((f) => path.resolve(f));
44-
const metaTagRegex = /(?<=applicable: ).+/gmu;
45-
46-
const isFileIgnore = (fileName) => {
47-
const absoluteFilePath = path.resolve(fileName);
48-
49-
return ignoreAbsolutePathsArr.includes(absoluteFilePath);
50-
};
51-
52-
const isContainTag = (filePath) => {
53-
if (!doc.metaTags.length) {
54-
return true;
55-
}
56-
57-
const groups = fs
58-
.readFileSync(filePath, 'utf8')
59-
.toString()
60-
.match(metaTagRegex);
61-
62-
if (!groups) {
63-
return false;
64-
}
65-
66-
for (const metaTag of doc.metaTags) {
67-
if (groups[0].indexOf(metaTag) !== -1) {
68-
return true;
69-
}
70-
}
71-
72-
return false;
73-
};
74-
75-
const mdFiles = (await this._getMdFiles(doc.path)).filter(
76-
(fn) => !isFileIgnore(fn) && isContainTag(fn)
77-
);
39+
const mdFiles = await this.fsService.getModuleFiles(doc);
7840

7941
mdFiles.forEach((file) => {
8042
this.queue
@@ -84,70 +46,28 @@ class MarkdownService {
8446
});
8547
}
8648

87-
async _getMdFiles(path) {
88-
const mdExt = '.md';
89-
90-
const allFiles = await this._getFolderFiles(path);
91-
92-
return allFiles.filter((file) => file.endsWith(mdExt));
93-
}
94-
95-
async _getFolderFiles(folderPath) {
96-
const files = await fs.readdir(folderPath);
97-
98-
return await files.reduce(async (promiseResult, filePath) => {
99-
const result = await promiseResult;
100-
const absolute = path.resolve(folderPath, filePath);
101-
102-
const fileStat = await fs.stat(absolute);
103-
104-
if (fileStat.isDirectory()) {
105-
const subDirFiles = await this._getFolderFiles(absolute);
106-
107-
return [...result, ...subDirFiles];
108-
}
109-
110-
return [...result, absolute];
111-
}, []);
112-
}
113-
11449
async processQueue({ file, doc }, cb) {
11550
let result, err;
11651

117-
const { name } = doc;
118-
119-
if (!this.installedDependencies.includes(name)) {
120-
this.installedDependencies.push(name);
121-
122-
await this.cds.installDependencies({ cmdletName: name });
123-
}
124-
125-
const getTempFolderName = () => {
126-
let tempFolders = this.logStoreService.getAllTempFolders();
127-
128-
if (!tempFolders.has(doc.name)) {
129-
const tempFolderPath = `${doc.path}\\${shortId()}`;
52+
const {
53+
copyMdInTempFolder,
54+
getTempFolderPath,
55+
getFileContent
56+
} = this.fsService;
13057

131-
this.logStoreService.addTempFolder(tempFolderPath, doc.name);
58+
await this._installDependenceIfNeeded(doc);
13259

133-
tempFolders = this.logStoreService.getAllTempFolders();
134-
}
60+
const [tempFolderPath] = getTempFolderPath(doc);
13561

136-
return tempFolders.get(doc.name);
137-
};
138-
139-
const [tempFolderPath] = getTempFolderName();
14062
const logFilePath = `${tempFolderPath}\\${shortId()}.log`;
14163

142-
[result, err] = await of(this.copyMdInTempFolder(file, tempFolderPath));
64+
[result, err] = await of(copyMdInTempFolder(file, tempFolderPath));
14365

14466
if (err) {
14567
return cb(err, null);
14668
}
14769

148-
[result, err] = await of(
149-
this.powerShellService.updateMarkdown(result, logFilePath)
150-
);
70+
[result, err] = await of(this.pss.updateMarkdown(result, logFilePath));
15171

15272
if (err) {
15373
console.error(err);
@@ -159,7 +79,7 @@ class MarkdownService {
15979

16080
console.log(result); // print powershell command result
16181

162-
[result, err] = await of(this.getLogFileContent(logFilePath));
82+
[result, err] = await of(getFileContent(logFilePath));
16383

16484
console.log(result); // print update file log
16585

@@ -182,58 +102,21 @@ class MarkdownService {
182102
}
183103

184104
async queueEmptyHandler() {
185-
this.powerShellService.dispose();
105+
this.pss.dispose();
186106

187107
this.logStoreService.saveInFs();
188108

189-
const tempFolders = [
190-
...this.logStoreService.getAllTempFolders().values()
191-
].map((path) => path[0]);
192-
193-
for (const path of tempFolders) {
194-
if (fs.pathExists(path)) {
195-
const [, fsError] = await of(fs.remove(path));
196-
197-
if (fsError) {
198-
throw new Error(fsError);
199-
}
200-
}
201-
}
109+
await this.fsService.removeTempFolders();
202110

203111
this.logParseService.parseAll();
204112
}
205113

206-
async copyMdInTempFolder(srcFilePath, tempFolderPath) {
207-
let err;
208-
209-
const fileName = path.basename(srcFilePath);
210-
const distFilePath = `${tempFolderPath}\\${fileName}`;
211-
212-
[, err] = await of(fs.ensureDir(tempFolderPath));
213-
214-
if (err) {
215-
throw new Error(markdownErrors.CANT_CREATE_TEMP_FOLDER);
216-
}
217-
218-
[, err] = await of(fs.copy(srcFilePath, distFilePath));
219-
220-
if (err) {
221-
throw new Error(markdownErrors.CANT_COPY_MD_FILE);
222-
}
223-
224-
return distFilePath;
225-
}
226-
227-
async getLogFileContent(logFilePath) {
228-
let err, result;
229-
230-
[result, err] = await of(fs.ensureFile(logFilePath));
114+
async _installDependenceIfNeeded({ name }) {
115+
if (!this.installedDependencies.includes(name)) {
116+
this.installedDependencies.push(name);
231117

232-
if (result || err) {
233-
throw new Error(markdownErrors.CANT_OPEN_LOG_FILE);
118+
await this.cds.installDependencies({ cmdletName: name });
234119
}
235-
236-
return (await fs.readFile(logFilePath)).toString();
237120
}
238121
}
239122

0 commit comments

Comments
 (0)