-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·115 lines (93 loc) · 2.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
/**
* react-command
* Generate React File with ease
*
* @author anjianto <github.com/anjianto>
*/
const path = require('path');
const fs = require('fs');
const pascalcase = require('pascalcase');
const shell = require('shelljs');
const init = require('./utils/init');
const cli = require('./utils/cli');
const log = require('./utils/log');
const output = require('./utils/output');
const { generateIndexComponent } = require('./utils/generateIndexComponent');
const input = cli.input;
const flags = cli.flags;
const { clear, debug } = flags;
(async () => {
const packageJson = JSON.parse(
fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')
);
const react = packageJson?.dependencies?.react && 'react';
const next = packageJson?.dependencies?.next && 'next';
if (!(react || next)) {
output.error({
title: 'This CLi only can be use inside React or Next.js Project.'
});
}
init({ clear });
input.includes(`help`) && cli.showHelp(0);
const name = flags.name || flags.n || null;
const extension = flags.extension || flags.e || null;
const page = flags.page;
const root = flags.root || flags.r || undefined;
if (!name) {
return output.error({
title: 'GIVE ME NAME!!!'
});
}
if (!['js', 'jsx', 'ts', 'tsx'].includes(extension)) {
return output.error({
title: 'Extension only can be "js" | "jsx" | "ts" | "tsx"'
});
}
const folder = extension === ('ts' || 'tsx') ? 'ts' : 'js';
const filepath = path.join(
root ?? '',
flags.page || flags.p ? 'pages' : 'components',
flags.parent ? pascalcase(name) : null,
`${pascalcase(name)}.${extension}`
);
const templateFilepath = path.resolve(
__dirname,
path.join(
'templates',
page ? 'pages' : 'components',
folder,
`${next || react}.${extension}`
)
);
if (fs.existsSync(filepath) && !(flags.force || flags.f)) {
output.error({
title: `Duplicate File ${filepath}`
});
process.exit(1);
}
const file = fs
.readFileSync(templateFilepath, 'utf8')
.replace(/__NAME__/g, pascalcase(name));
if (flags.parent) {
const r = root ? `${root}/` : '';
if (!(flags.page || flags.p)) {
shell.mkdir('-p', `${r}components/${pascalcase(name)}`);
} else {
shell.mkdir('-p', `${r}pages/${pascalcase(name)}`);
}
}
fs.writeFileSync(filepath, file, {
encoding: 'utf8'
});
generateIndexComponent({
dir: __dirname,
page,
folder,
extension,
name,
flags,
root
});
debug && log(flags);
})();