Skip to content

Commit 2f0f9be

Browse files
committed
fix: Fixed plain reporter for init
1 parent 0a9ae7b commit 2f0f9be

File tree

10 files changed

+36
-64
lines changed

10 files changed

+36
-64
lines changed

src/commands/init.ts

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
import fs from 'node:fs/promises';
22

33
import { BaseCommand } from '../common/base-command.js';
4-
import { InitializeOrchestrator } from '../orchestrators/initialize.js';
4+
import { InitializeOrchestrator } from '../orchestrators/init.js';
55
import { ShellUtils } from '../utils/shell.js';
66

77
export default class Init extends BaseCommand {
88
static strict = false;
99
static override description =
10-
`Generate codify configs from already installed packages. Use a list of space separated arguments to specify the resource types to import. Leave blank to import all resource in an existing *.codify.json file.
10+
`Initialize codify.`
1111

12-
Modes:
13-
1. No args: if no args are specified and an *.codify.json already exists. Then codify will update the existing file with any new changes to the resources specified in the file/files.
14-
15-
Command: codify import
16-
17-
2. With args: specify specific resources to import using arguments. Wild card matching is supported using '*' and ? (Note: in zsh * expands to the current dir and needs to be escaped using \\* or '*'). A prompt will be shown if more information is required to complete the import.
18-
19-
Example: codify import nvm asdf\\*, codify import \\* (for importing all supported resources)
20-
21-
The results can then be saved:
22-
a. To an existing *.codify.json file
23-
b. To a new file
24-
c. Or only printed to console
25-
26-
Codify will try to smartly insert new configs by following existing spacing and formatting.
27-
`
12+
static baseFlags= {
13+
...BaseCommand.baseFlags,
14+
path: { hidden: true },
15+
}
2816

2917
static override examples = [
3018
'<%= config.bin %> <%= command.id %> homebrew nvm asdf\\*',
@@ -34,39 +22,6 @@ Codify will try to smartly insert new configs by following existing spacing and
3422
]
3523

3624
public async run(): Promise<void> {
37-
const { raw, flags } = await this.parse(Init)
38-
3925
await InitializeOrchestrator.run(this.reporter);
40-
41-
// if (flags.path) {
42-
// this.log(`Applying Codify from: ${flags.path}`);
43-
// }
44-
//
45-
// const resolvedPath = path.resolve(flags.path ?? '.');
46-
//
47-
// const args = raw
48-
// .filter((r) => r.type === 'arg')
49-
// .map((r) => r.input);
50-
//
51-
// const cleanedArgs = await this.cleanupZshStarExpansion(args);
52-
//
53-
// await ImportOrchestrator.run({
54-
// typeIds: cleanedArgs,
55-
// path: resolvedPath,
56-
// secureMode: flags.secure,
57-
// }, this.reporter)
58-
//
59-
// process.exit(0)
60-
}
61-
62-
private async cleanupZshStarExpansion(args: string[]): Promise<string[]> {
63-
const combinedArgs = args.join(' ');
64-
const zshStarExpansion = (await ShellUtils.isZshShell())
65-
? (await fs.readdir(process.cwd())).filter((name) => !name.startsWith('.')).join(' ')
66-
: ''
67-
68-
return combinedArgs
69-
.replaceAll(zshStarExpansion, '*')
70-
.split(' ')
7126
}
7227
}

src/common/base-command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { prettyPrintError } from './errors.js';
1212
export abstract class BaseCommand extends Command {
1313
static baseFlags = {
1414
'debug': Flags.boolean({
15-
description: 'Add additional debug logs.'
15+
description: 'Print additional debug logs.'
1616
}),
1717
'output': Flags.option({
1818
char: 'o',

src/orchestrators/destroy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { InternalError } from '../common/errors.js';
2+
import { PluginInitOrchestrator } from '../common/initialize-plugins.js';
23
import { Project } from '../entities/project.js';
34
import { ResourceConfig } from '../entities/resource-config.js';
45
import { ProcessName, SubProcessName, ctx } from '../events/context.js';
56
import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js';
67
import { Reporter } from '../ui/reporters/reporter.js';
78
import { getTypeAndNameFromId } from '../utils/index.js';
8-
import { PluginInitOrchestrator } from './initialize-plugins.js';
99

1010
export interface DestroyArgs {
1111
ids: string[];

src/orchestrators/import.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path';
22

3+
import { InitializationResult, PluginInitOrchestrator } from '../common/initialize-plugins.js';
34
import { Project } from '../entities/project.js';
45
import { ResourceConfig } from '../entities/resource-config.js';
56
import { ResourceInfo } from '../entities/resource-info.js';
@@ -12,7 +13,6 @@ import { FileUtils } from '../utils/file.js';
1213
import { FileModificationCalculator, ModificationType } from '../utils/file-modification-calculator.js';
1314
import { groupBy, sleep } from '../utils/index.js';
1415
import { wildCardMatch } from '../utils/wild-card-match.js';
15-
import { InitializationResult, PluginInitOrchestrator } from './initialize-plugins.js';
1616

1717
export type ImportResult = { result: ResourceConfig[], errors: string[] }
1818

@@ -57,11 +57,7 @@ export class ImportOrchestrator {
5757
throw new Error('At least one resource [type] must be specified. Ex: "codify import homebrew". Or the import command must be run in a directory with a valid codify file')
5858
}
5959

60-
if (!typeIds || typeIds.length === 0) {
61-
await ImportOrchestrator.runExistingProject(reporter, initializationResult);
62-
} else {
63-
await ImportOrchestrator.runNewImport(typeIds, reporter, initializationResult)
64-
}
60+
await (!typeIds || typeIds.length === 0 ? ImportOrchestrator.runExistingProject(reporter, initializationResult) : ImportOrchestrator.runNewImport(typeIds, reporter, initializationResult));
6561
}
6662

6763
/** Import new resources. Type ids supplied. This will ask for any required parameters */
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import chalk from 'chalk';
22
import path from 'node:path';
33

4+
import { PluginInitOrchestrator } from '../common/initialize-plugins.js';
45
import { ResourceConfig } from '../entities/resource-config.js';
56
import { ProcessName, SubProcessName, ctx } from '../events/context.js';
67
import { Reporter } from '../ui/reporters/reporter.js';
78
import { FileUtils } from '../utils/file.js';
89
import { resolvePathWithVariables, untildify } from '../utils/index.js';
9-
import { PluginInitOrchestrator } from './initialize-plugins.js';
1010

1111
export const InitializeOrchestrator = {
1212

src/orchestrators/plan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { PluginInitOrchestrator } from '../common/initialize-plugins.js';
12
import { Plan } from '../entities/plan.js';
23
import { Project } from '../entities/project.js';
34
import { ProcessName, SubProcessName, ctx } from '../events/context.js';
45
import { PluginManager } from '../plugins/plugin-manager.js';
56
import { Reporter } from '../ui/reporters/reporter.js';
67
import { createStartupShellScriptsIfNotExists } from '../utils/file.js';
7-
import { PluginInitOrchestrator } from './initialize-plugins.js';
88
import { ValidateOrchestrator } from './validate.js';
99

1010
export interface PlanArgs {

src/orchestrators/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SubProcessName, ctx } from '../events/context.js';
22
import { Reporter } from '../ui/reporters/reporter.js';
3-
import { InitializationResult, PluginInitOrchestrator } from './initialize-plugins.js';
3+
import { InitializationResult, PluginInitOrchestrator } from '../common/initialize-plugins.js';
44

55
export interface ValidateArgs {
66
existing?: InitializationResult;

src/ui/reporters/plain-reporter.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export class PlainReporter implements Reporter {
2525
}
2626
}
2727

28+
async hide(): Promise<void> {}
29+
2830
async displayImportWarning(requiresParameters: string[], noParametersRequired: string[]): Promise<void> {
2931
console.log(chalk.bold('Additional information is required to continue import'))
3032
console.log('Some of the resources specified in the import support multiple instances. Additional information is required to identify the specific instance to import. If importing multiple instances is desired (for ex: multiple git clones) additional imports can be added in the prompt.')
@@ -92,6 +94,18 @@ export class PlainReporter implements Reporter {
9294
return result;
9395
}
9496

97+
async displayProgress(): Promise<void> {}
98+
99+
async promptInput(prompt: string, error?: string, validation?: () => Promise<boolean>, autoComplete?: (input: string) => string[]): Promise<string> {
100+
return new Promise((resolve) => {
101+
this.rl.question(prompt + (error ? chalk.red(`\n${error} `) : ''), (answer) => resolve(answer));
102+
});
103+
}
104+
105+
async promptInitResultSelection(availableTypes: string[]): Promise<string[]> {
106+
return availableTypes;
107+
}
108+
95109
displayImportResult(importResult: ImportResult) {
96110
console.log();
97111
console.log(JSON.stringify(importResult.result.map((r) => r.raw), null, 2));
@@ -107,6 +121,14 @@ export class PlainReporter implements Reporter {
107121
return undefined;
108122
}
109123

124+
async displayInitBanner(): Promise<void> {
125+
console.log(`Codify is a configuration-as-code tool that helps you setup and manage your system.
126+
Use this init flow to get started quickly with Codify.
127+
`);
128+
129+
await this.promptConfirmation('Codify will scan your system for any supported programs or settings and automatically generate configs for you.')
130+
}
131+
110132
async promptConfirmation(message: string): Promise<boolean> {
111133
const response = await new Promise((resolve) => {
112134
this.rl.question(`${message} (only 'yes' is accepted)`, (answer) => resolve(answer));
@@ -125,5 +147,4 @@ export class PlainReporter implements Reporter {
125147
console.log('🎉 Finished applying 🎉');
126148
console.log('Open a new terminal or source \'.zshrc\' for the new changes to be reflected')
127149
}
128-
129150
}

test/orchestrator/initialize/initialize.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as fs from 'node:fs';
33
import os from 'node:os';
44

55
import { MockOs } from '../mocks/system';
6-
import { PluginInitOrchestrator } from '../../../src/orchestrators/initialize-plugins';
6+
import { PluginInitOrchestrator } from '../../../src/common/initialize-plugins';
77
import path from 'node:path';
88
import { MockReporter } from '../mocks/reporter';
99
import { MockResource, MockResourceConfig } from '../mocks/resource';

0 commit comments

Comments
 (0)