@tsed/cli-core 7.0.0-alpha.9 → 7.0.0-beta.2

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.
@@ -1,16 +1,13 @@
1
1
  import "@tsed/logger-std";
2
2
  import { join, resolve } from "node:path";
3
- import { Type } from "@tsed/core";
4
- import { inject, injectable, InjectorService } from "@tsed/di";
3
+ import { constant, inject, injector } from "@tsed/di";
5
4
  import { $asyncEmit } from "@tsed/hooks";
6
5
  import chalk from "chalk";
7
6
  import { Command } from "commander";
8
7
  import semver from "semver";
9
8
  import updateNotifier from "update-notifier";
10
9
  import { CliError } from "./domains/CliError.js";
11
- import { CliPackageJson } from "./services/CliPackageJson.js";
12
10
  import { CliService } from "./services/CliService.js";
13
- import { ProjectPackageJson } from "./services/ProjectPackageJson.js";
14
11
  import { createInjector } from "./utils/createInjector.js";
15
12
  import { loadPlugins } from "./utils/loadPlugins.js";
16
13
  import { resolveConfiguration } from "./utils/resolveConfiguration.js";
@@ -18,9 +15,21 @@ function isHelpManual(argv) {
18
15
  return argv.includes("-h") || argv.includes("--help");
19
16
  }
20
17
  export class CliCore {
21
- constructor() {
22
- this.injector = inject(InjectorService);
23
- this.cliService = inject(CliService);
18
+ constructor(settings) {
19
+ createInjector(settings);
20
+ }
21
+ static checkPrecondition(settings) {
22
+ const { pkg } = settings;
23
+ this.checkPackage(pkg);
24
+ if (pkg?.engines?.node) {
25
+ this.checkNodeVersion(pkg.engines.node, pkg.name);
26
+ }
27
+ }
28
+ static checkPackage(pkg) {
29
+ if (!pkg) {
30
+ console.log(chalk.red(`settings.pkg is required. Require the package.json of your CLI when you bootstrap the CLI.`));
31
+ process.exit(1);
32
+ }
24
33
  }
25
34
  static checkNodeVersion(wanted, id) {
26
35
  if (!semver.satisfies(process.version, wanted)) {
@@ -35,45 +44,30 @@ export class CliCore {
35
44
  }
36
45
  return this;
37
46
  }
38
- static async create(settings, module = CliCore) {
47
+ static async bootstrap(settings) {
48
+ if (settings.checkPrecondition) {
49
+ this.checkPrecondition(settings);
50
+ }
51
+ if (settings.updateNotifier) {
52
+ await this.updateNotifier(settings.pkg);
53
+ }
39
54
  settings = resolveConfiguration(settings);
40
- const injector = this.createInjector(settings);
41
- settings.plugins && (await loadPlugins());
42
- await this.loadInjector(injector, module);
43
- await $asyncEmit("$onReady");
44
- return inject(CliCore);
45
- }
46
- static async bootstrap(settings, module = CliCore) {
47
- const cli = await this.create(settings, module);
48
- return cli.bootstrap();
49
- }
50
- static async loadInjector(injector, module = CliCore) {
51
- await injector.emit("$beforeInit");
52
- injector.addProvider(CliCore, {
53
- useClass: module
54
- });
55
- await injector.load();
56
- await injector.invoke(module);
57
- await $asyncEmit("$afterInit");
58
- injector.settings.set("loaded", true);
59
- }
60
- static async updateNotifier(pkg) {
61
- updateNotifier({ pkg, updateCheckInterval: 0 }).notify();
62
- return this;
63
- }
64
- static createInjector(settings) {
65
55
  const argv = settings.argv || process.argv;
66
- return createInjector({
56
+ return new CliCore({
67
57
  ...settings,
68
58
  name: settings.name || "tsed",
69
59
  argv,
70
60
  project: {
71
- rootDir: this.getProjectRoot(argv),
61
+ // rootDir: this.getProjectRoot(argv),
72
62
  srcDir: "src",
73
63
  scriptsDir: "scripts",
74
64
  ...(settings.project || {})
75
65
  }
76
- });
66
+ }).bootstrap();
67
+ }
68
+ static async updateNotifier(pkg) {
69
+ updateNotifier({ pkg, updateCheckInterval: 0 }).notify();
70
+ return this;
77
71
  }
78
72
  static getProjectRoot(argv) {
79
73
  if (!isHelpManual(argv)) {
@@ -84,7 +78,14 @@ export class CliCore {
84
78
  }
85
79
  async bootstrap() {
86
80
  try {
87
- await this.cliService.parseArgs(this.injector.settings.get("argv"));
81
+ const cliService = inject(CliService);
82
+ constant("plugins") && (await loadPlugins());
83
+ await $asyncEmit("$beforeInit");
84
+ await injector().load();
85
+ await $asyncEmit("$afterInit");
86
+ injector().settings.set("loaded", true);
87
+ await $asyncEmit("$onReady");
88
+ await cliService.parseArgs(constant("argv"));
88
89
  }
89
90
  catch (er) {
90
91
  throw new CliError({ origin: er, cli: this });
@@ -92,4 +93,3 @@ export class CliCore {
92
93
  return this;
93
94
  }
94
95
  }
95
- injectable(CliCore).imports([CliPackageJson, ProjectPackageJson, CliService]);
@@ -45,12 +45,21 @@ export class ProjectPackageJson {
45
45
  get path() {
46
46
  return join(this.dir, "package.json");
47
47
  }
48
+ /**
49
+ * @deprecated
50
+ */
48
51
  get dir() {
49
- return String(constant("project.rootDir"));
52
+ return this.cwd;
50
53
  }
54
+ /**
55
+ * @deprecated
56
+ * @param dir
57
+ */
51
58
  set dir(dir) {
52
- configuration().set("project.rootDir", dir);
53
- this.read();
59
+ this.setCWD(dir);
60
+ }
61
+ get cwd() {
62
+ return String(constant("project.rootDir"));
54
63
  }
55
64
  get name() {
56
65
  return this.raw.name;
@@ -83,6 +92,10 @@ export class ProjectPackageJson {
83
92
  get preferences() {
84
93
  return this.raw[constant("name")];
85
94
  }
95
+ setCWD(dir) {
96
+ configuration().set("project.rootDir", dir);
97
+ this.read();
98
+ }
86
99
  fillWithPreferences(ctx) {
87
100
  return {
88
101
  ...ctx,
@@ -222,7 +235,7 @@ export class ProjectPackageJson {
222
235
  refresh() {
223
236
  this.reinstall = false;
224
237
  this.rewrite = false;
225
- const cwd = constant("project.rootDir");
238
+ const cwd = this.cwd;
226
239
  const pkgPath = join(String(cwd), "package.json");
227
240
  const pkg = this.fs.readJsonSync(pkgPath, { encoding: "utf8" });
228
241
  pkg.scripts = {
@@ -14,7 +14,7 @@ export function createInjector(settings = {}) {
14
14
  inj.settings.set({
15
15
  ...settings,
16
16
  project: {
17
- root: process.cwd(),
17
+ // root: process.cwd(),
18
18
  srcDir: "src",
19
19
  ...(settings.project || {})
20
20
  }
@@ -1,16 +1,11 @@
1
1
  import "@tsed/logger-std";
2
- import { Type } from "@tsed/core";
3
- import { InjectorService } from "@tsed/di";
4
- import { CliService } from "./services/CliService.js";
5
2
  export declare class CliCore {
6
- readonly injector: InjectorService;
7
- readonly cliService: CliService;
3
+ protected constructor(settings: Partial<TsED.Configuration>);
4
+ static checkPrecondition(settings: any): void;
5
+ static checkPackage(pkg: any): void;
8
6
  static checkNodeVersion(wanted: string, id: string): typeof CliCore;
9
- static create<Cli extends CliCore = CliCore>(settings: Partial<TsED.Configuration>, module?: Type): Promise<Cli>;
10
- static bootstrap(settings: Partial<TsED.Configuration>, module?: Type): Promise<CliCore>;
11
- static loadInjector(injector: InjectorService, module?: Type): Promise<void>;
7
+ static bootstrap(settings: Partial<TsED.Configuration>): Promise<CliCore>;
12
8
  static updateNotifier(pkg: any): Promise<typeof CliCore>;
13
- protected static createInjector(settings: Partial<TsED.Configuration>): InjectorService;
14
9
  protected static getProjectRoot(argv: string[]): string;
15
10
  bootstrap(): Promise<this>;
16
11
  }
@@ -33,7 +33,7 @@ declare global {
33
33
  */
34
34
  templateDir?: string;
35
35
  /**
36
- *
36
+ * A function that return default projet settings set in fresh project.
37
37
  * @param pkg
38
38
  */
39
39
  defaultProjectPreferences?: (pkg?: any) => Record<string, any>;
@@ -50,6 +50,14 @@ declare global {
50
50
  * Enable plugins loading
51
51
  */
52
52
  plugins: boolean;
53
+ /**
54
+ * Check version and node version before running a command
55
+ */
56
+ checkPrecondition?: boolean;
57
+ /**
58
+ * Display available update on terminal before running a command
59
+ */
60
+ updateNotifier?: boolean;
53
61
  }
54
62
  }
55
63
  }
@@ -9,8 +9,16 @@ export declare class ProjectPackageJson {
9
9
  private raw;
10
10
  constructor();
11
11
  get path(): string;
12
+ /**
13
+ * @deprecated
14
+ */
12
15
  get dir(): string;
16
+ /**
17
+ * @deprecated
18
+ * @param dir
19
+ */
13
20
  set dir(dir: string);
21
+ get cwd(): string;
14
22
  get name(): string;
15
23
  set name(name: string);
16
24
  get version(): string;
@@ -28,6 +36,7 @@ export declare class ProjectPackageJson {
28
36
  [key: string]: string;
29
37
  };
30
38
  get preferences(): ProjectPreferences;
39
+ setCWD(dir: string): void;
31
40
  fillWithPreferences<T extends {}>(ctx: T): T & {
32
41
  packageManager: import("../interfaces/ProjectPreferences.js").PackageManager;
33
42
  runtime: any;
@@ -1 +1 @@
1
- export declare function resolveConfiguration(settings: any): any;
1
+ export declare function resolveConfiguration(settings: Partial<TsED.Configuration>): Partial<TsED.Configuration>;
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-alpha.9",
4
+ "version": "7.0.0-beta.2",
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-alpha.9",
68
+ "@tsed/typescript": "7.0.0-beta.2",
69
69
  "@types/commander": "2.12.2",
70
70
  "@types/figures": "3.0.1",
71
71
  "@types/fs-extra": "^11.0.4",
@@ -105,6 +105,6 @@
105
105
  "author": "Romain Lenzotti",
106
106
  "license": "MIT",
107
107
  "publishConfig": {
108
- "tag": "alpha"
108
+ "tag": "beta"
109
109
  }
110
110
  }
package/readme.md CHANGED
@@ -216,7 +216,7 @@ website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
216
216
 
217
217
  The MIT License (MIT)
218
218
 
219
- Copyright (c) 2016 - 2023 Romain Lenzotti
219
+ Copyright (c) 2016 - Today Romain Lenzotti
220
220
 
221
221
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
222
222
  documentation files (the "Software"), to deal in the Software without restriction, including without limitation the