@tsed/cli-core 7.0.0-beta.2 → 7.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -58,7 +58,7 @@ export class CliCore {
58
58
  name: settings.name || "tsed",
59
59
  argv,
60
60
  project: {
61
- // rootDir: this.getProjectRoot(argv),
61
+ rootDir: this.getProjectRoot(argv),
62
62
  srcDir: "src",
63
63
  scriptsDir: "scripts",
64
64
  ...(settings.project || {})
@@ -1,5 +1,4 @@
1
- import { __decorate, __metadata, __param } from "tslib";
2
- import { Inject, injectable } from "@tsed/di";
1
+ import { Inject, inject, injectable, injectMany } from "@tsed/di";
3
2
  import { EMPTY, throwError } from "rxjs";
4
3
  import { catchError } from "rxjs/operators";
5
4
  import { ProjectPackageJson } from "../services/ProjectPackageJson.js";
@@ -18,17 +17,19 @@ function mapPackagesWithInvalidVersion(deps) {
18
17
  .filter(([, version]) => !isValidVersion(version))
19
18
  .map(toString);
20
19
  }
21
- let PackageManagersModule = class PackageManagersModule {
22
- constructor(packageManagers) {
23
- this.packageManagers = packageManagers;
24
- this.packageManagers = packageManagers.filter((manager) => manager.has());
20
+ export class PackageManagersModule {
21
+ constructor() {
22
+ this.projectPackageJson = inject(ProjectPackageJson);
23
+ this.packageManagers = injectMany("package:manager").filter((manager) => {
24
+ return manager.has();
25
+ });
25
26
  }
26
27
  init(options = {}) {
27
28
  const packageManager = this.get(options.packageManager);
28
29
  options.packageManager = packageManager.name;
29
30
  options = {
30
31
  ...options,
31
- cwd: this.projectPackageJson.dir,
32
+ cwd: this.projectPackageJson.cwd,
32
33
  env: {
33
34
  ...process.env,
34
35
  GH_TOKEN: this.projectPackageJson.GH_TOKEN
@@ -45,7 +46,7 @@ let PackageManagersModule = class PackageManagersModule {
45
46
  const deps = mapPackagesWithInvalidVersion(this.projectPackageJson.dependencies);
46
47
  options = {
47
48
  ...options,
48
- cwd: this.projectPackageJson.dir,
49
+ cwd: this.projectPackageJson.cwd,
49
50
  env: {
50
51
  ...process.env,
51
52
  GH_TOKEN: this.projectPackageJson.GH_TOKEN
@@ -101,6 +102,7 @@ let PackageManagersModule = class PackageManagersModule {
101
102
  selectedPackageManager = this.packageManagers.find((manager) => manager.name === "npm");
102
103
  }
103
104
  this.projectPackageJson.setPreference("packageManager", selectedPackageManager.name);
105
+ console.log("==", name, selectedPackageManager);
104
106
  return selectedPackageManager;
105
107
  }
106
108
  runScript(scriptName, { ignoreError, ...opts } = {}) {
@@ -116,14 +118,5 @@ let PackageManagersModule = class PackageManagersModule {
116
118
  });
117
119
  return this.get().runScript(scriptName, options).pipe(errorPipe());
118
120
  }
119
- };
120
- __decorate([
121
- Inject(),
122
- __metadata("design:type", ProjectPackageJson)
123
- ], PackageManagersModule.prototype, "projectPackageJson", void 0);
124
- PackageManagersModule = __decorate([
125
- __param(0, Inject("package:manager")),
126
- __metadata("design:paramtypes", [Array])
127
- ], PackageManagersModule);
128
- export { PackageManagersModule };
121
+ }
129
122
  injectable(PackageManagersModule).imports([YarnManager, YarnBerryManager, NpmManager, PNpmManager, BunManager]);
@@ -1,10 +1,10 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Inject } from "@tsed/di";
1
+ import { inject } from "@tsed/di";
3
2
  import { Observable } from "rxjs";
4
3
  import { CliExeca } from "../../services/CliExeca.js";
5
4
  export class BaseManager {
6
5
  constructor() {
7
6
  this.verboseOpt = "--verbose";
7
+ this.cliExeca = inject(CliExeca);
8
8
  }
9
9
  has() {
10
10
  try {
@@ -23,7 +23,3 @@ export class BaseManager {
23
23
  return this.cliExeca.run(this.cmd, [cmd, options.verbose && this.verboseOpt, ...args].filter(Boolean), options);
24
24
  }
25
25
  }
26
- __decorate([
27
- Inject(CliExeca),
28
- __metadata("design:type", CliExeca)
29
- ], BaseManager.prototype, "cliExeca", void 0);
@@ -41,10 +41,11 @@ export class CliHttpClient extends CliHttpLogClient {
41
41
  return this.mapResponse(result, options);
42
42
  }
43
43
  getRequestParameters(method, endpoint, options) {
44
+ const url = (this.host || "") + endpoint.replace(this.host || "", "");
44
45
  options = {
45
46
  method,
46
- url: (this.host || "") + endpoint.replace(this.host || "", ""),
47
47
  ...options,
48
+ url,
48
49
  params: options.params || options.qs,
49
50
  data: options.data,
50
51
  headers: {
@@ -53,7 +54,7 @@ export class CliHttpClient extends CliHttpLogClient {
53
54
  ...(options.headers || {})
54
55
  }
55
56
  };
56
- this.configureProxy(endpoint, options);
57
+ this.configureProxy(url, options);
57
58
  return options;
58
59
  }
59
60
  configureProxy(endpoint, options) {
@@ -11,7 +11,7 @@ let CliHttpLogClient = class CliHttpLogClient {
11
11
  this.callee = options.callee || "http";
12
12
  }
13
13
  onSuccess(options) {
14
- return this.logger.debug({
14
+ this.logger.debug({
15
15
  ...this.formatLog(options),
16
16
  status: "OK"
17
17
  });
@@ -1,21 +1,12 @@
1
1
  import { extname } from "node:path";
2
2
  import { inject, injectable } from "@tsed/di";
3
- import { default as Ajv } from "ajv";
3
+ import { validate } from "../utils/validate.js";
4
4
  import { CliFs } from "./CliFs.js";
5
5
  import { CliYaml } from "./CliYaml.js";
6
6
  export class CliLoadFile {
7
- // @ts-ignore
8
- #ajv;
9
7
  constructor() {
10
8
  this.cliYaml = inject(CliYaml);
11
9
  this.cliFs = inject(CliFs);
12
- const options = {
13
- verbose: false,
14
- coerceTypes: true,
15
- strict: false
16
- };
17
- // @ts-ignore
18
- this.#ajv = new Ajv(options);
19
10
  }
20
11
  /**
21
12
  * Load a configuration file from yaml, json
@@ -33,18 +24,14 @@ export class CliLoadFile {
33
24
  throw new Error("Unsupported format file");
34
25
  }
35
26
  if (schema) {
36
- const validate = this.#ajv.compile(schema);
37
- const isValid = validate(config);
27
+ const { isValid, errors, value } = validate(config, schema);
38
28
  if (!isValid) {
39
- const [error] = validate.errors;
40
- throw new Error([
41
- `${error.instancePath.replace(/\//gi, ".")} `,
42
- error.message,
43
- error.params?.allowedValues && `. Allowed values: ${error.params?.allowedValues}`
44
- ]
29
+ const [error] = errors;
30
+ throw new Error([`${error.path.replace(/\//gi, ".")} `, error.message, error.expected && `. Allowed values: ${error.expected}`]
45
31
  .filter(Boolean)
46
32
  .join(""));
47
33
  }
34
+ return value;
48
35
  }
49
36
  return config;
50
37
  }
@@ -44,29 +44,25 @@ export class CliService {
44
44
  */
45
45
  runLifecycle(cmdName, data = {}, $ctx) {
46
46
  return runInContext($ctx, async () => {
47
- await $asyncEmit("$loadPackageJson");
48
- data = await this.beforePrompt(cmdName, data);
49
- $ctx.set("data", data);
50
- data = await this.prompt(cmdName, data);
51
- await this.dispatch(cmdName, data, $ctx);
52
- });
53
- }
54
- async dispatch(cmdName, data, $ctx) {
55
- try {
56
47
  $ctx.set("dispatchCmd", cmdName);
57
- $ctx.set("data", data);
58
- await this.exec(cmdName, data, $ctx);
59
- }
60
- catch (er) {
61
- await $asyncEmit("$onFinish", er);
48
+ await $asyncEmit("$loadPackageJson");
49
+ data = await this.beforePrompt(cmdName, data, $ctx);
50
+ data = await this.prompt(cmdName, data, $ctx);
51
+ try {
52
+ await this.exec(cmdName, data, $ctx);
53
+ }
54
+ catch (er) {
55
+ await $asyncEmit("$onFinish", [data, er]);
56
+ await destroyInjector();
57
+ throw er;
58
+ }
59
+ await $asyncEmit("$onFinish", [data]);
62
60
  await destroyInjector();
63
- throw er;
64
- }
65
- await $asyncEmit("$onFinish");
66
- await destroyInjector();
61
+ });
67
62
  }
68
63
  async exec(cmdName, data, $ctx) {
69
64
  const initialTasks = await this.getTasks(cmdName, data);
65
+ $ctx.set("data", data);
70
66
  if (initialTasks.length) {
71
67
  const tasks = [
72
68
  ...initialTasks,
@@ -77,45 +73,53 @@ export class CliService {
77
73
  },
78
74
  ...(await this.getPostInstallTasks(cmdName, data))
79
75
  ];
80
- return createTasksRunner(tasks, this.mapData(cmdName, data, $ctx));
76
+ data = this.mapData(cmdName, data, $ctx);
77
+ $ctx.set("data", data);
78
+ return createTasksRunner(tasks, data);
81
79
  }
82
80
  }
83
81
  /**
84
82
  * Run prompt for a given command
85
83
  * @param cmdName
86
84
  * @param data Initial data
85
+ * @param $ctx
87
86
  */
88
- async beforePrompt(cmdName, data = {}) {
87
+ async beforePrompt(cmdName, data = {}, $ctx) {
89
88
  const provider = this.commands.get(cmdName);
90
89
  const instance = inject(provider.useClass);
91
90
  const verbose = data.verbose;
91
+ $ctx.set("data", data);
92
92
  if (instance.$beforePrompt) {
93
93
  data = await instance.$beforePrompt(JSON.parse(JSON.stringify(data)));
94
94
  data.verbose = verbose;
95
95
  }
96
+ $ctx.set("data", data);
96
97
  return data;
97
98
  }
98
99
  /**
99
100
  * Run prompt for a given command
100
101
  * @param cmdName
101
- * @param ctx Initial data
102
+ * @param data
103
+ * @param $ctx
102
104
  */
103
- async prompt(cmdName, ctx = {}) {
105
+ async prompt(cmdName, data = {}, $ctx) {
104
106
  const provider = this.commands.get(cmdName);
105
107
  const instance = inject(provider.useClass);
108
+ $ctx.set("data", data);
106
109
  if (instance.$prompt) {
107
110
  const questions = [
108
- ...(await instance.$prompt(ctx)),
109
- ...(await this.hooks.emit(CommandStoreKeys.PROMPT_HOOKS, cmdName, ctx))
111
+ ...(await instance.$prompt(data)),
112
+ ...(await this.hooks.emit(CommandStoreKeys.PROMPT_HOOKS, cmdName, data))
110
113
  ];
111
114
  if (questions.length) {
112
- ctx = {
113
- ...ctx,
115
+ data = {
116
+ ...data,
114
117
  ...(await Inquirer.prompt(questions))
115
118
  };
116
119
  }
117
120
  }
118
- return ctx;
121
+ $ctx.set("data", data);
122
+ return data;
119
123
  }
120
124
  /**
121
125
  * Run lifecycle
@@ -59,7 +59,7 @@ export class ProjectPackageJson {
59
59
  this.setCWD(dir);
60
60
  }
61
61
  get cwd() {
62
- return String(constant("project.rootDir"));
62
+ return String(constant("project.rootDir", ""));
63
63
  }
64
64
  get name() {
65
65
  return this.raw.name;
@@ -8,3 +8,4 @@ export * from "./mapCommanderArgs.js";
8
8
  export * from "./mapCommanderOptions.js";
9
9
  export * from "./parseOption.js";
10
10
  export * from "./resolveConfiguration.js";
11
+ export * from "./validate.js";
@@ -0,0 +1,23 @@
1
+ import { Ajv } from "ajv";
2
+ const ajv = new Ajv({
3
+ verbose: false,
4
+ coerceTypes: true,
5
+ strict: false,
6
+ allErrors: true
7
+ });
8
+ export function validate(value, schema) {
9
+ const validate = ajv.compile(schema.toJSON());
10
+ const result = validate(value);
11
+ if (!result) {
12
+ const errors = (validate.errors || []).map((e) => ({
13
+ path: e.instancePath || e.schemaPath,
14
+ message: e.message,
15
+ expected: (e.params && e.params.type) || undefined
16
+ }));
17
+ return {
18
+ isValid: false,
19
+ errors: errors
20
+ };
21
+ }
22
+ return { isValid: true, value: value };
23
+ }
@@ -5,9 +5,8 @@ export interface InstallOptions {
5
5
  [key: string]: any;
6
6
  }
7
7
  export declare class PackageManagersModule {
8
- protected packageManagers: BaseManager[];
9
8
  protected projectPackageJson: ProjectPackageJson;
10
- constructor(packageManagers: BaseManager[]);
9
+ protected packageManagers: BaseManager[];
11
10
  init(options?: InstallOptions): Promise<void>;
12
11
  install(options?: InstallOptions): ({
13
12
  title: string;
@@ -6,7 +6,7 @@ export declare class CliHttpLogClient {
6
6
  callee: string;
7
7
  protected logger: Logger;
8
8
  constructor(options?: Partial<BaseLogClientOptions>);
9
- protected onSuccess(options: Record<string, unknown>): Logger;
9
+ protected onSuccess(options: Record<string, unknown>): void;
10
10
  protected onError(error: any, options: any): void;
11
11
  protected logToCurl(options: any): string;
12
12
  protected getStatusCodeFromError(error: any): any;
@@ -1,13 +1,11 @@
1
- import { type Schema } from "ajv";
1
+ import type { JsonSchema } from "@tsed/schema";
2
2
  import { CliFs } from "./CliFs.js";
3
3
  import { CliYaml } from "./CliYaml.js";
4
4
  export declare class CliLoadFile {
5
- #private;
6
5
  protected cliYaml: CliYaml;
7
6
  protected cliFs: CliFs;
8
- constructor();
9
7
  /**
10
8
  * Load a configuration file from yaml, json
11
9
  */
12
- loadFile<Model = any>(path: string, schema?: Schema): Promise<Model>;
10
+ loadFile<Model = any>(path: string, schema?: JsonSchema<Model>): Promise<Model>;
13
11
  }
@@ -26,20 +26,21 @@ export declare class CliService {
26
26
  * @param $ctx
27
27
  */
28
28
  runLifecycle(cmdName: string, data: CommandData | undefined, $ctx: DIContext): Promise<Promise<void>>;
29
- dispatch(cmdName: string, data: CommandData, $ctx: DIContext): Promise<void>;
30
29
  exec(cmdName: string, data: any, $ctx: DIContext): Promise<any>;
31
30
  /**
32
31
  * Run prompt for a given command
33
32
  * @param cmdName
34
33
  * @param data Initial data
34
+ * @param $ctx
35
35
  */
36
- beforePrompt(cmdName: string, data?: CommandData): Promise<CommandData>;
36
+ beforePrompt(cmdName: string, data: CommandData | undefined, $ctx: DIContext): Promise<CommandData>;
37
37
  /**
38
38
  * Run prompt for a given command
39
39
  * @param cmdName
40
- * @param ctx Initial data
40
+ * @param data
41
+ * @param $ctx
41
42
  */
42
- prompt(cmdName: string, ctx?: CommandData): Promise<CommandData>;
43
+ prompt(cmdName: string, data: CommandData | undefined, $ctx: DIContext): Promise<CommandData>;
43
44
  /**
44
45
  * Run lifecycle
45
46
  * @param cmdName
@@ -8,3 +8,4 @@ export * from "./mapCommanderArgs.js";
8
8
  export * from "./mapCommanderOptions.js";
9
9
  export * from "./parseOption.js";
10
10
  export * from "./resolveConfiguration.js";
11
+ export * from "./validate.js";
@@ -0,0 +1,14 @@
1
+ import type { JsonSchema } from "@tsed/schema";
2
+ export declare function validate<Value>(value: unknown, schema: JsonSchema<Value>): {
3
+ isValid: boolean;
4
+ errors: {
5
+ path: string;
6
+ message: string | undefined;
7
+ expected: any;
8
+ }[];
9
+ value?: undefined;
10
+ } | {
11
+ isValid: boolean;
12
+ value: Value;
13
+ errors?: undefined;
14
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tsed/cli-core",
3
3
  "description": "Build your CLI with TypeScript and Decorators",
4
- "version": "7.0.0-beta.2",
4
+ "version": "7.0.0-beta.3",
5
5
  "type": "module",
6
6
  "main": "./lib/esm/index.js",
7
7
  "source": "./src/index.ts",
@@ -65,7 +65,7 @@
65
65
  "uuid": "^10.0.0"
66
66
  },
67
67
  "devDependencies": {
68
- "@tsed/typescript": "7.0.0-beta.2",
68
+ "@tsed/typescript": "7.0.0-beta.3",
69
69
  "@types/commander": "2.12.2",
70
70
  "@types/figures": "3.0.1",
71
71
  "@types/fs-extra": "^11.0.4",