11#!/usr/bin/env node
22
3- import * as path from 'path' ;
4- import * as chokidar from 'chokidar' ;
5- import _glob from 'glob' ;
63import * as yargs from 'yargs' ;
7- import chalk from 'chalk' ;
8- import { DtsCreator } from './dts-creator' ;
9- import { DtsContent } from "./dts-content" ;
10- import * as util from "util" ;
4+ import { run } from "./run" ;
115
12- const glob = util . promisify ( _glob ) ;
136
14- const yarg = yargs . usage ( 'Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] <input directory>' )
7+ const yarg = yargs . usage ( 'Create .css.d.ts from CSS modules *.css files.\nUsage: $0 [options] <search directory>' )
158 . example ( '$0 src/styles' , '' )
169 . example ( '$0 src -o dist' , '' )
1710 . example ( '$0 -p styles/**/*.icss -w' , '' )
1811 . detectLocale ( false )
1912 . demand ( [ '_' ] )
20- . alias ( 'c' , 'camelCase' ) . describe ( 'c' , 'Convert CSS class tokens to camelcase' )
21- . alias ( 'o' , 'outDir' ) . describe ( 'o' , 'Output directory' )
2213 . alias ( 'p' , 'pattern' ) . describe ( 'p' , 'Glob pattern with css files' )
14+ . alias ( 'o' , 'outDir' ) . describe ( 'o' , 'Output directory' )
2315 . alias ( 'w' , 'watch' ) . describe ( 'w' , 'Watch input directory\'s css files or pattern' ) . boolean ( 'w' )
16+ . alias ( 'c' , 'camelCase' ) . describe ( 'c' , 'Convert CSS class tokens to camelcase' )
2417 . alias ( 'd' , 'dropExtension' ) . describe ( 'd' , 'Drop the input files extension' ) . boolean ( 'd' )
2518 . alias ( 's' , 'silent' ) . describe ( 's' , 'Silent output. Do not show "files written" messages' ) . boolean ( 's' )
2619 . alias ( 'h' , 'help' ) . help ( 'h' )
2720 . version ( ( ) => require ( '../package.json' ) . version ) ;
28- const argv = yarg . argv ;
29- let creator : DtsCreator ;
3021
31- async function writeFile ( f : string ) : Promise < void > {
32- try {
33- const content : DtsContent = await creator . create ( f , undefined , ! ! argv . w ) ;
34- await content . writeFile ( ) ;
3522
36- if ( ! argv . s ) {
37- console . log ( 'Wrote ' + chalk . green ( content . outputFilePath ) ) ;
38- }
39- }
40- catch ( error ) {
41- console . error ( chalk . red ( '[Error] ' + error ) ) ;
42- }
43- } ;
23+ main ( ) ;
24+
25+ async function main ( ) : Promise < void > {
26+ const argv = yarg . argv ;
4427
45- async function main ( ) {
46- let rootDir : string ;
47- let searchDir : string ;
4828 if ( argv . h ) {
4929 yarg . showHelp ( ) ;
5030 return ;
5131 }
5232
33+ let searchDir : string ;
5334 if ( argv . _ && argv . _ [ 0 ] ) {
5435 searchDir = argv . _ [ 0 ] ;
5536 } else if ( argv . p ) {
@@ -58,26 +39,13 @@ async function main() {
5839 yarg . showHelp ( ) ;
5940 return ;
6041 }
61- const filesPattern = path . join ( searchDir , argv . p || '**/*.css' ) ;
62- rootDir = process . cwd ( ) ;
63- creator = new DtsCreator ( {
64- rootDir,
65- searchDir,
42+
43+ await run ( searchDir , {
44+ pattern : argv . p ,
6645 outDir : argv . o ,
46+ watch : argv . w ,
6747 camelCase : argv . c ,
68- dropExtension : argv . d
48+ dropExtension : argv . d ,
49+ silent : argv . s
6950 } ) ;
70-
71- if ( ! argv . w ) {
72- const files = await glob ( filesPattern ) ;
73- files . forEach ( writeFile ) ;
74- } else {
75- console . log ( 'Watch ' + filesPattern + '...' ) ;
76-
77- const watcher = chokidar . watch ( [ filesPattern . replace ( / \\ / g, "/" ) ] ) ;
78- watcher . on ( 'add' , writeFile ) ;
79- watcher . on ( 'change' , writeFile ) ;
80- }
8151} ;
82-
83- main ( ) ;
0 commit comments