fix(cli): init command reading package.json from two folders up (#11789)

This commit is contained in:
Piotr Kuczynski 2025-11-25 14:13:25 +01:00 committed by GitHub
parent cb1284c8c0
commit dd55218648
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 23 deletions

View File

@ -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<string> {
const packageJson = JSON.parse(packageJsonContents)
const ourPackageJson = JSON.parse(
await CommandUtils.readFile(`${__dirname}/../package.json`),
)
if (!packageJson.devDependencies) packageJson.devDependencies = {}
packageJson.devDependencies = {

View File

@ -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) {