angular-ide
Version:
Provides a seamless integration with the Angular IDE from the command-line for developers looking for an enhanced development experience with Angular.
283 lines (234 loc) • 8.28 kB
JavaScript
const fs = require('fs');
const path = require('path');
const homedir = require('homedir');
const os = require('os');
const Rx = require('rxjs');
const chokidar = require('chokidar');
const readDir = Rx.Observable.bindNodeCallback(fs.readdir);
const readFile = Rx.Observable.bindNodeCallback(fs.readFile);
const mkDir = Rx.Observable.bindNodeCallback(fs.mkdir);
const fStat = Rx.Observable.bindNodeCallback(fs.stat);
const writeFile = Rx.Observable.bindNodeCallback(fs.writeFile);
function ls(dir) {
const algo = readDir(dir).map(function(files) {
return files;
});
return algo.combineLatest(Rx.Observable.of(dir));
}
function getProjectPath(openPath) {
return ls(openPath)
.expand(([filesInDirectory, d]) => {
const executableFilename = filesInDirectory.find((file) => {
return file === 'angular-cli.json' || file === '.angular-cli.json' || file === 'angular.json';
});
if (executableFilename) {
return Rx.Observable.empty();
} else {
const parentDir = path.resolve(d, '../');
const pathParsed = path.parse(parentDir);
if (parentDir === pathParsed.dir) {
return Rx.Observable.throw(new Error('root reached'));
}
return ls(parentDir);
}
})
.last();
}
function prepareMessage(payload) {
const replyPort = 12535;
const requestId = Date.now();
return Object.assign({
replyPort,
requestID: requestId,
}, payload);
}
function prepareImportProjectMessage(openPath) {
return prepareMessage({
method: 'importProject',
path: openPath,
});
}
function prepareOpenFileMessage(openPath) {
return prepareMessage({
method: 'openFile',
path: openPath,
});
}
function getMessageReceived(wsManager) {
return Rx.Observable.create((observer) => {
return Rx.Observable.fromEvent(wsManager, 'message', (ws, data) => {
return { ws, data };
})
.subscribe(wsEvent => {
const messageReceived = JSON.parse(wsEvent.data);
observer.next(messageReceived);
});
}).publish().refCount();
}
function ideHasProcessedOperation(wsManager) {
return Rx.Observable.create((observer) => {
observer.next(false);
wsManager.on('messageSent', () => {
observer.next(true);
});
});
}
function mustLaunchIDE(ideHasProcessedOp) {
return Rx.Observable.combineLatest(
Rx.Observable.timer(5000),
ideHasProcessedOp
)
.filter(([timer, hasProcessed]) => !hasProcessed);
};
function getExecutableLocation(installationPath, osType) {
let executableLocation = installationPath + path.sep;
switch (osType) {
case 'Linux':
executableLocation += 'angular-ide/angularide';
break;
case 'Darwin':
executableLocation += 'AngularIDE.app/Contents/MacOS/angularide';
break;
case 'Windows_NT':
executableLocation += 'angular-ide'+path.sep+'angularide.exe';
break;
default:
}
return executableLocation;
}
function getInstallPath(installPath, osType) {
let installPathLocation = installPath + path.sep;
switch (osType) {
case 'Linux':
installPathLocation += 'angular-ide' + path.sep;
break;
case 'Darwin':
installPathLocation += 'AngularIDE.app/Contents/Eclipse/';
break;
case 'Windows_NT':
installPathLocation += 'angular-ide' + path.sep;
break;
default:
}
return installPathLocation;
}
function registerInstallation(installPath, osType) {
const ideLocationsPath = path.resolve(homedir()+'/.webclipse/angular-ide.locations');
return fStat(path.dirname(ideLocationsPath)).catch(() => {
return mkDir(path.dirname(ideLocationsPath));
})
.flatMap(() => {
return fStat(ideLocationsPath).catch(() => {
return writeFile(ideLocationsPath, '[]');
});
})
.flatMap( x => {
return readFile(ideLocationsPath);
})
.map(fileContents => {
return JSON.parse(fileContents)
})
.flatMap(installations => {
const installationEntry = {
install: getInstallPath(installPath, osType),
workspace: null,
executable: getExecutableLocation(installPath, osType),
used: null,
};
installations.push(installationEntry);
return writeFile(ideLocationsPath, JSON.stringify(installations));
});
}
function getInstallations() {
const ideLocationsPath = path.resolve(homedir()+'/.webclipse/angular-ide.locations');
const watcher = chokidar.watch(ideLocationsPath, {
persistent: true,
});
return Rx.Observable.create((observer) => {
fStat(ideLocationsPath).first()
.subscribe(x => {
readFile(ideLocationsPath).subscribe((fileContents) => {;
const installs = JSON.parse(fileContents.toString('utf8'));
observer.next(installs);
});
watcher.on('all', (changed) => {
readFile(ideLocationsPath).subscribe((fileContents) => {;
const installs = JSON.parse(fileContents.toString('utf8'));
observer.next(installs);
});
});
}, e => {
observer.next([]);
});
});
}
function getRunningInstallations() {
return getInstallations()
.map(installations => {
return installations.filter(installation => installation.port);
});
}
function macStrategy(ideDirectory) {
return Rx.Observable.concat(
fStat(path.resolve(ideDirectory + '../MacOS/angularide')).map(x => ideDirectory + '../MacOS/angularide').doOnError( () => {
console.log('Failed finding executable in:', ideDirectory + '../MacOS/angularide');
}).catch(Rx.Observable.empty()),
fStat(path.resolve(ideDirectory + '../MacOS/eclipse')).map(x => ideDirectory + '../MacOS/eclipse').doOnError( () => {
console.log('Failed finding executable in:', ideDirectory + '../MacOS/eclipse');
}).catch(Rx.Observable.empty()),
fStat(path.resolve(ideDirectory + './eclipse')).map(x => ideDirectory + './eclipse').doOnError( () => {
console.log('Failed finding executable in:', ideDirectory + './eclipse');
}).catch(Rx.Observable.empty()),
fStat(path.resolve(ideDirectory + './Eclipse.app/Contents/MacOS/eclipse')).map(x => ideDirectory + './Eclipse.app/Contents/MacOS/eclipse').doOnError( () => {
console.log('Failed finding executable in:', ideDirectory + './Eclipse.app/Contents/MacOS/eclipse');
}).catch(Rx.Observable.empty())
);
}
function linuxStrategy(ideDirectory) {
}
function windowsStrategy(ideDirectory) {
return readDir(ideDirectory)
.first()
.map((filesInDirectory) => {
const executableFilename = filesInDirectory.find((file) => {
return file === 'eclipse.exe' || file === 'myeclipse.exe' || file === 'angularide.exe';
});
return executableFilename ? ideDirectory.concat(path.sep, executableFilename) : null;
});
}
function getIDEExecutablePath(ideInstallation) {
let executableFileStrategy = null;
const osType = os.type();
switch (osType) {
case 'Linux':
executableFileStrategy = linuxStrategy;
break;
case 'Darwin':
executableFileStrategy = macStrategy;
break;
case 'Windows_NT':
executableFileStrategy = windowsStrategy;
break;
default:
}
return Rx.Observable.of(ideInstallation.executable);
}
function getNPMPackage(packageName, prefix) {
const pathPrefix = prefix || path.resolve(__dirname, '../../');
const filename = path.join(pathPrefix, packageName, 'package.json');
if (!fs.existsSync(filename)) {
throw new Error(`Cannot find package file in folder ${packageName}`);
}
return JSON.parse(fs.readFileSync(filename, 'utf-8'));
}
module.exports.getIDEExecutablePath = getIDEExecutablePath;
module.exports.registerInstallation = registerInstallation;
module.exports.prepareImportProjectMessage = prepareImportProjectMessage;
module.exports.prepareOpenFileMessage = prepareOpenFileMessage;
module.exports.ideHasProcessedOperation = ideHasProcessedOperation;
module.exports.mustLaunchIDE = mustLaunchIDE;
module.exports.getInstallations = getInstallations;
module.exports.getRunningInstallations = getRunningInstallations;
module.exports.prepareMessage = prepareMessage;
module.exports.getProjectPath = getProjectPath;
module.exports.getNPMPackage = getNPMPackage;