UNPKG

dev-sync-injector

Version:

for hot update receiver

47 lines (46 loc) 2.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAllPackageStorePath = exports.getPackageFullPath = void 0; var os_1 = require("os"); var fs_extra_1 = __importDefault(require("fs-extra")); var path_1 = require("path"); var userHome = (0, os_1.homedir)(); var getStoreMainDir = function () { // TODO test in windows whether is precise if (process.platform === 'win32' && process.env.LOCALAPPDATA) { return (0, path_1.join)(process.env.LOCALAPPDATA, 'Yalc'); } return (0, path_1.join)(userHome, '.yalc'); }; var getPackageStorePath = function (packageName, version) { if (version === void 0) { version = ''; } return (0, path_1.join)(getStoreMainDir(), 'packages', packageName, version); }; var getLatestPackageVersion = function (packageName) { var dir = getPackageStorePath(packageName); var versions = fs_extra_1.default.readdirSync(dir); var latest = versions .map(function (version) { return ({ version: version, created: fs_extra_1.default.statSync((0, path_1.join)(dir, version)).ctime.getTime(), }); }) .sort(function (a, b) { return b.created - a.created; }) .map(function (x) { return x.version; })[0]; return latest || ''; }; var getPackageFullPath = function (packageName) { return getPackageStorePath(packageName, getLatestPackageVersion(packageName)); }; exports.getPackageFullPath = getPackageFullPath; var getAllPackageStorePath = function () { var rootPackageStorePath = (0, path_1.join)(getStoreMainDir(), 'packages'); var packageList = fs_extra_1.default.readdirSync(rootPackageStorePath); return packageList.reduce(function (previousValue, currentValue) { previousValue[currentValue] = getPackageFullPath(currentValue); return previousValue; }, {}); }; exports.getAllPackageStorePath = getAllPackageStorePath;