1// Copyright 2022 Google LLC 2 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6 7// http://www.apache.org/licenses/LICENSE-2.0 8 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License.import { exec } from 'child_process'; 14 15import { FileIO, TCSVRecord } from './FileIO'; 16import ProcessArgs from './processArgs'; 17 18interface IInputMigItem { 19 migrationToken: string; 20 materialToken: string; 21 newDefaultValue?: string; 22 newComment?: string; 23} 24 25interface IAditionalKeys { 26 step: ('update' | 'duplicate' | 'add' | 'ignore')[]; 27 isHidden: boolean; 28 replaceToken: string; 29} 30 31export type IMigItem = Omit<IInputMigItem, 'materialToken' | 'migrationToken'> & IAditionalKeys; 32 33export type IMigrationMap = Map<string, IMigItem>; 34 35function isMigrationRecord(record: TCSVRecord): record is string[] { 36 return !record.some((value) => typeof value != 'string') || record.length != 5; 37} 38 39export const loadMIgrationList = async function (): Promise<IMigrationMap> { 40 const out: IMigrationMap = new Map(); 41 const csv = await FileIO.loadCSV('resources/migrationList.csv'); 42 43 csv.forEach((record, i) => { 44 if (i == 0) return; // header 45 46 if (typeof record[0] != 'string') return; 47 48 if (!isMigrationRecord(record)) { 49 console.log(`Failed to validade CSV record as string[5].`, record); 50 process.exit(); 51 } 52 53 const [originalToken, materialToken, newDefaultValue, newComment, migrationToken] = record; 54 55 if (out.has(originalToken)) { 56 console.log('Duplicated entry on Migration CSV file: ', originalToken); 57 return; 58 } 59 60 out.set(originalToken, { 61 replaceToken: ProcessArgs.isDebug ? migrationToken : materialToken, 62 ...(!!newDefaultValue && { newDefaultValue }), 63 ...(!!newComment && { newComment }), 64 step: [], 65 isHidden: false, 66 }); 67 }); 68 69 return new Map([...out].sort((a, b) => b[0].length - a[0].length)); 70}; 71