Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#4204] Fix remaining eslint warnings - Rest of botbuilder-dialogs libraries - botbuilder declarative #4238

Merged
Merged
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
14 changes: 10 additions & 4 deletions libraries/botbuilder-dialogs-declarative/src/pathUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ import { readdirSync, lstatSync } from 'fs';
export class PathUtil {
/**
* Check if a path is a directory
* @param path Path of the diretory
*
* @param path Path of the directory.
* @returns True if the path is a directory; false otherwise.
*/
static isDirectory(path: string): boolean {
return lstatSync(path).isDirectory();
}

/**
* Get sub folders in a directory
* @param path Path of root directory
*
* @param path Path of root directory.
* @returns Sub folders in the directory.
*/
static getDirectories(path: string): string[] {
return readdirSync(path)
Expand All @@ -33,8 +37,10 @@ export class PathUtil {

/**
* Get files in a directory
* @param path Path of root directory
* @param includeSubFolders Whether include its sub folders
*
* @param path Path of root directory.
* @param includeSubFolders Whether include its sub folders.
* @returns The files in the directory.
*/
static getFiles(path: string, includeSubFolders = true): string[] {
return readdirSync(path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable security/detect-non-literal-fs-filename */
/**
* @module botbuilder-dialogs-declarative
*/
Expand All @@ -15,17 +16,20 @@ import { Resource } from './resource';
export class FileResource extends Resource {
/**
* Initialize a new instance of the `FileResouce` class.
*
* @param path Path to file.
*/
constructor(path: string) {
super();
this._fullname = path;
// The id will be the file name, without the path
this._id = this._fullname.replace(/^.*[\\\/]/, '');
this._id = this._fullname.replace(/^.*[\\/]/, '');
}

/**
* Read text content of a file resource.
*
* @returns Read content text.
*/
readText(): string {
const filePath = this._fullname;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class FolderResourceProvider extends ResourceProvider {

/**
* Initializes a new instance of the `FolderResourceProvider` class.
*
* @param resourceExplorer Resource explorer.
* @param folder Root folder.
* @param includeSubFolders Whether include its sub folders.
Expand Down Expand Up @@ -53,6 +54,8 @@ export class FolderResourceProvider extends ResourceProvider {

/**
* Gets attached file watcher.
*
* @returns The attached file watcher.
*/
get watcher(): FSWatcher {
return this._watcher;
Expand Down Expand Up @@ -86,15 +89,19 @@ export class FolderResourceProvider extends ResourceProvider {

/**
* Gets resource by its id.
*
* @param id Resource id.
* @returns The resource by id.
*/
getResource(id: string): Resource {
return this._resources.has(id) ? this._resources.get(id) : undefined;
}

/**
* Gets resources by extension.
*
* @param extension Resource extension.
* @returns Collection of resources.
*/
getResources(extension: string): Resource[] {
extension = extension.startsWith('.') ? extension.toLowerCase() : `.${extension.toLowerCase()}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ export abstract class Resource {

/**
* Resource id.
*
* @returns The resource id.
*/
get id(): string {
return this._id;
}

/**
* The full path to the resource on disk
*
* @returns The full path to the resource.
*/
get fullName(): string {
return this._fullname;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class ResourceExplorer {
* @param {ResourceExplorerOptions} options The configuration options.
*/
constructor(options?: ResourceExplorerOptions);
/**
* @param providersOrOptions The list of [ResourceProvider](xref:botbuilder-dialogs-declarative.ResourceProvider) or configuration options to initialize the current instance.
*/
constructor(providersOrOptions: ResourceProvider[] | ResourceExplorerOptions = []) {
if (Array.isArray(providersOrOptions)) {
const providers: ResourceProvider[] = providersOrOptions;
Expand Down Expand Up @@ -245,9 +248,13 @@ export class ResourceExplorer {
*
* @template T Type of object.
* @param {Resource} resource Resource id to bind to.
* @returns {T} Type created from resource
* @returns {T} Type created from resource.
*/
loadType<T>(resource: Resource): T;
/**
* @param resourceOrId The resource or resource id to bind to.
* @returns Type created from resource.
*/
loadType<T>(resourceOrId: Resource | string): T {
this.registerComponentTypes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export abstract class ResourceProvider {

/**
* Initialize an instance of `ResourceProvider` class.
*
* @param resourceExplorer Resource explorer.
*/
constructor(resourceExplorer: ResourceExplorer) {
Expand All @@ -40,6 +41,7 @@ export abstract class ResourceProvider {

/**
* Event which is fired if any resource managed by the resource provider detects changes to the underlining resource.
*
* @param callback Callback function to be called when an event fired.
*/
set changed(callback: (event: ResourceChangeEvent, resources: Resource[]) => void) {
Expand All @@ -56,26 +58,32 @@ export abstract class ResourceProvider {

/**
* Gets the resource explorer.
*
* @returns The resource explorer.
*/
get resourceExplorer(): ResourceExplorer {
return this._resourceExplorer;
}

/**
* Gets the ID for this resource provider.
*
* @returns The ID for this resource provider.
*/
get id(): string {
return this._id;
}

/**
* Gets resource by id.
*
* @param id Resource id.
*/
abstract getResource(id: string): Resource;

/**
* Enumerate resources.
*
* @param extension Extension filter.
*/
abstract getResources(extension: string): Resource[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ const { FileResource } = require('../lib');
const assert = require('assert');
const path = require('path');

describe('FileResource', function() {
describe('FileResource', function () {
this.timeout(5000);

it('FileResource load existing file relative path', async () => {
const fileResource = new FileResource(`${ __dirname }/resources/foo.dialog`);
it('FileResource load existing file relative path', async function () {
const fileResource = new FileResource(`${__dirname}/resources/foo.dialog`);
assert.equal(fileResource.id, 'foo.dialog');
const text = fileResource.readText();
assert.equal(text[0], '{');
});

it('FileResource load existing file absolute path', async () => {
const absolutePath = path.resolve(`${ __dirname }/resources/foo.dialog`);
it('FileResource load existing file absolute path', async function () {
const absolutePath = path.resolve(`${__dirname}/resources/foo.dialog`);
const fileResource = new FileResource(absolutePath);
assert.equal(fileResource.id, 'foo.dialog');
const text = fileResource.readText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function assertResourceFound(explorer, id) {
const dialogs = explorer.getResources('dialog');
assert(
dialogs.some((dialog) => dialog.id == id),
`getResources('dialog') should return resources`
"getResources('dialog') should return resources"
);
}

Expand All @@ -29,7 +29,7 @@ function assertResourceNotFound(explorer, id) {
const dialogs = explorer.getResources('dialog');
assert(
dialogs.every((dialog) => dialog.id != id),
`getResouces('dialog') should not return resources`
"getResouces('dialog') should not return resources"
);
}

Expand All @@ -38,14 +38,14 @@ function assertResourceContents(explorer, id, contents) {
let text = resource.readText();
assert.strictEqual(text, contents, `getResource(${id}) contents not the same`);
const dialogs = explorer.getResources('dialog').filter((dialog) => dialog.id == id);
assert(dialogs.length == 1, `getResouces('dialog') should return resources`);
assert(dialogs.length == 1, "getResouces('dialog') should return resources");
const dialog = dialogs[0];
text = dialog.readText();
assert.strictEqual(text, contents, `getResouces('dialog') contents not the same`);
assert.strictEqual(text, contents, "getResouces('dialog') contents not the same");
}

describe('ResourceExplorer', function () {
it('add folders recursively', async () => {
it('add folders recursively', async function () {
const explorer = new ResourceExplorer();
explorer.addFolder(join(__dirname, 'resources'), true, false);
assertResourceType(explorer, 'lu');
Expand All @@ -55,7 +55,7 @@ describe('ResourceExplorer', function () {
assertResourceNotFound(explorer, 'bar.dialog');
});

it('add root folder only', async () => {
it('add root folder only', async function () {
const explorer = new ResourceExplorer();
explorer.addFolder(join(__dirname, 'resources'), false, false);
assertResourceFound(explorer, 'foo.dialog');
Expand All @@ -66,7 +66,7 @@ describe('ResourceExplorer', function () {
assert(resources.length, 'should list dialog resources in root folder');
});

it('add filtered folders', async () => {
it('add filtered folders', async function () {
const explorer = new ResourceExplorer([]);
explorer.addFolders(join(__dirname, 'resources'), ['TestFolder'], false);
assertResourceFound(explorer, 'foo.dialog');
Expand All @@ -78,7 +78,7 @@ describe('ResourceExplorer', function () {
assert(resources.length, 'should list dialog resources in unfilterd folders');
});

it('add new resource type', async () => {
it('add new resource type', async function () {
const explorer = new ResourceExplorer([]);
explorer.addFolders(join(__dirname, 'resources'), [], false);
let resources = explorer.getResources('txt');
Expand All @@ -88,15 +88,15 @@ describe('ResourceExplorer', function () {
assert(resources.length > 0, 'should list txt resources');
});

it('avoid adding duplicated resource folders', async () => {
it('avoid adding duplicated resource folders', async function () {
const explorer = new ResourceExplorer([]);
assert.throws(() => {
explorer.addFolder(join(__dirname, 'resources'), true, false);
explorer.addFolder(join(__dirname, 'resources'), true, false);
}, 'should throw if adding duplicated resource folders');
});

it('dialog id assignment', async () => {
it('dialog id assignment', async function () {
const services = new ServiceCollection({
declarativeTypes: [],
});
Expand All @@ -119,7 +119,7 @@ describe('ResourceExplorer', function () {
assert.strictEqual(dialog2.id, '1234567890', 'id in the .dialog file should be honored.');
});

it('event fired when new file added', async () => {
it('event fired when new file added', async function () {
const testPath = join(__dirname, 'resources/TestFolder/file_to_be_added.dialog');

// clean the test file
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('ResourceExplorer', function () {
resourceProvider.watcher.close();
});

it('event fired when file changed', async () => {
it('event fired when file changed', async function () {
const testPath = join(__dirname, 'resources/TestFolder/file_to_be_changed.dialog');

// clean the test file
Expand Down Expand Up @@ -189,7 +189,7 @@ describe('ResourceExplorer', function () {
resourceProvider.watcher.close();
});

it('event fired when file removed', async () => {
it('event fired when file removed', async function () {
const testPath = join(__dirname, 'resources/TestFolder/file_to_be_removed.dialog');

// clean the test file
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('ResourceExplorer', function () {
resourceProvider.watcher.close();
});

it('watch file changes', async () => {
it('watch file changes', async function () {
const testPath = join(__dirname, 'resources/TestFolder/foobar.dialog');

// clean the test file
Expand Down Expand Up @@ -264,7 +264,7 @@ describe('ResourceExplorer', function () {
resourceProvider.watcher.close();
});

it('cycle reference', async () => {
it('cycle reference', async function () {
const services = new ServiceCollection({
declarativeTypes: [],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ const assert = require('assert');
describe('ResourceProvider', function () {
this.timeout(5000);

it('FolderResourceProvider load specific folder with dialog extension', async () => {
it('FolderResourceProvider load specific folder with dialog extension', async function () {
const resourceExplorer = new ResourceExplorer();
const resourceProvider = new FolderResourceProvider(resourceExplorer, `${ __dirname }/resources`, true, false);
const resourceProvider = new FolderResourceProvider(resourceExplorer, `${__dirname}/resources`, true, false);
const dialogResources = resourceProvider.getResources('dialog');
assert(dialogResources.length);
});

it('FolderResourceProvider load specific folder with lg extension', async () => {
it('FolderResourceProvider load specific folder with lg extension', async function () {
const resourceExplorer = new ResourceExplorer();
const resourceProvider = new FolderResourceProvider(resourceExplorer, `${ __dirname }/resources`, true, false);
const resourceProvider = new FolderResourceProvider(resourceExplorer, `${__dirname}/resources`, true, false);
const lgResources = resourceProvider.getResources('lg');
assert(lgResources.length);
});

});