-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
84 lines (65 loc) · 2.11 KB
/
Copy pathextension.js
File metadata and controls
84 lines (65 loc) · 2.11 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
const vscode = require('vscode');
const COMMAND_NAME = 'create-react-component.createReactComponent';
function utf8(str) {
return Buffer.from(str, 'utf8');
}
async function createReactComponent(basePath) {
const componentName = await vscode.window.showInputBox({
ignoreFocusOut: true,
placeHolder: "ComponentName",
validateInput: candidateName => {
if (!candidateName[0].match(/[a-zA-Z_$]/)) {
return "Must start with a letter";
}
if (candidateName.match(/ /g)) {
return "Can't include a space";
}
if (!candidateName[0].match(/^[a-zA-Z_$][0-9a-zA-Z_$]*$/)) {
return "Must be a valid js variable name";
}
}
})
console.log(basePath.toJSON());
const joinPath = vscode.Uri.joinPath;
const fs = vscode.workspace.fs;
if (!componentName) return;
const componentPath = joinPath(basePath, `/${componentName}`);
const indexFile = joinPath(componentPath, `/index.ts`);
const mainFile = joinPath(componentPath, `/${componentName}.tsx`);
const cssFile = joinPath(componentPath, `/${componentName}.module.styl`);
fs.createDirectory(componentPath);
fs.writeFile(indexFile, utf8(
`export { default } from './${componentName}';
`
));
fs.writeFile(mainFile, utf8(
`import React from 'react';
import styles from './${componentName}.module.styl';
interface ${componentName}Props extends React.ComponentPropsWithoutRef<'div'> {
};
const ${componentName} = (props: ${componentName}Props) => {
const {...rest} = props;
return (
<div className={styles.${componentName}} {...rest}>
</div>
);
};
export default ${componentName};
`
));
fs.writeFile(cssFile, utf8(
`.${componentName}
display flex
`
));
vscode.window.showInformationMessage(`Created ${componentName} in ${basePath}`);
}
function activate(context) {
let disposable = vscode.commands.registerCommand(COMMAND_NAME, createReactComponent);
context.subscriptions.push(disposable);
}
function deactivate() {}
module.exports = {
activate,
deactivate
}