-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
123 lines (101 loc) · 3.04 KB
/
mod.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
import { parse, Args } from "https://deno.land/std/flags/mod.ts";
import { ensureDir } from "https://deno.land/std/fs/ensure_dir.ts";
import {
yellow,
magenta,
green,
bold,
} from "https://deno.land/std/fmt/colors.ts";
const parsedArgs: Args = parse(Deno.args);
let name: string = "";
let path: string = "";
let fileNameAsInput: boolean = parsedArgs["e"] || parsedArgs["exact"];
let writeUtilFunction: boolean = parsedArgs["u"] || parsedArgs["utility"];
if (!parsedArgs["_"].length) {
throw "Component name required";
} else {
name = "" + parsedArgs["_"];
}
if (parsedArgs["p"] && "boolean" === typeof parsedArgs["p"]) {
throw "Bad Parameter. Perhaps you wanted to use --path?";
}
if (parsedArgs["p"]) {
path = parsedArgs["p"];
} else if (parsedArgs["path"]) {
path = parsedArgs["path"];
} else {
path = writeUtilFunction ? "app/utils" : "app/components";
}
const capitalize = (str: string): string =>
str.charAt(0).toUpperCase() + str.slice(1);
const ccName: string = name.split("-").map(
parsedArgs["u"]
? (nameFragment: string, idx: number): string =>
0 === idx ? nameFragment : capitalize(nameFragment)
: (nameFragment: string): string => capitalize(nameFragment),
).join("");
const basePath: string = `./${path}/${fileNameAsInput ? name : ccName}`;
if (writeUtilFunction) {
const utilContent: string = `const ${ccName} = param => {
return param;
};
export default ${ccName};
`;
console.info(`Writing function ${bold(ccName)}`);
ensureDir(`./${path}`)
.then(async () => {
await Deno.writeTextFile(basePath + ".js", utilContent);
console.info(yellow(`${bold(basePath + ".js")}`));
console.info("Done!");
});
} else {
const compoNameLc: string = (fileNameAsInput ? name : ccName).toLowerCase();
const compoContent: string = `/*
* External dependencies
*/
import React from 'react';
import PropTypes from 'prop-types';
/**
* Internal dependencies
*/
import './style.scss';
${ccName}.propTypes = {
};
function ${ccName}( props ) {
return (
<div className="${compoNameLc}">
</div>
);
}
export default ${ccName};
`;
const compoTest: string = `/*
* External dependencies
*/
import React from 'react';
import { mount } from 'enzyme';
/**
* Internal dependencies
*/
import ${ccName} from './index';
describe('${ccName}', () => {
const wrapper = mount(<${ccName} />);
it('renders correctly', () => {
expect( wrapper.find( '.${compoNameLc}' ) ).toHaveLength(1);
});
});
`;
const compoStyles: string = `/* Styles for ${ccName} */
`;
console.info(`Writing component ${bold(ccName)}`);
ensureDir(basePath)
.then(async () => {
await Deno.writeTextFile(basePath + "/index.js", compoContent);
console.info(yellow(`${bold(basePath + "/index.js")}`));
await Deno.writeTextFile(basePath + "/style.scss", compoStyles);
console.info(magenta(`${bold(basePath + "/style.scss")}`));
await Deno.writeTextFile(basePath + "/test.js", compoTest);
console.info(green(`${bold(basePath + "/test.js")}`));
console.info("Done!");
});
}