11import * as vscode from "vscode" ;
2- import * as path from "path" ;
3- import * as fs from "fs" ;
42import {
5- CONFIG_EXCLUDE ,
6- CONFIG_INCLUDE_FOLDERS ,
7- CONFIG_RELATIVE_EXCLUDE ,
8- CONFIG_SEMIS ,
9- CONFIG_QUOTE ,
103 EXTENSION_NAME ,
11- CONFIG_MESSAGE ,
124 EXTENSION_KEY ,
135 CONFIG_FOLDERS ,
14- CONFIG_NAMED_EXPORTS ,
6+ BarrelFiles ,
157} from "../constants" ;
168import {
179 clearWildcard ,
10+ fileExists ,
1811 getAbsoluteFolderPath ,
1912 getFileContents ,
2013 getRelativeFolderPath ,
2114 parseFileForNamedExports ,
2215 parseWinPath ,
16+ writeFile ,
2317} from "../helpers" ;
2418import { Logger } from "../helpers/logger" ;
25-
26- interface FileOrFolderToExport {
27- name : string ;
28- type : "file" | "folder" ;
29- }
19+ import { join , parse } from "path" ;
20+ import { getConfig } from "../helpers/getConfig" ;
21+ import { FileOrFolderToExport } from "../models" ;
3022
3123export class ExportAll {
32- public static barrelFiles = [ "index.ts" , "index.tsx" ] ;
33-
3424 public static async start ( crntUri : vscode . Uri , runSilent : boolean = true ) {
3525 const uri = vscode . Uri . file ( clearWildcard ( parseWinPath ( crntUri . fsPath ) ) ) ;
3626
3727 try {
38- const config = vscode . workspace . getConfiguration ( EXTENSION_KEY ) ;
39- const excludeFiles : string | undefined = config . get ( CONFIG_EXCLUDE ) ;
40- const excludeRel : string | undefined = config . get (
41- CONFIG_RELATIVE_EXCLUDE
42- ) ;
43- const includeFolders : boolean | undefined = config . get (
44- CONFIG_INCLUDE_FOLDERS
45- ) ;
46- const namedExports : boolean | undefined = config . get ( CONFIG_NAMED_EXPORTS ) ;
47- const semis : boolean | undefined = config . get ( CONFIG_SEMIS ) ;
48- const quote : '"' | "'" = config . get ( CONFIG_QUOTE ) ?? "'" ;
49- const message : string | string [ ] | undefined = config . get <
50- string | string [ ]
51- > ( CONFIG_MESSAGE ) ;
28+ const {
29+ excludeFiles,
30+ excludeRel,
31+ includeFolders,
32+ message,
33+ namedExports,
34+ quote,
35+ semis,
36+ } = getConfig ( ) ;
5237
5338 const folderPath = uri . fsPath ;
54- const files = fs . readdirSync ( folderPath ) ;
39+ const files = await vscode . workspace . fs . readDirectory (
40+ vscode . Uri . parse ( folderPath )
41+ ) ;
5542 let filesToExport : FileOrFolderToExport [ ] = [ ] ;
5643
5744 if ( files && files . length > 0 ) {
58- for ( const file of files ) {
59- const absPath = path . join ( folderPath , file ) ;
45+ for ( const [ file ] of files ) {
46+ const absPath = join ( folderPath , file ) ;
6047 let include = false ;
6148 let relPath = file ;
6249
6350 // Include all TS files except for the index
6451 if (
6552 ( file . endsWith ( ".ts" ) || file . endsWith ( ".tsx" ) ) &&
66- this . barrelFiles . indexOf ( file . toLowerCase ( ) ) === - 1
53+ BarrelFiles . indexOf ( file . toLowerCase ( ) ) === - 1
6754 ) {
6855 relPath = getRelativeFolderPath ( absPath ) ;
6956 include = true ;
@@ -72,10 +59,13 @@ export class ExportAll {
7259 // Check if folders should be included
7360 if ( includeFolders ) {
7461 // Only allow folder which contain an index file
75- if ( fs . lstatSync ( absPath ) . isDirectory ( ) ) {
76- for ( const indexFile of this . barrelFiles ) {
77- const indexPath = path . join ( absPath , indexFile ) ;
78- if ( fs . existsSync ( indexPath ) ) {
62+ const stat = await vscode . workspace . fs . stat (
63+ vscode . Uri . file ( absPath )
64+ ) ;
65+ if ( stat . type === vscode . FileType . Directory ) {
66+ for ( const indexFile of BarrelFiles ) {
67+ const indexPath = join ( absPath , indexFile ) ;
68+ if ( await fileExists ( indexPath ) ) {
7969 relPath = getRelativeFolderPath ( absPath ) ;
8070 include = true ;
8171 break ;
@@ -114,9 +104,13 @@ export class ExportAll {
114104 // Add the file/folder to the array
115105 if ( include ) {
116106 try {
107+ const stat = await vscode . workspace . fs . stat (
108+ vscode . Uri . file ( absPath )
109+ ) ;
117110 filesToExport . push ( {
118111 name : file ,
119- type : fs . statSync ( absPath ) . isDirectory ( ) ? "folder" : "file" ,
112+ type :
113+ stat . type === vscode . FileType . Directory ? "folder" : "file" ,
120114 } ) ;
121115 } catch ( ex ) {
122116 // Ignore
@@ -127,33 +121,45 @@ export class ExportAll {
127121
128122 // Check if there are still files after the filter
129123 if ( filesToExport && filesToExport . length > 0 ) {
130- let output = filesToExport . map ( ( item ) => {
124+ const output : string [ ] = [ ] ;
125+
126+ for ( const item of filesToExport ) {
131127 const fileWithoutExtension =
132- item . type === "folder" ? item . name : path . parse ( item . name ) . name ;
128+ item . type === "folder" ? item . name : parse ( item . name ) . name ;
133129 if ( namedExports ) {
134- const filePath = path . join ( uri . fsPath , item . name ) ;
135- const fileContents = getFileContents ( filePath ) ;
136- const { namedExports, typeExports } = parseFileForNamedExports ( fileContents ) ;
130+ const filePath = join ( uri . fsPath , item . name ) ;
131+ const fileContents = await getFileContents ( filePath ) ;
132+ const { namedExports, typeExports } = parseFileForNamedExports (
133+ fileContents || ""
134+ ) ;
137135
138- const namedExportsStr = namedExports . filter ( Boolean ) . join ( ', ' ) ;
139- const typeExportsStr = typeExports . filter ( Boolean ) . join ( ', ' ) ;
140- let exportStr = '' ;
136+ const namedExportsStr = namedExports . filter ( Boolean ) . join ( ", " ) ;
137+ const typeExportsStr = typeExports . filter ( Boolean ) . join ( ", " ) ;
138+ let exportStr = "" ;
141139 if ( namedExportsStr ) {
142- exportStr += `export { ${ namedExportsStr } } from ${ quote } ./${ fileWithoutExtension } ${ quote } ${ semis ? ";" : "" } \n` ;
140+ exportStr += `export { ${ namedExportsStr } } from ${ quote } ./${ fileWithoutExtension } ${ quote } ${
141+ semis ? ";" : ""
142+ } \n`;
143143 }
144144 if ( typeExportsStr ) {
145- exportStr += `export type { ${ typeExportsStr } } from ${ quote } ./${ fileWithoutExtension } ${ quote } ${ semis ? ";" : "" } \n` ;
145+ exportStr += `export type { ${ typeExportsStr } } from ${ quote } ./${ fileWithoutExtension } ${ quote } ${
146+ semis ? ";" : ""
147+ } \n`;
146148 }
147- return exportStr ;
149+ output . push ( exportStr ) ;
148150 } else {
149- return `export * from ${ quote } ./${ fileWithoutExtension } ${ quote } ${ semis ? ";" : "" } \n` ;
151+ output . push (
152+ `export * from ${ quote } ./${ fileWithoutExtension } ${ quote } ${
153+ semis ? ";" : ""
154+ } \n`
155+ ) ;
150156 }
151- } ) ;
157+ }
152158
153159 if ( output && output . length > 0 ) {
154- const filePath = path . join ( uri . fsPath , "index.ts" ) ;
155- if ( ! fs . existsSync ( filePath ) ) {
156- fs . writeFileSync ( filePath , "" ) ;
160+ const filePath = join ( uri . fsPath , "index.ts" ) ;
161+ if ( ! ( await fileExists ( filePath ) ) ) {
162+ await writeFile ( filePath , "" ) ;
157163 }
158164
159165 const fileUri = vscode . Uri . file ( filePath ) ;
@@ -220,11 +226,10 @@ export class ExportAll {
220226 }
221227 }
222228 } catch ( e ) {
223- console . error ( ( e as Error ) . message ) ;
224-
225229 Logger . error (
226230 `Sorry, something failed when exporting all modules in ${ uri . fsPath } `
227231 ) ;
232+ Logger . error ( ( e as Error ) . message ) ;
228233 vscode . window . showErrorMessage (
229234 `${ EXTENSION_NAME } : Sorry, something failed when exporting all modules in the current folder.`
230235 ) ;
0 commit comments