Skip to content

Commit 54eba29

Browse files
authored
Merge pull request MicrosoftDocs#2114 from Fogolan/tools
Add CLI interface
2 parents cc9ff75 + 3e4c1aa commit 54eba29

File tree

9 files changed

+166
-25
lines changed

9 files changed

+166
-25
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"name": "Office cmdlet updater",
3+
"description": "Update documentation for modules in this repository",
4+
"version": "0.0.3",
25
"platyPS": {
36
"credentials": {
47
"login": "",
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class CliController {
2+
constructor(
3+
cliService,
4+
cmdletService,
5+
markdownController,
6+
powerShellService
7+
) {
8+
this.cliService = cliService;
9+
this.cmdletService = cmdletService;
10+
this.markdownController = markdownController;
11+
this.powerShellService = powerShellService;
12+
}
13+
start(argv) {
14+
try {
15+
this.startCli(argv);
16+
} catch (e) {
17+
this.powerShellService.dispose();
18+
console.error(e.message);
19+
}
20+
}
21+
22+
startCli(argv) {
23+
this.cliService.addOption({
24+
option: '-m --module <module>',
25+
description: 'update documentation for module',
26+
defaultValue: 'all',
27+
action: (cli) => {
28+
const { module } = cli;
29+
30+
if (module === 'all') {
31+
return;
32+
}
33+
34+
this.cmdletService.ensureModuleExist(module);
35+
}
36+
});
37+
38+
this.cliService.addOption({
39+
option: '-c --cmdlet <cmdet>',
40+
description: 'update documentation for cmdlet in module',
41+
action: (cli) => {
42+
const { module, cmdlet } = cli;
43+
44+
if (module === 'all' && cmdlet) {
45+
throw new Error('You must specify a module for cmdlet.');
46+
}
47+
}
48+
});
49+
50+
this.cliService.start(argv, async (cli) => {
51+
const { module, cmdlet } = cli;
52+
53+
await this.markdownController.updateMarkdown({
54+
moduleName: module,
55+
cmdlet
56+
});
57+
});
58+
}
59+
}
60+
61+
module.exports = CliController;

tools/office-cmdlet-updater/controllers/markdown.controller.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,32 @@ class MarkdownController {
99
this.config = config;
1010
}
1111

12-
async updateMarkdown() {
12+
async updateMarkdown({ moduleName, cmdlet }) {
1313
let err;
1414
const { docs } = this.config.get('platyPS');
1515

16-
docs.forEach(async (doc) => {
17-
if (!(await fs.pathExists(doc.path))) {
18-
throw new Error(powerShellErrors.DOC_PATH_DOESNT_EXIST);
19-
}
16+
docs.filter((doc) => this._filterModules(doc, moduleName)).forEach(
17+
async (doc) => {
18+
if (!(await fs.pathExists(doc.path))) {
19+
throw new Error(powerShellErrors.DOC_PATH_DOESNT_EXIST);
20+
}
2021

21-
[, err] = await of(this.markdownService.updateMd(doc));
22+
[, err] = await of(this.markdownService.updateMd(doc, cmdlet));
2223

23-
if (err) {
24-
this.powerShellService.dispose();
25-
throw new Error(err);
24+
if (err) {
25+
this.powerShellService.dispose();
26+
console.error(err.message);
27+
}
2628
}
27-
});
29+
);
30+
}
31+
32+
_filterModules(doc, moduleName) {
33+
if (moduleName === 'all') {
34+
return true;
35+
}
36+
37+
return doc.name === moduleName;
2838
}
2939
}
3040

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const MarkdownController = require('../controllers/markdown.controller');
99
const db = require('../db')();
1010
const CmdletDependenciesService = require('../services/cmdlet.dependencies.service');
1111
const FsService = require('../services/fs.service');
12+
const CliService = require('../services/cli.service');
13+
const CliController = require('../controllers/cli.controller');
14+
const CmdletService = require('../services/cmdlet.service');
1215

1316
module.exports = () => {
1417
const container = awilix.createContainer({
@@ -22,6 +25,7 @@ module.exports = () => {
2225
});
2326

2427
container.register({
28+
cliService: awilix.asClass(CliService).singleton(),
2529
mailNotificationService: awilix
2630
.asClass(MailNotificationService)
2731
.singleton(),
@@ -32,11 +36,13 @@ module.exports = () => {
3236
.asClass(CmdletDependenciesService)
3337
.singleton(),
3438
fsService: awilix.asClass(FsService).singleton(),
35-
markdownService: awilix.asClass(MarkdownService).singleton()
36-
});
39+
markdownService: awilix.asClass(MarkdownService).singleton(),
40+
cmdletService: awilix.asClass(CmdletService).singleton()
41+
});
3742

3843
container.register({
39-
markdownController: awilix.asClass(MarkdownController).singleton()
44+
markdownController: awilix.asClass(MarkdownController).singleton(),
45+
cliController: awilix.asClass(CliController).singleton()
4046
});
4147

4248
return container;

tools/office-cmdlet-updater/index.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
const container = require('./helpers/di.container')();
22

3-
(async function() {
4-
try {
5-
const markdownController = container.resolve('markdownController');
3+
const cliController = container.resolve('cliController');
64

7-
await markdownController.updateMarkdown();
8-
} catch (e) {
9-
console.log(e);
10-
}
11-
})();
5+
cliController.start(process.argv);

tools/office-cmdlet-updater/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"await-of": "^1.1.2",
2828
"awilix": "^3.0.9",
2929
"better-queue": "^3.8.10",
30+
"commander": "^2.19.0",
3031
"config": "^2.0.1",
3132
"fs-extra": "^7.0.0",
3233
"lowdb": "^1.0.0",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const cli = require('commander');
2+
3+
class CliService {
4+
constructor(config) {
5+
this.config = config;
6+
this.cli = cli;
7+
8+
const { description, version } = config;
9+
10+
this.cli.version(version, '-v, --version').description(description);
11+
}
12+
13+
addOption({
14+
option,
15+
description = '',
16+
defaultValue = '',
17+
action = () => {}
18+
}) {
19+
this.cli
20+
.option(option, description, defaultValue)
21+
.action(() => action(this.cli));
22+
}
23+
24+
start(argv, cb = () => {}) {
25+
this.cli.parse(argv);
26+
27+
cb(this.cli);
28+
}
29+
}
30+
31+
module.exports = CliService;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class CmdletService {
2+
constructor(config) {
3+
this.config = config;
4+
}
5+
6+
ensureModuleExist(moduleName) {
7+
const modules = this.config.get('platyPS.docs');
8+
9+
const isExist = modules.find(({ name }) => name === moduleName);
10+
11+
if (!isExist) {
12+
throw new Error(`Module with name "${moduleName}" didn't exist`);
13+
}
14+
}
15+
}
16+
17+
module.exports = CmdletService;

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ class MarkdownService {
3131
this.installedDependencies = [];
3232
}
3333

34-
async updateMd(doc) {
35-
return this.addMdFilesInQueue(doc);
34+
async updateMd(doc, cmdlet) {
35+
return this.addMdFilesInQueue(doc, cmdlet);
3636
}
3737

38-
async addMdFilesInQueue(doc) {
39-
const mdFiles = await this.fsService.getModuleFiles(doc);
38+
async addMdFilesInQueue(doc, cmdlet) {
39+
const mdFiles = (await this.fsService.getModuleFiles(doc)).filter(
40+
(file) => this._filterCmdlets(file, cmdlet)
41+
);
42+
43+
if (!mdFiles.length) {
44+
throw new Error(
45+
`Can't find cmdlet "${cmdlet}" in module "${doc.name}"`
46+
);
47+
}
4048

4149
mdFiles.forEach((file) => {
4250
this.queue
@@ -46,6 +54,16 @@ class MarkdownService {
4654
});
4755
}
4856

57+
_filterCmdlets(mdPath, cmdletName) {
58+
if (!cmdletName) {
59+
return true;
60+
}
61+
62+
const mdExt = '.md';
63+
64+
return mdPath.endsWith(`${cmdletName}${mdExt}`);
65+
}
66+
4967
async processQueue({ file, doc }, cb) {
5068
let result, err;
5169

0 commit comments

Comments
 (0)