particle-commands
Version:
Library of UX-neutral commands that provide key functionality for developer tools
198 lines (153 loc) • 8.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.LibraryMigrateCommand = exports.LibraryMigrateTestCommand = exports.LibraryMigrateCommandSite = undefined;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _command = require('./command');
var _particleLibraryManager = require('particle-library-manager');
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _library_install = require('./library_install');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var LibraryMigrateCommandSite = exports.LibraryMigrateCommandSite = function (_CommandSite) {
_inherits(LibraryMigrateCommandSite, _CommandSite);
function LibraryMigrateCommandSite() {
_classCallCheck(this, LibraryMigrateCommandSite);
return _possibleConstructorReturn(this, (LibraryMigrateCommandSite.__proto__ || Object.getPrototypeOf(LibraryMigrateCommandSite)).apply(this, arguments));
}
_createClass(LibraryMigrateCommandSite, [{
key: 'getLibraries',
/**
* Provides the list of library directories to process.
* Can return a value or a promise.
*/
value: function getLibraries() {}
/**
* Notify that the given library is being migrated.
* @param {string} _dir The directory containing the library
*/
}, {
key: 'notifyStart',
value: function notifyStart(_dir) {}
/**
*
* @param {string} _lib The directory containing the library that migration was attempted on.
* @param {object} _result There result of the migration.
* @param {object} _err if defined, is the error that occurred migrating the library.
*/
}, {
key: 'notifyEnd',
value: function notifyEnd(_lib, _result, _err) {}
}, {
key: 'isAdaptersRequired',
value: function isAdaptersRequired() {
return false;
}
}]);
return LibraryMigrateCommandSite;
}(_command.CommandSite);
var AbstractLibraryMigrateCommand = function (_Command) {
_inherits(AbstractLibraryMigrateCommand, _Command);
function AbstractLibraryMigrateCommand() {
_classCallCheck(this, AbstractLibraryMigrateCommand);
return _possibleConstructorReturn(this, (AbstractLibraryMigrateCommand.__proto__ || Object.getPrototypeOf(AbstractLibraryMigrateCommand)).apply(this, arguments));
}
_createClass(AbstractLibraryMigrateCommand, [{
key: 'run',
/**
* Executes the library command.
* @param {object} state Conversation state
* @param {LibraryMigrateCommandSite} site Conversation interface
* @return {Array<object>} Returns a promise for an array, one index for each library processed.
* Each element has properties:
* - libdir: the directory of the library
* - result: result of running `processLibrary()` if no errors were produced.
* - err: any error that was produced.
*/
value: async function run(state, site) {
var _this3 = this;
var libraries = await site.getLibraries();
var promises = libraries.map(function (libdir) {
return _this3._executeLibrary(libdir, state, site);
});
return Promise.all(promises);
}
}, {
key: '_executeLibrary',
value: async function _executeLibrary(libdir, state, site) {
await site.notifyStart(libdir);
var dir = _path2.default.resolve(libdir);
var repo = new _particleLibraryManager.FileSystemLibraryRepository(dir, _particleLibraryManager.FileSystemNamingStrategy.DIRECT);
var _ref = await this.processLibrary(repo, '', state, site, libdir),
_ref2 = _slicedToArray(_ref, 2),
res = _ref2[0],
err = _ref2[1];
await site.notifyEnd(libdir, res, err);
return { libdir: libdir, res: res, err: err };
}
/**
* Handle migration of a single library.
* @param {FileSystemLibraryRepo} _repo The filesystem repo containing the library.
* @param {string} _libname The identifier of the library
* @param {object} _state the current command state
* @param {LibraryMigrateCommandSite} _site the command site
*/
}, {
key: 'processLibrary',
value: function processLibrary(_repo, _libname, _state, _site) {}
}]);
return AbstractLibraryMigrateCommand;
}(_command.Command);
function resultError(promise) {
return promise.then(function (result) {
return [result, null];
}).catch(function (err) {
return [null, err];
});
}
var LibraryMigrateTestCommand = exports.LibraryMigrateTestCommand = function (_AbstractLibraryMigra) {
_inherits(LibraryMigrateTestCommand, _AbstractLibraryMigra);
function LibraryMigrateTestCommand() {
_classCallCheck(this, LibraryMigrateTestCommand);
return _possibleConstructorReturn(this, (LibraryMigrateTestCommand.__proto__ || Object.getPrototypeOf(LibraryMigrateTestCommand)).apply(this, arguments));
}
_createClass(LibraryMigrateTestCommand, [{
key: 'processLibrary',
value: function processLibrary(repo, libname, _state, _site, _libdir) {
return resultError(repo.getLibraryLayout(libname));
}
}]);
return LibraryMigrateTestCommand;
}(AbstractLibraryMigrateCommand);
var LibraryMigrateCommand = exports.LibraryMigrateCommand = function (_AbstractLibraryMigra2) {
_inherits(LibraryMigrateCommand, _AbstractLibraryMigra2);
function LibraryMigrateCommand() {
_classCallCheck(this, LibraryMigrateCommand);
return _possibleConstructorReturn(this, (LibraryMigrateCommand.__proto__ || Object.getPrototypeOf(LibraryMigrateCommand)).apply(this, arguments));
}
_createClass(LibraryMigrateCommand, [{
key: 'processLibrary',
value: async function processLibrary(repo, libname, state, site, libdir) {
return resultError(this._processLibrary(repo, libname, state, site, libdir));
}
}, {
key: '_processLibrary',
value: async function _processLibrary(repo, libname, state, site, libdir) {
var layout = await repo.getLibraryLayout(libname);
if (layout === 2) {
return false;
}
await repo.setLibraryLayout(libname, 2);
if (site.isAdaptersRequired()) {
await (0, _library_install.buildAdapters)(libdir, libname);
}
return true;
}
}]);
return LibraryMigrateCommand;
}(AbstractLibraryMigrateCommand);