diff --git a/package.json b/package.json index 3cf35c16..9265088c 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "dependencies": { "@codemod-utils/blueprints": "^0.1.2", "@codemod-utils/files": "^0.3.1", - "@codemod-utils/json": "^0.1.2", + "@codemod-utils/json": "^0.2.0", "strip-json-comments": "^5.0.0", "yargs": "^17.7.2" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66fdf75e..109c3827 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,8 +8,8 @@ dependencies: specifier: ^0.3.1 version: 0.3.1 '@codemod-utils/json': - specifier: ^0.1.2 - version: 0.1.2 + specifier: ^0.2.0 + version: 0.2.0 strip-json-comments: specifier: ^5.0.0 version: 5.0.0 @@ -288,8 +288,8 @@ packages: glob: 10.2.7 dev: false - /@codemod-utils/json@0.1.2: - resolution: {integrity: sha512-G05WyxlngqbrQK+quudBjJbHK/a2omNzqL7/oNrkc09IascvVM27Fb6GcNrOTIPU2atvP+zi9jDmZlIWiarHcA==} + /@codemod-utils/json@0.2.0: + resolution: {integrity: sha512-ysdsEs6X9TjHHjM36znyj2njmsccil94bMJ90whTn3HH2KCJnMt0SpjFDZk+dJOQP0SFpc9lG5JLA+UwVCit8A==} engines: {node: 16.* || >= 18} dev: false diff --git a/src/migration/ember-addon/steps/create-options.js b/src/migration/ember-addon/steps/create-options.js index 0cebb93f..2039c2ec 100644 --- a/src/migration/ember-addon/steps/create-options.js +++ b/src/migration/ember-addon/steps/create-options.js @@ -1,14 +1,20 @@ import { findFiles, unionize } from '@codemod-utils/files'; -import { readPackageJson } from '@codemod-utils/json'; +import { readPackageJson, validatePackageJson } from '@codemod-utils/json'; function analyzePackageJson(codemodOptions) { + const { projectRoot } = codemodOptions; + + const packageJson = readPackageJson({ projectRoot }); + + validatePackageJson(packageJson); + const { dependencies, devDependencies, 'ember-addon': emberAddon, name, version, - } = readPackageJson(codemodOptions); + } = packageJson; const projectDependencies = new Map([ ...Object.entries(dependencies ?? {}), diff --git a/src/migration/ember-addon/steps/update-addon-package-json.js b/src/migration/ember-addon/steps/update-addon-package-json.js index d754c73d..20c1289b 100644 --- a/src/migration/ember-addon/steps/update-addon-package-json.js +++ b/src/migration/ember-addon/steps/update-addon-package-json.js @@ -1,7 +1,11 @@ -import { readFileSync, writeFileSync } from 'node:fs'; +import { writeFileSync } from 'node:fs'; import { join } from 'node:path'; -import { convertToMap, convertToObject } from '@codemod-utils/json'; +import { + convertToMap, + convertToObject, + readPackageJson, +} from '@codemod-utils/json'; import { getVersion } from '../../../utils/blueprints.js'; @@ -158,16 +162,17 @@ function updateScripts(packageJson) { export function updateAddonPackageJson(context, options) { const { locations, projectRoot } = options; - const oldPath = join(projectRoot, locations.addon, 'package.json'); - const oldFile = readFileSync(oldPath, 'utf8'); - const packageJson = JSON.parse(oldFile); + const packageJson = readPackageJson({ + projectRoot: join(projectRoot, locations.addon), + }); updateDependencies(packageJson, options); updateDevDependencies(packageJson, options); updateScripts(packageJson); updateOtherFields(packageJson, context, options); - const newFile = JSON.stringify(packageJson, null, 2) + '\n'; + const destination = join(projectRoot, locations.addon, 'package.json'); + const file = JSON.stringify(packageJson, null, 2) + '\n'; - writeFileSync(oldPath, newFile, 'utf8'); + writeFileSync(destination, file, 'utf8'); } diff --git a/src/migration/ember-addon/steps/update-test-app-package-json.js b/src/migration/ember-addon/steps/update-test-app-package-json.js index f84007b0..f146dc80 100644 --- a/src/migration/ember-addon/steps/update-test-app-package-json.js +++ b/src/migration/ember-addon/steps/update-test-app-package-json.js @@ -1,7 +1,11 @@ -import { readFileSync, writeFileSync } from 'node:fs'; +import { writeFileSync } from 'node:fs'; import { join } from 'node:path'; -import { convertToMap, convertToObject } from '@codemod-utils/json'; +import { + convertToMap, + convertToObject, + readPackageJson, +} from '@codemod-utils/json'; import { getVersion } from '../../../utils/blueprints.js'; @@ -75,16 +79,17 @@ function updateOtherFields(packageJson, options) { export function updateTestAppPackageJson(options) { const { locations, projectRoot } = options; - const oldPath = join(projectRoot, locations.testApp, 'package.json'); - const oldFile = readFileSync(oldPath, 'utf8'); - const packageJson = JSON.parse(oldFile); + const packageJson = readPackageJson({ + projectRoot: join(projectRoot, locations.testApp), + }); moveDependenciesToDevDependencies(packageJson, options); updateDependencies(packageJson); updateDevDependencies(packageJson, options); updateOtherFields(packageJson, options); - const newFile = JSON.stringify(packageJson, null, 2) + '\n'; + const destination = join(projectRoot, locations.testApp, 'package.json'); + const file = JSON.stringify(packageJson, null, 2) + '\n'; - writeFileSync(oldPath, newFile, 'utf8'); + writeFileSync(destination, file, 'utf8'); }