Skip to content

Commit 214be26

Browse files
committed
Makes logging more fly. Abstracts replaceInContent and replaceInFileName functions.
1 parent 9ebc912 commit 214be26

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

index.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const options = program.opts();
3131

3232
// Report options errors and exit if necessary.
3333
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'));
3535
program.help();
3636
}
3737

@@ -41,7 +41,7 @@ const dest = path.join(options.path, args.name);
4141

4242
// Check if the component already exists and exit if necessary.
4343
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)));
4545
program.help();
4646
}
4747

@@ -61,12 +61,31 @@ function walkDirectories(dirPath) {
6161
return results;
6262
}
6363

64+
function replaceInContent(content) {
65+
return content.replace(/_Template/g, args.name)
66+
.replace(/_template/g, paramCase(args.name))
67+
.replace(/_version/g, packageData.version)
68+
.replace(
69+
/_date/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(/_Template/g, args.name)
80+
.replace(/_template/g, paramCase(args.name));
81+
}
82+
6483
function create() {
6584
// 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));
6786
// Copy the template directory.
6887
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));
7089
// Get list of all file paths
7190
const files = walkDirectories(dest);
7291
// Loop over the files array inorder to rename and replace
@@ -75,36 +94,23 @@ function create() {
7594
// set up individual file paths and perform file name replacements.
7695
const fileSrcDir = path.dirname(file);
7796
const fileSrcName = path.basename(file);
78-
const fileDestName = fileSrcName
79-
.replace(/_Template/g, args.name)
80-
.replace(/_template/g, paramCase(args.name));
97+
const fileDestName = replaceInFileName(fileSrcName);
8198
const fileDest = path.join(fileSrcDir, fileDestName);
8299
// Ensure to Check if the file exists in this template.
83100
if (fs.existsSync(file)) {
84101
// rename the file.
85102
fs.renameSync(file, fileDest);
86103
// Get the content of the file and replace the
87104
// placeholder text.
88-
let content = fs
89-
.readFileSync(fileDest, 'utf8')
90-
.replace(/_Template/g, args.name)
91-
.replace(/_template/g, paramCase(args.name))
92-
.replace(/_version/g, packageData.version)
93-
.replace(
94-
/_date/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);
101107
// Write the file back into place.
102108
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));
104110
}
105111
});
106112
// 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));
108114
}
109115

110116
create();

0 commit comments

Comments
 (0)