Skip to content

Commit

Permalink
fix: fixed help system which was chocking on chalk symbols for comman…
Browse files Browse the repository at this point in the history
…ds with argv
  • Loading branch information
yankeeinlondon committed Jan 28, 2023
1 parent 7e0f3b2 commit 7f4b4b4
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 52 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@
},
"pnpm": {
"overrides": {
"simple-git@<3.3.0": ">=3.3.0",
"simple-git@<3.5.0": ">=3.5.0",
"sharp@<0.30.5": ">=0.30.5"
"simple-git@<3.5.0": ">=3.5.0"
}
}
}
5 changes: 2 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/commands/autoindex/parts/autoindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getMonoRepoPackages, getSubdirectories } from "src/shared/file";
* the file's current directory
*/
export const handler: DoDevopsHandler<IAutoindexOptions> = async ({ opts, observations, argv }) => {

// the sfc flag on CLI is inverted logically
opts = {
...opts,
Expand Down
23 changes: 15 additions & 8 deletions src/commands/autoindex/parts/description.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import chalk from "chalk";

import { IDoDevopsCommand } from "src/@types";
const autoindex = chalk.bgWhite.black`autoindex`;
// const autoindex = chalk.bgWhite.black`autoindex`;
// const autoindexComment = chalk.bgWhiteBright.blackBright`// autoindex`;

export const description: IDoDevopsCommand["description"] = {
short: `Automates the building of ${chalk.italic`index.ts`} files to aggregate folder's content`,
complete: `Automates the building of ${chalk.italic`index`} files; if you include a comment starting with ${chalk.bold.yellow`\n// #autoindex\n`}in a index file it will be auto-indexed when calling ${chalk.blue`do autoindex`}.
By default ${autoindex} will assume that you are using ${chalk.italic`named`} exports but this can be configured to what you need. Options are: ${chalk.italic`named, default,`} and ${chalk.italic`named-offset`}. To configure, simply add something like ${chalk.bold.yellow`\n// #autoindex:default\n`}to your file.
If you need to exclude certain files you can state the exclusions after the autoindex declaration: ${chalk.bold.yellow`#autoindex, exclude:a,b,c`}`,
short: `Automates the building of index files`,
complete: `Automates the building of index files`
};


// {
// short: `Automates the building of ${chalk.italic`index.[ts | js]`} files`,
// complete: `Automates the building of ${chalk.italic`index`} files in a given repo.

// Any index file (JS or TS) which is either completely empty ${chalk.italic`or`} contains a comment
// which looks has a line that starts with ${autoindexComment} will be managed by the ${autoindex}
// command.`,
// };
10 changes: 6 additions & 4 deletions src/commands/autoindex/parts/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import chalk from "chalk";
import { IOptionDefinition } from "src/@types/option-types";

export interface IAutoindexOptions {
config: boolean;
add: string;
glob: string;
/**
* Reports what would happen _if_ you were to run the command without actually making any changes
*/
dryRun: boolean;
/**
* boolean flag to indicate whether VueJS SFC files should have their default
Expand Down Expand Up @@ -35,7 +37,7 @@ export const options: IOptionDefinition = {
multiple: true,
type: String,
group: "local",
description: `${chalk.italic`optionally`} state one or more explicit autoindex files to evaluate instead of glob patterns`,
description: `optionally state one or more explicit autoindex files to evaluate instead of glob patterns`,
typeLabel: "string[]",
},

Expand Down Expand Up @@ -71,7 +73,7 @@ export const options: IOptionDefinition = {
alias: "w",
type: Boolean,
group: "local",
description: `watches for changes and runs ${chalk.italic`autoindex`} when detected`,
description: `watches for changes and runs autoindex when detected`,
},
force: {
alias: "f",
Expand All @@ -84,6 +86,6 @@ export const options: IOptionDefinition = {
alias: "p",
type: Boolean,
group: "local",
description: `exports -- by default -- will ${chalk.italic`not`} include the file's ${chalk.blue`.js`} extension but sometimes with ES modules you want to include this. If you do then you should set this flag.`,
description: `by default exports do not include the file's .js extension but sometimes with ES modules you want to include this. If you do then you should set this flag.`,
},
};
2 changes: 1 addition & 1 deletion src/commands/info/parts/thisRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "src/shared/git";
import { askConfirmQuestion, resolvePackageManagerConflict } from "src/shared/interactive";
import { getExternalPackageJson, getPackageJson } from "src/shared/npm";
import { dim, emoji, green } from "src/shared/ui";
import { emoji, green } from "src/shared/ui";
import { monorepoInfo } from "./components/monorepo";
import { currentDirectory } from "src/shared/file/base-paths/currentDirectory";
import { symlinks } from "src/shared/file/utility/symlinks";
Expand Down
1 change: 1 addition & 0 deletions src/dd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { isDevopsError } from "./@type-guards";
// Show help on the command
if (cmdInput.opts.help) {
commandAnnouncement(cmdDefn, cmdInput);

help(observations, cmdDefn);
process.exit();
}
Expand Down
3 changes: 3 additions & 0 deletions src/shared/core/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export function help(
options: globalOptions,
} as Finalized<IDoDevopsCommand>);

console.log({kind,subCommands,description,syntax,options});


const sections: commandLineUsage.Section[] = [
{
header: "Description",
Expand Down
9 changes: 5 additions & 4 deletions src/shared/core/helpContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ import { getObservations } from "../observations";
* command. If not provided a default syntax will be used.
*/
export function getSyntax(fn?: string): string {
if (!fn) {
return "dd [command] <options>";
} else {
if (fn) {
const validCommands = keys(commands);
if ((validCommands as string[]).includes(fn)) {
const defn: IDoDevopsCommand<any> = commands[fn as keyof typeof commands];
const hasSubCommands = defn?.subCommands ? true : false;
return defn.syntax ?? `do ${fn} ${hasSubCommands ? "[command] " : ""}`;
} else {
return `dd [command] <options>\nnote: the command {red ${fn}} is not recognized!`;
const fnName = chalk.red(fn);
return `dd [command] <options>\nnote: the command ${fnName} is not recognized!`;
}
} else {
return "dd [command] <options>";
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/shared/core/parseCmdArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export function parseCmdArgs(cmd: IDoDevopsCommand, incomingArgv: string[]) {
}
);

// opts come from both "local" and "global" options
const opts = { ...(global ? global : {}), ...(local ? local : {}) };
// combine "local" and "global" options
const opts = { ...global, ...local };

const sc = subCommand ? (cmd.greedy ? subCommand?.command[0] : subCommand?.command) : undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/shared/core/util/argv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IDoDevopsCommand } from "src/@types/command";
import { IOptionDefinition } from "src/@types/option-types";

/**
* Determines if a given commmand has an `argv` parameter
* Determines if a given command has an `argv` parameter
*/
export function hasArgv(cmd: IDoDevopsCommand<any>) {
return !Object.keys(cmd?.options || {}).every(
Expand Down
19 changes: 11 additions & 8 deletions src/shared/core/util/commandAnnouncement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@ import { doDevopsVersion } from "./doDevopsVersion";
export function commandAnnouncement(cmdDefn?: IDoDevopsCommand, cmd?: ICommandParsing) {
const log = logger(cmd ? cmd.opts : {});
const version = doDevopsVersion();

const argv = cmd && cmd.argv.length > 0 ? ` ${chalk.italic(cmd.argv.join(" "))}` : "";

const argv = cmd && cmd.argv.length > 0
? chalk.italic` ${cmd.argv.join(" ")}`
: "";

const subCmd = cmd && cmd.subCommand ? chalk.dim` ${cmd.subCommand}` : "";

const helpText =
(!cmdDefn && !cmd) || (cmd && cmd.opts?.help)
? cmdDefn
? chalk.gray` [help, v${version}]`
: chalk.gray` v${version}`
? ` [ help, v${version} ]`
: ` v${version}`
: "";

const brand = cmdDefn
? chalk.green.italic.bold(`${cmdDefn.kind}${subCmd}${argv}`)
: chalk.green.italic.bold("Help");

log.info(
chalk.bold(
`\ndo-devops ${chalk.green.italic.bold(
cmdDefn ? `${cmdDefn.kind}${subCmd}${argv}` : "Help"
)}${helpText}\n`
`\ndo-devops ${brand}${chalk.gray(helpText)}\n`
)
);
}
9 changes: 6 additions & 3 deletions src/shared/core/util/finalizeCommandDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ export function finalizeCommandDefinition(
? cmdDefn.subCommands(observations, options)
: cmdDefn.subCommands;

const argv = hasArgv(cmdDefn) ? chalk.italic.dim` argv[]}}` : "";
const argv = hasArgv(cmdDefn) ? chalk.italic.dim` argv[]` : "";
const argvStatic = chalk.bold.blue("[argv]:");
console.log({argvOption: getArgvOption(cmdDefn)?.description, argvStatic});

const argvDescription = getArgvOption(cmdDefn)?.description
? `\n\n\t\t${chalk.bold.blue`[argv]:`} ${getArgvOption(cmdDefn)?.description}`
? `\n\n\t\t${argvStatic} ${getArgvOption(cmdDefn)?.description}`
: "";

const subCommandSyntax =
Expand All @@ -51,7 +54,7 @@ export function finalizeCommandDefinition(
handler: cmdDefn.handler,
syntax:
cmdDefn.syntax ||
`dd ${cmdDefn.kind}${subCommandSyntax}${argv} [${chalk.italic`options`}]${argvDescription}`,
`dd ${cmdDefn.kind}${subCommandSyntax}${argv} [${chalk.dim.italic`options`}]${argvDescription}`,
description,
subCommands,
options: { ...cmdDefn.options, ...globalOptions },
Expand Down
3 changes: 2 additions & 1 deletion src/shared/file/crud/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IWriteOptions } from "src/@types";
import { interpolateFilePath } from "../../../shared/file/helpers";
import path from "node:path";
import { isClassification } from "../../../@type-guards";
import chalk from "chalk";

/**
* **write**
Expand All @@ -31,7 +32,7 @@ export function write(filename: string, data: any, options: IWriteOptions = {})

filename = interpolateFilePath(filename);

// avoid collisions if offset avoidence is enabled
// avoid collisions if offset avoidance is enabled
let offset: number | undefined;
while (options.offsetIfExists && fileExists(filename)) {
const before = new RegExp(`-${offset}.(.*)$`);
Expand Down
2 changes: 1 addition & 1 deletion src/shared/ui/highlightFilepath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ export function highlightFilepath(
? chalk[highForeground]`${file}}`
: `${file}}`;

return `${filePath}${fileName}`;
return `${fp}${fileName}`;
}
23 changes: 10 additions & 13 deletions test/hashcode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { describe, it, expect, beforeAll } from "vitest";
import xxhash, { Exports } from "xxhash-wasm";
import { describe, it, expect } from "vitest";
import xxhash from "xxhash-wasm";
const { h32 } = await xxhash();


describe("Validate that same inputs produce same hash", () => {
const person1 = { id: 1, name: "Bob", age: 35 };
const person2 = { id: 2, name: "Jane", age: 22 };
const person3 = { id: 3, name: "Max", age: 12 };
let xx: Exports["h32"];
beforeAll(async () => {
const { h32 } = await xxhash();
xx = h32;
});

it("different data is different with same seed", () => {
const t1 = xx(JSON.stringify(person1), 0xCA_FE_BA_BE);
const t2 = xx(JSON.stringify(person2), 0xCA_FE_BA_BE);
const t3 = xx(JSON.stringify(person3), 0xCA_FE_BA_BE);
const t1 = h32(JSON.stringify(person1), 0xCA_FE_BA_BE);
const t2 = h32(JSON.stringify(person2), 0xCA_FE_BA_BE);
const t3 = h32(JSON.stringify(person3), 0xCA_FE_BA_BE);

expect(t1).not.toBe(t2);
expect(t1).not.toBe(t3);
Expand All @@ -23,9 +20,9 @@ describe("Validate that same inputs produce same hash", () => {
});

it("same hash is always the same, even with same seed", () => {
const t1 = xx(JSON.stringify(person1), 0xCA_FE_BA_BE);
const t2 = xx(JSON.stringify(person1), 0xCA_FE_BA_BE);
const t3 = xx(JSON.stringify(person1), 0xCA_FE_BA_BE);
const t1 = h32(JSON.stringify(person1), 0xCA_FE_BA_BE);
const t2 = h32(JSON.stringify(person1), 0xCA_FE_BA_BE);
const t3 = h32(JSON.stringify(person1), 0xCA_FE_BA_BE);

expect(t1).toBe(t2);
expect(t1).toBe(t3);
Expand Down

0 comments on commit 7f4b4b4

Please sign in to comment.