Skip to content

readPackageSync() could be simplified, 17 fewer deps #2853

@live627

Description

@live627

read-pkg is designated to read package files and optionally normalize them using normalize-package-data. Thing is, only the type attribute is used.

ava/lib/cli.js

Lines 314 to 323 in b0b76a0

let pkg;
try {
pkg = readPackageSync({cwd: projectDir});
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
const {type: defaultModuleType = 'commonjs'} = pkg || {};

Doesn't need to be normalized because it is validated

ava/lib/module-types.js

Lines 1 to 25 in b0b76a0

const requireTrueValue = value => {
if (value !== true) {
throw new TypeError('When specifying module types, use `true` for ’cjs’, ’mjs’ and ’js’ extensions');
}
};
const normalize = (extension, type, defaultModuleType) => {
switch (extension) {
case 'cjs':
requireTrueValue(type);
return 'commonjs';
case 'mjs':
requireTrueValue(type);
return 'module';
case 'js':
requireTrueValue(type);
return defaultModuleType;
default:
if (type !== 'commonjs' && type !== 'module') {
throw new TypeError(`Module type for ’${extension}’ must be ’commonjs’ or ’module’`);
}
return type;
}
};

Could this be simplified?

 	let pkg;
 	try {
-		pkg = readPackageSync({cwd: projectDir});
+		pkg = parseJson(fs.readFileSync(path.resolve(projectDir, 'package.json'), 'utf8'));
 	} catch (error) {
 		if (error.code !== 'ENOENT') {
 			throw error;

read-pkg could then be dropped

[email protected] (202 deps)
│   ...
├─┬ [email protected] (32 deps, 554.2kb, 281 files)
│ ├── @types/[email protected] (6.14kb, 4 files)
│ ├─┬ [email protected] (13 deps, 350.19kb, 173 files)
│ │ ├── [email protected] (25.21kb, 7 files)
│ │ ├─┬ [email protected] (4 deps, 167.67kb, 122 files)
│ │ │ ├─┬ [email protected] (2 deps, 51.03kb, 27 files)
│ │ │ │ ╰─┬ [email protected] (1 dep, 27.27kb, 17 files)
│ │ │ │   ╰── [email protected] (24.56kb, 12 files)
│ │ │ ╰── [email protected] (4.41kb, 4 files)
│ │ ├── [email protected] (60.13kb, 7 files)
│ │ ╰─┬ [email protected] (5 deps, 71.15kb, 26 files)
│ │   ├─┬ [email protected] (4 deps, 54.94kb, 22 files)
│ │   │ ├── [email protected] (🔗, 2 deps, 23.62kb, 14 files)
│ │   │ ╰── [email protected] (9.45kb, 4 files)
│ │   ╰─┬ [email protected] (2 deps, 23.62kb, 14 files)
│ │     ├── [email protected] (2.6kb, 3 files)
│ │     ╰── [email protected] (9.45kb, 4 files)
│ ├─┬ [email protected] (15 deps, 163.03kb, 85 files)
│ │ ├─┬ @babel/[email protected] (10 deps, 127.88kb, 58 files)
│ │ │ ╰─┬ @babel/[email protected] (9 deps, 121.09kb, 54 files)
│ │ │   ├── @babel/[email protected] (18.55kb, 7 files)
│ │ │   ├─┬ [email protected] (6 deps, 83.07kb, 38 files)
│ │ │   │ ├─┬ [email protected] (2 deps, 44.62kb, 18 files)
│ │ │   │ │ ╰─┬ [email protected] (1 dep, 35.47kb, 14 files)
│ │ │   │ │   ╰── [email protected] (9.14kb, 7 files)
│ │ │   │ ├── [email protected] (🔗, 2.63kb, 4 files)
│ │ │   │ ╰─┬ [email protected] (1 dep, 9.53kb, 9 files)
│ │ │   │   ╰── [email protected] (3.05kb, 4 files)
│ │ │   ╰── [email protected] (14.72kb, 5 files)
│ │ ├─┬ [email protected] (1 dep, 12.78kb, 12 files)
│ │ │ ╰── [email protected] (3.96kb, 8 files)
│ │ ├── [email protected] (10.18kb, 5 files)
│ │ ╰── [email protected] (6.91kb, 6 files)
│ ╰── [email protected] (28.93kb, 14 files)

and replaced with parse-json

[email protected] (188 deps)
│   ...
├─┬ [email protected] (15 deps, 163.03kb, 85 files)
│ ├─┬ @babel/[email protected] (10 deps, 127.88kb, 58 files)
│ │ ╰─┬ @babel/[email protected] (9 deps, 121.09kb, 54 files)
│ │   ├── @babel/[email protected] (18.55kb, 7 files)
│ │   ├─┬ [email protected] (6 deps, 83.07kb, 38 files)
│ │   │ ├─┬ [email protected] (2 deps, 44.62kb, 18 files)
│ │   │ │ ╰─┬ [email protected] (1 dep, 35.47kb, 14 files)
│ │   │ │   ╰── [email protected] (9.14kb, 7 files)
│ │   │ ├── [email protected] (2.63kb, 4 files)
│ │   │ ╰─┬ [email protected] (1 dep, 9.53kb, 9 files)
│ │   │   ╰── [email protected] (3.05kb, 4 files)
│ │   ╰── [email protected] (14.72kb, 5 files)
│ ├─┬ [email protected] (1 dep, 12.78kb, 12 files)
│ │ ╰── [email protected] (3.96kb, 8 files)
│ ├── [email protected] (10.18kb, 5 files)
│ ╰── [email protected] (6.91kb, 6 files)

That in turn could also be dropped in favor of JSON.parse, but then the enhanced error messages would be lost.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions