Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 14x 14x 14x 14x 14x 392x 392x 14x 14x 14x 14x 14x 14x 1x 13x 5x 5x | import * as path from "path";
import type { BucketBy, Context, Tag } from "@featurevisor/types";
import { Parser, parsers } from "@featurevisor/parsers";
import { FilesystemAdapter } from "../datasource/filesystemAdapter";
import type { Plugin } from "../cli";
import type { BuildTags } from "../builder/buildDatafile";
export const FEATURES_DIRECTORY_NAME = "features";
export const SEGMENTS_DIRECTORY_NAME = "segments";
export const ATTRIBUTES_DIRECTORY_NAME = "attributes";
export const GROUPS_DIRECTORY_NAME = "groups";
export const SCHEMAS_DIRECTORY_NAME = "schemas";
export const TESTS_DIRECTORY_NAME = "tests";
export const STATE_DIRECTORY_NAME = ".featurevisor";
export const DATAFILES_DIRECTORY_NAME = "datafiles";
export const DATAFILE_NAME_PATTERN = "featurevisor-%s.json";
export const REVISION_FILE_NAME = "REVISION";
export const SITE_EXPORT_DIRECTORY_NAME = "out";
export const ENVIRONMENTS_DIRECTORY_NAME = "environments";
export const CONFIG_MODULE_NAME = "featurevisor.config.js";
export const ROOT_DIR_PLACEHOLDER = "<rootDir>";
export const DEFAULT_ENVIRONMENTS = ["staging", "production"];
export const DEFAULT_TAGS = ["all"];
export const DEFAULT_BUCKET_BY_ATTRIBUTE = "userId";
export const DEFAULT_PRETTY_STATE = true;
export const DEFAULT_PRETTY_DATAFILE = false;
export const DEFAULT_PARSER: Parser = "yml";
export const SCHEMA_VERSION = "2"; // default schema version
export interface Scope {
name: string;
context: Context;
tag?: Tag;
tags?: BuildTags;
}
export interface ProjectConfig {
featuresDirectoryPath: string;
segmentsDirectoryPath: string;
attributesDirectoryPath: string;
groupsDirectoryPath: string;
schemasDirectoryPath: string;
testsDirectoryPath: string;
stateDirectoryPath: string;
datafilesDirectoryPath: string;
datafileNamePattern: string;
revisionFileName: string;
siteExportDirectoryPath: string;
environmentsDirectoryPath: string;
environments: string[] | false;
splitByEnvironment: boolean;
tags: string[];
scopes?: Scope[];
adapter: any; // @NOTE: type this properly later
plugins: Plugin[];
defaultBucketBy: BucketBy;
parser: Parser;
prettyState: boolean;
prettyDatafile: boolean;
stringify: boolean;
enforceCatchAllRule?: boolean;
maxVariableStringLength?: number;
maxVariableArrayStringifiedLength?: number;
maxVariableObjectStringifiedLength?: number;
maxVariableJSONStringifiedLength?: number;
}
// rootDirectoryPath: path to the root directory of the project (without ending with a slash)
export function getProjectConfig(rootDirectoryPath: string): ProjectConfig {
const baseConfig: ProjectConfig = {
environments: DEFAULT_ENVIRONMENTS,
tags: DEFAULT_TAGS,
scopes: [],
defaultBucketBy: "userId",
parser: DEFAULT_PARSER,
prettyState: DEFAULT_PRETTY_STATE,
prettyDatafile: DEFAULT_PRETTY_DATAFILE,
stringify: true,
adapter: FilesystemAdapter,
featuresDirectoryPath: path.join(rootDirectoryPath, FEATURES_DIRECTORY_NAME),
environmentsDirectoryPath: path.join(rootDirectoryPath, ENVIRONMENTS_DIRECTORY_NAME),
segmentsDirectoryPath: path.join(rootDirectoryPath, SEGMENTS_DIRECTORY_NAME),
attributesDirectoryPath: path.join(rootDirectoryPath, ATTRIBUTES_DIRECTORY_NAME),
groupsDirectoryPath: path.join(rootDirectoryPath, GROUPS_DIRECTORY_NAME),
schemasDirectoryPath: path.join(rootDirectoryPath, SCHEMAS_DIRECTORY_NAME),
testsDirectoryPath: path.join(rootDirectoryPath, TESTS_DIRECTORY_NAME),
stateDirectoryPath: path.join(rootDirectoryPath, STATE_DIRECTORY_NAME),
datafilesDirectoryPath: path.join(rootDirectoryPath, DATAFILES_DIRECTORY_NAME),
datafileNamePattern: DATAFILE_NAME_PATTERN,
revisionFileName: REVISION_FILE_NAME,
siteExportDirectoryPath: path.join(rootDirectoryPath, SITE_EXPORT_DIRECTORY_NAME),
enforceCatchAllRule: false,
plugins: [],
splitByEnvironment: false,
maxVariableStringLength: undefined,
maxVariableArrayStringifiedLength: undefined,
maxVariableObjectStringifiedLength: undefined,
maxVariableJSONStringifiedLength: undefined,
};
const configModulePath = path.join(rootDirectoryPath, CONFIG_MODULE_NAME);
const customConfig = require(configModulePath);
const mergedConfig = {};
Object.keys(baseConfig).forEach((key) => {
mergedConfig[key] =
typeof customConfig[key] !== "undefined" ? customConfig[key] : baseConfig[key];
Iif (key.endsWith("Path") && mergedConfig[key].indexOf(ROOT_DIR_PLACEHOLDER) !== -1) {
mergedConfig[key] = mergedConfig[key].replace(ROOT_DIR_PLACEHOLDER, rootDirectoryPath);
}
});
const finalConfig = mergedConfig as ProjectConfig;
if (typeof finalConfig.parser === "string") {
const allowedParsers = Object.keys(parsers);
Iif (allowedParsers.indexOf(finalConfig.parser) === -1) {
throw new Error(`Invalid parser: ${finalConfig.parser}`);
}
finalConfig.parser = parsers[finalConfig.parser];
}
if (finalConfig.splitByEnvironment && finalConfig.environments === false) {
throw new Error(
"Invalid configuration: splitByEnvironment=true requires environments to be an array",
);
}
return finalConfig as ProjectConfig;
}
export interface ShowProjectConfigOptions {
json?: boolean;
pretty?: boolean;
}
export function showProjectConfig(
projectConfig: ProjectConfig,
options: ShowProjectConfigOptions = {},
) {
Iif (options.json) {
console.log(
options.pretty ? JSON.stringify(projectConfig, null, 2) : JSON.stringify(projectConfig),
);
return;
}
console.log("\nProject configuration:\n");
const keys = Object.keys(projectConfig);
const longestKeyLength = keys.reduce((acc, key) => (key.length > acc ? key.length : acc), 0);
const ignoreKeys = ["adapter", "parser"];
for (const key of keys) {
Iif (ignoreKeys.indexOf(key) !== -1) {
continue;
}
console.log(` - ${key.padEnd(longestKeyLength, " ")}: ${projectConfig[key]}`);
}
}
export const configPlugin: Plugin = {
command: "config",
handler: async ({ rootDirectoryPath, parsed }) => {
const projectConfig = getProjectConfig(rootDirectoryPath);
showProjectConfig(projectConfig, {
json: parsed.json,
pretty: parsed.pretty,
});
},
examples: [
{
command: "config",
description: "show the project configuration",
},
{
command: "config --print",
description: "show the project configuration as JSON",
},
{
command: "config --print --pretty",
description: "show the project configuration (as pretty JSON)",
},
],
};
|