Skip to content

Commit

Permalink
feat: ✨ Added custom prefix seed
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed May 13, 2024
1 parent 94103da commit 052e6c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
26 changes: 18 additions & 8 deletions packages/cli/src/cmds/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ dotenv.config();

configSetup(process.argv);

export type RunCommandArgs = { envName: string; GrepTest?: string; subDirectory?: string };
export type RunCommandArgs = {
envName: string;
GrepTest?: string;
subDirectory?: string;
};

yargs(hideBin(process.argv))
.wrap(null)
Expand Down Expand Up @@ -124,17 +128,23 @@ yargs(hideBin(process.argv))
await runNetworkCmd(argv);
}
)
.command<{ suitesRootDir: string }>(
.command<{ suitesRootDir: string; prefixPhrase?: string }>(
"derive <suitesRootDir>",
"Derive test IDs based on positional order in the directory tree",
(yargs) => {
return yargs.positional("suitesRootDir", {
describe: "Root directory of the suites",
type: "string",
});
return yargs
.positional("suitesRootDir", {
describe: "Root directory of the suites",
type: "string",
})
.option("prefixPhrase", {
describe: "Root phrase to generate prefixes from (e.g. DEV)",
alias: "p",
type: "string",
});
},
async (argv) => {
await deriveTestIds({ rootDir: argv.suitesRootDir });
async ({ suitesRootDir, prefixPhrase }) => {
await deriveTestIds({ rootDir: suitesRootDir, prefixPhrase });
}
)
.demandCommand(1)
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/internal/deriveTestIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from "node:path";
interface DeriveTestIdsOptions {
rootDir: string;
prefixDepth?: number;
prefixName?: string;
prefixPhrase?: string;
}

export async function deriveTestIds(params: DeriveTestIdsOptions) {
Expand All @@ -30,7 +30,7 @@ export async function deriveTestIds(params: DeriveTestIdsOptions) {
const foldersToRename: { prefix: string; dir: string }[] = [];

for (const dir of topLevelDirs) {
const prefix = params.prefixName ?? generatePrefix(dir, usedPrefixes);
const prefix = generatePrefix(dir, usedPrefixes, params.prefixPhrase);
foldersToRename.push({ prefix, dir });
}

Expand Down Expand Up @@ -64,13 +64,13 @@ function getTopLevelDirs(rootDir: string): string[] {
.filter((dir) => fs.statSync(path.join(rootDir, dir)).isDirectory());
}

function generatePrefix(directory: string, usedPrefixes: Set<string>): string {
function generatePrefix(directory: string, usedPrefixes: Set<string>, rootPrefix?: string): string {
const sanitizedDir = directory.replace(/[-_ ]/g, "").toUpperCase();
let prefix = sanitizedDir[0];
let prefix = rootPrefix ?? sanitizedDir[0];

let additionalIndex = 1;
while (usedPrefixes.has(prefix) && additionalIndex < sanitizedDir.length) {
prefix += sanitizedDir[additionalIndex];
prefix += rootPrefix?.[additionalIndex] ?? sanitizedDir[additionalIndex];
additionalIndex++;
}

Expand Down

0 comments on commit 052e6c0

Please sign in to comment.