Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@codemod-utils/json

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codemod-utils/json

Utilities for handling JSON

  • 1.1.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

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.

Example

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);
Example

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.

Example

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

Check if the fields name and version exist, in the sense that their values are a non-empty string.

[!NOTE] You may still need the non-null assertion operator !, to tell TypeScript that name and version are not undefined.

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

const packageJson = readPackageJson({
  projectRoot,
});

validatePackageJson(packageJson);

const { name, version } = packageJson;

Compatibility

  • Node.js v18 or above

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 02 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc