Skip to content

Commit

Permalink
Update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Sep 22, 2024
1 parent 20ec3b9 commit 2cb83e2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 102 deletions.
128 changes: 53 additions & 75 deletions src/bin.test.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -23,71 +23,71 @@ 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',
Y: 'unknown.js',
},
}),
);
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',
baz: 'baz',
},
}),
);
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',
'./dist/lib.esm.js': './dist/lib.esm.browser.js',
},
}),
);
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',
'./dist/lib.esm.js': 'dist/lib.esm.browser.js',
},
}),
);
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/,
);
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/fields.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -54,7 +54,7 @@ const FIELDS: Readonly<Field[]> = [
const FIELD_NAMES: Readonly<string[]> = 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) {
Expand Down
28 changes: 13 additions & 15 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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/,
);
});
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/scripts.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 2cb83e2

Please sign in to comment.