11#!/usr/bin/env node
22
3- const fs = require ( 'fs' ) ;
4- const path = require ( 'path' ) ;
3+ const fs = require ( 'node: fs' ) ;
4+ const path = require ( 'node: path' ) ;
55const phyx = require ( '..' ) ;
66
77/*
@@ -10,21 +10,29 @@ const phyx = require('..');
1010
1111// Read command line arguments.
1212const argv = require ( 'yargs' )
13- . usage ( "$0 [files or directories to convert into OWL ontologies]" )
14- . describe ( 'max-internal-specifiers' , 'The maximum number of internal specifiers (phylorefs with more than this number will be ignored)' )
13+ . usage ( '$0 [files or directories to convert into OWL ontologies]' )
14+ . describe (
15+ 'max-internal-specifiers' ,
16+ 'The maximum number of internal specifiers (phylorefs with more than this number will be ignored)' ,
17+ )
1518 . default ( 'max-internal-specifiers' , 8 )
16- . describe ( 'max-external-specifiers' , 'The maximum number of external specifiers (phylorefs with more than this number will be ignored)' )
19+ . describe (
20+ 'max-external-specifiers' ,
21+ 'The maximum number of external specifiers (phylorefs with more than this number will be ignored)' ,
22+ )
1723 . default ( 'max-external-specifiers' , 8 )
1824 . describe ( 'base-iri' , 'The base IRI to use for the input files' )
1925 . help ( )
20- . alias ( 'h' , 'help' )
21- . argv
26+ . alias ( 'h' , 'help' ) . argv ;
2227
2328/*
2429 * Get a list of all files in a directory. We will recurse into directories and choose
2530 * files that meet the criteria in the function `check(filename) => boolean`.
2631 */
27- function getFilesInDir ( filePath , check = ( filename => filename . toLowerCase ( ) . endsWith ( ".json" ) ) ) {
32+ function getFilesInDir (
33+ filePath ,
34+ check = filename => filename . toLowerCase ( ) . endsWith ( '.json' ) ,
35+ ) {
2836 // console.debug(`Processing file: ${filePath}`)
2937 if ( ! fs . existsSync ( filePath ) ) return [ ] ;
3038
@@ -41,7 +49,8 @@ function getFilesInDir(filePath, check = (filename => filename.toLowerCase().end
4149 } else if ( lsync . isDirectory ( ) ) {
4250 // If `path` is a directory, recurse into every file in that directory.
4351 const files = fs . readdirSync ( filePath ) ;
44- return files . map ( file => getFilesInDir ( path . join ( filePath , file ) , check ) )
52+ return files
53+ . map ( file => getFilesInDir ( path . join ( filePath , file ) , check ) )
4554 . reduce ( ( acc , curr ) => acc . concat ( curr ) , [ ] ) ;
4655 } else {
4756 // console.debug(`${filePath} is neither a file nor a directory; skipping.`);
@@ -53,7 +62,7 @@ function getFilesInDir(filePath, check = (filename => filename.toLowerCase().end
5362// At this point, we convert directories into lists of files.
5463const filenames = argv . _ ;
5564if ( filenames . length === 0 ) {
56- console . error ( " No input files provided." ) ;
65+ console . error ( ' No input files provided.' ) ;
5766 process . exit ( 1 ) ;
5867}
5968
@@ -63,7 +72,9 @@ const files = filenames
6372// console.debug(`Files to process: ${files.join(", ")}`);
6473
6574if ( files . length === 0 ) {
66- console . error ( `Input files do not exist or consist of directories that do not contain JSON files: ${ filenames . join ( ', ' ) } ` ) ;
75+ console . error (
76+ `Input files do not exist or consist of directories that do not contain JSON files: ${ filenames . join ( ', ' ) } ` ,
77+ ) ;
6778 process . exit ( 1 ) ;
6879}
6980
@@ -73,20 +84,20 @@ if (files.length === 0) {
7384 * filename: either by replacing '.json' with '.owl', or by concatenating
7485 * '.owl' at the end.
7586 */
76- function convertFileToOWL ( filename , argOutputFilename = "" ) {
87+ function convertFileToOWL ( filename , argOutputFilename = '' ) {
7788 // console.debug(`Starting with ${filename}.`);
7889 let outputFilename ;
79- if ( argOutputFilename != "" ) {
90+ if ( argOutputFilename !== '' ) {
8091 outputFilename = argOutputFilename ;
81- } else if ( filename . toLowerCase ( ) . endsWith ( " .json" ) ) {
82- outputFilename = filename . substring ( 0 , filename . length - 5 ) + " .owl" ;
92+ } else if ( filename . toLowerCase ( ) . endsWith ( ' .json' ) ) {
93+ outputFilename = ` ${ filename . substring ( 0 , filename . length - 5 ) } .owl` ;
8394 } else {
84- outputFilename = filename + " .owl" ;
95+ outputFilename = ` ${ filename } .owl` ;
8596 }
8697
8798 try {
8899 // Parse the input file into JSON.
89- let phyxContent = JSON . parse ( fs . readFileSync ( filename ) ) ;
100+ const phyxContent = JSON . parse ( fs . readFileSync ( filename ) ) ;
90101
91102 // Remove any phylorefs that have too many specifiers.
92103 const phylorefCount = ( phyxContent . phylorefs || [ ] ) . length ;
@@ -95,10 +106,14 @@ function convertFileToOWL(filename, argOutputFilename = "") {
95106 const internalSpecifiersCount = wrappedPhyloref . internalSpecifiers . length ;
96107 const externalSpecifiersCount = wrappedPhyloref . externalSpecifiers . length ;
97108 if ( internalSpecifiersCount > argv . maxInternalSpecifiers ) {
98- console . warn ( `Phyloreference ${ wrappedPhyloref . label } was skipped, since it has ${ internalSpecifiersCount } internal specifiers.` ) ;
109+ console . warn (
110+ `Phyloreference ${ wrappedPhyloref . label } was skipped, since it has ${ internalSpecifiersCount } internal specifiers.` ,
111+ ) ;
99112 return false ;
100113 } else if ( externalSpecifiersCount > argv . maxExternalSpecifiers ) {
101- console . warn ( `Phyloreference ${ wrappedPhyloref . label } was skipped, since it has ${ externalSpecifiersCount } external specifiers.` ) ;
114+ console . warn (
115+ `Phyloreference ${ wrappedPhyloref . label } was skipped, since it has ${ externalSpecifiersCount } external specifiers.` ,
116+ ) ;
102117 return false ;
103118 }
104119 return true ;
@@ -107,41 +122,47 @@ function convertFileToOWL(filename, argOutputFilename = "") {
107122
108123 // Convert the Phyx file into JSON-LD.
109124 const wrappedPhyx = new phyx . PhyxWrapper ( phyxContent ) ;
110- wrappedPhyx . toRDF ( argv . baseIri , path . dirname ( filename ) )
125+ wrappedPhyx
126+ . toRDF ( argv . baseIri , path . dirname ( filename ) )
111127 . then ( nquads => {
112- fs . writeFileSync (
113- outputFilename ,
114- nquads
115- ) ;
128+ fs . writeFileSync ( outputFilename , nquads ) ;
116129 } )
117130 . catch ( err => {
118131 throw err ;
119132 } ) ;
120133
121134 // Report on whether any phyloreferences were converted.
122- if ( filteredPhylorefs . length == 0 ) {
123- console . warn ( `No phyloreferences in ${ filename } were converted to ${ outputFilename } , as they were all filtered out.` ) ;
124- return false ;
135+ if ( filteredPhylorefs . length === 0 ) {
136+ console . warn (
137+ `No phyloreferences in ${ filename } were converted to ${ outputFilename } , as they were all filtered out.` ,
138+ ) ;
139+ return false ;
125140 } else if ( phylorefCount > filteredPhylorefs . length ) {
126- console . warn ( `Only ${ filteredPhylorefs . length } out of ${ phylorefCount } were converted from ${ filename } to ${ outputFilename } .` ) ;
127- return true ;
141+ console . warn (
142+ `Only ${ filteredPhylorefs . length } out of ${ phylorefCount } were converted from ${ filename } to ${ outputFilename } .` ,
143+ ) ;
144+ return true ;
128145 } else {
129- console . info ( `Converted ${ filename } to ${ outputFilename } .` ) ;
130- return true ;
146+ console . info ( `Converted ${ filename } to ${ outputFilename } .` ) ;
147+ return true ;
131148 }
132149
133150 return true ;
134- } catch ( e ) {
135- console . error ( `Could not convert ${ filename } to ${ outputFilename } : ${ e } at ${ e . stack } ` ) ;
136- console . error ( `` )
151+ } catch ( e ) {
152+ console . error (
153+ `Could not convert ${ filename } to ${ outputFilename } : ${ e } at ${ e . stack } ` ,
154+ ) ;
155+ console . error ( `` ) ;
137156 }
138157 return false ;
139158}
140159
141160// Count and report all the successes in converting files to OWL.
142161const successes = files . map ( file => convertFileToOWL ( file ) ) ;
143- if ( successes . every ( x => x ) ) {
162+ if ( successes . every ( x => x ) ) {
144163 console . log ( `${ successes . length } files converted successfully.` ) ;
145164} else {
146- console . log ( `Errors occurred; ${ successes . filter ( x => x ) . length } files converted successfully, ${ successes . filter ( x => ! x ) . length } files failed.` ) ;
165+ console . log (
166+ `Errors occurred; ${ successes . filter ( x => x ) . length } files converted successfully, ${ successes . filter ( x => ! x ) . length } files failed.` ,
167+ ) ;
147168}
0 commit comments