@k14v/nlink
Version:
Modifies the global NodeJS module links to transpiled directory src -> lib
45 lines (44 loc) • 1.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readConfig = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const read_tsconfig_1 = __importDefault(require("read-tsconfig"));
const logger_1 = __importDefault(require("./logger"));
const readConfig = async (opts) => {
var _a, _b;
const { cwd = process.cwd(), logger = logger_1.default, } = { ...opts };
const pkgPath = path_1.default.resolve(cwd, 'package.json');
const pkgContents = fs_1.default.readFileSync(pkgPath);
const pkgJSON = JSON.parse(pkgContents.toString());
let libDir = (_a = pkgJSON === null || pkgJSON === void 0 ? void 0 : pkgJSON.nlink) === null || _a === void 0 ? void 0 : _a.lib;
if (libDir) {
logger.info(`Detected configuration for lib directory from pkg.nlink.lib: "${libDir}"`);
}
else {
libDir = (_b = (await (0, read_tsconfig_1.default)({ cwd })).compilerOptions) === null || _b === void 0 ? void 0 : _b.outDir;
if (libDir) {
logger.info(`Detected configuration for lib directory from tsconfig: "${libDir}"`);
}
}
if (typeof libDir !== 'string') {
throw new Error('nlink: Can\'t not guess the transpiled lib directory');
}
const resolvedLib = path_1.default.resolve(cwd, libDir);
if (!fs_1.default.existsSync(resolvedLib)) {
throw new Error(`nlink: Missing is not exists "${resolvedLib}"`);
}
if (!pkgJSON.name) {
throw new Error(`nlink: Empty package.json name property`);
}
return {
pkgPath,
name: pkgJSON.name,
lib: resolvedLib
};
};
exports.readConfig = readConfig;
exports.default = exports.readConfig;