-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.js
More file actions
52 lines (46 loc) · 1.45 KB
/
generator.js
File metadata and controls
52 lines (46 loc) · 1.45 KB
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
/* jshint node: true, devel: true */
'use strict';
const fs = require('fs');
const { getInstalledPath } = require('get-installed-path');
const TEMPLATE = 'template/index.js';
const CURR_DIR = process.cwd();
let TEMPLATE_DIR = '';
const formatName = (s) => {
let input = s.split(/(?=[A-Z])/).join('-').toLowerCase();
return input.substr(0, input.lastIndexOf('.')) || input;
};
const createModule = (DIR_NAME)=> {
DIR_NAME = formatName(DIR_NAME);
const FULL_DIR = `${CURR_DIR}/${DIR_NAME}`;
if (!fs.existsSync(FULL_DIR)){
fs.mkdir(FULL_DIR, (err) => {
if (err) throw err;
createFile('index', FULL_DIR);
console.log(`${DIR_NAME} module successfully created`);
});
} else {
console.error(`ERROR: ${DIR_NAME} already exists!`);
}
};
const createFile = (FILE_NAME, FILE_DIR)=> {
if(!FILE_DIR){
FILE_DIR = CURR_DIR;
}
FILE_NAME = formatName(FILE_NAME);
if (!fs.existsSync(`${FILE_DIR}/${FILE_NAME}.js`)){
getInstalledPath('@ebarahona/edcli').then((path) => {
TEMPLATE_DIR = path;
fs.copyFile(`${TEMPLATE_DIR}/${TEMPLATE}`, `${FILE_DIR}/${FILE_NAME}.js`, (err) => {
if (err) throw err;
console.log(`${FILE_NAME}.js successfully created`);
});
});
} else {
console.error(`ERROR: ${FILE_NAME} already exists!`);
}
};
const API = {
createFile,
createModule
};
module.exports = API;