diff --git a/src/commands/InitCommand.ts b/src/commands/InitCommand.ts index cf4d5e5bb..babfba35a 100644 --- a/src/commands/InitCommand.ts +++ b/src/commands/InitCommand.ts @@ -6,8 +6,6 @@ import { TypeORMError } from "../error" import { PlatformTools } from "../platform/PlatformTools" import { CommandUtils } from "./CommandUtils" -import ourPackageJson from "../../package.json" - /** * Generates a new project with TypeORM. */ @@ -117,7 +115,7 @@ export class InitCommand implements yargs.CommandModule { ) await CommandUtils.createFile( basePath + "/package.json", - InitCommand.appendPackageJson( + await InitCommand.appendPackageJson( packageJsonContents, database, isExpress, @@ -673,13 +671,16 @@ Steps to run this project: /** * Appends to a given package.json template everything needed. */ - protected static appendPackageJson( + protected static async appendPackageJson( packageJsonContents: string, database: string, express: boolean, projectIsEsm: boolean /*, docker: boolean*/, - ): string { + ): Promise { const packageJson = JSON.parse(packageJsonContents) + const ourPackageJson = JSON.parse( + await CommandUtils.readFile(`${__dirname}/../package.json`), + ) if (!packageJson.devDependencies) packageJson.devDependencies = {} packageJson.devDependencies = { diff --git a/test/github-issues/8975/issue-8975.ts b/test/github-issues/8975/issue-8975.ts index b7e6c408d..8175f534e 100644 --- a/test/github-issues/8975/issue-8975.ts +++ b/test/github-issues/8975/issue-8975.ts @@ -1,10 +1,9 @@ import { expect } from "chai" import { exec } from "child_process" -import { readFile, writeFile, chmod, unlink, rmdir } from "fs/promises" -import { dirname } from "path" +import { readFile, rm, unlink, writeFile } from "fs/promises" describe("cli init command", () => { - const cliPath = `${dirname(dirname(dirname(__dirname)))}/src/cli.js` + const cliPath = `${__dirname}/../../../src/cli.js` const databaseOptions = [ "mysql", "mariadb", @@ -20,21 +19,18 @@ describe("cli init command", () => { const builtSrcDirectory = "build/compiled/src" before(async () => { - const copyPackageJson = async () => { - // load package.json from the root of the project - const packageJson = JSON.parse( - await readFile("./package.json", "utf8"), - ) - packageJson.version = `0.0.0` // install no version but - packageJson.installFrom = `file:../${builtSrcDirectory}` // use the built src directory - // write the modified package.json to the build directory - await writeFile( - `./${builtSrcDirectory}/package.json`, - JSON.stringify(packageJson, null, 4), - ) - } + // load package.json from the root of the project + const packageJson = JSON.parse(await readFile("./package.json", "utf8")) - await Promise.all([chmod(cliPath, 0o755), copyPackageJson()]) + // init command is taking typeorm version from package.json + // so ensure we are working against local build + packageJson.version = `file:../${builtSrcDirectory}` + + // write the modified package.json to the build directory + await writeFile( + `./${builtSrcDirectory}/package.json`, + JSON.stringify(packageJson, null, 4), + ) }) after(async () => { @@ -42,7 +38,7 @@ describe("cli init command", () => { }) afterEach(async () => { - await rmdir(`./${testProjectPath}`, { recursive: true }) + await rm(`./${testProjectPath}`, { recursive: true, force: true }) }) for (const databaseOption of databaseOptions) {