Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions src/core/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,32 +282,28 @@ instead of "${definition.name}"`);
let targetPath: string | undefined;
let targetModule: CommandModule | undefined;

let contexts: SubcommandSearchContext[] = await v.map(
this.roots,
async root => {
let path: string | undefined = Path.join(root.path, 'default.js');
path = (await existsFile(path)) ? path : undefined;
let contexts: SubcommandSearchContext[] = this.roots.map(root => {
let path = existsFile(root.path);

let module: CommandModule | undefined;
let module: CommandModule | undefined;

if (path) {
module = require(path) as CommandModule;
if (path) {
module = require(path) as CommandModule;

if (module.default || !targetPath) {
targetPath = path;
targetModule = module;
}
if (module.default || !targetPath) {
targetPath = path;
targetModule = module;
}
}

return {
label: root.label,
name: this.name,
searchBase: root.path,
path,
module,
};
},
);
return {
label: root.label,
name: this.name,
searchBase: root.path,
path,
module,
};
});

for (let i = argsIndex; i < argv.length && contexts.length; i++) {
let possibleCommandName = argv[i];
Expand Down Expand Up @@ -415,18 +411,12 @@ instead of "${definition.name}"`);
private static async findEntryBySearchBase(
searchBase: string,
): Promise<CommandEntry | undefined> {
let possiblePaths = [
`${searchBase}.js`,
Path.join(searchBase, 'default.js'),
];

for (let possiblePath of possiblePaths) {
if (await existsFile(possiblePath)) {
return {
path: possiblePath,
module: require(possiblePath) as CommandModule,
};
}
const srcFile = existsFile(searchBase);
if (srcFile !== undefined) {
return {
path: srcFile,
module: require(srcFile) as CommandModule,
};
}

if (await existsDir(searchBase)) {
Expand Down
19 changes: 12 additions & 7 deletions src/core/command/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {CLI, CommandModule} from '../cli';
import {
buildTableOutput,
existsDir,
existsFile,
indent,
safeStat,
} from '../../internal-util';
Expand Down Expand Up @@ -102,18 +103,22 @@ export class HelpInfo implements Printable {
let names = await v.call<string[]>(FS.readdir, dir);

for (let name of names) {
let path = Path.join(dir, name);
let path: string | undefined = Path.join(dir, name);
let stats = await safeStat(path);

if (stats!.isFile()) {
if (name === 'default.js' || Path.extname(path) !== '.js') {
const ext = Path.extname(path);
name = Path.basename(path).replace(ext, '');

if (
name === 'default' ||
(ext !== '.js' && ext !== '.ts') ||
path.endsWith('.d.ts')
) {
continue;
}

name = Path.basename(name, '.js');
} else {
path = Path.join(path, 'default.js');
stats = await safeStat(path);
path = await existsFile(path);
}

let existingItem = helpItemMap.get(name);
Expand All @@ -130,7 +135,7 @@ export class HelpInfo implements Printable {
let commandConstructor: CommandClass | undefined;
let brief: string | undefined;

if (stats) {
if (path) {
let module = require(path) as CommandModule;
commandConstructor = module.default;
brief =
Expand Down
23 changes: 20 additions & 3 deletions src/internal-util/fs.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import * as FS from 'fs';
import * as Path from 'path';

import * as v from 'villa';

export async function safeStat(path: string): Promise<FS.Stats | undefined> {
return v.call(FS.stat, path).catch(v.bear);
}

export async function existsFile(path: string): Promise<boolean> {
let stats = await safeStat(path);
return !!stats && stats.isFile();
export function existsFile(
path: string,
filename: string = 'default',
): string | undefined {
const checkPaths = [Path.join(path, filename)];

if (filename === 'default') {
checkPaths.unshift(path);
}

for (const check of checkPaths) {
try {
return require.resolve(check);
} catch {
continue;
}
}

return undefined;
}

export async function existsDir(path: string): Promise<boolean> {
Expand Down