@@ -31,7 +31,7 @@ const options = program.opts();
31
31
32
32
// Report options errors and exit if necessary.
33
33
if ( ! fs . existsSync ( path . join ( options . path , '_Template' ) ) ) {
34
- console . error ( chalk . red ( ' Error: No template exists at this path\n') ) ;
34
+ console . error ( chalk . redBright ( '❌ Error: No template exists at this path\n') ) ;
35
35
program . help ( ) ;
36
36
}
37
37
@@ -41,7 +41,7 @@ const dest = path.join(options.path, args.name);
41
41
42
42
// Check if the component already exists and exit if necessary.
43
43
if ( fs . existsSync ( dest ) ) {
44
- console . error ( chalk . red ( ' Error: There is already a component named "%s" \n') , args . name ) ;
44
+ console . error ( chalk . redBright ( '❌ Error: There is already a component named %s \n') , chalk . bold ( chalk . yellow ( args . name ) ) ) ;
45
45
program . help ( ) ;
46
46
}
47
47
@@ -61,12 +61,31 @@ function walkDirectories(dirPath) {
61
61
return results ;
62
62
}
63
63
64
+ function replaceInContent ( content ) {
65
+ return content . replace ( / _ T e m p l a t e / g, args . name )
66
+ . replace ( / _ t e m p l a t e / g, paramCase ( args . name ) )
67
+ . replace ( / _ v e r s i o n / g, packageData . version )
68
+ . replace (
69
+ / _ d a t e / g,
70
+ new Date ( ) . toLocaleDateString ( 'en-US' , {
71
+ year : 'numeric' ,
72
+ month : 'numeric' ,
73
+ day : 'numeric' ,
74
+ } )
75
+ ) ;
76
+ }
77
+
78
+ function replaceInFileName ( fileName ) {
79
+ return fileName . replace ( / _ T e m p l a t e / g, args . name )
80
+ . replace ( / _ t e m p l a t e / g, paramCase ( args . name ) ) ;
81
+ }
82
+
64
83
function create ( ) {
65
84
// Announce start of creation.
66
- console . info ( chalk . bold ( 'Creating component: "%s" \n' ) , args . name ) ;
85
+ console . info ( chalk . bold ( '⚡ Creating component: %s \n' ) , chalk . yellowBright ( args . name ) ) ;
67
86
// Copy the template directory.
68
87
fs . copySync ( src , dest ) ;
69
- console . info ( chalk . dim ( 'Directory built: "%s" \n' ) , dest ) ;
88
+ console . info ( chalk . dim ( '📁 Directory cloned: %s \n' ) , chalk . yellow ( dest ) ) ;
70
89
// Get list of all file paths
71
90
const files = walkDirectories ( dest ) ;
72
91
// Loop over the files array inorder to rename and replace
@@ -75,36 +94,23 @@ function create() {
75
94
// set up individual file paths and perform file name replacements.
76
95
const fileSrcDir = path . dirname ( file ) ;
77
96
const fileSrcName = path . basename ( file ) ;
78
- const fileDestName = fileSrcName
79
- . replace ( / _ T e m p l a t e / g, args . name )
80
- . replace ( / _ t e m p l a t e / g, paramCase ( args . name ) ) ;
97
+ const fileDestName = replaceInFileName ( fileSrcName ) ;
81
98
const fileDest = path . join ( fileSrcDir , fileDestName ) ;
82
99
// Ensure to Check if the file exists in this template.
83
100
if ( fs . existsSync ( file ) ) {
84
101
// rename the file.
85
102
fs . renameSync ( file , fileDest ) ;
86
103
// Get the content of the file and replace the
87
104
// placeholder text.
88
- let content = fs
89
- . readFileSync ( fileDest , 'utf8' )
90
- . replace ( / _ T e m p l a t e / g, args . name )
91
- . replace ( / _ t e m p l a t e / g, paramCase ( args . name ) )
92
- . replace ( / _ v e r s i o n / g, packageData . version )
93
- . replace (
94
- / _ d a t e / g,
95
- new Date ( ) . toLocaleDateString ( 'en-US' , {
96
- year : 'numeric' ,
97
- month : 'numeric' ,
98
- day : 'numeric' ,
99
- } )
100
- ) ;
105
+ let content = fs . readFileSync ( fileDest , 'utf8' ) ;
106
+ content = replaceInContent ( content ) ;
101
107
// Write the file back into place.
102
108
fs . writeFile ( fileDest , content , 'utf8' ) ;
103
- console . info ( chalk . dim ( 'File built: "%s" ' ) , fileDest ) ;
109
+ console . info ( chalk . dim ( '🔨 File built: %s ' ) , chalk . yellow ( fileDest ) ) ;
104
110
}
105
111
} ) ;
106
112
// Announce end of creation.
107
- console . info ( chalk . bold ( '\nCreated component "%s" ' ) , args . name ) ;
113
+ console . info ( chalk . bold ( '\n🎉 Created component: %s ' ) , chalk . yellowBright ( args . name ) ) ;
108
114
}
109
115
110
116
create ( ) ;
0 commit comments