textlint
Version:
The pluggable linting tool for natural language.
46 lines • 1.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTextlintVersion = exports.readClosestPackageJson = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const isFile = (filePath) => {
try {
return node_fs_1.default.statSync(filePath).isFile();
}
catch (_a) {
return false;
}
};
/**
* Find the closest package.json from `cwd` toward the root directory and return its parsed content.
* Return `undefined` if no package.json is found.
* Throw an error if the found package.json cannot be parsed.
*/
const readClosestPackageJson = (cwd) => {
let directory = node_path_1.default.resolve(cwd);
while (true) {
const filePath = node_path_1.default.join(directory, "package.json");
if (isFile(filePath)) {
// Throw if the closest package.json cannot be read or parsed
return JSON.parse(node_fs_1.default.readFileSync(filePath, "utf-8"));
}
const parentDirectory = node_path_1.default.dirname(directory);
if (parentDirectory === directory) {
return undefined;
}
directory = parentDirectory;
}
};
exports.readClosestPackageJson = readClosestPackageJson;
/**
* Return the version of textlint from its package.json
*/
const getTextlintVersion = () => {
var _a, _b;
return (_b = (_a = (0, exports.readClosestPackageJson)(__dirname)) === null || _a === void 0 ? void 0 : _a.version) !== null && _b !== void 0 ? _b : "unknown";
};
exports.getTextlintVersion = getTextlintVersion;
//# sourceMappingURL=package-json.js.map