Skip to content

Commit bb20056

Browse files
authored
Merge pull request MicrosoftDocs#1826 from Fogolan/tools
Tools
2 parents baac5f1 + 14d599a commit bb20056

File tree

7 files changed

+169
-64
lines changed

7 files changed

+169
-64
lines changed

tools/office-cmdlet-updater/config/default.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
"docs": [
44
{
55
"name": "teams",
6-
"path": "..\\..\\teams\\teams-ps\\teams"
6+
"path": "..\\..\\teams\\teams-ps\\teams",
7+
"metaTags": []
8+
},
9+
{
10+
"name": "skype",
11+
"path": "..\\..\\skype\\skype-ps\\skype",
12+
"metaTags": ["Skype for Business Online"]
713
}
814
],
9-
"ignoreFiles": []
15+
"ignoreFiles": [
16+
"..\\..\\teams\\teams-ps\\teams\\teams.md"
17+
]
1018
},
1119
"sendgrid": {
1220
"sendMailNotification": false,

tools/office-cmdlet-updater/constants/commands.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ module.exports = {
22
GET_TEAM: 'Get-Team',
33
INSTALL_MICROSOFT_TEAM: 'Install-Module MicrosoftTeams -SkipPublisherCheck',
44
CONNECT_MICROSOFT_TEAM: 'Connect-MicrosoftTeams',
5-
INSTALL_PLATYPS: 'Install-Module -Name platyPS -Scope CurrentUser',
5+
INSTALL_PLATYPS: 'Install-Module -Name platyPS -SkipPublisherCheck -Scope CurrentUser',
66
IMPORT_PLATYPS: 'Import-Module platyPS',
7-
UPDATE_MARKDOWN: 'Update-MarkdownHelp -Path "{}" -LogPath "{}"'
7+
UPDATE_MARKDOWN: 'Update-MarkdownHelp -Path "{}" -LogPath "{}"',
8+
SKYPE_SET_POLICY: 'Set-ExecutionPolicy RemoteSigned ',
9+
SKYPE_INSTALL_MODULE: 'Import-Module "C:\\\\Program Files\\\\Common Files\\\\Skype for Business Online\\\\Modules\\\\SkypeOnlineConnector\\\\SkypeOnlineConnector.psd1" -Force',
10+
SKYPE_GET_CRED: '$cred = Get-Credential',
11+
SKYPE_CREATE_SESSION: '$session = New-CsOnlineSession -Credential $cred -Verbose',
12+
SKYPE_IMPORT_SESSION: 'Import-PSSession -Session $session'
813
};

tools/office-cmdlet-updater/db/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ const Memory = require('lowdb/adapters/Memory');
44
module.exports = () => {
55
const db = low(new Memory());
66

7-
db.defaults({ logs: [], errors: [] }).write();
7+
db.defaults({
8+
logs: new Map(),
9+
errors: new Map(),
10+
tempFolders: new Map()
11+
}).write();
812

913
return db;
1014
};

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ class LogParseService {
3939
}
4040

4141
parseAll() {
42-
const logs = this.logStoreService.getAllLogs();
43-
const logObjs = [];
42+
const parseLogs = []
43+
const logs = [...this.logStoreService.getAllLogs().values()].reduce(
44+
(acc, cur) => [...acc, ...cur],
45+
[]
46+
);
4447

4548
logs.forEach((log) => {
4649
const logObj = this.parse(log);
4750

48-
logObjs.push(logObj);
51+
parseLogs.push(logObj);
4952
});
5053

51-
const msg = this.generateMailText(logObjs);
54+
const msg = this.generateMailText(parseLogs);
5255

5356
if (msg) {
5457
const outputText = msg.replace(new RegExp('<br>', 'g'), '\n');

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

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ class LogStoreService {
77
this.db = db;
88
}
99

10-
addLog(log) {
11-
this._add('logs', log);
10+
addLog(log, name) {
11+
this._addMap('logs', log, name);
1212
}
1313

14-
addError(err) {
15-
this._add('errors', err);
14+
addError(err, name) {
15+
this._addMap('errors', err, name);
16+
}
17+
18+
addTempFolder(name, tempFolderName) {
19+
this._addMap('tempFolders', name, tempFolderName);
1620
}
1721

1822
getAllLogs() {
@@ -23,27 +27,45 @@ class LogStoreService {
2327
return this._getAll('errors');
2428
}
2529

26-
_add(collection, obj) {
27-
this.db
28-
.get(collection)
29-
.push(obj)
30-
.write();
30+
getAllTempFolders() {
31+
return this._getAll('tempFolders');
32+
}
33+
34+
_addMap(collection, obj, name) {
35+
const map = this.db.get(collection).value();
36+
37+
if (!map.has(name)) {
38+
map.set(name, []);
39+
}
40+
41+
const arr = [...map.get(name), obj];
42+
43+
map.set(name, arr);
44+
45+
this.db.set(collection, map).write();
3146
}
3247

3348
_getAll(collection) {
3449
return this.db.get(collection).value();
3550
}
3651

3752
saveInFs() {
38-
const fileName = `${this._getLogName()}.log`;
39-
const filePath = path.join('.local', 'logs', fileName);
53+
const logs = this.getAllLogs();
54+
55+
for (const key of logs.keys()) {
56+
const fileName = `${this._getLogName()}.log`;
57+
const filePath = path.join('.local', 'logs', key, fileName);
58+
59+
fs.ensureFileSync(filePath);
4060

41-
fs.ensureFileSync(filePath);
61+
const logs = this.getAllLogs().get(key);
62+
const logsContent = logs ? logs.join('\n') : '';
4263

43-
const logs = this.getAllLogs().join('\n') || '';
44-
const errors = this.getAllErrors().join('\n') || '';
64+
const errors = this.getAllErrors().get(key);
65+
const errorContent = errors ? errors.join('\n') : '';
4566

46-
return fs.writeFile(filePath, logs + errors);
67+
fs.writeFile(filePath, logsContent + errorContent);
68+
}
4769
}
4870

4971
_getLogName() {

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

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,83 @@ class MarkdownService {
2323

2424
this.queue = new Queue(this.processQueue);
2525
this.queue.on('empty', this.queueEmptyHandler);
26-
27-
this.tempFolderPath = null;
2826
}
2927

3028
async updateMd(doc) {
31-
const { path } = doc;
32-
33-
this.tempFolderPath = `${path}\\${shortId()}`;
34-
35-
await this.addMdFilesInQueue(path);
29+
return this.addMdFilesInQueue(doc);
3630
}
3731

38-
async addMdFilesInQueue(folderPath) {
32+
async addMdFilesInQueue(doc) {
3933
const mdExt = '.md';
4034
const { ignoreFiles } = this.config.get('platyPS');
4135
const ignoreAbsolutePathsArr = ignoreFiles.map((f) => path.resolve(f));
36+
const metaTagRegex = /(?<=applicable: ).+/gmu;
4237

4338
const isFileIgnore = (fileName) => {
4439
const absoluteFilePath = path.resolve(fileName);
4540

4641
return ignoreAbsolutePathsArr.includes(absoluteFilePath);
4742
};
4843

49-
const mdFiles = (await fs.readdir(folderPath))
50-
.map((f) => path.resolve(folderPath, f))
51-
.filter((fn) => fn.endsWith(mdExt) && !isFileIgnore(fn));
44+
const isContainTag = (filePath) => {
45+
if (!doc.metaTags.length) {
46+
return true;
47+
}
48+
49+
const groups = fs
50+
.readFileSync(filePath, 'utf8')
51+
.toString()
52+
.match(metaTagRegex);
53+
54+
if (!groups) {
55+
return false;
56+
}
57+
58+
for (const metaTag of doc.metaTags) {
59+
if (groups[0].indexOf(metaTag) !== -1) {
60+
return true;
61+
}
62+
}
63+
64+
return false;
65+
};
66+
67+
const mdFiles = (await fs.readdir(doc.path))
68+
.map((f) => path.resolve(doc.path, f))
69+
.filter(
70+
(fn) =>
71+
fn.endsWith(mdExt) && !isFileIgnore(fn) && isContainTag(fn)
72+
);
5273

5374
mdFiles.forEach((file) => {
5475
this.queue
55-
.push(file)
76+
.push({ file, doc })
5677
.on('failed', this.queueFailedHandler)
5778
.on('finish', this.queueFinishHandler);
5879
});
5980
}
6081

61-
async processQueue(input, cb) {
82+
async processQueue({ file, doc }, cb) {
6283
let result, err;
63-
const logFilePath = `${this.tempFolderPath}\\${shortId()}.log`;
6484

65-
[result, err] = await of(
66-
this.copyMdInTempFolder(input, this.tempFolderPath)
67-
);
85+
const getTempFolderName = () => {
86+
let tempFolders = this.logStoreService.getAllTempFolders();
87+
88+
if (!tempFolders.has(doc.name)) {
89+
const tempFolderPath = `${doc.path}\\${shortId()}`;
90+
91+
this.logStoreService.addTempFolder(tempFolderPath, doc.name);
92+
93+
tempFolders = this.logStoreService.getAllTempFolders();
94+
}
95+
96+
return tempFolders.get(doc.name);
97+
};
98+
99+
const [tempFolderPath] = getTempFolderName();
100+
const logFilePath = `${tempFolderPath}\\${shortId()}.log`;
101+
102+
[result, err] = await of(this.copyMdInTempFolder(file, tempFolderPath));
68103

69104
if (err) {
70105
return cb(err, null);
@@ -77,7 +112,7 @@ class MarkdownService {
77112
if (err) {
78113
console.error(err);
79114

80-
this.logStoreService.addError(err);
115+
this.logStoreService.addError(err, doc.name);
81116

82117
return cb(null, '');
83118
}
@@ -92,34 +127,36 @@ class MarkdownService {
92127
return cb(err, null);
93128
}
94129

95-
return cb(null, result);
130+
return cb(null, { result, doc });
96131
}
97132

98133
async queueFailedHandler(err) {
99134
throw new Error(err);
100135
}
101136

102-
queueFinishHandler(result) {
137+
queueFinishHandler({ result, doc }) {
103138
if (!result) {
104139
return;
105140
}
106-
this.logStoreService.addLog(result);
141+
this.logStoreService.addLog(result, doc.name);
107142
}
108143

109144
async queueEmptyHandler() {
110145
this.powerShellService.dispose();
111146

112-
const [, err] = await of(this.logStoreService.saveInFs());
147+
this.logStoreService.saveInFs();
113148

114-
if (err) {
115-
throw new Error(err);
116-
}
149+
const tempFolders = [
150+
...this.logStoreService.getAllTempFolders().values()
151+
].map((path) => path[0]);
117152

118-
if (fs.pathExists(this.tempFolderPath)) {
119-
const [, fsError] = await of(this.clearTempFolder());
153+
for (const path of tempFolders) {
154+
if (fs.pathExists(path)) {
155+
const [, fsError] = await of(fs.remove(path));
120156

121-
if (fsError) {
122-
throw new Error(fsError);
157+
if (fsError) {
158+
throw new Error(fsError);
159+
}
123160
}
124161
}
125162

@@ -158,10 +195,6 @@ class MarkdownService {
158195

159196
return (await fs.readFile(logFilePath)).toString();
160197
}
161-
162-
clearTempFolder() {
163-
return fs.remove(this.tempFolderPath);
164-
}
165198
}
166199

167200
module.exports = MarkdownService;

tools/office-cmdlet-updater/services/power.shell.service.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PowerShellService {
2525

2626
result += output;
2727

28-
[output, err] = await of(this.authIfNeeded());
28+
[output, err] = await of(this.preInstallTeams());
2929

3030
if (err) {
3131
await this.dispose();
@@ -34,17 +34,47 @@ class PowerShellService {
3434

3535
result += output;
3636

37+
[output, err] = await of(this.preInstallSkype());
38+
39+
if (err) {
40+
await this.dispose();
41+
throw new Error(err);
42+
}
43+
44+
result += output;
45+
46+
return result;
47+
}
48+
49+
async preInstallSkype() {
50+
let result;
51+
52+
//result += await this._invokeCommand(commands.SKYPE_SET_POLICY);
53+
54+
result += await this._invokeCommand(commands.SKYPE_INSTALL_MODULE);
55+
56+
result += await this._invokeCommand(commands.SKYPE_GET_CRED);
57+
58+
result += await this._invokeCommand(commands.SKYPE_CREATE_SESSION);
59+
60+
result += await this._invokeCommand(commands.SKYPE_IMPORT_SESSION);
61+
3762
return result;
3863
}
3964

40-
async authIfNeeded() {
65+
async _invokeCommand(command) {
66+
const [output, err] = await of(this.invokeCommand(command));
67+
68+
if (err) {
69+
throw new Error(err);
70+
}
71+
72+
return output;
73+
}
74+
75+
async preInstallTeams() {
4176
let output, err, result;
4277

43-
// const checkAuthCommand = commands.GET_TEAM;
44-
//
45-
// [output, err] = await of(this.invokeCommand(checkAuthCommand));
46-
//
47-
// result += output;
4878
// TODO: check if user already auth
4979

5080
const installModule = commands.INSTALL_MICROSOFT_TEAM;

0 commit comments

Comments
 (0)