Skip to content

Commit

Permalink
Merge pull request #15 from ieedan/add-verbose-flag
Browse files Browse the repository at this point in the history
feat: Add verbose flag for troubleshooting
  • Loading branch information
ieedan authored Sep 9, 2024
2 parents c7cfcb1 + e2e7b1a commit 757b250
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-seals-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ts-blocks": patch
---

Add `--verbose` flag for debugging.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
"type": "git",
"url": "git+https://github.com/ieedan/ts-blocks"
},
"keywords": [
"changelog",
"date"
],
"keywords": ["changelog", "date"],
"author": "Aidan Bleser",
"license": "MIT",
"bugs": {
Expand Down
29 changes: 26 additions & 3 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { resolveCommand } from 'package-manager-detector/commands';
import { detect } from 'package-manager-detector/detect';
import { Project, type SourceFile } from 'ts-morph';
import { type InferInput, boolean, object, parse } from 'valibot';
import { WARN } from '.';
import { INFO, WARN } from '.';
import { blocks } from '../blocks';
import { getConfig } from '../config';

const schema = object({
yes: boolean(),
verbose: boolean(),
});

type Options = InferInput<typeof schema>;
Expand All @@ -25,27 +26,40 @@ const add = new Command('add')
)
)
.option('-y, --yes', 'Add and install any required dependencies.', false)
.option('--verbose', 'Include debug logs.', false)
.action(async (blockNames, opts) => {
const options = parse(schema, opts);

await _add(blockNames, options);
});

const _add = async (blockNames: string[], options: Options) => {
const verbose = (msg: string) => {
if (options.verbose) {
console.info(`${INFO} ${msg}`);
}
};

verbose(`Attempting to add ${JSON.stringify(blockNames)}`);

intro(color.bgBlueBright('ts-blocks'));

const config = getConfig();

const loading = spinner();

for (const blockName of blockNames) {
verbose(`Attempting to add ${blockName}`);

// in the future maybe we add a registry but for now it can just be fs
const block = blocks[blockName];

if (!block) {
program.error(color.red(`Invalid block! ${color.bold(blockName)} does not exist!`));
}

verbose(`Found block ${JSON.stringify(block)}`);

const registryDir = path.join(import.meta.dirname, '../../blocks');

const registryFilePath = path.join(registryDir, `${block.category}/${blockName}.ts`);
Expand All @@ -64,7 +78,7 @@ const _add = async (blockNames: string[], options: Options) => {
// in case the directory didn't already exist
fs.mkdirSync(directory, { recursive: true });

if (fs.existsSync(newPath)) {
if (fs.existsSync(newPath) && !options.yes) {
const result = await confirm({
message: `${color.bold(blockName)} already exists in your project would you like to overwrite it?`,
initialValue: false,
Expand All @@ -76,11 +90,16 @@ const _add = async (blockNames: string[], options: Options) => {
}
}

loading.start(`Adding ${blockName}`);
// this will clear other logs and we don't want that
if (!options.verbose) {
loading.start(`Adding ${blockName}`);
}

fs.copyFileSync(registryFilePath, newPath);

if (config.includeIndexFile) {
verbose('Trying to include index file');

const indexPath = path.join(directory, 'index.ts');

const project = new Project();
Expand All @@ -106,6 +125,8 @@ const _add = async (blockNames: string[], options: Options) => {
}

if (config.includeTests) {
verbose('Trying to include tests');

const registryTestPath = path.join(
registryDir,
`${block.category}/${blockName}.test.ts`
Expand Down Expand Up @@ -153,6 +174,8 @@ const _add = async (blockNames: string[], options: Options) => {
}

if (block.dependencies) {
verbose('Trying to include dependencies');

if (!options.yes) {
const result = await confirm({
message: 'Add and install dependencies?',
Expand Down
4 changes: 3 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import { init } from './init';

const WARN = color.bgRgb(245, 149, 66).white('WARN');

export { add, init, WARN };
const INFO = color.bgBlueBright.white('INFO');

export { add, init, WARN, INFO };

0 comments on commit 757b250

Please sign in to comment.