Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(screen-orientation): make OrientationLockType available in TS 5.2+ #1929

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
16 changes: 14 additions & 2 deletions screen-orientation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down Expand Up @@ -40,11 +50,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",
Expand All @@ -54,6 +65,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",
Expand Down
89 changes: 89 additions & 0 deletions screen-orientation/scripts/ts5-2_patch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* global console */
// @ts-check
/// <reference types="node" />
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();
Loading