@blitzjs/cli
Version:
Blitz.js CLI
74 lines (73 loc) • 2.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isBlitzRoot = exports.IsBlitzRootError = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const pkg_dir_1 = (0, tslib_1.__importDefault)(require("pkg-dir"));
var IsBlitzRootError;
(function (IsBlitzRootError) {
IsBlitzRootError[IsBlitzRootError["NotBlitz"] = 0] = "NotBlitz";
IsBlitzRootError[IsBlitzRootError["NotRoot"] = 1] = "NotRoot";
IsBlitzRootError[IsBlitzRootError["BadPackageJson"] = 2] = "BadPackageJson";
})(IsBlitzRootError = exports.IsBlitzRootError || (exports.IsBlitzRootError = {}));
const checkParent = () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
const rootDir = yield (0, pkg_dir_1.default)("./");
if (rootDir) {
const file = yield (0, fs_extra_1.readJSON)((0, path_1.resolve)(rootDir, "package.json"));
if (file && Object.keys(file.dependencies || {}).includes("blitz")) {
return process.cwd().slice(rootDir.length).split("/").length - 1;
}
}
return false;
});
/**
* @name isBlitzRoot
* @returns {IsBlitzRootError}
* notBlitz -> when can't find package.json in current folder and first found in parent
* doesn't have blitz in dependencies
* notRoot -> if in a nested folder of blitz project (found blitz as depend in a parent package.json)
* badPackageJson -> an error occurred while reading local package.json
*/
const isBlitzRoot = () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
var _a, _b;
try {
const local = yield (0, fs_extra_1.readJSON)("./package.json");
if (local) {
if (((_a = local.dependencies) === null || _a === void 0 ? void 0 : _a["blitz"]) || ((_b = local.devDependencies) === null || _b === void 0 ? void 0 : _b["blitz"])) {
return { err: false };
}
else {
return {
err: true,
message: IsBlitzRootError.NotBlitz,
};
}
}
return { err: true, message: IsBlitzRootError.BadPackageJson };
}
catch (err) {
// No local package.json
if (err.code === "ENOENT") {
const out = yield checkParent();
if ((0, fs_extra_1.existsSync)("./blitz.config.js") || (0, fs_extra_1.existsSync)("./blitz.config.ts")) {
return { err: false };
}
if (out === false) {
return {
err: true,
message: IsBlitzRootError.NotBlitz,
};
}
else {
return {
err: true,
message: IsBlitzRootError.NotRoot,
depth: out,
};
}
}
}
return { err: true };
});
exports.isBlitzRoot = isBlitzRoot;