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C;SACtB;KACJ;AACL,CAAC;AATD,8BASC;AAQD,SAAgB,QAAQ,CAAC,IAAY;IACjC,MAAM,MAAM,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IACrC,QAAQ,MAAM,CAAC,CAAC,CAAC,EAAE;QACf,KAAK,IAAI;YACL,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE;gBACpB,IAAI,CAAC,GAAG,CAAC,CAAC;gBACV,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE;oBAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;oBACvB,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;oBAC1B,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC;oBACrB,CAAC,IAAI,CAAC,CAAC;iBACV;gBACD,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;aACrC;YACD,MAAM;QACV,KAAK,IAAI;YACL,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE;gBACpB,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;aACrC;YACD,MAAM;QACV,KAAK,IAAI;YACL,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE;gBACpB,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;aACrC;KACR;IAED,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AACtC,CAAC;AA3BD,4BA2BC","sourcesContent":["import * as ts from 'typescript';\nimport * as FS from 'fs';\nimport { dirname } from 'path';\n\n/**\n * List of known existent directories. Used to speed up [[directoryExists]].\n */\nconst existingDirectories: ts.MapLike<boolean> = {};\n\n/**\n * Normalize the given path.\n *\n * @param path The path that should be normalized.\n * @returns The normalized path.\n */\nexport function normalizePath(path: string) {\n return path.replace(/\\\\/g, '/');\n}\n\n/**\n * Test whether the given directory exists.\n *\n * @param directoryPath The directory that should be tested.\n * @returns TRUE if the given directory exists, FALSE otherwise.\n */\nexport function directoryExists(directoryPath: string): boolean {\n if (existingDirectories.hasOwnProperty(directoryPath)) {\n return true;\n }\n\n if (ts.sys.directoryExists(directoryPath)) {\n existingDirectories[directoryPath] = true;\n return true;\n }\n\n return false;\n}\n\n/**\n * Make sure that the given directory exists.\n *\n * @param directoryPath The directory that should be validated.\n */\nexport function ensureDirectoriesExist(directoryPath: string) {\n if (!directoryExists(directoryPath)) {\n const parentDirectory = dirname(directoryPath);\n ensureDirectoriesExist(parentDirectory);\n ts.sys.createDirectory(directoryPath);\n }\n}\n\n/**\n * Write a file to disc.\n *\n * If the containing directory does not exist it will be created.\n *\n * @param fileName The name of the file that should be written.\n * @param data The contents of the file.\n * @param writeByteOrderMark Whether the UTF-8 BOM should be written or not.\n * @param onError A callback that will be invoked if an error occurs.\n */\nexport function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {\n try {\n ensureDirectoriesExist(dirname(normalizePath(fileName)));\n ts.sys.writeFile(fileName, data, writeByteOrderMark);\n } catch (e) {\n if (onError) {\n onError(e.message);\n }\n }\n}\n\n/**\n * Load the given file and return its contents.\n *\n * @param file The path of the file to read.\n * @returns The files contents.\n */\nexport function readFile(file: string): string {\n const buffer = FS.readFileSync(file);\n switch (buffer[0]) {\n case 0xFE:\n if (buffer[1] === 0xFF) {\n let i = 0;\n while ((i + 1) < buffer.length) {\n const temp = buffer[i];\n buffer[i] = buffer[i + 1];\n buffer[i + 1] = temp;\n i += 2;\n }\n return buffer.toString('ucs2', 2);\n }\n break;\n case 0xFF:\n if (buffer[1] === 0xFE) {\n return buffer.toString('ucs2', 2);\n }\n break;\n case 0xEF:\n if (buffer[1] === 0xBB) {\n return buffer.toString('utf8', 3);\n }\n }\n\n return buffer.toString('utf8', 0);\n}\n"]}
\No newline at end of file