|
| 1 | +constchalk=require('chalk'); |
| 2 | +constfs=require('fs'); |
| 3 | + |
| 4 | +constcountry=process.argv[2]; |
| 5 | +constviewBoxW=process.argv[3]; |
| 6 | +constviewBoxH=process.argv[3]; |
| 7 | + |
| 8 | +constcapFirst=(str)=>str.charAt(0).toUpperCase()+str.slice(1); |
| 9 | + |
| 10 | +constcapName=capFirst(country); |
| 11 | + |
| 12 | +constdir=`./data/${capName}`; |
| 13 | + |
| 14 | +constsvgData=fs.readFileSync(`./temp/${country}.svg`,'utf8',(err,data)=>{ |
| 15 | +if(err){ |
| 16 | +console.error(err); |
| 17 | +return; |
| 18 | +} |
| 19 | +returndata; |
| 20 | +}); |
| 21 | +conststateData=fs.readFileSync(`./temp/${country}.data`,'utf8',(err,data)=>{ |
| 22 | +if(err){ |
| 23 | +console.error(err); |
| 24 | +return; |
| 25 | +} |
| 26 | +returndata; |
| 27 | +}); |
| 28 | + |
| 29 | +constfilesArr=[ |
| 30 | +{ |
| 31 | +file:`${capName}.map.tsx`, |
| 32 | +content:` |
| 33 | +import MapLayout from '@/layouts/MapLayout'; |
| 34 | +import React from 'react'; |
| 35 | +import {${capName}StateCodes } from './${capName}StateCodes'; |
| 36 | +
|
| 37 | +const${capName}Map = () => ( |
| 38 | + <MapLayout name="${country}" width={650} viewBox={[0, 0,${viewBoxW},${viewBoxH}]} stateCodes={${capName}StateCodes}> |
| 39 | +${svgData} |
| 40 | + </MapLayout> |
| 41 | +); |
| 42 | +
|
| 43 | +export default${capName}Map; |
| 44 | + ` |
| 45 | +}, |
| 46 | +{ |
| 47 | +file:`${capName}StateCodes.tsx`, |
| 48 | +content:`export const${capName}StateCodes ={ |
| 49 | +${stateData} |
| 50 | + }` |
| 51 | +} |
| 52 | +]; |
| 53 | + |
| 54 | +constgenTemplate=(country)=>{ |
| 55 | +// Create folder |
| 56 | +if(!fs.existsSync(dir)){ |
| 57 | +fs.mkdirSync(dir); |
| 58 | +filesArr.forEach((el)=>{ |
| 59 | +fs.writeFile( |
| 60 | +`${dir}/${el.file}`, |
| 61 | +el.content, |
| 62 | +{ |
| 63 | +encoding:'utf8', |
| 64 | +flag:'w', |
| 65 | +mode:0o666 |
| 66 | +}, |
| 67 | +(err)=>{ |
| 68 | +if(err)console.log(err); |
| 69 | +else{ |
| 70 | +console.log(chalk.green(`Created:${chalk.blue.bold(el.file)}`)); |
| 71 | +} |
| 72 | +} |
| 73 | +); |
| 74 | +}); |
| 75 | +// Generate Pages file |
| 76 | +fs.writeFile( |
| 77 | +`./pages/${country}.tsx`, |
| 78 | +`import ControlContainer from '@/components/ControlContainer'; |
| 79 | +import${capName}Map from '@/data/${capName}/${capName}.map'; |
| 80 | +import {${capName}StateCodes } from '@/data/${capName}/${capName}StateCodes'; |
| 81 | +import MainLayout from '@/layouts/MainLayout'; |
| 82 | +import React from 'react'; |
| 83 | +
|
| 84 | +const${capName} = () => ( |
| 85 | + <MainLayout> |
| 86 | + <div className="flex justify-between container"> |
| 87 | + <ControlContainer stateCodes={${capName}StateCodes} mapId="${country}-map" /> |
| 88 | + <${capName}Map /> |
| 89 | + </div> |
| 90 | + </MainLayout> |
| 91 | +); |
| 92 | +
|
| 93 | +export default${capName};`, |
| 94 | +{ |
| 95 | +encoding:'utf8', |
| 96 | +flag:'w', |
| 97 | +mode:0o666 |
| 98 | +}, |
| 99 | +(err)=>{ |
| 100 | +if(err)console.log(err); |
| 101 | +else{ |
| 102 | +console.log(chalk.green(`Created:${chalk.blue.bold(`pages/${country}.tsx`)}`)); |
| 103 | +} |
| 104 | +} |
| 105 | +); |
| 106 | +}else{ |
| 107 | +console.log(chalk.red('Folder Already Exists')); |
| 108 | +console.log(chalk.red('Delete and Run the Script Again ')); |
| 109 | +} |
| 110 | +}; |
| 111 | + |
| 112 | +constnoArgsFunc=()=>{ |
| 113 | +console.log(chalk.red(`Please Pass Map Name as an Argument.`)); |
| 114 | +console.log(chalk.blue('Example :')); |
| 115 | +console.log(chalk.green('yarn generate india 1000 1000 \n')); |
| 116 | +}; |
| 117 | + |
| 118 | +country!==undefined ?genTemplate(country.toLowerCase()) :noArgsFunc(); |
| 119 | +module.exports=genTemplate; |