UNPKG

alm

Version:

The best IDE for TypeScript

172 lines (171 loc) 6.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * All our interaction with the file system generally goes through here */ var fileModel_1 = require("./fileModel"); var events_1 = require("../../common/events"); var fsu = require("../utils/fsu"); var chalk = require("chalk"); exports.savedFileChangedOnDisk = new events_1.TypedEvent(); exports.didEdits = new events_1.TypedEvent(); exports.didStatusChange = new events_1.TypedEvent(); exports.editorOptionsChanged = new events_1.TypedEvent(); exports.didOpenFile = new events_1.TypedEvent(); var openFiles = []; function getOpenFile(filePath) { if (openFiles.some(function (f) { return f.config.filePath == filePath; })) { return openFiles.filter(function (f) { return f.config.filePath == filePath; })[0]; } } exports.getOpenFile = getOpenFile; function getOrCreateOpenFile(filePath, autoCreate) { if (autoCreate === void 0) { autoCreate = false; } filePath = fsu.consistentPath(filePath); var file = getOpenFile(filePath); if (!file) { /** If you request a file that isn't there ... we are going to create it */ if (!fsu.existsSync(filePath) && autoCreate) { fsu.writeFile(filePath, ''); } file = new fileModel_1.FileModel({ filePath: filePath }); exports.didOpenFile.emit({ filePath: filePath, contents: file.getContents() }); file.onSavedFileChangedOnDisk.on(function (evt) { exports.savedFileChangedOnDisk.emit({ filePath: filePath, contents: evt.contents }); }); file.didEdits.on(function (evt) { exports.didEdits.emit({ filePath: filePath, edits: evt.codeEdits }); }); file.didStatusChange.on(function (evt) { exports.didStatusChange.emit({ filePath: filePath, saved: evt.saved, eol: evt.eol }); }); file.editorOptionsChanged.on(function (editorOptions) { exports.editorOptionsChanged.emit({ filePath: filePath, editorOptions: editorOptions }); }); openFiles.push(file); } return file; } exports.getOrCreateOpenFile = getOrCreateOpenFile; function closeOpenFile(filePath) { var file = getOpenFile(filePath); if (file) { file.save(); // Right now we still keep the file open indefinitely // openFiles = openFiles.filter(f=> f.config.filePath !== filePath); } } exports.closeOpenFile = closeOpenFile; function getOpenFiles() { return openFiles; } exports.getOpenFiles = getOpenFiles; function isFileOpen(filePath) { return !!getOpenFile(filePath); } exports.isFileOpen = isFileOpen; function saveOpenFile(filePath) { var file = getOpenFile(filePath); file.save(); } exports.saveOpenFile = saveOpenFile; /** * Editor Config Stuff */ function watchedEditorConfigChanged() { getOpenFiles().forEach(function (fm) { return fm.recheckEditorOptions(); }); // TODO: // Recheck editor config for all open files :-/ // The files should emit '`editorConfigChanged`' individually // We should be listening to these events and pushing them out // The front end editor should be listening to this event by filePath too. } exports.watchedEditorConfigChanged = watchedEditorConfigChanged; /** * File Tree managment functions */ var mkdirp = require("mkdirp"); function addFolder(filePath) { mkdirp.sync(filePath); } exports.addFolder = addFolder; function deleteFromDisk(data) { data.files.forEach(function (filePath) { var file = getOpenFile(filePath); if (file) { file.delete(); openFiles = openFiles.filter(function (f) { return f.config.filePath !== filePath; }); } else { fsu.deleteFile(filePath); } }); data.dirs.forEach(function (dirPath) { // delete any open files var toClose = function (filePath) { return filePath.startsWith(dirPath); }; openFiles.filter(function (f) { return toClose(f.config.filePath); }).forEach(function (f) { return f.delete(); }); openFiles = openFiles.filter(function (f) { return !toClose(f.config.filePath); }); // delete the dir fsu.deleteDir(dirPath); }); } exports.deleteFromDisk = deleteFromDisk; function duplicateFile(data) { var contents = fsu.readFile(data.src); fsu.writeFile(data.dest, contents); } exports.duplicateFile = duplicateFile; var ncp_1 = require("ncp"); function duplicateDir(data) { return new Promise(function (resolve) { ncp_1.ncp(data.src, data.dest, function (err) { if (err) console.log('Move failed', err); resolve(JSON.stringify(err)); }); }); } exports.duplicateDir = duplicateDir; var mv = require("mv"); function movePath(data) { return new Promise(function (resolve) { mv(data.src, data.dest, { mkdirp: true, clobber: true }, function (err) { if (err) console.log('Move failed', err); resolve(JSON.stringify(err)); }); }); } exports.movePath = movePath; var open = require("open"); function launchDirectory(data) { return new Promise(function (resolve) { open(data.filePath); resolve({ error: null }); }); } exports.launchDirectory = launchDirectory; var cp = require("child_process"); function launchTerminal(data) { return new Promise(function (resolve) { if (process.platform === 'darwin') { cp.execSync("osascript -e 'tell application \"Terminal\" to activate' -e 'tell application \"Terminal\" to do script \"cd " + data.filePath + "\"'"); } else if (process.platform === 'win32') { cp.execSync("start cmd.exe /K \"cd " + data.filePath + "\""); } else { // http://stackoverflow.com/a/31737949/390330 console.error(chalk.red("We don't have a command for your OS. Would love for you to help us")); } resolve({ error: null }); }); } exports.launchTerminal = launchTerminal;