rs-runner
Version:
RS is a CLI tool for quickly detecting package.json scripts, and running them.
199 lines (198 loc) • 7.63 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importConfig = exports.getImportConflicts = exports.exportConfig = exports.writeConfig = exports.getConfig = exports.validateConfig = exports.getConfigPath = void 0;
var fs = __importStar(require("fs"));
var os = __importStar(require("os"));
var path_1 = __importDefault(require("path"));
var output_1 = require("./output");
var getConfigDir = function () {
var homedir = os.homedir();
return path_1.default.join(homedir, '.rs-runner');
};
var getConfigPath = function () {
var configDir = getConfigDir();
var configPath = path_1.default.join(configDir, 'config.json');
// Ensure the config directory exists
if (!fs.existsSync(configDir)) {
try {
fs.mkdirSync(configDir, { recursive: true });
}
catch (err) {
output_1.output.error("Failed to create config directory: ".concat(err.message));
return null;
}
}
return configPath;
};
exports.getConfigPath = getConfigPath;
var validateConfig = function (config) {
return (config &&
typeof config === 'object' &&
config.globalScripts &&
typeof config.globalScripts === 'object' &&
(!config.directoryScripts || typeof config.directoryScripts === 'object'));
};
exports.validateConfig = validateConfig;
var getConfig = function () {
var configPath = (0, exports.getConfigPath)();
if (!configPath)
return null;
if (fs.existsSync(configPath)) {
try {
var configFile = fs.readFileSync(configPath, 'utf8');
var config = JSON.parse(configFile);
return (0, exports.validateConfig)(config) ? config : null;
}
catch (err) {
output_1.output.error("Failed to read config: ".concat(err.message));
return null;
}
}
return null;
};
exports.getConfig = getConfig;
var writeConfig = function (newConfig) {
var configPath = (0, exports.getConfigPath)();
if (!configPath)
return false;
try {
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2), 'utf8');
return true;
}
catch (err) {
output_1.output.error("Failed to save config: ".concat(err.message));
return false;
}
};
exports.writeConfig = writeConfig;
var exportConfig = function () {
var config = (0, exports.getConfig)();
if (!config)
return null;
return JSON.stringify(config, null, 2);
};
exports.exportConfig = exportConfig;
var getImportConflicts = function (newConfig) {
var _a;
var currentConfig = (0, exports.getConfig)();
if (!currentConfig)
return [];
var conflicts = [];
// Check global script conflicts
for (var _i = 0, _b = Object.keys(newConfig.globalScripts || {}); _i < _b.length; _i++) {
var key = _b[_i];
if (currentConfig.globalScripts && currentConfig.globalScripts[key]) {
conflicts.push("global:".concat(key));
}
}
// Check directory script conflicts
for (var _c = 0, _d = Object.keys(newConfig.directoryScripts || {}); _c < _d.length; _c++) {
var dir = _d[_c];
var newDirScripts = newConfig.directoryScripts[dir] || {};
var currentDirScripts = ((_a = currentConfig.directoryScripts) === null || _a === void 0 ? void 0 : _a[dir]) || {};
for (var _e = 0, _f = Object.keys(newDirScripts); _e < _f.length; _e++) {
var key = _f[_e];
if (currentDirScripts[key]) {
conflicts.push("dir:".concat(dir, ":").concat(key));
}
}
}
return conflicts;
};
exports.getImportConflicts = getImportConflicts;
var importConfig = function (newConfig, replace) {
if (replace) {
var success_1 = (0, exports.writeConfig)(newConfig);
if (!success_1)
return null;
var globalCount = Object.keys(newConfig.globalScripts || {}).length;
var dirCount = Object.values(newConfig.directoryScripts || {}).reduce(function (sum, scripts) { return sum + Object.keys(scripts).length; }, 0);
return "Replaced config: ".concat(globalCount, " global, ").concat(dirCount, " directory scripts");
}
// Merge mode
var currentConfig = (0, exports.getConfig)() || { globalScripts: {}, directoryScripts: {} };
var addedGlobal = 0;
var updatedGlobal = 0;
var addedDir = 0;
var updatedDir = 0;
// Merge global scripts
for (var _i = 0, _a = Object.entries(newConfig.globalScripts || {}); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], value = _b[1];
if (currentConfig.globalScripts[key]) {
updatedGlobal++;
}
else {
addedGlobal++;
}
currentConfig.globalScripts[key] = value;
}
// Merge directory scripts
for (var _c = 0, _d = Object.entries(newConfig.directoryScripts || {}); _c < _d.length; _c++) {
var _e = _d[_c], dir = _e[0], scripts = _e[1];
if (!currentConfig.directoryScripts[dir]) {
currentConfig.directoryScripts[dir] = {};
}
for (var _f = 0, _g = Object.entries(scripts); _f < _g.length; _f++) {
var _h = _g[_f], key = _h[0], value = _h[1];
if (currentConfig.directoryScripts[dir][key]) {
updatedDir++;
}
else {
addedDir++;
}
currentConfig.directoryScripts[dir][key] = value;
}
}
var success = (0, exports.writeConfig)(currentConfig);
if (!success)
return null;
var parts = [];
if (addedGlobal > 0)
parts.push("added ".concat(addedGlobal, " global"));
if (updatedGlobal > 0)
parts.push("updated ".concat(updatedGlobal, " global"));
if (addedDir > 0)
parts.push("added ".concat(addedDir, " directory"));
if (updatedDir > 0)
parts.push("updated ".concat(updatedDir, " directory"));
if (parts.length === 0)
return 'No changes made';
return "Imported: ".concat(parts.join(', '), " scripts");
};
exports.importConfig = importConfig;