const Sentry = require('@sentry/node'); Sentry.init({ dsn: 'https://f298f9ce12e34065a3df8785863a4ecc@sentry.io/33357' }); import * as fs from "fs"; import * as path from "path"; import Uri from "vscode-uri"; export default class EkoProject { public rootFolder: string; constructor() { this.rootFolder = 'src'; } private getRootDirPath(documentPath: any): string { const legacyRootIndicator = 'eko.json'; const rootIndicator = 'package.json'; let projectRoot = null; let projectPath = Uri.parse(documentPath).fsPath; let pathArr = projectPath.split(path.sep); pathArr.pop(); while (pathArr.length) { if (pathArr.length === 1) { pathArr[0] += path.sep; } const testFolder = pathArr.join(path.sep); const files = fs.readdirSync(testFolder); if (files.includes(legacyRootIndicator)) { projectRoot = pathArr.join(path.sep); } else if (files.includes(rootIndicator)) { let projectPkg; try { projectPkg = JSON.parse(fs.readFileSync(`${testFolder}/${rootIndicator}`, 'utf-8')); } catch (err) {} if (projectPkg && typeof projectPkg.eko === 'object') { projectRoot = pathArr.join(path.sep); } } pathArr.pop(); } return projectRoot; } public getPath(documentPath: any): string { let projectPath = null; if ((typeof documentPath === 'string') && documentPath.split("/").indexOf(this.rootFolder) > 0) { const rootPath = documentPath .split("/") .slice(0, documentPath.split("/").indexOf(this.rootFolder) + 1) .join("/"); const fsPath = Uri.parse(rootPath).fsPath; if (fs.readdirSync(fsPath).includes("_eko_")) { projectPath = fsPath; } } return projectPath; } public addAPICompletions(documentUri: any, apiCompletions: any): void { const rootDirPath = this.getRootDirPath(documentUri); const jsconfigFilePath = `${(rootDirPath) ? rootDirPath + path.sep : ''}jsconfig.json`; const declarationsFilePath = `${(rootDirPath) ? rootDirPath + path.sep : ''}autocomplete.d.ts`; if (!fs.existsSync(jsconfigFilePath)) { this.addJSConfigFile(jsconfigFilePath); } else { fs.readFile(jsconfigFilePath, 'utf-8', (err, data: string) => { if (err) { Sentry.captureException(err); } else if (!data) { this.addJSConfigFile(jsconfigFilePath); } }); } if (apiCompletions) { this.addDeclarationsFile(declarationsFilePath, apiCompletions); } } private addJSConfigFile(jsconfigFilePath: any): void { const JSCONFIG_DATA = { include: ['src'], files: ['autocomplete.d.ts'] }; fs.writeFile(jsconfigFilePath, JSON.stringify(JSCONFIG_DATA), (err) => { if (err) { Sentry.captureException(err); } }); } private addDeclarationsFile(declarationsFilePath: string, apiCompletions: any): void { fs.writeFile(declarationsFilePath, apiCompletions, (err) => { if (err) { Sentry.captureException(err); } }); } }