Skip to content

Commit

Permalink
Speeds up loading commands
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz committed Nov 5, 2023
1 parent 2970c86 commit c24fcb5
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 298 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ coverage
docs/site
# generated commands info
commands.json
allCommands.json

# VS env folder
.vs/
Expand Down
12 changes: 11 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
"skipFiles": [
"<node_internals>/**"
]
}
},
{
"type": "pwa-node",
"request": "launch",
"name": "Debug current file",
"skipFiles": [
"<node_internals>/**"
],
"program": "${file}",
"args": []
},
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"type": "module",
"scripts": {
"build": "tsc -p . && node scripts/copy-files.js",
"build": "tsc -p . && node scripts/write-all-commands.js && node scripts/copy-files.js",
"watch": "tsc -w -p .",
"clean": "rimraf ./dist",
"test": "npm run test:version && npm run lint && npm run test:cov",
Expand Down
60 changes: 60 additions & 0 deletions scripts/write-all-commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import fs from 'fs';
import path from 'path';
import url, { pathToFileURL } from 'url';
import Command from '../dist/Command.js';
import { Cli } from '../dist/cli/Cli.js';
import { fsUtil } from '../dist/utils/fsUtil.js';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const commandsFolder = path.join(__dirname, '..', 'dist', 'm365');
const commandHelpFolder = path.join(commandsFolder, '..', '..', 'docs', 'docs', 'cmd');

async function loadAllCommands() {
const files = fsUtil.readdirR(commandsFolder);
const cli = Cli.getInstance();

await Promise.all(files.map(async (filePath) => {
const file = pathToFileURL(filePath).toString();
if (file.indexOf(`/commands/`) > -1 &&
file.indexOf(`/assets/`) < 0 &&
file.endsWith('.js') &&
!file.endsWith('.spec.js')) {

const command = await import(file);
if (command.default instanceof Command) {
const helpFilePath = path.relative(commandHelpFolder, getCommandHelpFilePath(command.default.name));
cli.commands.push(Cli.getCommandInfo(command.default, path.relative(commandsFolder, filePath), helpFilePath));
}
}
}));

cli.commands.forEach(c => {
delete c.command;
delete c.defaultProperties;
delete c.options;
});

fs.writeFileSync('allCommands.json', JSON.stringify(cli.commands));
}

function getCommandHelpFilePath(commandName) {
const commandNameWords = commandName.split(' ');
const pathChunks = [];

if (commandNameWords.length === 1) {
pathChunks.push(`${commandNameWords[0]}.mdx`);
}
else {
if (commandNameWords.length === 2) {
pathChunks.push(commandNameWords[0], `${commandNameWords.join('-')}.mdx`);
}
else {
pathChunks.push(commandNameWords[0], commandNameWords[1], commandNameWords.slice(1).join('-') + '.mdx');
}
}

const helpFilePath = path.join(commandHelpFolder, ...pathChunks);
return helpFilePath;
}

loadAllCommands();
12 changes: 5 additions & 7 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Cli, CommandOutput } from "./cli/Cli.js";
import path from 'path';

export function executeCommand(commandName: string, options: any, listener?: {
export async function executeCommand(commandName: string, options: any, listener?: {
stdout: (message: any) => void,
stderr: (message: any) => void,
}): Promise<CommandOutput> {
const cli = Cli.getInstance();
cli.commandsFolder = path.join(__dirname, 'm365');
cli.commands = [];
cli.loadCommandFromArgs(commandName.split(' '));
if (cli.commands.length !== 1) {
cli.loadAllCommandsInfo();
await cli.loadCommandFromArgs(commandName.split(' '));
if (!cli.commandToExecute) {
return Promise.reject(`Command not found: ${commandName}`);
}

return Cli.executeCommandWithOutput(cli.commands[0].command, { options: options ?? {} }, listener);
return Cli.executeCommandWithOutput(cli.commandToExecute.command, { options: options ?? {} }, listener);
}
12 changes: 6 additions & 6 deletions src/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('autocomplete', () => {

it('writes sh completion to disk', () => {
const writeFileSyncStub = sinon.stub(fs, 'writeFileSync').callsFake(() => { });
(cli as any).loadCommand(new SimpleCommand());
cli.commands.push(Cli.getCommandInfo(new SimpleCommand(), 'command.js', 'command.mdx'));
autocomplete.generateShCompletion();
assert(writeFileSyncStub.calledWith(path.join(__dirname, `..${path.sep}commands.json`), JSON.stringify({
cli: {
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('autocomplete', () => {
});

it('builds clink completion', () => {
(cli as any).loadCommand(new SimpleCommand());
cli.commands.push(Cli.getCommandInfo(new SimpleCommand(), 'command.js', 'command.mdx'));
const clink: string = autocomplete.getClinkCompletion();

assert.strictEqual(clink, [
Expand All @@ -167,7 +167,7 @@ describe('autocomplete', () => {
});

it('includes long options in clink completion', () => {
(cli as any).loadCommand(new CommandWithOptions());
cli.commands.push(Cli.getCommandInfo(new CommandWithOptions(), 'command.js', 'command.mdx'));
const clink: string = autocomplete.getClinkCompletion();

assert.strictEqual(clink, [
Expand All @@ -180,7 +180,7 @@ describe('autocomplete', () => {
});

it('includes short options in clink completion', () => {
(cli as any).loadCommand(new CommandWithOptions());
cli.commands.push(Cli.getCommandInfo(new CommandWithOptions(), 'command.js', 'command.mdx'));
const clink: string = autocomplete.getClinkCompletion();

assert.strictEqual(clink, [
Expand All @@ -193,7 +193,7 @@ describe('autocomplete', () => {
});

it('includes autocomplete for options in clink completion', () => {
(cli as any).loadCommand(new CommandWithOptions());
cli.commands.push(Cli.getCommandInfo(new CommandWithOptions(), 'command.js', 'command.mdx'));
const clink: string = autocomplete.getClinkCompletion();

assert.strictEqual(clink, [
Expand All @@ -206,7 +206,7 @@ describe('autocomplete', () => {
});

it('includes command alias in clink completion', () => {
(cli as any).loadCommand(new CommandWithAlias());
cli.commands.push(Cli.getCommandInfo(new CommandWithAlias(), 'command.js', 'command.mdx'));
const clink: string = autocomplete.getClinkCompletion();

assert.strictEqual(clink, [
Expand Down
Loading

0 comments on commit c24fcb5

Please sign in to comment.