2.0.2 • Published 5 months ago

@codemod-utils/json v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

This project uses GitHub Actions for continuous integration.

@codemod-utils/json

Utilities for handling JSON

What is it?

@codemod-utils/json helps you update files like package.json and tsconfig.json.

API

convertToMap, convertToObject

convertToMap() converts an object to a Map, while convertToObject() converts the Map back to an object. Use these two utilities to update JSONs.

!NOTE convertToObject() creates an object with keys in alphabetical order.

Remove dependencies (if they exist) from package.json.

const dependencies = convertToMap(packageJson['dependencies']);

const packagesToDelete = [
  '@embroider/macros',
  'ember-auto-import',
  'ember-cli-babel',
  'ember-cli-htmlbars',
];

packagesToDelete.forEach((packageName) => {
  dependencies.delete(packageName);
});

packageJson['dependencies'] = convertToObject(dependencies);

Configure tsconfig.json in an Ember app.

const compilerOptions = convertToMap(tsConfigJson['compilerOptions']);

compilerOptions.set('paths', {
  [`${appName}/tests/*`]: ['tests/*'],
  [`${appName}/*`]: ['app/*'],
  '*': ['types/*'],
});

tsConfigJson['compilerOptions'] = convertToObject(compilerOptions);

readPackageJson

Reads package.json and returns the parsed JSON.

!NOTE readPackageJson() checks that package.json exists and is a valid JSON.

Check if the project, against which the codemod is run, has typescript as a dependency.

import { readPackageJson } from '@codemod-utils/json';

const { dependencies, devDependencies } = readPackageJson({
  projectRoot,
});

const projectDependencies = new Map([
  ...Object.entries(dependencies ?? {}),
  ...Object.entries(devDependencies ?? {}),
]);

const hasTypeScript = projectDependencies.has('typescript');

validatePackageJson

(Type-)Checks that the fields name and version exist, in the sense that their values are a non-empty string.

import { readPackageJson, validatePackageJson } from '@codemod-utils/json';

const packageJson = readPackageJson({ projectRoot });

validatePackageJson(packageJson);

// Both guaranteed to be `string` (not `undefined`)
const { name, version } = packageJson;

Compatibility

  • Node.js v20 or above

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

1.2.0

7 months ago

2.0.2

5 months ago

1.1.12

9 months ago

1.1.11

9 months ago

1.1.10

11 months ago

1.1.15

8 months ago

2.0.1

6 months ago

1.1.14

8 months ago

2.0.0

6 months ago

1.1.13

9 months ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.1.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.4

2 years ago

0.4.2

2 years ago

0.3.3

2 years ago

0.3.0

2 years ago

0.1.2

2 years ago

0.2.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.1.1

2 years ago