nodekit-cli-lib
Version:
Command-line helpers for NodeKit command line interface
86 lines (70 loc) • 3.64 kB
JavaScript
/**
Licensed to OffGrid Networks (OGN) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. OGN licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
var path = require('path');
var util = require('../nodekit/util');
var platforms = require('./platformsConfig.json');
var events = require('nodekit-cli-lib')['nodekit-common'].events;
// Avoid loading the same platform projects more than once (identified by path)
var cachedApis = {};
// getPlatformApi() should be the only method of instantiating the
// PlatformProject classes for now.
function getPlatformApi(platform, platformRootDir) {
// if platformRootDir is not specified, try to detect it first
if (!platformRootDir) {
var projectRootDir = util.isNodeKit();
platformRootDir = projectRootDir && path.join(projectRootDir, 'platforms', platform);
}
if (!platformRootDir) {
// If platformRootDir is still undefined, then we're probably is not inside of nodekit project
throw new Error('Current location is not a NodeKit project');
}
// CB-11174 Resolve symlinks first before working with root directory
platformRootDir = util.convertToRealPathSafe(platformRootDir);
var cached = cachedApis[platformRootDir];
if (cached && cached.platform == platform) return cached;
if (!platforms[platform]) throw new Error('Unknown platform ' + platform);
var PlatformApi;
try {
// First we need to find whether platform exposes its' API via js module
// If it does, then we require and instantiate it.
var platformApiModule = path.join(platformRootDir, 'nodekit', 'Api.js');
PlatformApi = require(platformApiModule);
} catch (err) {
// Check if platform already compatible w/ PlatformApi and show deprecation warning
if (err && err.code === 'MODULE_NOT_FOUND') {
if (platforms[platform].apiCompatibleSince) {
events.emit('warn', ' Using this version of NodeKit with older version of nodekit-' + platform +
' is being deprecated. Consider upgrading to nodekit-' + platform + '@' +
platforms[platform].apiCompatibleSince + ' or newer.');
}
// else nothing - there is no Api.js and no deprecation information hence
// the platform just does not expose Api and we will use polyfill as usual
} else {
events.emit('warn', 'Error loading nodekit-'+platform);
}
PlatformApi = require('./PlatformApiPoly');
}
var platformApi = new PlatformApi(platform, platformRootDir, events);
cachedApis[platformRootDir] = platformApi;
return platformApi;
}
module.exports = platforms;
// We don't want these methods to be enumerable on the platforms object, because we expect enumerable properties of the
// platforms object to be platforms.
Object.defineProperties(module.exports, {
'getPlatformApi': {value: getPlatformApi, configurable: true, writable: true}
});