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

@codemod-utils/json

Package Overview
Dependencies
Maintainers
1
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

  • 0.3.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
69
decreased by-2.82%
Maintainers
1
Weekly downloads
 
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

Converts an object to a Map. The Map data structure helps you add, update, and remove entries.

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

function updateDependencies(packageJson) {
  const dependencies = convertToMap(packageJson['dependencies']);

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

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

convertToObject

Converts a Map (back) to an object. convertToObject helps you update the JSON.

Example
import { convertToMap, convertToObject } from '@codemod-utils/json';

function updateDependencies(packageJson) {
  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);
}

readPackageJson, validatePackageJson

Reads package.json and returns the parsed JSON.

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

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

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

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

readPackageJson checks that package.json exists and is a valid JSON. Call validatePackageJson if you need to know that the name and version fields exist.

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

const packageJson = readPackageJson({
  projectRoot: '__projectRoot__',
});

validatePackageJson(packageJson);

const { name, version } = packageJson;

Compatibility

  • Node.js v16 or above

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 09 Jul 2023

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