Skip to content

Commit

Permalink
fix(cli): fix read package on command start
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jun 13, 2023
1 parent 63443f6 commit 8f35a67
Show file tree
Hide file tree
Showing 17 changed files with 528 additions and 385 deletions.
8 changes: 4 additions & 4 deletions packages/cli-core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = {
},
coverageThreshold: {
global: {
statements: 71.03,
branches: 72.88,
functions: 48.71,
lines: 71.03
statements: 71.31,
branches: 73.06,
functions: 48.9,
lines: 71.31
}
},
};
4 changes: 2 additions & 2 deletions packages/cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"chalk": "4.1.2",
"change-case": "^4.1.2",
"commander": "9.3.0",
"consolidate": "0.16.0",
"consolidate": "1.0.1",
"create-frame": "1.0.0",
"enquirer": "2.3.6",
"execa": "5.1.1",
Expand Down Expand Up @@ -94,4 +94,4 @@
"@tsed/core": ">=7.14.2",
"@tsed/di": ">=7.14.2"
}
}
}
3 changes: 3 additions & 0 deletions packages/cli-core/src/services/CliService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class CliService {
*/
public runLifecycle(cmdName: string, data: any = {}, $ctx: DIContext) {
return runInContext($ctx, async () => {
await this.injector.emit("$loadPackageJson");

data = await this.beforePrompt(cmdName, data);

$ctx.set("data", data);
Expand Down Expand Up @@ -214,6 +216,7 @@ export class CliService {

$ctx.set("data", data);
$ctx.set("command", metadata);
this.injector.settings.set("command.metadata", metadata);

return this.runLifecycle(name, data, $ctx);
};
Expand Down
19 changes: 16 additions & 3 deletions packages/cli-core/src/services/ProjectPackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class ProjectPackageJson {
dependencies: {},
devDependencies: {}
});
this.read();

this.packageManagers = packageManagers.filter((manager) => manager.has());
}

Expand Down Expand Up @@ -144,6 +144,10 @@ export class ProjectPackageJson {
return this.packageManagers.map((manager) => manager.name);
}

$loadPackageJson() {
return this.read();
}

toJSON() {
return this.raw;
}
Expand Down Expand Up @@ -394,7 +398,16 @@ export class ProjectPackageJson {
return this.fs.importModule(mod, this.dir);
}

public runScript(scriptName: string, {ignoreError, ...opts}: {ignoreError?: boolean} & Options & Record<string, any> = {}) {
public runScript(
scriptName: string,
{
ignoreError,
...opts
}: {
ignoreError?: boolean;
} & Options &
Record<string, any> = {}
) {
const options = {
cwd: this.dir,
...opts
Expand Down Expand Up @@ -452,7 +465,7 @@ export class ProjectPackageJson {

protected getPackageJson() {
const cwd = this.configuration.get("project.rootDir");
const disableReadUpPkg = this.configuration.get("disableReadUpPkg");
const disableReadUpPkg = this.configuration.get("command.metadata.disableReadUpPkg");
const name = this.configuration.get("name");

const pkgPath = join(String(cwd), "package.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class OidcProviderInitHook {
onExec(ctx: InitCmdContext) {
return [
{
title: "Generate files",
title: "Generate OIDC files",
task: () =>
this.rootRenderer.renderAll(
[
Expand Down
1 change: 1 addition & 0 deletions packages/cli-testing/src/CliPlatformTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class CliPlatformTest extends DITest {

await DITest.injector.load();
await DITest.injector.emit("$onReady");
await DITest.injector.emit("$loadPackageJson");

CliPlatformTest.get(CliService).load();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-testing/src/FakeCliFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class FakeCliFs {
async readJson(file: string | Buffer | number, encoding?: any): Promise<string> {
const content = await this.readFile(file, encoding);

return JSON.parse(content);
return content ? JSON.parse(content) : {};
}

readJsonSync(file: string | Buffer | number, encoding?: any): Promise<string> {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = {

coverageThreshold: {
global: {
statements: 91.77,
branches: 72.61,
statements: 91.69,
branches: 78.54,
functions: 75.94,
lines: 91.77
lines: 91.69
}
}
};
7 changes: 6 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"start:help:i": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/bin/tsed.ts init -h",
"start:version": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/bin/tsed.ts --version",
"start:init:help": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/bin/tsed.ts init -h",

"start:init:test": "cross-env NODE_ENV=development cross-env CI=true ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init/default --features=oidc --arch=default --convention=conv_default --platform=express --skip-prompt .",



"start:init:run": "cross-env NODE_ENV=development cross-env CI=true ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init/default",
"start:init:params": "cross-env NODE_ENV=development cross-env CI=true ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init/default --skip-prompt --features swagger,jest,lintstaged",
"start:init:run:name": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/bin/tsed.ts init -r ./.tmp/init awesome --verbose",
Expand Down Expand Up @@ -87,4 +92,4 @@
"jest": "29.5.0"
},
"peerDependencies": {}
}
}
12 changes: 7 additions & 5 deletions packages/cli/src/commands/init/InitCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,14 @@ export class InitCmd implements CommandProvider {
ctx.features.forEach((value) => {
const feature = FeaturesMap[value.toLowerCase()];

if (feature.dependencies) {
this.packageJson.addDependencies(feature.dependencies, ctx);
}
if (feature) {
if (feature.dependencies) {
this.packageJson.addDependencies(feature.dependencies, ctx);
}

if (feature.devDependencies) {
this.packageJson.addDevDependencies(feature.devDependencies, ctx);
if (feature.devDependencies) {
this.packageJson.addDevDependencies(feature.devDependencies, ctx);
}
}
});

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/init/config/FeaturesPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ export const FeaturesMap: Record<string, Feature> = {
[FeatureType.TYPEORM]: {
name: "TypeORM",
devDependencies: {
"@tsed/cli-plugin-typeorm": "{{cliVersion}}"
"@tsed/cli-plugin-typeorm": "{{cliVersion}}",
typeorm: "latest"
}
},
[FeatureType.JEST]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ describe("getFeaturesPrompt", () => {
Object {
"devDependencies": Object {
"@tsed/cli-plugin-typeorm": "{{cliVersion}}",
"typeorm": "latest",
},
"name": "TypeORM",
"value": "typeorm",
Expand Down
Loading

0 comments on commit 8f35a67

Please sign in to comment.