Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separated validation concerns #39

Merged
merged 2 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/migration/ember-addon/steps/create-options.js
Original file line number Diff line number Diff line change
@@ -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 ?? {}),
Expand Down
19 changes: 12 additions & 7 deletions src/migration/ember-addon/steps/update-addon-package-json.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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');
}
19 changes: 12 additions & 7 deletions src/migration/ember-addon/steps/update-test-app-package-json.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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');
}