Skip to content

Commit e27880d

Browse files
committed
[ add ] --folder option
[ small refactoring]
1 parent bc705d2 commit e27880d

File tree

3 files changed

+79
-53
lines changed

3 files changed

+79
-53
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
node_modules
2-
components/*
2+
components

cmd/make-js-component.js

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ var enabledFramework;
2626
enabledFramework["Vue"] = "vue";
2727
enabledFramework["React"] = "react";
2828
})(enabledFramework || (enabledFramework = {}));
29-
program.name('make-js-component').version(packageJson.version);
30-
program.option("--vue", "creates a vue component");
31-
program.option("-c", "creates a vue component using composition API: use options API instea");
32-
program.requiredOption('-n, --name <component name>', "the name of the component");
29+
//program config and setup
30+
program.name('make-js-component')
31+
.version(packageJson.version)
32+
.option("--vue", "creates a vue component")
33+
.option("-c", "creates a vue component using composition API: use options API instea")
34+
.requiredOption('-n, --name <component name>', "the name of the component")
35+
.option("-f, --folder <custom folder path>", "a custom folder inside components to save the component")
36+
.parse(process.argv);
37+
var options = program.opts();
3338
var usedFramework = enabledFramework.Empty;
3439
var componentName = "";
35-
program.parse(process.argv);
36-
var options = program.opts();
37-
console.log(options);
3840
if (options.vue)
3941
usedFramework = enabledFramework.Vue;
40-
// add here more options
42+
// add here options for the framework
4143
if (options.name)
4244
componentName = options.name;
4345
if (usedFramework == "") {
@@ -46,20 +48,44 @@ if (usedFramework == "") {
4648
}
4749
var componentTemplate = options.c ? 'component-composition.vue' : 'component-options.vue';
4850
try {
49-
var folderArgIndex = process.argv.indexOf('--f');
50-
var customFolder_1 = folderArgIndex > -1 ? process.argv[folderArgIndex + 1] : '';
51-
customFolder_1 = customFolder_1.charAt(-1) == '/' ? customFolder_1 : "".concat(customFolder_1, "/");
52-
customFolder_1 = customFolder_1.charAt(0) == '/' ? customFolder_1 : "/".concat(customFolder_1);
51+
var customFolder = options.folder || "";
52+
createComponent(componentName, usedFramework, componentTemplate, customFolder);
53+
/*if(!fs.existsSync(`${configs.BASE_DIR}${configs.COMPONENT_FOLDER}`)){
54+
fs.mkdirSync(`${configs.BASE_DIR}${configs.COMPONENT_FOLDER}`);
55+
}
56+
fs.readFile(path.join(configs.INIT_PATH,'src',configs.STUBS_DIR,usedFramework,componentTemplate), 'utf8', (err: Error,data: String)=>{
57+
data = data.replaceAll("Component",capitalizeFirstLetter(componentName))
58+
if(!fs.existsSync(path.join(configs.BASE_DIR,configs.COMPONENT_FOLDER,customFolder))){
59+
fs.mkdirSync(path.join(configs.BASE_DIR,configs.COMPONENT_FOLDER,customFolder));
60+
}
61+
const compFileName = componentName+'.vue';
62+
fs.writeFile(path.join(configs.BASE_DIR,configs.COMPONENT_FOLDER,customFolder,compFileName),data, (err: Error)=>{
63+
if(err){
64+
console.error(err)
65+
}else{
66+
console.log('Done')
67+
}
68+
})
69+
})*/
70+
}
71+
catch (error) {
72+
console.error(error);
73+
}
74+
function capitalizeFirstLetter(string) {
75+
return string.charAt(0).toUpperCase() + string.slice(1);
76+
}
77+
function createComponent(componentName, framework, template, customFolder) {
78+
if (customFolder === void 0) { customFolder = ""; }
5379
if (!fs.existsSync("".concat(configs.BASE_DIR).concat(configs.COMPONENT_FOLDER))) {
5480
fs.mkdirSync("".concat(configs.BASE_DIR).concat(configs.COMPONENT_FOLDER));
5581
}
56-
console.log(path.join(configs.INIT_PATH, 'src', configs.STUBS_DIR, usedFramework, componentTemplate));
57-
fs.readFile(path.join(configs.INIT_PATH, 'src', configs.STUBS_DIR, usedFramework, componentTemplate), 'utf8', function (err, data) {
82+
fs.readFile(path.join(configs.INIT_PATH, 'src', configs.STUBS_DIR, framework, template), 'utf8', function (err, data) {
5883
data = data.replaceAll("Component", capitalizeFirstLetter(componentName));
59-
if (!fs.existsSync("".concat(configs.BASE_DIR).concat(configs.COMPONENT_FOLDER).concat(customFolder_1))) {
60-
fs.mkdirSync("".concat(configs.BASE_DIR).concat(configs.COMPONENT_FOLDER).concat(customFolder_1));
84+
if (!fs.existsSync(path.join(configs.BASE_DIR, configs.COMPONENT_FOLDER, customFolder))) {
85+
fs.mkdirSync(path.join(configs.BASE_DIR, configs.COMPONENT_FOLDER, customFolder));
6186
}
62-
fs.writeFile("".concat(configs.BASE_DIR).concat(configs.COMPONENT_FOLDER).concat(customFolder_1).concat(componentName, ".vue"), data, function (err) {
87+
var compFileName = componentName + '.vue';
88+
fs.writeFile(path.join(configs.BASE_DIR, configs.COMPONENT_FOLDER, customFolder, compFileName), data, function (err) {
6389
if (err) {
6490
console.error(err);
6591
}
@@ -69,9 +95,3 @@ try {
6995
});
7096
});
7197
}
72-
catch (error) {
73-
console.error(error);
74-
}
75-
function capitalizeFirstLetter(string) {
76-
return string.charAt(0).toUpperCase() + string.slice(1);
77-
}

cmd/make-js-component.ts

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,58 +29,64 @@ enum enabledFramework {
2929
React = "react"
3030
}
3131

32+
//program config and setup
33+
program.name('make-js-component')
34+
.version(packageJson.version)
35+
.option("--vue","creates a vue component")
36+
.option("-c","creates a vue component using composition API: use options API instea")
37+
.requiredOption('-n, --name <component name>', "the name of the component")
38+
.option("-f, --folder <custom folder path>", "a custom folder inside components to save the component")
39+
.parse(process.argv)
3240

33-
program.name('make-js-component').version(packageJson.version);
34-
program.option("--vue","creates a vue component")
35-
program.option("-c","creates a vue component using composition API: use options API instea")
36-
program.requiredOption('-n, --name <component name>', "the name of the component")
41+
42+
const options = program.opts();
3743
let usedFramework : enabledFramework = enabledFramework.Empty;
3844
let componentName : String = "";
39-
program.parse(process.argv)
40-
const options = program.opts();
41-
console.log(options)
45+
46+
47+
4248
if(options.vue) usedFramework = enabledFramework.Vue
43-
// add here more options
49+
// add here options for the framework
4450
if(options.name) componentName = options.name
4551

4652
if(usedFramework == ""){
4753
console.error("You must specify the framework [--vue, --react...]")
4854
process.exit();
4955
}
5056

51-
const componentTemplate = options.c ? 'component-composition.vue' : 'component-options.vue'
57+
const componentTemplate : String = options.c ? 'component-composition.vue' : 'component-options.vue'
58+
const customFolder: String = options.folder || "";
5259

5360
try {
54-
55-
const folderArgIndex = process.argv.indexOf('--f')
56-
let customFolder = folderArgIndex > -1 ? process.argv[folderArgIndex+1] : '';
57-
customFolder = customFolder.charAt(-1) == '/' ? customFolder : `${customFolder}/`
58-
customFolder = customFolder.charAt(0) == '/' ? customFolder : `/${customFolder}`
59-
61+
createComponent(componentName,usedFramework,componentTemplate,customFolder);
62+
} catch (error) {
63+
console.error(error)
64+
}
65+
66+
67+
68+
69+
function capitalizeFirstLetter(string: String) {
70+
return string.charAt(0).toUpperCase() + string.slice(1);
71+
}
72+
73+
74+
function createComponent(componentName : String, framework: String, template: String, customFolder : String = ""){
6075
if(!fs.existsSync(`${configs.BASE_DIR}${configs.COMPONENT_FOLDER}`)){
6176
fs.mkdirSync(`${configs.BASE_DIR}${configs.COMPONENT_FOLDER}`);
6277
}
63-
console.log(path.join(configs.INIT_PATH,'src',configs.STUBS_DIR,usedFramework,componentTemplate))
64-
fs.readFile(path.join(configs.INIT_PATH,'src',configs.STUBS_DIR,usedFramework,componentTemplate), 'utf8', (err: Error,data: String)=>{
78+
fs.readFile(path.join(configs.INIT_PATH,'src',configs.STUBS_DIR,framework,template), 'utf8', (err: Error,data: String)=>{
6579
data = data.replaceAll("Component",capitalizeFirstLetter(componentName))
66-
if(!fs.existsSync(`${configs.BASE_DIR}${configs.COMPONENT_FOLDER}${customFolder}`)){
67-
fs.mkdirSync(`${configs.BASE_DIR}${configs.COMPONENT_FOLDER}${customFolder}`);
80+
if(!fs.existsSync(path.join(configs.BASE_DIR,configs.COMPONENT_FOLDER,customFolder))){
81+
fs.mkdirSync(path.join(configs.BASE_DIR,configs.COMPONENT_FOLDER,customFolder));
6882
}
69-
fs.writeFile(`${configs.BASE_DIR}${configs.COMPONENT_FOLDER}${customFolder}${componentName}.vue`,data, (err: Error)=>{
83+
const compFileName = componentName+'.vue';
84+
fs.writeFile(path.join(configs.BASE_DIR,configs.COMPONENT_FOLDER,customFolder,compFileName),data, (err: Error)=>{
7085
if(err){
7186
console.error(err)
7287
}else{
7388
console.log('Done')
7489
}
7590
})
7691
})
77-
} catch (error) {
78-
console.error(error)
79-
}
80-
81-
82-
83-
84-
function capitalizeFirstLetter(string: String) {
85-
return string.charAt(0).toUpperCase() + string.slice(1);
8692
}

0 commit comments

Comments
 (0)