Skip to content

Commit

Permalink
docs: Add cli help information
Browse files Browse the repository at this point in the history
  • Loading branch information
FatehAK committed Jan 5, 2023
1 parent 281749c commit d1823d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import minimist from 'minimist';
import inquirer from 'inquirer';
import { createSpinner } from 'nanospinner';
import { version } from '../package.json';
import { downloadDirectory, callGitHubApi, copyDirectory, getLocalTemplates, initializeGit, installPackages, openInEditor } from './utils';
import {
downloadDirectory,
callGitHubApi,
copyDirectory,
getLocalTemplates,
initializeGit,
installPackages,
openInEditor,
getHelpTemplate,
} from './utils';
import { CONFIG_FILE_NAME, GITHUB_PATH_REGEX, INQUIRER_DEFAULT_OPTS } from './constants';

(async function () {
Expand All @@ -20,12 +29,18 @@ import { CONFIG_FILE_NAME, GITHUB_PATH_REGEX, INQUIRER_DEFAULT_OPTS } from './co
},
});

// print version info
// prints version string
if (argv.version) {
console.log(version);
process.exit(0);
}

// prints help information
if (argv.help) {
console.log(getHelpTemplate());
process.exit(0);
}

// check if config file and template path exists
const homeDir = os.homedir();
let config;
Expand Down Expand Up @@ -158,17 +173,18 @@ import { CONFIG_FILE_NAME, GITHUB_PATH_REGEX, INQUIRER_DEFAULT_OPTS } from './co
}
}

// STEP 7 - open created repository in editor
const selectedEditor = argv.editor || config.editorBinary;
if (selectedEditor) {
// STEP 7 - open generated repository in the code editor
if (!argv.editor) {
const { isOpenEditorInput } = await inquirer.prompt({
type: 'confirm',
message: 'Open in code editor?',
name: 'isOpenEditorInput',
default: true,
...INQUIRER_DEFAULT_OPTS,
});
if (isOpenEditorInput) await openInEditor(selectedEditor, repoAbsPath);
if (isOpenEditorInput) await openInEditor(config.editorBinary, repoAbsPath);
} else {
await openInEditor(argv.editor, repoAbsPath);
}

process.exit(0);
Expand Down
25 changes: 25 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,28 @@ export async function openInEditor(editor, targetDir) {
if (result.failed) return Promise.reject(new Error('Failed to open in code editor'));
return true;
}

export function getHelpTemplate() {
return `
NAME:
gen - rapidly scaffold projects for development with automation
USAGE:
gen [OPTIONS]
OPTIONS:
-h, --help show this message and exit
-v, --version print the version string
-g, --git auto-initialize a git repository
-i, --install auto-install packages
-e, --editor <string> open in the specified editor
EXAMPLES:
gen -g # auto-initializes a git repository without querying the user
gen -i # auto-installs packages without querying the user
gen -e code # opens the generated repository in the specified code editor (eg. VSCode)
gen -gi -e code # does all the tasks defined above in a single command
Author: <github.com/FatehAK>
`;
}

0 comments on commit d1823d2

Please sign in to comment.