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 - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

src/json/validate-package-json.js

6

package.json
{
"name": "@codemod-utils/json",
"version": "0.1.2",
"version": "0.2.0",
"description": "Utilities for handling JSON",

@@ -28,4 +28,4 @@ "keywords": [

"@codemod-utils/tests": "0.1.2",
"@shared-configs/eslint-config": "0.0.0",
"@shared-configs/prettier": "0.0.0"
"@shared-configs/prettier": "0.0.0",
"@shared-configs/eslint-config": "0.0.0"
},

@@ -32,0 +32,0 @@ "engines": {

@@ -77,5 +77,5 @@ [![This project uses GitHub Actions for continuous integration.](https://github.com/ijlee2/codemod-utils/actions/workflows/ci.yml/badge.svg)](https://github.com/ijlee2/codemod-utils/actions/workflows/ci.yml)

### readPackageJson
### readPackageJson, validatePackageJson
Reads `package.json` in the user's project. If the file is valid, `readPackageJson` returns the parsed JSON.
Reads `package.json` and returns the parsed JSON.

@@ -89,30 +89,32 @@ <details>

function analyzePackageJson(codemodOptions) {
const {
dependencies,
devDependencies,
'ember-addon': emberAddon,
name,
version,
} = readPackageJson(codemodOptions);
const { dependencies, devDependencies } = readPackageJson({
projectRoot: '__projectRoot__',
});
const projectDependencies = new Map([
...Object.entries(dependencies ?? {}),
...Object.entries(devDependencies ?? {}),
]);
const projectDependencies = new Map([
...Object.entries(dependencies ?? {}),
...Object.entries(devDependencies ?? {}),
]);
// Return information that the codemod needs
return {
dependencies: projectDependencies,
hasGlint: projectDependencies.has('@glint/core'),
hasTypeScript: projectDependencies.has('typescript'),
isV1Addon: Boolean(emberAddon),
name,
version,
};
}
const hasTypeScript = projectDependencies.has('typescript');
```
analyzePackageJson({
projectRoot: // ...
</details>
`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.
<details>
<summary>Example</summary>
```js
import { readPackageJson, validatePackageJson } from '@codemod-utils/json';
const packageJson = readPackageJson({
projectRoot: '__projectRoot__',
});
validatePackageJson(packageJson);
const { name, version } = packageJson;
```

@@ -119,0 +121,0 @@

export * from './json/convert-to-map.js';
export * from './json/convert-to-object.js';
export * from './json/read-package-json.js';
export * from './json/validate-package-json.js';

@@ -1,38 +0,21 @@

import { readFileSync } from 'node:fs';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
function validatePackageJson({ name, version }) {
if (!name) {
throw new SyntaxError('Package name is missing.');
}
export function readPackageJson(options) {
const { projectRoot } = options;
if (name.includes('/')) {
// eslint-disable-next-line no-unused-vars
const [_scope, packageName] = name.split('/');
const filePath = join(projectRoot, 'package.json');
if (!packageName) {
throw new SyntaxError('Package name is missing.');
}
if (!existsSync(filePath)) {
throw new SyntaxError(`ERROR: package.json is missing.\n`);
}
if (!version) {
throw new SyntaxError('Package version is missing.');
}
}
export function readPackageJson(codemodOptions) {
const { projectRoot } = codemodOptions;
try {
const file = readFileSync(join(projectRoot, 'package.json'), 'utf8');
const file = readFileSync(filePath, 'utf8');
const packageJson = JSON.parse(file);
validatePackageJson(packageJson);
return packageJson;
} catch (e) {
throw new SyntaxError(
`ERROR: package.json is missing or is not valid. (${e.message})\n`,
);
throw new SyntaxError(`ERROR: package.json is not valid. (${e.message})\n`);
}
}
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