UNPKG

@iotechpis/strapi-plugin-schemas-to-ts

Version:

A Strapi plugin that automatically generates Typescript interfaces for Strapi entities and components.

66 lines (65 loc) 2.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileHelpers = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); class FileHelpers { static ensureFolderPathExistRecursive(...subfolders) { let folder = strapi.dirs.app.src; for (const subfolder of subfolders) { folder = path_1.default.join(folder, subfolder); if (!fs_1.default.existsSync(folder)) { fs_1.default.mkdirSync(folder); } } return folder; } static folderExists(folderPath) { try { return fs_1.default.statSync(folderPath).isDirectory(); } catch (err) { return false; } } static fileExists(filePath) { try { return fs_1.default.statSync(filePath).isFile(); } catch { return false; } } static writeInterfaceFile(folderPath, fileName, interfacesFileContent) { let writeFile = true; const destinationPath = path_1.default.join(folderPath, fileName); if (FileHelpers.fileExists(destinationPath)) { const fileContent = fs_1.default.readFileSync(destinationPath, 'utf8'); if (fileContent === interfacesFileContent) { console.log(`File ${destinationPath} is up to date.`); writeFile = false; } } if (writeFile) { console.log(`Writing file ${destinationPath}`); fs_1.default.writeFileSync(destinationPath, interfacesFileContent, 'utf8'); } } static getRelativePath(fromPath, toPath) { let stat = fs_1.default.statSync(fromPath); if (stat.isDirectory()) { // path.relative works better with file paths, so we add an unexisting file to the route fromPath += '/.dumbFile.txt'; } stat = fs_1.default.statSync(toPath); if (stat.isDirectory()) { toPath += '/.dumbFile.txt'; } const relativePath = path_1.default.relative(path_1.default.dirname(fromPath), path_1.default.dirname(toPath)); return relativePath === '' ? './' : relativePath; } } exports.FileHelpers = FileHelpers;