(
inputFile: string,
outputFile: string)
| 69 | |
| 70 | // Central function to process the file and generate the output |
| 71 | async function processFile( |
| 72 | inputFile: string, |
| 73 | outputFile: string) : Promise<{ buildings: { |
| 74 | [key: string]: number }; |
| 75 | items: ParserItemDataInterface; |
| 76 | recipes: ParserRecipe[], |
| 77 | powerGenerationRecipes: ParserPowerRecipe[]; |
| 78 | } | undefined> { |
| 79 | try { |
| 80 | const fileContent = await readFileAsUtf8(inputFile); |
| 81 | const cleanedContent = cleanInput(fileContent); |
| 82 | const data = JSON.parse(cleanedContent); |
| 83 | |
| 84 | // Get parts |
| 85 | const items = getItems(data); |
| 86 | fixItemNames(items); |
| 87 | |
| 88 | // Get an array of all buildings that produce something |
| 89 | const producingBuildings = getProducingBuildings(data); |
| 90 | |
| 91 | // Get power consumption for the producing buildings |
| 92 | const buildings = getPowerConsumptionForBuildings(data, producingBuildings); |
| 93 | |
| 94 | // Pass the producing buildings with power data to getRecipes to calculate perMin and powerPerProduct |
| 95 | const recipes = getProductionRecipes(data, buildings); |
| 96 | removeRubbishItems(items, recipes); |
| 97 | fixTurbofuel(items, recipes); |
| 98 | |
| 99 | //IMPORTANT: The order here matters - don't run this because fixing the turbofuel. |
| 100 | const powerGenerationRecipes = getPowerGeneratingRecipes(data, items); |
| 101 | |
| 102 | // Since we've done some manipulation of the items data, re-sort it |
| 103 | const sortedItems: { [key: string]: ParserPart } = {}; |
| 104 | Object.keys(items.parts).sort().forEach(key => { |
| 105 | sortedItems[key] = items.parts[key]; |
| 106 | }); |
| 107 | items.parts = sortedItems; |
| 108 | |
| 109 | // Construct the final JSON object |
| 110 | const finalData = { |
| 111 | buildings, |
| 112 | items, |
| 113 | recipes, |
| 114 | powerGenerationRecipes |
| 115 | }; |
| 116 | |
| 117 | // Write the output to the file |
| 118 | await fs.writeJson(path.resolve(outputFile), finalData, {spaces: 4}); |
| 119 | console.log(`Processed parts, buildings, and recipes have been written to ${outputFile}.`); |
| 120 | |
| 121 | return finalData; |
| 122 | } catch (error) { |
| 123 | if (error instanceof Error) { |
| 124 | console.error(`Error processing file: ${error.message}`); |
| 125 | } else { |
| 126 | console.error(`Error processing file: ${error}`); |
| 127 | } |
| 128 | } |
no test coverage detected
searching dependent graphs…