-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
207 lines (186 loc) · 5.91 KB
/
utils.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import fs from 'fs';
import path from 'path';
export function write(file, data) {
fs.writeFileSync(file, data);
}
export function read(file) {
return fs.readFileSync(file, 'utf8');
}
export function exists(file) {
return fs.existsSync(file);
}
export function remove(file) {
return fs.unlinkSync(file);
}
export function removeFolder(p) {
if (fs.existsSync(p)) {
fs.readdirSync(p).forEach((file) => {
const curPath = path.join(p, file);
if (fs.lstatSync(curPath).isDirectory()) { // recurse
removeFolder(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(p);
}
}
export function folder(targetFolder) {
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
}
export function copyFileSync(source, target) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
export function copyFolderSync(source, target) {
var files = [];
//check if folder needs to be created or integrated
var targetFolder = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
//copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function (file) {
var curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
};
export function copyFolderContentsSync(source, target) {
var files = [];
//check if folder needs to be created or integrated
var targetFolder = target;
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
//copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function (file) {
var curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
};
export function eachComponent(srcDir, callback) {
const namespaces = fs.readdirSync(srcDir)
.filter((f) => f.match(/^[a-z]+$/) !== null);
namespaces.forEach((namespace) => {
if (namespace === '@types' || namespace === 'dist') {
return;
}
const namespaceDir = path.join(srcDir, namespace);
const components = fs.readdirSync(namespaceDir)
.filter((f) => f.match(/^[a-zA-Z0-9]+$/) !== null);
components.forEach((component) => {
const componentDir = path.join(namespaceDir, component);
const file = path.join(componentDir, `${component}.ts`);
if (fs.existsSync(file)) {
const componentU = `${component[0].toUpperCase()}${component.substr(1)}`;
const namespaceU = `${namespace[0].toUpperCase()}${namespace.substr(1)}`;
const name = `${namespace}${componentU}`;
const cls = `${namespaceU}${componentU}`;
const input = path.join(srcDir, namespace, component, `${component}.ts`);
callback({
cls,
name,
component,
namespace,
input
});
}
});
});
};
/*
[{
input, ./src/pg/grid/grid.ts
name, pgGrid
namespace, pg
examples: {
exampleInput, ./src/pg/grid/__examples/basic/basic.ts
example basic
}
}]
*/
export function getComponents(srcDir) {
const srcDir2 = srcDir
? path.join('./', srcDir)
: path.join('./');
const components = [];
const namespaces = fs.readdirSync(srcDir2)
.filter((f) => f.match(/^[a-z]+$/) !== null);
namespaces.forEach((namespace) => {
const namespaceDir = path.join(srcDir2, namespace);
const comps = fs.readdirSync(namespaceDir)
.filter((f) => f.match(/^[a-zA-Z0-9]+$/) !== null);
comps.forEach((component) => {
const componentDir = path.join(namespaceDir, component);
const file = path.join(componentDir, `${component}.ts`);
if (fs.existsSync(file)) {
const name = `${namespace}${component[0].toUpperCase()}${component.substring(1)}`;
const input = path.join(srcDir2, namespace, component, `${component}.ts`);
components.push({ input, name, namespace, component });
components[components.length - 1].examples = [];
const examplesDir = path.join(componentDir, '__examples__');
if ((fs.existsSync(examplesDir))) {
const examples2 = fs.readdirSync(examplesDir)
.filter((f) => f.match(/^[a-zA-Z0-9]+$/) !== null);
examples2.forEach((example) => {
const exampleDir = path.join(examplesDir, example);
const exampleInput = path.join(exampleDir, `${example}.ts`);
components[components.length - 1].examples.push({
exampleInput,
example
});
});
}
} else {
// console.error(`Unable to find ${file}!`);
}
});
});
return components;
};
export function dashToCamel(str) {
return str.replace(/-([a-z])/g, m => m[1].toUpperCase());
}
export function camelToDash(str) {
return str.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase()
}
/**
* npm start pgButton, button
*
* @returns Array of component string names. Ex: ['pgButton']
*/
export function getComponentsFromNpmStart() {
if (process.argv.length > 2 && process.argv[2]) {
const aComp = process.argv.slice(2).join(' ');
const aComps = aComp.split(/(?:,\s*|\s+)/g);
aComps.forEach((aC) => {
if (aC.match(/^\w+([A-Z]|-)/) === null) {
throw new Error(`${aC} must be formatted as namespace-component or namespaceComponent`);
}
});
return aComps.map(x => dashToCamel(x));
}
return [];
}