-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenMapFiles.js
121 lines (108 loc) · 3.36 KB
/
genMapFiles.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
const chalk = require('chalk');
const fs = require('fs');
const country = process.argv[2];
const viewBoxW = process.argv[3];
const viewBoxH = process.argv[4];
const capFirst = (str) => str.charAt(0).toUpperCase() + str.slice(1);
const capName = capFirst(country);
const dir = `./data/${capName}`;
const svgData = fs.readFileSync(`./temp/map.svg`, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
return data;
});
const stateData = fs.readFileSync(`./temp/map.data`, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
return data;
});
const filesArr = [
{
file: `${capName}.map.tsx`,
content: `
import MapLayout from '@/layouts/MapLayout';
import React from 'react';
import { ${capName}StateCodes } from './${capName}StateCodes';
const ${capName}Map = () => (
<MapLayout name="${country}" width={650} viewBox={[0, 0, ${viewBoxW}, ${viewBoxH}]} stateCodes={${capName}StateCodes}>
${svgData}
</MapLayout>
);
export default ${capName}Map;
`
},
{
file: `${capName}StateCodes.tsx`,
content: `export const ${capName}StateCodes ={
${stateData}
}`
}
];
const genTemplate = (country) => {
// Create folder
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
filesArr.forEach((el) => {
fs.writeFile(
`${dir}/${el.file}`,
el.content,
{
encoding: 'utf8',
flag: 'w',
mode: 0o666
},
(err) => {
if (err) console.log(err);
else {
console.log(chalk.green(`Created: ${chalk.blue.bold(el.file)}`));
}
}
);
});
// Generate Pages file
fs.writeFile(
`./pages/map/${country}.tsx`,
`import ControlContainer from '@/components/ControlContainer';
import MapSEO from '@/components/Seo/MapSEO';
import ${capName}Map from '@/data/${capName}/${capName}.map';
import { ${capName}StateCodes } from '@/data/${capName}/${capName}StateCodes';
import MainLayout from '@/layouts/MainLayout';
import React from 'react';
const ${capName} = () => (
<MainLayout>
<MapSEO name="${capName}" />
<div className="flex justify-between container">
<ControlContainer stateCodes={${capName}StateCodes} mapId="${country}-map" />
<${capName}Map />
</div>
</MainLayout>
);
export default ${capName};`,
{
encoding: 'utf8',
flag: 'w',
mode: 0o666
},
(err) => {
if (err) console.log(err);
else {
console.log(chalk.green(`Created: ${chalk.blue.bold(`pages/${country}.tsx`)}`));
}
}
);
} else {
console.log(chalk.red('Folder Already Exists'));
console.log(chalk.red('Delete and Run the Script Again '));
}
};
const noArgsFunc = () => {
console.log(chalk.red(`Please Pass Map Name as an Argument.`));
console.log(chalk.blue('Example :'));
console.log(chalk.green('yarn generate india 1000 1000 \n'));
};
country !== undefined ? genTemplate(country.toLowerCase()) : noArgsFunc();
module.exports = genTemplate;