forked from ninecat-ui/ninecat-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-component.js
More file actions
85 lines (74 loc) · 3.25 KB
/
create-component.js
File metadata and controls
85 lines (74 loc) · 3.25 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
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
const fs = require('fs');
const path = require('path');
const args = process.argv.splice(2);
const componentName = args[0];
main();
function main () {
createComponentDir();
createDocDir();
};
function createComponentDir () {
const componentrRoot = '../packages/';
const targetDirPathName = 'n' + componentName;
const targetDirPath = path.join(__dirname, componentrRoot, targetDirPathName);
const subRoot = `../packages/${targetDirPathName}`;
const subDirPath = path.join(__dirname, subRoot, 'src');
createFileDir(targetDirPath);
createFileDir(subDirPath);
createVueFile(subRoot);
createJsFile(subRoot);
}
function createDocDir () {
const docRoot = '../doc/markdown';
const docTargetDirPathName = 'n' + componentName;
const docTargetDirPath = path.join(__dirname, docRoot, docTargetDirPathName);
createFileDir(docTargetDirPath);
const enSubDirPath = path.join(__dirname, `../doc/markdown/${docTargetDirPathName}`, 'en-US');
createFileDir(enSubDirPath);
const zhSubDirPath = path.join(__dirname, `../doc/markdown/${docTargetDirPathName}`, 'zh-CN');
createFileDir(zhSubDirPath);
createEnDocFile(docTargetDirPathName);
createZhDocFile(docTargetDirPathName);
}
function createVueFile (subRoot) {
const vueTemplate = fs.readFileSync(path.join(__dirname, './component-template/template.vue'), 'utf8');
const className = 'n-' + componentName.toLowerCase();
let vueContent = vueTemplate.replace(/n-ComponentName/g, className);
vueContent = vueContent.replace(/ComponentName/g, componentName);
const vueTargetFilePath = path.join(__dirname, subRoot, 'src', 'index.vue');
createFile(vueTargetFilePath, vueContent);
}
function createJsFile (subRoot) {
const jsTemplate = fs.readFileSync(path.join(__dirname, './component-template/template.js'), 'utf8');
const jsContent = jsTemplate.replace(/nComponentName/g, 'n' + componentName);
const jsTargetFilePath = path.join(__dirname, subRoot, 'index.js');
createFile(jsTargetFilePath, jsContent);
}
function createEnDocFile (docTargetDirPathName) {
const enDocTemplate = fs.readFileSync(path.join(__dirname, './component-template/en-doc-template.md'), 'utf8');
const enDocContent = enDocTemplate.replace(/componentName/g, componentName.toLowerCase());
const enDocFilePath = path.join(__dirname, `../doc/markdown/${docTargetDirPathName}/en-US`, 'index.md');
createFile(enDocFilePath, enDocContent);
}
function createZhDocFile (docTargetDirPathName) {
const zhDocTemplate = fs.readFileSync(path.join(__dirname, './component-template/en-doc-template.md'), 'utf8');
const zhDocContent = zhDocTemplate.replace(/componentName/g, componentName.toLowerCase());
const zhDocFilePath = path.join(__dirname, `../doc/markdown/${docTargetDirPathName}/zh-CN`, 'index.md');
createFile(zhDocFilePath, zhDocContent);
}
function createFile (targetFilePath, content) {
if (!fs.existsSync(targetFilePath)) {
fs.writeFile(targetFilePath, content, (err) => {
if (err) throw err;
console.log('The ' + targetFilePath + ' file has been created!');
});
} else {
console.error('error!\n' + targetFilePath + ' has already been existed!');
}
}
function createFileDir (dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
console.log('The ' + dirPath + ' folder has been created!');
}
}