GT2/GT2-iOS/node_modules/xdl/build/project/ProjectUtils.js

335 lines
10 KiB
JavaScript
Raw Normal View History

2018-02-12 17:26:06 +00:00
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.readConfigJsonAsync = exports.setCustomConfigPath = exports.readExpRcAsync = exports.configFilenameAsync = exports.fileExistsAsync = undefined;
let fileExistsAsync = exports.fileExistsAsync = (() => {
var _ref = _asyncToGenerator(function* (file) {
try {
return (yield (_fs2 || _load_fs()).default.stat(file)).isFile();
} catch (e) {
return false;
}
});
return function fileExistsAsync(_x) {
return _ref.apply(this, arguments);
};
})();
let configFilenameAsync = exports.configFilenameAsync = (() => {
var _ref2 = _asyncToGenerator(function* (projectRoot) {
// we should always default to exp.json, and only use app.json if it exists
const appJsonExists = yield fileExistsAsync(_path.default.join(projectRoot, 'app.json'));
const expJsonExists = yield fileExistsAsync(_path.default.join(projectRoot, 'exp.json'));
if (appJsonExists) {
return 'app.json';
} else if (expJsonExists || (_Config || _load_Config()).default.developerTool !== 'crna') {
return 'exp.json';
} else {
return 'app.json';
}
});
return function configFilenameAsync(_x2) {
return _ref2.apply(this, arguments);
};
})();
let readExpRcAsync = exports.readExpRcAsync = (() => {
var _ref3 = _asyncToGenerator(function* (projectRoot) {
const expRcPath = _path.default.join(projectRoot, '.exprc');
if (!_fs.default.existsSync(expRcPath)) {
return {};
}
try {
return yield new (_jsonFile || _load_jsonFile()).default(expRcPath, { json5: true }).readAsync();
} catch (e) {
logError(projectRoot, 'expo', `Error parsing JSON file: ${e.toString()}`);
return {};
}
});
return function readExpRcAsync(_x3) {
return _ref3.apply(this, arguments);
};
})();
let setCustomConfigPath = exports.setCustomConfigPath = (() => {
var _ref4 = _asyncToGenerator(function* (projectRoot, configPath) {
customConfigPaths[projectRoot] = configPath;
});
return function setCustomConfigPath(_x4, _x5) {
return _ref4.apply(this, arguments);
};
})();
let readConfigJsonAsync = exports.readConfigJsonAsync = (() => {
var _ref5 = _asyncToGenerator(function* (projectRoot) {
let exp;
let pkg;
let configPath, configName;
if (customConfigPaths[projectRoot]) {
configPath = customConfigPaths[projectRoot];
configName = _path.default.basename(configPath);
} else {
configName = yield configFilenameAsync(projectRoot);
configPath = _path.default.join(projectRoot, configName);
}
try {
exp = yield new (_jsonFile || _load_jsonFile()).default(configPath, { json5: true }).readAsync();
if (configName === 'app.json') {
// if we're not using exp.json, then we've stashed everything under an expo key
// this is only for app.json at time of writing
exp = exp.expo;
}
} catch (e) {
if (e.isJsonFileError) {
// TODO: add error codes to json-file
if (e.message.startsWith('Error parsing JSON file')) {
logError(projectRoot, 'expo', `Error parsing JSON file: ${e.cause.toString()}`);
return { exp: null, pkg: null };
}
}
// exp missing. might be in package.json
}
try {
const packageJsonPath = exp && exp.nodeModulesPath ? _path.default.join(_path.default.resolve(projectRoot, exp.nodeModulesPath), 'package.json') : _path.default.join(projectRoot, 'package.json');
pkg = yield new (_jsonFile || _load_jsonFile()).default(packageJsonPath).readAsync();
} catch (e) {
if (e.isJsonFileError) {
// TODO: add error codes to json-file
if (e.message.startsWith('Error parsing JSON file')) {
logError(projectRoot, 'expo', `Error parsing JSON file: ${e.cause.toString()}`);
return { exp: null, pkg: null };
}
}
// pkg missing
}
// Easiest bail-out: package.json is missing
if (!pkg) {
logError(projectRoot, 'expo', `Error: Can't find package.json`);
return { exp: null, pkg: null };
}
// Grab our exp config from package.json (legacy) or exp.json
if (!exp && pkg.exp) {
exp = pkg.exp;
logError(projectRoot, 'expo', `Error: Move your "exp" config from package.json to exp.json.`);
} else if (!exp && !pkg.exp) {
logError(projectRoot, 'expo', `Error: Missing ${configName}. See https://docs.expo.io/`);
return { exp: null, pkg: null };
}
// fill any required fields we might care about
// TODO(adam) decide if there are other fields we want to provide defaults for
if (exp && !exp.name) {
exp.name = pkg.name;
}
if (exp && !exp.slug) {
exp.slug = (0, (_slugify || _load_slugify()).default)(exp.name.toLowerCase());
}
if (exp && !exp.version) {
exp.version = pkg.version;
}
return { exp, pkg };
});
return function readConfigJsonAsync(_x6) {
return _ref5.apply(this, arguments);
};
})();
exports.logWithLevel = logWithLevel;
exports.logDebug = logDebug;
exports.logInfo = logInfo;
exports.logError = logError;
exports.logWarning = logWarning;
exports.clearNotification = clearNotification;
exports.attachLoggerStream = attachLoggerStream;
var _fs = _interopRequireDefault(require('fs'));
var _fs2;
function _load_fs() {
return _fs2 = _interopRequireDefault(require('mz/fs'));
}
var _path = _interopRequireDefault(require('path'));
var _jsonFile;
function _load_jsonFile() {
return _jsonFile = _interopRequireDefault(require('@expo/json-file'));
}
var _slugify;
function _load_slugify() {
return _slugify = _interopRequireDefault(require('slugify'));
}
var _Analytics;
function _load_Analytics() {
return _Analytics = _interopRequireWildcard(require('../Analytics'));
}
var _Config;
function _load_Config() {
return _Config = _interopRequireDefault(require('../Config'));
}
var _Logger;
function _load_Logger() {
return _Logger = _interopRequireDefault(require('../Logger'));
}
var _state;
function _load_state() {
return _state = _interopRequireWildcard(require('../state'));
}
var _Sentry;
function _load_Sentry() {
return _Sentry = _interopRequireWildcard(require('../Sentry'));
}
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
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"); }); }; }
const MAX_MESSAGE_LENGTH = 200;
let _projectRootToLogger = {};
function _getLogger(projectRoot) {
let logger = _projectRootToLogger[projectRoot];
if (!logger) {
logger = (_Logger || _load_Logger()).default.child({
type: 'project',
project: _path.default.resolve(projectRoot)
});
_projectRootToLogger[projectRoot] = logger;
}
return logger;
}
function logWithLevel(projectRoot, level, object, msg, id) {
let useRedux = id && (_Config || _load_Config()).default.useReduxNotifications;
let logger = _getLogger(projectRoot);
switch (level) {
case 'debug':
logger.debug(object, msg);
break;
case 'info':
logger.info(object, msg);
break;
case 'warn':
if (!useRedux) {
logger.warn(object, msg);
}
break;
case 'error':
if (!useRedux) {
logger.error(object, msg);
}
break;
default:
logger.debug(object, msg);
break;
}
if (useRedux && (level === 'warn' || level === 'error')) {
(_state || _load_state()).store.dispatch((_state || _load_state()).actions.notifications.add(projectRoot, id, msg, projectRoot, level));
}
}
function logDebug(projectRoot, tag, message, id) {
_getLogger(projectRoot).debug({ tag }, message.toString());
}
function logInfo(projectRoot, tag, message, id) {
if (id && (_Config || _load_Config()).default.useReduxNotifications) {
(_state || _load_state()).store.dispatch((_state || _load_state()).actions.notifications.add(projectRoot, id, message, tag, 'info'));
} else {
_getLogger(projectRoot).info({ tag }, message.toString());
}
}
function logError(projectRoot, tag, message, id) {
if (id && (_Config || _load_Config()).default.useReduxNotifications) {
(_state || _load_state()).store.dispatch((_state || _load_state()).actions.notifications.add(projectRoot, id, message, tag, 'error'));
} else {
_getLogger(projectRoot).error({ tag }, message.toString());
}
let truncatedMessage = message.toString();
if (truncatedMessage.length > MAX_MESSAGE_LENGTH) {
truncatedMessage = truncatedMessage.substring(0, MAX_MESSAGE_LENGTH);
}
// temporarily remove sentry until we can trim events
// send error to Sentry
// Sentry.logError(message.toString(), {
// tags: { tag },
// });
}
function logWarning(projectRoot, tag, message, id) {
if (id && (_Config || _load_Config()).default.useReduxNotifications) {
(_state || _load_state()).store.dispatch((_state || _load_state()).actions.notifications.add(projectRoot, id, message, tag, 'warn'));
} else {
_getLogger(projectRoot).warn({ tag }, message.toString());
}
let truncatedMessage = message.toString();
if (truncatedMessage.length > MAX_MESSAGE_LENGTH) {
truncatedMessage = truncatedMessage.substring(0, MAX_MESSAGE_LENGTH);
}
(_Analytics || _load_Analytics()).logEvent('Project Warning', {
projectRoot,
tag,
message: truncatedMessage
});
}
function clearNotification(projectRoot, id) {
if ((_Config || _load_Config()).default.useReduxNotifications) {
(_state || _load_state()).store.dispatch((_state || _load_state()).actions.notifications.clear(projectRoot, id));
}
}
function attachLoggerStream(projectRoot, stream) {
_getLogger(projectRoot).addStream(stream);
}
let customConfigPaths = {};
//# sourceMappingURL=../__sourcemaps__/project/ProjectUtils.js.map