From acc7d1de666470b19574a6f5921d2b2ae7e6c69b Mon Sep 17 00:00:00 2001 From: IT-MikeS <20338451+IT-MikeS@users.noreply.github.com> Date: Thu, 30 Nov 2023 16:16:58 -0500 Subject: [PATCH] fix(screen-orientation): make OrientationLockType available in TS 5.2+ --- .vscode/settings.json | 3 + screen-orientation/package.json | 16 +++- screen-orientation/scripts/ts5-2_patch.mjs | 89 ++++++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 screen-orientation/scripts/ts5-2_patch.mjs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..3662b3700 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib" +} \ No newline at end of file diff --git a/screen-orientation/package.json b/screen-orientation/package.json index d15d37291..5c592d3bd 100644 --- a/screen-orientation/package.json +++ b/screen-orientation/package.json @@ -5,6 +5,16 @@ "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", "types": "dist/esm/index.d.ts", + "typesVersions": { + ">=5.2": { + "dist/esm/index.d.ts": [ + "dist/esm/TS_5-2/index.d.ts" + ], + "dist/esm/definitions.d.ts": [ + "dist/esm/TS_5-2/definitions.d.ts" + ] + } + }, "unpkg": "dist/plugin.js", "files": [ "android/src/main/", @@ -38,11 +48,12 @@ "prettier": "prettier \"**/*.{css,html,ts,js,java}\"", "swiftlint": "node-swiftlint", "docgen": "docgen --api ScreenOrientationPlugin --output-readme README.md --output-json dist/docs.json", - "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.js", + "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.js && npm run patch-ts-5_2", "clean": "rimraf ./dist", "watch": "tsc --watch", "prepublishOnly": "npm run build", - "publish:cocoapod": "pod trunk push ./CapacitorScreenOrientation.podspec --allow-warnings" + "publish:cocoapod": "pod trunk push ./CapacitorScreenOrientation.podspec --allow-warnings", + "patch-ts-5_2": "node ./scripts/ts5-2_patch.mjs" }, "devDependencies": { "@capacitor/android": "next", @@ -52,6 +63,7 @@ "@ionic/eslint-config": "^0.3.0", "@ionic/prettier-config": "~1.0.1", "@ionic/swiftlint-config": "^1.1.2", + "@types/node": "18.11.9", "eslint": "^7.11.0", "prettier": "~2.3.0", "prettier-plugin-java": "~1.0.2", diff --git a/screen-orientation/scripts/ts5-2_patch.mjs b/screen-orientation/scripts/ts5-2_patch.mjs new file mode 100644 index 000000000..0a8f737c2 --- /dev/null +++ b/screen-orientation/scripts/ts5-2_patch.mjs @@ -0,0 +1,89 @@ +/* global console */ +// @ts-check +/// +import { + readdir, + mkdir, + copyFile, + unlink, + rmdir, + access, + readFile, + writeFile, +} from 'node:fs/promises'; +import { join, extname } from 'node:path'; + +const sourceDir = './dist/esm'; +const targetDir = './dist/esm/TS_5-2'; + +const replaceInFile = async (filePath, regex, replacement) => { + try { + const data = await readFile(filePath, 'utf8'); + const result = data.replace(regex, replacement); + await writeFile(filePath, result, 'utf8'); + } catch (err) { + console.error(`Error processing file ${filePath}: ${err}`); + } +}; + +async function deleteDirectory(dir) { + let entries = await readdir(dir, { withFileTypes: true }); + + await Promise.all( + entries.map(entry => { + let fullPath = join(dir, entry.name); + return entry.isDirectory() ? deleteDirectory(fullPath) : unlink(fullPath); + }), + ); + + await rmdir(dir); +} + +async function copyDtsFilesAndPatch() { + try { + // Delete target directory if it exists + if ( + await access(targetDir) + .then(() => true) + .catch(() => false) + ) { + await deleteDirectory(targetDir); + } + // Create target directory if it doesn't exist + await mkdir(targetDir, { recursive: true }); + + const files = await readdir(sourceDir); + + for (const file of files) { + if (extname(file) === '.ts') { + const sourceFile = join(sourceDir, file); + const targetFile = join(targetDir, file); + + await copyFile(sourceFile, targetFile); + } + } + + const regex = + /import\stype\s\{\sPluginListenerHandle\s\}\sfrom\s'@capacitor\/core';/; + const replacement = `import type { PluginListenerHandle } from '@capacitor/core'; +declare global { +\ttype OrientationLockType = +\t\t| 'any' +\t\t| 'natural' +\t\t| 'landscape' +\t\t| 'portrait' +\t\t| 'portrait-primary' +\t\t| 'portrait-secondary' +\t\t| 'landscape-primary' +\t\t| 'landscape-secondary'; +}`; + const fileToPatch = join(targetDir, 'definitions.d.ts'); + await replaceInFile(fileToPatch, regex, replacement); + + console.log('TypeScript declaration files for TS 5.2+ Patched.'); + } catch (err) { + console.error('Error occurred:', err); + } +} + +copyDtsFilesAndPatch();