UNPKG

alm

Version:

The best IDE for TypeScript

261 lines (260 loc) 9.4 kB
"use strict"; /** * The session manages the state associated with a browser window connection. * This allows us to restore tabs + userconfiguration etc. * We just drive the session id from the browser url */ Object.defineProperty(exports, "__esModule", { value: true }); var types = require("../../common/types"); var json = require("../../common/json"); var fsu = require("../utils/fsu"); var utils = require("../../common/utils"); var workingDir = require("./workingDir"); var commandLine = require("../commandLine"); var tsconfig = require("../workers/lang/core/tsconfig"); var sessionFile = types.cacheDir + '/sessionsV2.json'; /** * If there is no session then a default one will be created for you and sent over */ function getDefaultOrNewSession(sessionId) { var sessions = readDiskSessionsFile().sessions; /** * Cases to handle * if default select first (but if none create) * if duplicate then duplicate (but if none create and use that one) * if session id then search (but if none create) */ // if none create var ifNoneCreate = function (session, id) { if (id === void 0) { id = utils.createId(); } if (!session) { session = { id: id, tabLayout: { type: 'stack', width: 100, height: 100, tabs: [], subItems: [], activeItemIndex: 0, }, lastUsed: new Date().getTime(), selectedTabId: null, }; writeDiskSession(session); return session; } else { return session; } }; var session; if (commandLine.getOptions().debug) { session = ifNoneCreate(sessions.find(function (session) { return session.id === types.urlHashDebugSession; }), types.urlHashDebugSession); } else if (!sessionId || sessionId === types.urlHashNormal) { session = ifNoneCreate(sessions.filter(function (s) { return s.id !== types.urlHashDebugSession; })[0]); } else if (sessionId === types.urlHashNewSession) { session = sessions[0]; // last used is always on top if (session) { session = { id: utils.createId(), tabLayout: session.tabLayout, lastUsed: new Date().getTime(), selectedTabId: session.selectedTabId, }; writeDiskSession(session); } else { session = ifNoneCreate(session); } } else { session = ifNoneCreate(sessions.find(function (session) { return session.id === sessionId; })); } /** * Update the session on disk for future calls to be stable */ var commandLineTabs = getCommandLineTabs(); if (commandLineTabs.length) { // Utility to find first `stack` and just add to its tabs var done_1 = false; var tryAddingToStack_1 = function (layout) { if (done_1) return; if (layout.type === 'stack') { layout.tabs = layout.tabs.concat(commandLineTabs); done_1 = true; } else { layout.subItems.forEach(tryAddingToStack_1); } }; // Start at root tryAddingToStack_1(session.tabLayout); /** Write it out */ writeDiskSession(session); } return session; } exports.getDefaultOrNewSession = getDefaultOrNewSession; /** * Only returns the command line tabs once * Means you can call it as many times as you like */ function getCommandLineTabs() { /** Add any command line files to the session */ var files = commandLine.getOptions().filePaths; var tabs = files .map(function (file) { return utils.getUrlFromFilePathAndProtocol({ protocol: 'file', filePath: file }); }) .map(function (url) { return workingDir.makeRelativeUrl(url); }) .map(function (relativeUrl) { return ({ id: utils.createId(), relativeUrl: relativeUrl, additionalData: null }); }); // clear for future // Doing it multiple times would mean that we would polute the user session on each new tab opening commandLine.getOptions().filePaths = []; return tabs; } /** * UI to disk and Disk to UI helpers */ function uiToDiskTab(uiTab) { var relativeUrl = workingDir.makeRelativeUrl(uiTab.url); return { id: uiTab.id, relativeUrl: relativeUrl, additionalData: uiTab.additionalData, }; } function diskToUITab(diskTab) { var url = workingDir.makeAbsoluteUrl(diskTab.relativeUrl); var _a = utils.getFilePathAndProtocolFromUrl(url), filePath = _a.filePath, protocol = _a.protocol; if (protocol === 'file' && !fsu.existsSync(filePath)) return null; return { id: diskTab.id, url: url, additionalData: diskTab.additionalData, }; } function uiToDiskTabLayout(uiLayout) { return { type: uiLayout.type, width: uiLayout.width, height: uiLayout.height, tabs: uiLayout.tabs.map(uiToDiskTab), subItems: uiLayout.subItems.map(uiToDiskTabLayout), activeItemIndex: uiLayout.activeItemIndex, }; } function diskToUITabLayout(diskLayout) { return { type: diskLayout.type, width: diskLayout.width, height: diskLayout.height, tabs: diskLayout.tabs.map(diskToUITab).filter(function (x) { return !!x; }), subItems: diskLayout.subItems.map(diskToUITabLayout), activeItemIndex: diskLayout.activeItemIndex, }; } function readDiskSessionsFile() { var sessionFileContents = { sessions: [] }; if (fsu.existsSync(sessionFile) && !commandLine.getOptions().safe) { var contents = json.parse(fsu.readFile(sessionFile)); if (contents.data) { sessionFileContents = contents.data; } } /** * Active project setup logic. In decreasing order * - If there is an active project in the command line * - If there is an active project in the last session * - Common locations */ var commonTsconfigLocations = [ '.', './src', './ts', './lib', './App-UI/src' ].map(function (x) { return x + '/tsconfig.json'; }); if (commandLine.getOptions().init) { sessionFileContents.relativePathToTsconfig = './tsconfig.json'; // Write the tsconfig.json file tsconfig.createProjectRootSync(workingDir.getProjectRoot()); writeDiskSessionFile(sessionFileContents); // Clear so we don't keep creating commandLine.getOptions().init = false; } else if (commandLine.getOptions().project) { sessionFileContents.relativePathToTsconfig = workingDir.makeRelative(commandLine.getOptions().project); writeDiskSessionFile(sessionFileContents); } else if (!sessionFileContents.relativePathToTsconfig) { var found = commonTsconfigLocations.find(function (cl) { return fsu.existsSync(cl); }); if (found) { sessionFileContents.relativePathToTsconfig = found; writeDiskSessionFile(sessionFileContents); } } return sessionFileContents; } exports.readDiskSessionsFile = readDiskSessionsFile; function writeDiskSession(session) { // Update last used time session.lastUsed = new Date().getTime(); var sessionFileContents = readDiskSessionsFile(); // Merge with what is on disk by id var sessions = sessionFileContents.sessions .filter(function (sesh) { return sesh.id !== session.id; }); sessions.unshift(session); // last used is always on top sessionFileContents.sessions = sessions; writeDiskSessionFile(sessionFileContents); } function writeDiskSessionFile(sessionFileContents) { fsu.writeFile(sessionFile, json.stringify(sessionFileContents)); } function setTsconfigPath(tsconfigFilePath) { var sessionFileContents = readDiskSessionsFile(); sessionFileContents.relativePathToTsconfig = workingDir.makeRelative(tsconfigFilePath); writeDiskSessionFile(sessionFileContents); } exports.setTsconfigPath = setTsconfigPath; function setOpenUITabs(sessionId, layout, selectedTabId) { var session = getDefaultOrNewSession(sessionId); session.tabLayout = uiToDiskTabLayout(layout); session.selectedTabId = selectedTabId; writeDiskSession(session); } exports.setOpenUITabs = setOpenUITabs; function getOpenUITabs(sessionId) { var session = getDefaultOrNewSession(sessionId); return { tabLayout: diskToUITabLayout(session.tabLayout), selectedTabId: session.selectedTabId }; } exports.getOpenUITabs = getOpenUITabs; function getValidSessionId(sessionId) { var session = getDefaultOrNewSession(sessionId); return { sessionId: session.id }; } exports.getValidSessionId = getValidSessionId; /** * For various simple settings * setter */ function setSetting(config) { var session = getDefaultOrNewSession(config.sessionId); session[config.settingId] = config.value; writeDiskSession(session); } exports.setSetting = setSetting; /** * For various simple settings * getter */ function getSetting(config) { var session = getDefaultOrNewSession(config.sessionId); return session[config.settingId]; } exports.getSetting = getSetting;