From 2cb83e2c4ae98faa2c216a4e5811f973b0acadbe Mon Sep 17 00:00:00 2001 From: Abraham Williams <4braham@gmail.com> Date: Sat, 21 Sep 2024 21:17:02 -0500 Subject: [PATCH] Update imports --- src/bin.test.ts | 128 +++++++++++++++++++--------------------------- src/fields.ts | 6 +-- src/index.test.ts | 28 +++++----- src/index.ts | 8 +-- src/scripts.ts | 10 ++-- 5 files changed, 78 insertions(+), 102 deletions(-) diff --git a/src/bin.test.ts b/src/bin.test.ts index 76106ba..d156c3f 100644 --- a/src/bin.test.ts +++ b/src/bin.test.ts @@ -1,17 +1,17 @@ import { beforeAll, describe, expect, it } from '@jest/globals'; import { execFile } from 'child_process'; -import fs from 'fs'; -import os from 'os'; -import path from 'path'; +import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'fs'; +import { tmpdir } from 'os'; +import { join } from 'path'; let directory = ''; describe('pkg-ok', () => { beforeAll(() => { - directory = fs.mkdtempSync(path.join(os.tmpdir(), 'pkg-ok-')); - fs.mkdirSync(path.join(directory, 'A')); - fs.writeFileSync( - path.join(directory, 'A/package.json'), + directory = mkdtempSync(join(tmpdir(), 'pkg-ok-')); + mkdirSync(join(directory, 'A')); + writeFileSync( + join(directory, 'A/package.json'), JSON.stringify({ main: 'unknown.js', bin: 'unknown.js', @@ -23,9 +23,9 @@ describe('pkg-ok', () => { exports: 'unknown.js', }), ); - fs.mkdirSync(path.join(directory, 'B')); - fs.writeFileSync( - path.join(directory, 'B/package.json'), + mkdirSync(join(directory, 'B')); + writeFileSync( + join(directory, 'B/package.json'), JSON.stringify({ bin: { X: 'unknown.js', @@ -33,16 +33,16 @@ describe('pkg-ok', () => { }, }), ); - fs.mkdirSync(path.join(directory, 'C')); - fs.writeFileSync( - path.join(directory, 'C/package.json'), + mkdirSync(join(directory, 'C')); + writeFileSync( + join(directory, 'C/package.json'), JSON.stringify({ foo: 'unknown.js', }), ); - fs.mkdirSync(path.join(directory, 'D')); - fs.writeFileSync( - path.join(directory, 'D/package.json'), + mkdirSync(join(directory, 'D')); + writeFileSync( + join(directory, 'D/package.json'), JSON.stringify({ foo: { bar: 'bar', @@ -50,19 +50,19 @@ describe('pkg-ok', () => { }, }), ); - fs.mkdirSync(path.join(directory, 'E')); - fs.writeFileSync( - path.join(directory, 'E/package.json'), + mkdirSync(join(directory, 'E')); + writeFileSync( + join(directory, 'E/package.json'), JSON.stringify({ bin: './script.js', }), ); - fs.writeFileSync(path.join(directory, 'E/script.js'), 'foo\r\nbar'); - fs.writeFileSync(path.join(directory, 'E/another-script.js'), 'baz\r\nqux'); - fs.mkdirSync(path.join(directory, 'F')); - fs.mkdirSync(path.join(directory, 'F', 'dist')); - fs.writeFileSync( - path.join(directory, 'F/package.json'), + writeFileSync(join(directory, 'E/script.js'), 'foo\r\nbar'); + writeFileSync(join(directory, 'E/another-script.js'), 'baz\r\nqux'); + mkdirSync(join(directory, 'F')); + mkdirSync(join(directory, 'F', 'dist')); + writeFileSync( + join(directory, 'F/package.json'), JSON.stringify({ browser: { './dist/lib.cjs.js': './dist/lib.cjs.browser.js', @@ -70,12 +70,12 @@ describe('pkg-ok', () => { }, }), ); - fs.writeFileSync(path.join(directory, 'F/dist/lib.cjs.browser.js'), 'cjs'); - fs.writeFileSync(path.join(directory, 'F/dist/lib.esm.browser.js'), 'esm'); - fs.mkdirSync(path.join(directory, 'G')); - fs.mkdirSync(path.join(directory, 'G', 'dist')); - fs.writeFileSync( - path.join(directory, 'G/package.json'), + writeFileSync(join(directory, 'F/dist/lib.cjs.browser.js'), 'cjs'); + writeFileSync(join(directory, 'F/dist/lib.esm.browser.js'), 'esm'); + mkdirSync(join(directory, 'G')); + mkdirSync(join(directory, 'G', 'dist')); + writeFileSync( + join(directory, 'G/package.json'), JSON.stringify({ browser: { 'dist/lib.cjs.js': './dist/lib.cjs.browser.js', @@ -83,11 +83,11 @@ describe('pkg-ok', () => { }, }), ); - fs.writeFileSync(path.join(directory, 'G/dist/lib.cjs.js'), './dist/lib.cjs.browser.js'); + writeFileSync(join(directory, 'G/dist/lib.cjs.js'), './dist/lib.cjs.browser.js'); }); it('checks /A', (done) => { - execFile('node', ['dist/bin.js', path.join(directory, 'A')], (_error, stdout) => { + execFile('node', ['dist/bin.js', join(directory, 'A')], (_error, stdout) => { expect(stdout).toMatch( /main[\s\S]*bin[\s\S]*types[\s\S]*typings[\s\S]*module[\s\S]*es2015[\s\S]*browser[\s\S]*exports/, ); @@ -96,68 +96,46 @@ describe('pkg-ok', () => { }); it('checks /B', (done) => { - execFile('node', ['dist/bin.js', path.join(directory, 'B')], (_error, stdout) => { + execFile('node', ['dist/bin.js', join(directory, 'B')], (_error, stdout) => { expect(stdout).toMatch(/bin\.X[\s\S]*bin\.Y/); done(); }); }); it('checks /C', (done) => { - execFile( - 'node', - ['dist/bin.js', path.join(directory, 'C'), '--field', 'foo'], - (_error, stdout) => { - expect(stdout).toMatch(/foo/); - done(); - }, - ); + execFile('node', ['dist/bin.js', join(directory, 'C'), '--field', 'foo'], (_error, stdout) => { + expect(stdout).toMatch(/foo/); + done(); + }); }); it('checks /D', (done) => { - execFile( - 'node', - ['dist/bin.js', path.join(directory, 'D'), '--field', 'foo'], - (_error, stdout) => { - expect(stdout).toMatch(/foo\.bar[\s\S]*foo\.baz/); - done(); - }, - ); + execFile('node', ['dist/bin.js', join(directory, 'D'), '--field', 'foo'], (_error, stdout) => { + expect(stdout).toMatch(/foo\.bar[\s\S]*foo\.baz/); + done(); + }); }); it('checks /E', (done) => { - execFile( - 'node', - ['dist/bin.js', path.join(directory, 'E'), '--bin', 'another-script.js'], - () => { - expect(fs.readFileSync(path.join(directory, '/E/script.js'), 'utf-8')).toEqual('foo\nbar'); - expect(fs.readFileSync(path.join(directory, '/E/another-script.js'), 'utf-8')).toEqual( - 'baz\nqux', - ); - done(); - }, - ); + execFile('node', ['dist/bin.js', join(directory, 'E'), '--bin', 'another-script.js'], () => { + expect(readFileSync(join(directory, '/E/script.js'), 'utf-8')).toEqual('foo\nbar'); + expect(readFileSync(join(directory, '/E/another-script.js'), 'utf-8')).toEqual('baz\nqux'); + done(); + }); }); it('checks /F', (done) => { - execFile( - 'node', - ['dist/bin.js', path.join(directory, 'F'), '--bin', 'another-script.js'], - () => { - expect( - fs.readFileSync(path.join(directory, '/F/dist/lib.cjs.browser.js'), 'utf-8'), - ).toEqual('cjs'); - expect( - fs.readFileSync(path.join(directory, '/F/dist/lib.esm.browser.js'), 'utf-8'), - ).toEqual('esm'); - done(); - }, - ); + execFile('node', ['dist/bin.js', join(directory, 'F'), '--bin', 'another-script.js'], () => { + expect(readFileSync(join(directory, '/F/dist/lib.cjs.browser.js'), 'utf-8')).toEqual('cjs'); + expect(readFileSync(join(directory, '/F/dist/lib.esm.browser.js'), 'utf-8')).toEqual('esm'); + done(); + }); }); it('checks /G', (done) => { execFile( 'node', - ['dist/bin.js', path.join(directory, 'G'), '--bin', 'another-script.js'], + ['dist/bin.js', join(directory, 'G'), '--bin', 'another-script.js'], (_error, stdout) => { expect(stdout).toMatch(/browser.*path[\s\S]*browser.*path[\s\S]*browser.*must/); done(); diff --git a/src/fields.ts b/src/fields.ts index ed9ca57..d44e533 100644 --- a/src/fields.ts +++ b/src/fields.ts @@ -1,5 +1,5 @@ -import fs from 'fs'; -import path from 'path'; +import { existsSync } from 'fs'; +import { join } from 'path'; import { doesNotExistError, mustBeRelativeError } from './errors.js'; import { isObject, Pkg } from './pkg.js'; @@ -54,7 +54,7 @@ const FIELDS: Readonly = [ const FIELD_NAMES: Readonly = FIELDS.map((field) => field.name); function doesNotExist(dir: string, file: string) { - return !fs.existsSync(path.join(dir, file)); + return !existsSync(join(dir, file)); } function findField(name: string) { diff --git a/src/index.test.ts b/src/index.test.ts index 9862f7d..9815387 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,7 +1,7 @@ import { afterEach, beforeEach, describe, expect, it } from '@jest/globals'; -import fs from 'fs'; +import { readFileSync } from 'fs'; import mock from 'mock-fs'; -import path from 'path'; +import { join } from 'path'; import { pkgOk } from './index.js'; describe('pkg-ok', () => { @@ -58,39 +58,37 @@ describe('pkg-ok', () => { afterEach(() => mock.restore()); it('checks /A', () => { - expect(() => pkgOk(path.join('/A'))).toThrowError( + expect(() => pkgOk(join('/A'))).toThrowError( /main[\s\S]*bin[\s\S]*types[\s\S]*typings[\s\S]*module[\s\S]*es2015[\s\S]*browser[\s\S]*exports/, ); }); it('checks /B', () => { - expect(() => pkgOk(path.join('/B'))).toThrowError(/bin\.X[\s\S]*bin\.Y/); + expect(() => pkgOk(join('/B'))).toThrowError(/bin\.X[\s\S]*bin\.Y/); }); it('checks /C', () => { - expect(() => pkgOk(path.join('/C'), { fields: ['foo'] })).toThrowError(/foo/); + expect(() => pkgOk(join('/C'), { fields: ['foo'] })).toThrowError(/foo/); }); it('checks /D', () => { - expect(() => pkgOk(path.join('/D'), { fields: ['foo'] })).toThrowError( - /foo\.bar[\s\S]*foo\.baz/, - ); + expect(() => pkgOk(join('/D'), { fields: ['foo'] })).toThrowError(/foo\.bar[\s\S]*foo\.baz/); }); it('checks /E', () => { - pkgOk(path.join('/E'), { bin: ['another-script.js'] }); - expect(fs.readFileSync('/E/script.js', 'utf-8')).toEqual('foo\nbar'); - expect(fs.readFileSync('/E/another-script.js', 'utf-8')).toEqual('baz\nqux'); + pkgOk(join('/E'), { bin: ['another-script.js'] }); + expect(readFileSync('/E/script.js', 'utf-8')).toEqual('foo\nbar'); + expect(readFileSync('/E/another-script.js', 'utf-8')).toEqual('baz\nqux'); }); it('checks /F', () => { - pkgOk(path.join('/F')); - expect(fs.readFileSync('/F/dist/lib.cjs.browser.js', 'utf-8')).toEqual('cjs'); - expect(fs.readFileSync('/F/dist/lib.esm.browser.js', 'utf-8')).toEqual('esm'); + pkgOk(join('/F')); + expect(readFileSync('/F/dist/lib.cjs.browser.js', 'utf-8')).toEqual('cjs'); + expect(readFileSync('/F/dist/lib.esm.browser.js', 'utf-8')).toEqual('esm'); }); it('checks /G', () => { - expect(() => pkgOk(path.join('/G'))).toThrowError( + expect(() => pkgOk(join('/G'))).toThrowError( /browser.*path[\s\S]*browser.*path[\s\S]*browser.*must/, ); }); diff --git a/src/index.ts b/src/index.ts index bd808d5..6731757 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import fs from 'fs'; -import path from 'path'; +import { readFileSync } from 'fs'; +import { join } from 'path'; import { checkFields } from './fields.js'; import { normalizeScripts } from './scripts.js'; @@ -10,8 +10,8 @@ export interface Options { // Main function export function pkgOk(dir: string, { fields = [], bin = [] }: Options = {}) { - const pkgPath = path.join(dir, 'package.json'); - const pkg = JSON.parse(fs.readFileSync(pkgPath).toString()); + const pkgPath = join(dir, 'package.json'); + const pkg = JSON.parse(readFileSync(pkgPath).toString()); // Check files exist in package.json fields and additional fields const errors = checkFields(pkg, dir, fields); diff --git a/src/scripts.ts b/src/scripts.ts index b5a69e3..1071063 100644 --- a/src/scripts.ts +++ b/src/scripts.ts @@ -1,13 +1,13 @@ -import fs from 'fs'; +import { readFileSync, writeFileSync } from 'fs'; import normalizeNewline from 'normalize-newline'; -import path from 'path'; +import { join } from 'path'; import { isObject, Pkg } from './pkg.js'; function normalize(dir: string, file: string) { - const filename = path.join(dir, file); - const data = fs.readFileSync(filename, 'utf-8'); + const filename = join(dir, file); + const data = readFileSync(filename, 'utf-8'); const normalizedData = normalizeNewline(data); - fs.writeFileSync(filename, normalizedData); + writeFileSync(filename, normalizedData); } function normalizeField(pkg: Pkg, dir: string, field: string) {