Skip to content

Commit 2cb83e2

Browse files
committed
Update imports
1 parent 20ec3b9 commit 2cb83e2

File tree

5 files changed

+78
-102
lines changed

5 files changed

+78
-102
lines changed

src/bin.test.ts

+53-75
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { beforeAll, describe, expect, it } from '@jest/globals';
22
import { execFile } from 'child_process';
3-
import fs from 'fs';
4-
import os from 'os';
5-
import path from 'path';
3+
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'fs';
4+
import { tmpdir } from 'os';
5+
import { join } from 'path';
66

77
let directory = '';
88

99
describe('pkg-ok', () => {
1010
beforeAll(() => {
11-
directory = fs.mkdtempSync(path.join(os.tmpdir(), 'pkg-ok-'));
12-
fs.mkdirSync(path.join(directory, 'A'));
13-
fs.writeFileSync(
14-
path.join(directory, 'A/package.json'),
11+
directory = mkdtempSync(join(tmpdir(), 'pkg-ok-'));
12+
mkdirSync(join(directory, 'A'));
13+
writeFileSync(
14+
join(directory, 'A/package.json'),
1515
JSON.stringify({
1616
main: 'unknown.js',
1717
bin: 'unknown.js',
@@ -23,71 +23,71 @@ describe('pkg-ok', () => {
2323
exports: 'unknown.js',
2424
}),
2525
);
26-
fs.mkdirSync(path.join(directory, 'B'));
27-
fs.writeFileSync(
28-
path.join(directory, 'B/package.json'),
26+
mkdirSync(join(directory, 'B'));
27+
writeFileSync(
28+
join(directory, 'B/package.json'),
2929
JSON.stringify({
3030
bin: {
3131
X: 'unknown.js',
3232
Y: 'unknown.js',
3333
},
3434
}),
3535
);
36-
fs.mkdirSync(path.join(directory, 'C'));
37-
fs.writeFileSync(
38-
path.join(directory, 'C/package.json'),
36+
mkdirSync(join(directory, 'C'));
37+
writeFileSync(
38+
join(directory, 'C/package.json'),
3939
JSON.stringify({
4040
foo: 'unknown.js',
4141
}),
4242
);
43-
fs.mkdirSync(path.join(directory, 'D'));
44-
fs.writeFileSync(
45-
path.join(directory, 'D/package.json'),
43+
mkdirSync(join(directory, 'D'));
44+
writeFileSync(
45+
join(directory, 'D/package.json'),
4646
JSON.stringify({
4747
foo: {
4848
bar: 'bar',
4949
baz: 'baz',
5050
},
5151
}),
5252
);
53-
fs.mkdirSync(path.join(directory, 'E'));
54-
fs.writeFileSync(
55-
path.join(directory, 'E/package.json'),
53+
mkdirSync(join(directory, 'E'));
54+
writeFileSync(
55+
join(directory, 'E/package.json'),
5656
JSON.stringify({
5757
bin: './script.js',
5858
}),
5959
);
60-
fs.writeFileSync(path.join(directory, 'E/script.js'), 'foo\r\nbar');
61-
fs.writeFileSync(path.join(directory, 'E/another-script.js'), 'baz\r\nqux');
62-
fs.mkdirSync(path.join(directory, 'F'));
63-
fs.mkdirSync(path.join(directory, 'F', 'dist'));
64-
fs.writeFileSync(
65-
path.join(directory, 'F/package.json'),
60+
writeFileSync(join(directory, 'E/script.js'), 'foo\r\nbar');
61+
writeFileSync(join(directory, 'E/another-script.js'), 'baz\r\nqux');
62+
mkdirSync(join(directory, 'F'));
63+
mkdirSync(join(directory, 'F', 'dist'));
64+
writeFileSync(
65+
join(directory, 'F/package.json'),
6666
JSON.stringify({
6767
browser: {
6868
'./dist/lib.cjs.js': './dist/lib.cjs.browser.js',
6969
'./dist/lib.esm.js': './dist/lib.esm.browser.js',
7070
},
7171
}),
7272
);
73-
fs.writeFileSync(path.join(directory, 'F/dist/lib.cjs.browser.js'), 'cjs');
74-
fs.writeFileSync(path.join(directory, 'F/dist/lib.esm.browser.js'), 'esm');
75-
fs.mkdirSync(path.join(directory, 'G'));
76-
fs.mkdirSync(path.join(directory, 'G', 'dist'));
77-
fs.writeFileSync(
78-
path.join(directory, 'G/package.json'),
73+
writeFileSync(join(directory, 'F/dist/lib.cjs.browser.js'), 'cjs');
74+
writeFileSync(join(directory, 'F/dist/lib.esm.browser.js'), 'esm');
75+
mkdirSync(join(directory, 'G'));
76+
mkdirSync(join(directory, 'G', 'dist'));
77+
writeFileSync(
78+
join(directory, 'G/package.json'),
7979
JSON.stringify({
8080
browser: {
8181
'dist/lib.cjs.js': './dist/lib.cjs.browser.js',
8282
'./dist/lib.esm.js': 'dist/lib.esm.browser.js',
8383
},
8484
}),
8585
);
86-
fs.writeFileSync(path.join(directory, 'G/dist/lib.cjs.js'), './dist/lib.cjs.browser.js');
86+
writeFileSync(join(directory, 'G/dist/lib.cjs.js'), './dist/lib.cjs.browser.js');
8787
});
8888

8989
it('checks /A', (done) => {
90-
execFile('node', ['dist/bin.js', path.join(directory, 'A')], (_error, stdout) => {
90+
execFile('node', ['dist/bin.js', join(directory, 'A')], (_error, stdout) => {
9191
expect(stdout).toMatch(
9292
/main[\s\S]*bin[\s\S]*types[\s\S]*typings[\s\S]*module[\s\S]*es2015[\s\S]*browser[\s\S]*exports/,
9393
);
@@ -96,68 +96,46 @@ describe('pkg-ok', () => {
9696
});
9797

9898
it('checks /B', (done) => {
99-
execFile('node', ['dist/bin.js', path.join(directory, 'B')], (_error, stdout) => {
99+
execFile('node', ['dist/bin.js', join(directory, 'B')], (_error, stdout) => {
100100
expect(stdout).toMatch(/bin\.X[\s\S]*bin\.Y/);
101101
done();
102102
});
103103
});
104104

105105
it('checks /C', (done) => {
106-
execFile(
107-
'node',
108-
['dist/bin.js', path.join(directory, 'C'), '--field', 'foo'],
109-
(_error, stdout) => {
110-
expect(stdout).toMatch(/foo/);
111-
done();
112-
},
113-
);
106+
execFile('node', ['dist/bin.js', join(directory, 'C'), '--field', 'foo'], (_error, stdout) => {
107+
expect(stdout).toMatch(/foo/);
108+
done();
109+
});
114110
});
115111

116112
it('checks /D', (done) => {
117-
execFile(
118-
'node',
119-
['dist/bin.js', path.join(directory, 'D'), '--field', 'foo'],
120-
(_error, stdout) => {
121-
expect(stdout).toMatch(/foo\.bar[\s\S]*foo\.baz/);
122-
done();
123-
},
124-
);
113+
execFile('node', ['dist/bin.js', join(directory, 'D'), '--field', 'foo'], (_error, stdout) => {
114+
expect(stdout).toMatch(/foo\.bar[\s\S]*foo\.baz/);
115+
done();
116+
});
125117
});
126118

127119
it('checks /E', (done) => {
128-
execFile(
129-
'node',
130-
['dist/bin.js', path.join(directory, 'E'), '--bin', 'another-script.js'],
131-
() => {
132-
expect(fs.readFileSync(path.join(directory, '/E/script.js'), 'utf-8')).toEqual('foo\nbar');
133-
expect(fs.readFileSync(path.join(directory, '/E/another-script.js'), 'utf-8')).toEqual(
134-
'baz\nqux',
135-
);
136-
done();
137-
},
138-
);
120+
execFile('node', ['dist/bin.js', join(directory, 'E'), '--bin', 'another-script.js'], () => {
121+
expect(readFileSync(join(directory, '/E/script.js'), 'utf-8')).toEqual('foo\nbar');
122+
expect(readFileSync(join(directory, '/E/another-script.js'), 'utf-8')).toEqual('baz\nqux');
123+
done();
124+
});
139125
});
140126

141127
it('checks /F', (done) => {
142-
execFile(
143-
'node',
144-
['dist/bin.js', path.join(directory, 'F'), '--bin', 'another-script.js'],
145-
() => {
146-
expect(
147-
fs.readFileSync(path.join(directory, '/F/dist/lib.cjs.browser.js'), 'utf-8'),
148-
).toEqual('cjs');
149-
expect(
150-
fs.readFileSync(path.join(directory, '/F/dist/lib.esm.browser.js'), 'utf-8'),
151-
).toEqual('esm');
152-
done();
153-
},
154-
);
128+
execFile('node', ['dist/bin.js', join(directory, 'F'), '--bin', 'another-script.js'], () => {
129+
expect(readFileSync(join(directory, '/F/dist/lib.cjs.browser.js'), 'utf-8')).toEqual('cjs');
130+
expect(readFileSync(join(directory, '/F/dist/lib.esm.browser.js'), 'utf-8')).toEqual('esm');
131+
done();
132+
});
155133
});
156134

157135
it('checks /G', (done) => {
158136
execFile(
159137
'node',
160-
['dist/bin.js', path.join(directory, 'G'), '--bin', 'another-script.js'],
138+
['dist/bin.js', join(directory, 'G'), '--bin', 'another-script.js'],
161139
(_error, stdout) => {
162140
expect(stdout).toMatch(/browser.*path[\s\S]*browser.*path[\s\S]*browser.*must/);
163141
done();

src/fields.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import { existsSync } from 'fs';
2+
import { join } from 'path';
33
import { doesNotExistError, mustBeRelativeError } from './errors.js';
44
import { isObject, Pkg } from './pkg.js';
55

@@ -54,7 +54,7 @@ const FIELDS: Readonly<Field[]> = [
5454
const FIELD_NAMES: Readonly<string[]> = FIELDS.map((field) => field.name);
5555

5656
function doesNotExist(dir: string, file: string) {
57-
return !fs.existsSync(path.join(dir, file));
57+
return !existsSync(join(dir, file));
5858
}
5959

6060
function findField(name: string) {

src/index.test.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it } from '@jest/globals';
2-
import fs from 'fs';
2+
import { readFileSync } from 'fs';
33
import mock from 'mock-fs';
4-
import path from 'path';
4+
import { join } from 'path';
55
import { pkgOk } from './index.js';
66

77
describe('pkg-ok', () => {
@@ -58,39 +58,37 @@ describe('pkg-ok', () => {
5858
afterEach(() => mock.restore());
5959

6060
it('checks /A', () => {
61-
expect(() => pkgOk(path.join('/A'))).toThrowError(
61+
expect(() => pkgOk(join('/A'))).toThrowError(
6262
/main[\s\S]*bin[\s\S]*types[\s\S]*typings[\s\S]*module[\s\S]*es2015[\s\S]*browser[\s\S]*exports/,
6363
);
6464
});
6565

6666
it('checks /B', () => {
67-
expect(() => pkgOk(path.join('/B'))).toThrowError(/bin\.X[\s\S]*bin\.Y/);
67+
expect(() => pkgOk(join('/B'))).toThrowError(/bin\.X[\s\S]*bin\.Y/);
6868
});
6969

7070
it('checks /C', () => {
71-
expect(() => pkgOk(path.join('/C'), { fields: ['foo'] })).toThrowError(/foo/);
71+
expect(() => pkgOk(join('/C'), { fields: ['foo'] })).toThrowError(/foo/);
7272
});
7373

7474
it('checks /D', () => {
75-
expect(() => pkgOk(path.join('/D'), { fields: ['foo'] })).toThrowError(
76-
/foo\.bar[\s\S]*foo\.baz/,
77-
);
75+
expect(() => pkgOk(join('/D'), { fields: ['foo'] })).toThrowError(/foo\.bar[\s\S]*foo\.baz/);
7876
});
7977

8078
it('checks /E', () => {
81-
pkgOk(path.join('/E'), { bin: ['another-script.js'] });
82-
expect(fs.readFileSync('/E/script.js', 'utf-8')).toEqual('foo\nbar');
83-
expect(fs.readFileSync('/E/another-script.js', 'utf-8')).toEqual('baz\nqux');
79+
pkgOk(join('/E'), { bin: ['another-script.js'] });
80+
expect(readFileSync('/E/script.js', 'utf-8')).toEqual('foo\nbar');
81+
expect(readFileSync('/E/another-script.js', 'utf-8')).toEqual('baz\nqux');
8482
});
8583

8684
it('checks /F', () => {
87-
pkgOk(path.join('/F'));
88-
expect(fs.readFileSync('/F/dist/lib.cjs.browser.js', 'utf-8')).toEqual('cjs');
89-
expect(fs.readFileSync('/F/dist/lib.esm.browser.js', 'utf-8')).toEqual('esm');
85+
pkgOk(join('/F'));
86+
expect(readFileSync('/F/dist/lib.cjs.browser.js', 'utf-8')).toEqual('cjs');
87+
expect(readFileSync('/F/dist/lib.esm.browser.js', 'utf-8')).toEqual('esm');
9088
});
9189

9290
it('checks /G', () => {
93-
expect(() => pkgOk(path.join('/G'))).toThrowError(
91+
expect(() => pkgOk(join('/G'))).toThrowError(
9492
/browser.*path[\s\S]*browser.*path[\s\S]*browser.*must/,
9593
);
9694
});

src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import { readFileSync } from 'fs';
2+
import { join } from 'path';
33
import { checkFields } from './fields.js';
44
import { normalizeScripts } from './scripts.js';
55

@@ -10,8 +10,8 @@ export interface Options {
1010

1111
// Main function
1212
export function pkgOk(dir: string, { fields = [], bin = [] }: Options = {}) {
13-
const pkgPath = path.join(dir, 'package.json');
14-
const pkg = JSON.parse(fs.readFileSync(pkgPath).toString());
13+
const pkgPath = join(dir, 'package.json');
14+
const pkg = JSON.parse(readFileSync(pkgPath).toString());
1515

1616
// Check files exist in package.json fields and additional fields
1717
const errors = checkFields(pkg, dir, fields);

src/scripts.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import fs from 'fs';
1+
import { readFileSync, writeFileSync } from 'fs';
22
import normalizeNewline from 'normalize-newline';
3-
import path from 'path';
3+
import { join } from 'path';
44
import { isObject, Pkg } from './pkg.js';
55

66
function normalize(dir: string, file: string) {
7-
const filename = path.join(dir, file);
8-
const data = fs.readFileSync(filename, 'utf-8');
7+
const filename = join(dir, file);
8+
const data = readFileSync(filename, 'utf-8');
99
const normalizedData = normalizeNewline(data);
10-
fs.writeFileSync(filename, normalizedData);
10+
writeFileSync(filename, normalizedData);
1111
}
1212

1313
function normalizeField(pkg: Pkg, dir: string, field: string) {

0 commit comments

Comments
 (0)