Skip to content

Commit

Permalink
fix: Fix module resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Mar 18, 2020
1 parent 05d8556 commit 55827e0
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 31 deletions.
4 changes: 3 additions & 1 deletion packages/cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"rxjs": "6.5.4",
"split": "1.0.1",
"tslib": "1.11.1",
"update-notifier": "4.1.0"
"update-notifier": "4.1.0",
"module-alias": "2.2.2",
"semver": "7.1.3"
},
"devDependencies": {
"@types/axios": "0.14.0",
Expand Down
45 changes: 45 additions & 0 deletions packages/cli-core/src/Cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Module} from "@tsed/di";
import * as chalk from "chalk";
import {Command} from "commander";
import {join, resolve} from "path";
import * as UpdateNotifier from "update-notifier";
Expand All @@ -11,6 +12,37 @@ import {createInjector} from "./utils/createInjector";
import {loadInjector} from "./utils/loadInjector";
import {loadPlugins} from "./utils/loadPlugins";

const semver = require("semver");

function checkSettings(settings: any) {
if (!settings.pkg) {
console.log(chalk.red(`settings.pkg is required. Require the package.json of your CLI when you bootstrap the CLI.`));
process.exit(1);
}

if (!settings.name) {
console.log(chalk.red(`settings.name is required. Add the name of your CLI.`));
process.exit(1);
}
}

function checkNodeVersion(wanted: string, id: string) {
if (!semver.satisfies(process.version, wanted)) {
console.log(
chalk.red(
"You are using Node " +
process.version +
", but this version of " +
id +
" requires Node " +
wanted +
".\nPlease upgrade your Node version."
)
);
process.exit(1);
}
}

@Module({
imports: [CliPackageJson, ProjectPackageJson, CliService, CliConfiguration, RenderService]
})
Expand All @@ -20,6 +52,19 @@ export class Cli {
}

static async bootstrap(settings: TsED.Configuration): Promise<Cli> {
checkSettings(settings);

if (settings.pkg.engines?.node) {
checkNodeVersion(settings.pkg.engines.node, settings.pkg.name);
}

require("module-alias").addAliases({
"@tsed/core": require.resolve("@tsed/core"),
"@tsed/di": require.resolve("@tsed/di"),
"@tsed/cli-core": require.resolve("@tsed/cli-core"),
[settings.pkg.name]: require.resolve(settings.pkg.name)
});

const injector = createInjector({
...settings,
project: {
Expand Down
2 changes: 0 additions & 2 deletions packages/cli-core/src/utils/importModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ export function importModule(mod: string, root: string = process.cwd()) {
path = require.resolve(mod);
}

// console.debug("Import " + mod + " from " + path);

return import(path);
}
9 changes: 2 additions & 7 deletions packages/cli-core/src/utils/loadPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {GlobalProviderRegistry, GlobalProviders, InjectorService} from "@tsed/di";
import {GlobalProviders, InjectorService} from "@tsed/di";
import {ProjectPackageJson} from "../services/ProjectPackageJson";
import {importModule} from "./importModule";

Expand All @@ -10,18 +10,13 @@ export async function loadPlugins(injector: InjectorService) {

const projectPackageJson = injector.invoke<ProjectPackageJson>(ProjectPackageJson);

const localDi = await importModule("@tsed/di", root);
const localGlobalProviders = localDi.GlobalProviders as GlobalProviderRegistry;

const promises = Object.keys(projectPackageJson.allDependencies)
.filter(mod => mod.startsWith(`@${name}/cli-plugin`) || mod.startsWith(`${name}-cli-plugin`))
.map(async mod => {
const {default: plugin} = await importModule(mod, root);

if (!injector.has(plugin)) {
const provider = localGlobalProviders.has(plugin)
? localGlobalProviders.get(plugin)?.clone()
: GlobalProviders.get(plugin)?.clone();
const provider = GlobalProviders.get(plugin)?.clone();

if (provider?.imports.length) {
provider.imports.forEach(token => injector.add(token, provider));
Expand Down
9 changes: 5 additions & 4 deletions packages/cli-plugin-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"typings": "./lib/index.d.ts",
"devDependencies": {
"@tsed/cli": "1.1.0",
"@tsed/cli-core": "1.1.0",
"tslib": "1.11.1"
"@tsed/cli-core": "1.1.0"
},
"scripts": {
"build": "tsc --build tsconfig.compile.json",
"build:doc": "tsc --build tsconfig.doc.json"
},
"dependencies": {},
"dependencies": {
"tslib": "1.11.1"
},
"peerDependencies": {}
}
}
9 changes: 5 additions & 4 deletions packages/cli-plugin-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"typings": "./lib/index.d.ts",
"devDependencies": {
"@tsed/cli": "1.1.0",
"@tsed/cli-core": "1.1.0",
"tslib": "1.11.1"
"@tsed/cli-core": "1.1.0"
},
"scripts": {
"build": "tsc --build tsconfig.compile.json",
"build:doc": "tsc --build tsconfig.doc.json"
},
"dependencies": {},
"dependencies": {
"tslib": "1.11.1"
},
"peerDependencies": {}
}
}
4 changes: 1 addition & 3 deletions packages/cli-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
],
"dependencies": {
"@tsed/cli-core": "1.1.0",
"@tsed/core": "^5.44.8",
"@tsed/di": "^5.44.8",
"tslib": "1.11.1"
},
"scripts": {
Expand All @@ -26,4 +24,4 @@
},
"devDependencies": {},
"peerDependencies": {}
}
}
4 changes: 1 addition & 3 deletions packages/cli-testing/src/CliTestContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {Cli, createInjector, loadInjector} from "@tsed/cli-core";
import {Env} from "@tsed/core";
import {InjectorService, LocalsContainer, OnInit, TokenProvider} from "@tsed/di";
import {Cli, createInjector, Env, InjectorService, loadInjector, LocalsContainer, OnInit, TokenProvider} from "@tsed/cli-core";

export interface IInvokeOptions {
token?: TokenProvider;
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"start:generate:help": "ts-node -r tsconfig-paths/register src/bin/tsed.ts generate -h",
"start:generate:run": "ts-node -r tsconfig-paths/register src/bin/tsed.ts generate --project-root ./.tmp"
},
"peerDependencies": {}
}
"peerDependencies": {},
"engines": {
"node": ">=8.9"
}
}
15 changes: 10 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9636,6 +9636,11 @@ modify-values@^1.0.0:
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
integrity sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==

[email protected]:
version "2.2.2"
resolved "https://registry.yarnpkg.com/module-alias/-/module-alias-2.2.2.tgz#151cdcecc24e25739ff0aa6e51e1c5716974c0e0"
integrity sha512-A/78XjoX2EmNvppVWEhM2oGk3x4lLxnkEA4jTbaK97QKSDjkIoOsKQlfylt/d3kKKi596Qy3NP5XrXJ6fZIC9Q==

move-concurrently@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
Expand Down Expand Up @@ -12577,16 +12582,16 @@ [email protected]:
resolved "https://registry.yarnpkg.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e"
integrity sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A==

[email protected], semver@^7.1.1, semver@^7.1.2:
version "7.1.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.1.3.tgz#e4345ce73071c53f336445cfc19efb1c311df2a6"
integrity sha512-ekM0zfiA9SCBlsKa2X1hxyxiI4L3B6EbVJkkdgQXnSEEaHlGdvyodMruTiulSRWMMB4NeIuYNMC9rTKTz97GxA==

semver@^6.0.0, semver@^6.2.0, semver@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.1.1, semver@^7.1.2:
version "7.1.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.1.3.tgz#e4345ce73071c53f336445cfc19efb1c311df2a6"
integrity sha512-ekM0zfiA9SCBlsKa2X1hxyxiI4L3B6EbVJkkdgQXnSEEaHlGdvyodMruTiulSRWMMB4NeIuYNMC9rTKTz97GxA==

semver@~5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
Expand Down

0 comments on commit 55827e0

Please sign in to comment.