@@ -16,58 +16,61 @@ import { CLIOptions } from "./types";
1616 * @param file the SCSS file to generate types for
1717 * @param options the CLI options
1818 */
19- export const writeFile = ( file : string , options : CLIOptions ) : Promise < void > => {
20- return fileToClassNames ( file , options )
21- . then ( async ( classNames ) => {
22- const typeDefinition = await classNamesToTypeDefinitions ( {
23- classNames,
24- ...options ,
25- } ) ;
19+ export const writeFile = async (
20+ file : string ,
21+ options : CLIOptions
22+ ) : Promise < void > => {
23+ try {
24+ const classNames = await fileToClassNames ( file , options ) ;
25+ const typeDefinition = await classNamesToTypeDefinitions ( {
26+ classNames,
27+ ...options ,
28+ } ) ;
2629
27- const typesPath = getTypeDefinitionPath ( file , options ) ;
28- const typesExist = fs . existsSync ( typesPath ) ;
30+ const typesPath = getTypeDefinitionPath ( file , options ) ;
31+ const typesExist = fs . existsSync ( typesPath ) ;
2932
30- // Avoid outputting empty type definition files.
31- // If the file exists and the type definition is now empty, remove the file.
32- if ( ! typeDefinition ) {
33- if ( typesExist ) {
34- removeSCSSTypeDefinitionFile ( file , options ) ;
35- } else {
36- alerts . notice ( `[NO GENERATED TYPES] ${ file } ` ) ;
37- }
38- return ;
33+ // Avoid outputting empty type definition files.
34+ // If the file exists and the type definition is now empty, remove the file.
35+ if ( ! typeDefinition ) {
36+ if ( typesExist ) {
37+ removeSCSSTypeDefinitionFile ( file , options ) ;
38+ } else {
39+ alerts . notice ( `[NO GENERATED TYPES] ${ file } ` ) ;
3940 }
41+ return ;
42+ }
4043
41- // Avoid re-writing the file if it hasn't changed.
42- // First by checking the file modification time, then
43- // by comparing the file contents.
44- if ( options . updateStaleOnly && typesExist ) {
45- const fileModified = fs . statSync ( file ) . mtime ;
46- const typeDefinitionModified = fs . statSync ( typesPath ) . mtime ;
44+ // Avoid re-writing the file if it hasn't changed.
45+ // First by checking the file modification time, then
46+ // by comparing the file contents.
47+ if ( options . updateStaleOnly && typesExist ) {
48+ const fileModified = fs . statSync ( file ) . mtime ;
49+ const typeDefinitionModified = fs . statSync ( typesPath ) . mtime ;
4750
48- if ( fileModified < typeDefinitionModified ) {
49- return ;
50- }
51-
52- const existingTypeDefinition = fs . readFileSync ( typesPath , "utf8" ) ;
53- if ( existingTypeDefinition === typeDefinition ) {
54- return ;
55- }
51+ if ( fileModified < typeDefinitionModified ) {
52+ return ;
5653 }
5754
58- // Files can be written to arbitrary directories and need to
59- // be nested to match the project structure so it's possible
60- // there are multiple directories that need to be created.
61- const dirname = path . dirname ( typesPath ) ;
62- if ( ! fs . existsSync ( dirname ) ) {
63- fs . mkdirSync ( dirname , { recursive : true } ) ;
55+ const existingTypeDefinition = fs . readFileSync ( typesPath , "utf8" ) ;
56+ if ( existingTypeDefinition === typeDefinition ) {
57+ return ;
6458 }
59+ }
6560
66- fs . writeFileSync ( typesPath , typeDefinition ) ;
67- alerts . success ( `[GENERATED TYPES] ${ typesPath } ` ) ;
68- } )
69- . catch ( ( { message, file, line, column } : SassError ) => {
70- const location = file ? `(${ file } [${ line } :${ column } ])` : "" ;
71- alerts . error ( `${ message } ${ location } ` ) ;
72- } ) ;
61+ // Files can be written to arbitrary directories and need to
62+ // be nested to match the project structure so it's possible
63+ // there are multiple directories that need to be created.
64+ const dirname = path . dirname ( typesPath ) ;
65+ if ( ! fs . existsSync ( dirname ) ) {
66+ fs . mkdirSync ( dirname , { recursive : true } ) ;
67+ }
68+
69+ fs . writeFileSync ( typesPath , typeDefinition ) ;
70+ alerts . success ( `[GENERATED TYPES] ${ typesPath } ` ) ;
71+ } catch ( error ) {
72+ const { message, file, line, column } = error as SassError ;
73+ const location = file ? ` (${ file } [${ line } :${ column } ])` : "" ;
74+ alerts . error ( `${ message } ${ location } ` ) ;
75+ }
7376} ;
0 commit comments