-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.js
More file actions
110 lines (84 loc) · 3.08 KB
/
utils.js
File metadata and controls
110 lines (84 loc) · 3.08 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
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
'use strict';
const vscode = require('vscode');
const fse = require('fs-extra');
const fs = require('fs');
const path = require('path');
const pascalCase = require('change-case').pascalCase;
function logger(type, msg = '') {
switch (type) {
case 'success':
return vscode.window.setStatusBarMessage(`Success: ${msg}`, 5000);
case 'warning':
return vscode.window.showWarningMessage(`Warning: ${msg}`);
case 'error':
return vscode.window.showErrorMessage(`Failed: ${msg}`);
}
}
module.exports = {
logger,
generators: {
templatesDir: path.join(__dirname, '/templates'),
createFile: (file, data) =>
new Promise(resolve => {
let output = fse.outputFile(file, data);
resolve(output);
}),
resolveWorkspaceRoot: path =>
path.replace('${workspaceFolder}', vscode.workspace.rootPath),
createComponentDir: function(uri, componentName) {
let contextMenuSourcePath;
if (uri && fs.lstatSync(uri.fsPath).isDirectory()) {
contextMenuSourcePath = uri.fsPath;
} else if (uri) {
contextMenuSourcePath = path.dirname(uri.fsPath);
} else {
contextMenuSourcePath = vscode.workspace.rootPath;
}
let componentDir = `${contextMenuSourcePath}/${pascalCase(
componentName
)}`;
fse.mkdirsSync(componentDir);
return componentDir;
},
createComponent: function(componentDir, componentName, type) {
let templateFileName = this.templatesDir + `/${type}.template`;
const compName = pascalCase(componentName);
let componentContent = fs
.readFileSync(templateFileName)
.toString()
.replace(/{componentName}/g, compName);
let filename = `${componentDir}/${compName}.jsx`;
return this.createFile(filename, componentContent);
},
createTestFile: function(componentDir, componentName) {
let templateFileName = this.templatesDir + `/test.template`;
const compName = pascalCase(componentName);
let componentContent = fs
.readFileSync(templateFileName)
.toString()
.replace(/{componentName}/g, compName);
let filename = `${componentDir}/${compName}.test.jsx`;
return this.createFile(filename, componentContent);
},
createPackageJSON: function(componentDir, componentName) {
let templateFileName = this.templatesDir + '/package.template';
const compName = pascalCase(componentName);
let indexContent = fs
.readFileSync(templateFileName)
.toString()
.replace(/{componentName}/g, compName);
let filename = `${componentDir}/package.json`;
return this.createFile(filename, indexContent);
},
createCSS: function(componentDir, componentName) {
let templateFileName = `${this.templatesDir}/sass.template`;
const compName = pascalCase(componentName);
let cssContent = fs
.readFileSync(templateFileName)
.toString()
.replace(/{componentName}/g, compName);
let filename = `${componentDir}/${compName}.sass`;
return this.createFile(filename, cssContent);
}
}
};