UNPKG

lacona-osx

Version:

Lacona Extensions for OSX, tied into the global-context hosted API

162 lines (134 loc) 4.88 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectoryPath = exports.FilePath = undefined; var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); var _elliptical = require('elliptical'); var _laconaPhrases = require('lacona-phrases'); var _path = require('path'); var _Observable = require('rxjs/Observable'); var _laconaApi = require('lacona-api'); var _fs = require('fs'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /** @jsx createElement */ function fetchPath(dir, file) { return new Promise((resolve, reject) => { (0, _fs.stat)((0, _path.join)(dir, file), (err, stats) => { if (err) { reject(err); } else { resolve({ file, isDir: stats.isDirectory() }); } }); }); } function fetchDirectoryContents(dir) { return new Promise((resolve, reject) => { (0, _fs.readdir)(dir, (err, files) => { if (err) { reject(err); } else { const infoPromises = files.map(file => fetchPath(dir, file)); resolve(Promise.all(infoPromises)); } }); }); } const DirectorySource = { fetch({ props }) { return new _Observable.Observable((() => { var _ref = _asyncToGenerator(function* (observer) { observer.next([]); const expandedPath = props.path.replace(/^~/, (0, _laconaApi.userHome)()); const files = yield fetchDirectoryContents(expandedPath); const absDirs = _lodash2.default.chain(files).filter('isDir').map(function ({ file }) { return { text: (0, _path.join)(props.path, file) + '/', value: (0, _path.join)(expandedPath, file) + '/' }; }).value(); observer.next([{ text: props.path, value: expandedPath }].concat(absDirs)); }); return function (_x) { return _ref.apply(this, arguments); }; })()); }, clear: true }; const FileSource = { fetch({ props }) { return new _Observable.Observable((() => { var _ref2 = _asyncToGenerator(function* (observer) { observer.next([]); const expandedPath = props.path.replace(/^~/, (0, _laconaApi.userHome)()); const files = yield fetchDirectoryContents(expandedPath); const absFiles = _lodash2.default.chain(files).filter(function (file) { return !file.isDir; }).map(function ({ file }) { return { text: (0, _path.join)(props.path, file), value: (0, _path.join)(expandedPath, file) }; }).value(); observer.next(absFiles); }); return function (_x2) { return _ref2.apply(this, arguments); }; })()); }, clear: true // function observeFiles (input) { // const path = _.endsWith(input, '/') ? input : dirname(input) // return <FileSource path={path} /> // } // function observeDirectories (input) { // const path = _.endsWith(input, '/') ? input : dirname(input) // return <DirectorySource path={path} /> // } };function describeFile(input, observe, Source) { if (/^(~\/|\/)/.test(input)) { const path = _lodash2.default.endsWith(input, '/') ? input : (0, _path.dirname)(input); const data = observe((0, _elliptical.createElement)(Source, { path: path })); const items = _lodash2.default.map(data, ({ text, value }) => ({ text, value, annotation: { type: 'icon', value } })); return (0, _elliptical.createElement)('list', { items: items }); } } const FilePath = exports.FilePath = { extends: [_laconaPhrases.File], describe({ props, observe }) { return (0, _elliptical.createElement)( 'placeholder', { argument: 'path' }, (0, _elliptical.createElement)('dynamic', { describe: input => describeFile(input, observe, FileSource), greedy: true, splitOn: props.splitOn, limit: 1 }) ); } }; const DirectoryPath = exports.DirectoryPath = { extends: [_laconaPhrases.Directory], describe({ props, observe }) { return (0, _elliptical.createElement)( 'placeholder', { argument: 'path' }, (0, _elliptical.createElement)('dynamic', { describe: input => describeFile(input, observe, DirectorySource), greedy: true, splitOn: props.splitOn, limit: 1 }) ); } };