Skip to content

Commit 287cb16

Browse files
refine Open Data File/Package checks and errors (#53 & #104)
to cover local and remote data files and and supported data formats, including listing tabular data resources from datapackage.json descriptor files
1 parent b1c516d commit 287cb16

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/commands/openDataFile.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,48 @@ export async function registerOpenDataFileCommand(context: ExtensionContext) {
3030
if (dataFileUrl && dataFileUrl !== undefined && dataFileUrl.length > 0) {
3131
// create data file Uri
3232
const dataFileUri: Uri = Uri.parse(fileUtils.convertToGitHubContentUrl(dataFileUrl));
33-
34-
// check supported data files
33+
const isLocalDataFile: boolean = dataFileUrl?.startsWith('file:///');
34+
const isRemoteDataFile: boolean = dataFileUrl?.startsWith('https://');
3535
const fileExtension: string = path.extname(dataFileUrl);
36-
if (!dataFileUrl?.startsWith('file:///') && !dataFileUrl?.startsWith('https://')) {
36+
const isFileDirectory: boolean = (fileExtension.length <= 0);
37+
38+
// check supported data files and call the corresponding tabular data view command
39+
if ((!isLocalDataFile && !isRemoteDataFile) ||
40+
(isRemoteDataFile && isFileDirectory)) {
3741
window.showErrorMessage(
38-
`Tabular Data Viewer requires a valid \`file:///\` or \`https://\` data file Url \
39-
to display Table View. Invalid data document Url: \`${dataFileUrl}\`.`);
42+
`Tabular Data Viewer requires a valid \`file:///\` or \`https://\` Data File Url \
43+
to display a Table View. Invalid Data Url: \`${dataFileUrl}\`.`);
4044
}
41-
else if (dataFileUrl?.startsWith('file:///') && !fs.existsSync(dataFileUri.fsPath)) {
45+
else if (isLocalDataFile && !fs.existsSync(dataFileUri.fsPath)) {
4246
window.showErrorMessage(
4347
`Unable to locate requested data file: \`${dataFileUrl}\`.`);
4448
}
45-
else if (fileUtils.supportedDataFormats.includes(fileExtension)) {
46-
// open table view for requested remote or local data file
47-
commands.executeCommand(ViewCommands.viewTable, dataFileUri);
48-
}
4949
else if (dataFileUrl.endsWith('datapackage.json')) {
5050
// load and display data package resource list
5151
commands.executeCommand(ViewCommands.listDataResources, dataFileUri);
5252
}
53-
else if (fileExtension.length === 0 && // data directory
53+
else if (fileUtils.supportedDataFormats.includes(fileExtension)) {
54+
// open table view for a remote or local data file
55+
commands.executeCommand(ViewCommands.viewTable, dataFileUri);
56+
}
57+
else if (isLocalDataFile && isFileDirectory &&
5458
fs.existsSync(path.join(dataFileUri.fsPath, 'datapackage.json'))) {
55-
// show data resources for a data package from directory
59+
// show data resources for a data package from local file directory
5660
commands.executeCommand(
5761
ViewCommands.listDataResources, Uri.joinPath(dataFileUri, 'datapackage.json'));
5862
}
59-
else if (fileExtension.length === 0) {
63+
else if (isLocalDataFile && isFileDirectory) {
6064
// must be a data directory without the datapackage.json descriptor
6165
window.showErrorMessage(
62-
`Tabular Data Viewer doesn't support data directory views yet.\
66+
`Tabular Data Viewer doesn't support local data directory views yet.\
6367
Use View Table menu option from VSCode File Explorer to open tabular data file in Table View.
6468
Requested data directory: \`${dataFileUrl}\`.`);
6569
}
66-
else {
70+
else if (!fileUtils.supportedDataFormats.includes(fileExtension)) {
6771
// unsupported data format
6872
window.showErrorMessage(
6973
`Tabular Data Viewer doesn't support ${fileExtension} data files.\
70-
Unable to show Table View for data file: \`${dataFileUrl}\`.`);
74+
Unable to show Table View for a data file: \`${dataFileUrl}\`.`);
7175
}
7276
}
7377
});

0 commit comments

Comments
 (0)