Skip to content

Commit

Permalink
feat(cli): add autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Jan 15, 2025
1 parent 9b39c4b commit 45e4e78
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 5 deletions.
44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Changelog

All notable changes to this project will be documented in this file.

## [5.1.0]

### Added

- `CHANGELOG.md` file is not present and can be used.
- script completion file is installed on program run, and the install bash is reloading the SHELL.

### Change

-

### Fixed

-

## [5.0.1]

### Added

- (internal) Make it work on the Crystallize Staging environement

## [5.0.0]

This is the new version that will live in the future, built on top of Bun.js it is shipped through Github and does not require anything on the user side.

### Added

- `login` command to save your credentials `~/.crystallize/credentials.json`.
- `whoami` tells you who is logged-in via `~/.crystallize/credentials.json`.
- (wip) `install-boilerplate` to install boilerplates.
- `run-mass-operation` to run Mass Operation files.

## [4.x]

The 4.x version called `@crystallize/cli-next`, hosted in NPMJS is now deprecated, it was mainly used to install boilerplates, dump and import
tenant data. It was using `@crystallize/import-utilities` which is now deprecated in favor of Mass Operation

## [3.x]

The 3.x version called `@crystallize/cli`, hosted in NPMJS is now deprecated, it was mainly used to install old-legacy boilerplates
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ codeclean: ## Code Clean
build: ## Build
@bun build --bundle src/index.ts --outfile crystallize.js --target=bun
@bun shim.ts
@bun build --compile crystallize.js --outfile crystallize
@bun build --compile --minify crystallize.js --outfile crystallize
@rm crystallize.js
@rm -f ./.*.bun-build

Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions docs/install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ echo "- You may want to put ~/crystallize in you PATH"
echo "- You may want to creat an alias (in your .zshrc or .bashrc) alias crystallize='~/crystallize'"

~/crystallize
exec "$SHELL" -l
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"@types/marked-terminal": "^6.1.1",
"@types/react": "^18",
"@types/signale": "^1.4.7",
"react-devtools-core": "^6.0.1",
"prettier": "^3.4.2"
"@types/tabtab": "^3.0.4",
"prettier": "^3.4.2",
"react-devtools-core": "^6.0.1"
},
"dependencies": {
"@crystallize/js-api-client": "^4.1.0",
Expand Down
36 changes: 36 additions & 0 deletions src/content/completion_file.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
_crystallize_completions() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local cmd="${COMP_WORDS[1]}"
local subcmd="${COMP_WORDS[2]}"
local subsubcmd="${COMP_WORDS[3]}"

local commands="help install-boilerplate login whoami run-mass-operation"
local program_options="--version"
local default_options="--help"

COMPREPLY=()

case "${cmd}" in
login)
local options="${default_options}"
COMPREPLY=($(compgen -W "${options}" -- "${cur}"))
return 0
;;
install-boilerplate)
local options="${default_options} --bootstrap-tenant"
COMPREPLY=($(compgen -W "${options}" -- "${cur}"))
return 0
;;
run-mass-operation)
local options="${default_options} --token_id= --token_secret= --legacy-spec"
COMPREPLY=($(compgen -W "${options}" -- "${cur}"))
return 0
;;
esac

if [[ "${COMP_CWORD}" -eq 1 ]]; then
COMPREPLY=($(compgen -W "${commands} ${program_options} ${default_options}" -- "${cur}"))
fi
}

complete -F _crystallize_completions crystallize
37 changes: 37 additions & 0 deletions src/domain/use-cases/install-completion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os from 'os';
import type { Logger } from '../contracts/logger';
//@ts-expect-error - This is a workaround to import the completion file as text. It's working with bun.
import Completion from '../../content/completion_file.bash' with { type: 'text' };

type Deps = {
logger: Logger;
};
export const installCompletion = async (shell: string, { logger }: Deps): Promise<void> => {
const shellName = shell.split('/').pop();
let rcFile;
if (shellName === 'bash') {
rcFile = `${os.homedir()}/.bashrc`;
} else if (shellName === 'zsh') {
rcFile = `${os.homedir()}/.zshrc`;
} else {
logger.warn(`Unsupported shell: ${shellName}. Completion file was not installed.`);
return;
}
const dest = `${os.homedir()}/.crystallize/completion_file`;
const fileRc = Bun.file(rcFile);
if (!fileRc.exists()) {
logger.warn(`The rc file ${rcFile} does not exist. Completion file was not installed.`);
return;
}

const completionLine = `[ -s "${dest}" ] && source "${dest}"`;
const fileContent = await fileRc.text();
// we always write the new completion
await Bun.write(dest, Completion);
if (fileContent.includes(completionLine)) {
logger.debug(`Completion is already set up in ${rcFile}`);
return;
}
await Bun.write(rcFile, `${fileContent}# Crystallize CLI completion\n${completionLine}\n`);
logger.success(`Installing completion file in ${rcFile}`);
};
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { Command, Option } from 'commander';
import packageJson from '../package.json';
import pc from 'picocolors';
import { buildServices } from './core/di';
import { installCompletion } from './domain/use-cases/install-completion';

const services = buildServices();
const { logger, commands } = services;

const program = new Command();
program.version(packageJson.version);
program.name('crystallize');
program.addOption(new Option('--install-completion', 'Install the completion').hideHelp());
program.addOption(new Option('--uninstall-completion', 'Install the completion').hideHelp());

const helpStyling = {
styleTitle: (str: string) => pc.bold(str),
Expand Down Expand Up @@ -40,9 +43,13 @@ program.addHelpText('beforeAll', pc.cyanBright(logo));
program.description(
"Crystallize CLI helps you manage your Crystallize tenant(s) and improve your DX.\nWe've got your back(end)!\n 🤜✨🤛.",
);
program.addOption(new Option('-e, --env <env>', 'Environment to use').choices(['staging', 'production']));
program.action(async () => {
const shell = Bun.env.SHELL || '/bin/bash';
await installCompletion(shell, { logger });
program.help();
});

commands.forEach((command) => {
// command.addOption(new Option('-e, --env <env>', 'Environment to use').choices(['staging', 'production']));
command.configureHelp(helpStyling);
program.addCommand(command);
});
Expand Down

0 comments on commit 45e4e78

Please sign in to comment.