forked from Eurofunk/ag-grid-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-generator.js
183 lines (159 loc) · 7.27 KB
/
example-generator.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
const {JSDOM} = require('jsdom');
const {window, document} = new JSDOM('<html></html>');
global.window = window;
global.document = document;
const jQuery = require('jquery');
const glob = require('glob');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const copy = require('copy');
const fsExtra = require('fs-extra');
const prettier = require('prettier');
function copyFilesSync(files, dest) {
files.forEach(file => fsExtra.copySync(file, dest + '/' + path.basename(file)));
}
function moveScriptsWithoutToken(scripts, dest, token) {
let removeTokenFromFile = file => {
let filename = path.basename(file);
fsExtra.rename(dest + '/' + filename, dest + '/' + filename.replace(token, ''));
};
copyFilesSync(scripts, dest);
scripts.forEach(file => removeTokenFromFile(file));
}
function extractComponentFileNames(scripts, token) {
return scripts.map(script => path.basename(script).replace(token, ''));
}
function copyGlobSync(globString, dest) {
copyFilesSync(glob.sync(globString), dest);
}
function phpArrayToJSON(string) {
if (!string) {
return {};
}
const replaced = string
.replace(/^, /, '')
.replace(/'/g, '"')
.replace(/array\((("\w+"(, )?)+)\)/, '[$1]')
.replace(/array/g, '')
.replace(/\(/g, '{')
.replace(/\)/g, '}')
.replace(/\=\>/g, ':');
try {
return JSON.parse(replaced);
} catch (e) {
console.error(replaced, e);
throw new Error(' The hackish conversion of PHP syntax to JSON failed. check ./example-generator.js');
}
}
function forEachExampleToGenerate(cb, final, scope = '*') {
glob(`src/${scope}/*.php`, {}, (er, files) => {
files.forEach(file => {
const contents = fs.readFileSync(file, {encoding: 'utf8'});
const section = path.dirname(file).replace('src/', '');
const exampleRegEx = /example\('.+?',\s?'(.+?)',\s?'(.+?)'(.+)?\)\s?\?>/g;
let matches;
while ((matches = exampleRegEx.exec(contents))) {
const [_, example, type, options] = matches;
if (type === 'generated') {
try {
cb(section, example, phpArrayToJSON(options));
} catch (error) {
console.error(`Could not process example ${example } in ${file}. Does the example directory exist?`);
console.error(`The error: ${error.message}`);
}
}
}
});
final();
});
}
module.exports = (cb, scope) => {
require('ts-node').register();
const {vanillaToReact} = require('./src/example-runner/vanilla-to-react.ts');
const {vanillaToAngular} = require('./src/example-runner/vanilla-to-angular.ts');
const {appModuleAngular} = require('./src/example-runner/angular-app-module.ts');
let count = 0;
forEachExampleToGenerate(
(section, example, options) => {
count++;
const document = glob.sync(path.join('./src', section, example, 'index.html'))[0];
let script, scripts;
if (glob.sync(path.join('./src', section, example, '*.js')).length > 1) {
script = glob.sync(path.join('./src', section, example, 'main.js'))[0];
scripts = glob.sync(
path.join('./src', section, example, '*.js'),
{ ignore: ['**/main.js', '**/*_angular.js', '**/*_react.js', '**/*_vanilla.js'] }
);
} else {
script = glob.sync(path.join('./src', section, example, '*.js'))[0];
scripts = [];
}
const stylesGlob = path.join('./src', section, example, '*.css');
const sources = [fs.readFileSync(script, {encoding: 'utf8'}), fs.readFileSync(document, {encoding: 'utf8'})];
const _gen = path.join('./src', section, example, '_gen');
let source, indexJSX;
let inlineStyles;
const style = jQuery(`<div>${sources[1]}</div>`).find('style');
if (style.length) {
inlineStyles = prettier.format(style.text(), {parser: 'css'});
}
const reactScripts = glob.sync(path.join('./src', section, example, '*_react*'));
try {
source = vanillaToReact(sources, options, extractComponentFileNames(reactScripts, '_react'));
indexJSX = prettier.format(source, {printWidth: 120});
} catch (e) {
console.error(`Failed at ./src/${section}/${example}`, e);
return;
// console.error(source);
//throw new Error('Failed generating the react version');
}
const angularScripts = glob.sync(path.join('./src', section, example, '*_angular*'));
let angularComponentFileNames = extractComponentFileNames(angularScripts, '_angular');
let appComponentTS, appModuleTS;
try {
source = vanillaToAngular(sources, options, angularComponentFileNames);
appComponentTS = prettier.format(source, {printWidth: 120, parser: 'typescript'});
appModuleTS = prettier.format(appModuleAngular(angularComponentFileNames), {printWidth: 120, parser: 'typescript'});
} catch (e) {
console.error(`Failed at ./src/${section}/${example}`, e);
return;
// console.error(source);
// throw new Error('Failed generating the angular version');
}
// fetch and move react files to _gen/react
const reactPath = path.join(_gen, 'react');
mkdirp.sync(reactPath);
fs.writeFileSync(path.join(reactPath, 'index.jsx'), indexJSX);
if (inlineStyles) {
fs.writeFileSync(path.join(reactPath, 'styles.css'), inlineStyles);
}
copyGlobSync(stylesGlob, reactPath);
copyFilesSync(scripts, reactPath);
moveScriptsWithoutToken(reactScripts, reactPath, '_react');
// fetch and move angular files to _gen/angular
const angularPath = path.join(_gen, 'angular');
mkdirp.sync(path.join(angularPath, 'app'));
fs.writeFileSync(path.join(angularPath, 'app', 'app.component.ts'), appComponentTS);
fs.writeFileSync(path.join(angularPath, 'app', 'app.module.ts'), appModuleTS);
if (inlineStyles) {
fs.writeFileSync(path.join(angularPath, 'styles.css'), inlineStyles);
}
copyGlobSync(stylesGlob, angularPath);
copyFilesSync(scripts, angularPath);
moveScriptsWithoutToken(angularScripts, angularPath + '/app', '_angular');
// fetch and move vanilla files to _gen/vanilla
const vanillaPath = path.join(_gen, 'vanilla');
mkdirp(vanillaPath);
const vanillaScripts = glob.sync(
path.join('./src', section, example, '*.{html,js,css}'),
{ ignore: ['**/*_angular.js', '**/*_react.js'] }
);
moveScriptsWithoutToken(vanillaScripts, vanillaPath, '_vanilla');
},
() => {
console.log(`// ${count} examples generated`);
cb();
}
, scope);
};