Skip to content

Commit

Permalink
To enable support for ES modules in TypeScript >=4.7 (enabled with `m…
Browse files Browse the repository at this point in the history
…odule` and `moduleResolution` of `node16` or `nodenext` in the `tsconfig`), export `simpleGit` as a named export instead of as the default export from the `simple-git` package.

Add `simpleGit` to the TypeScript types while keeping default export for backward compatibility.
  • Loading branch information
steveukx committed Jun 25, 2022
1 parent 24a7a71 commit aef5f67
Show file tree
Hide file tree
Showing 27 changed files with 272 additions and 137 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"simple-git"
],
"resolutions": {
"typescript": "4.1.2"
"typescript": "4.7.4"
},
"scripts": {
"build": "lerna run build",
Expand Down
28 changes: 28 additions & 0 deletions packages/test-es-module-consumer/suite.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { strictEqual } from "assert";

export async function suite(name, simpleGit, ResetMode) {
exec(`${name}: imports default`, async () => {
strictEqual(
await simpleGit().checkIsRepo(),
true,
'expected the current directory to be a valid git root',
);
});

exec(`${name}: imports named exports`, async () => {
strictEqual(
/hard/.test(ResetMode.HARD),
true,
'expected valid ResetMode enum'
);
});
}

function exec (name, runner) {
runner()
.then(() => console.log(`${ name }: OK`))
.catch((e) => {
console.error(`${ name }: ${ e.message }`);
throw e;
});
}
4 changes: 4 additions & 0 deletions packages/test-es-module-consumer/test-default-as.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { simpleGit, ResetMode } from 'simple-git';
import { suite } from './suite.mjs';

await suite('import named', simpleGit, ResetMode);
4 changes: 4 additions & 0 deletions packages/test-es-module-consumer/test-default.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import simpleGit, { ResetMode } from 'simple-git';
import { suite } from './suite.mjs';

await suite('import default', simpleGit, ResetMode);
4 changes: 4 additions & 0 deletions packages/test-es-module-consumer/test-named.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { default as simpleGit, ResetMode } from 'simple-git';
import { suite } from './suite.mjs';

await suite('import default-as', simpleGit, ResetMode);
30 changes: 3 additions & 27 deletions packages/test-es-module-consumer/test.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
import { strictEqual } from 'assert';
import simpleGit, { ResetMode } from 'simple-git';

exec('imports default', async () => {
strictEqual(
await simpleGit().checkIsRepo(),
true,
'expected the current directory to be a valid git root',
);
});

exec('imports named exports', async () => {
strictEqual(
/hard/.test(ResetMode.HARD),
true,
'expected valid ResetMode enum'
);
});

function exec (name, runner) {
runner()
.then(() => console.log(`${ name }: OK`))
.catch((e) => {
console.error(`${ name }: ${ e.message }`);
throw e;
});
}
import './test-default.mjs';
import './test-default-as.mjs';
import './test-named.mjs';
30 changes: 30 additions & 0 deletions packages/test-javascript-consumer/suite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const {strictEqual} = require('assert');

module.exports = {
async suite (name, simpleGit, ResetMode) {
exec(`${ name }: imports default`, async () => {
strictEqual(
await simpleGit().checkIsRepo(),
true,
'expected the current directory to be a valid git root',
);
});

exec(`${ name }: imports named exports`, async () => {
strictEqual(
/hard/.test(ResetMode.HARD),
true,
'expected valid ResetMode enum'
);
});
}
};

function exec (name, runner) {
runner()
.then(() => console.log(`${ name }: OK`))
.catch((e) => {
console.error(`${ name }: ${ e.message }`);
throw e;
});
}
6 changes: 6 additions & 0 deletions packages/test-javascript-consumer/test-default-as.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const {default: simpleGit, ResetMode} = require('simple-git');
const {suite} = require('./suite');

(async () => {
await suite('require default-as', simpleGit, ResetMode);
})();
30 changes: 5 additions & 25 deletions packages/test-javascript-consumer/test-default.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
const {default: simpleGit, ResetMode} = require('simple-git');
const {strictEqual} = require("assert");
const simpleGit = require('simple-git');
const {suite} = require('./suite');

exec('requires default', async () => {
strictEqual(
await simpleGit().checkIsRepo(),
true,
'expected the current directory to be a valid git root',
);
});
(async () => {
await suite('require default', simpleGit, simpleGit.ResetMode);
})();

exec('imports named exports', async () => {
strictEqual(
/hard/.test(ResetMode.HARD),
true,
'expected valid ResetMode enum'
);
});

function exec (name, runner) {
runner()
.then(() => console.log(`${ name }: OK`))
.catch((e) => {
console.error(`${ name }: ${ e.message }`);
throw e;
});
}
7 changes: 7 additions & 0 deletions packages/test-javascript-consumer/test-named.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const {simpleGit, ResetMode} = require('simple-git');
const {suite} = require('./suite');

(async () => {
await suite('require named', simpleGit, ResetMode);
})();

30 changes: 3 additions & 27 deletions packages/test-javascript-consumer/test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
const simpleGit = require('simple-git');
const {strictEqual} = require("assert");

exec('requires default', async () => {
strictEqual(
await simpleGit().checkIsRepo(),
true,
'expected the current directory to be a valid git root',
);
});

exec('imports named exports', async () => {
strictEqual(
/hard/.test(simpleGit.ResetMode.HARD),
true,
'expected valid ResetMode enum'
);
});

function exec (name, runner) {
runner()
.then(() => console.log(`${ name }: OK`))
.catch((e) => {
console.error(`${ name }: ${ e.message }`);
throw e;
});
}
require('./test-default');
require('./test-default-as');
require('./test-named');
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import simpleGit, { gitP, CleanOptions, SimpleGit, TaskConfigurationError } from 'simple-git';
import { simpleGit, CleanOptions, SimpleGit, TaskConfigurationError } from 'simple-git';

describe('simple-git', () => {

describe('default export', () => {
describe('named export', () => {
it('is the simple-git factory', async () => {
expect(await simpleGit().checkIsRepo()).toBe(true);
});
Expand All @@ -14,28 +14,6 @@ describe('simple-git', () => {
});
});

describe('gitP export', () => {
it('is the simple-git factory', async () => {
expect(await gitP().checkIsRepo()).toBe(true);
});

it('builds exported types', async () => {
const git: SimpleGit = gitP();

expect(git).not.toBeUndefined();
});
});

it('default export is the simple-git factory', async () => {
expect(await simpleGit().checkIsRepo()).toBe(true);
});

it('named type exports', async () => {
const git: SimpleGit = simpleGit();

expect(git).not.toBeUndefined();
});

it('named class constructors', async () => {
expect(new TaskConfigurationError('foo')).toBeInstanceOf(TaskConfigurationError);
});
Expand Down
47 changes: 47 additions & 0 deletions packages/test-typescript-consumer/test/ts-named-import.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import simpleGit, { gitP, CleanOptions, SimpleGit, TaskConfigurationError } from 'simple-git';

describe('simple-git', () => {

describe('default export', () => {
it('is the simple-git factory', async () => {
expect(await simpleGit().checkIsRepo()).toBe(true);
});

it('builds exported types', async () => {
const git: SimpleGit = simpleGit();

expect(git).not.toBeUndefined();
});
});

describe('gitP export', () => {
it('is the simple-git factory', async () => {
expect(await gitP().checkIsRepo()).toBe(true);
});

it('builds exported types', async () => {
const git: SimpleGit = gitP();

expect(git).not.toBeUndefined();
});
});

it('default export is the simple-git factory', async () => {
expect(await simpleGit().checkIsRepo()).toBe(true);
});

it('named type exports', async () => {
const git: SimpleGit = simpleGit();

expect(git).not.toBeUndefined();
});

it('named class constructors', async () => {
expect(new TaskConfigurationError('foo')).toBeInstanceOf(TaskConfigurationError);
});

it('named enums', async () => {
expect(CleanOptions.DRY_RUN).toBe('n');
});

});
1 change: 1 addition & 0 deletions packages/test-typescript-esm-consumer/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@simple-git/babel-config')(true);
25 changes: 25 additions & 0 deletions packages/test-typescript-esm-consumer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@simple-git/test-typescript-esm-consumer",
"private": true,
"version": "1.0.0",
"jest": {
"roots": [
"<rootDir>/test/"
],
"testMatch": [
"**/test/*.spec.ts"
]
},
"scripts": {
"test": "jest"
},
"dependencies": {
"@simple-git/babel-config": "^1.0.0",
"simple-git": "^3.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/steveukx/git-js.git",
"directory": "packages/test-typescript-consumer"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { default as simpleGit, CleanOptions, SimpleGit, TaskConfigurationError } from 'simple-git';

describe('simple-git', () => {

describe('renamed default export', () => {
it('is the simple-git factory', async () => {
expect(await simpleGit().checkIsRepo()).toBe(true);
});

it('builds exported types', async () => {
const git: SimpleGit = simpleGit();

expect(git).not.toBeUndefined();
});
});

it('named type exports', async () => {
const git: SimpleGit = simpleGit();

expect(git).not.toBeUndefined();
});

it('named class constructors', async () => {
expect(new TaskConfigurationError('foo')).toBeInstanceOf(TaskConfigurationError);
});

it('named enums', async () => {
expect(CleanOptions.DRY_RUN).toBe('n');
});

});
31 changes: 31 additions & 0 deletions packages/test-typescript-esm-consumer/test/ts-named-import.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { simpleGit, CleanOptions, SimpleGit, TaskConfigurationError } from 'simple-git';

describe('simple-git', () => {

describe('named export', () => {
it('is the simple-git factory', async () => {
expect(await simpleGit().checkIsRepo()).toBe(true);
});

it('builds exported types', async () => {
const git: SimpleGit = simpleGit();

expect(git).not.toBeUndefined();
});
});

it('named type exports', async () => {
const git: SimpleGit = simpleGit();

expect(git).not.toBeUndefined();
});

it('named class constructors', async () => {
expect(new TaskConfigurationError('foo')).toBeInstanceOf(TaskConfigurationError);
});

it('named enums', async () => {
expect(CleanOptions.DRY_RUN).toBe('n');
});

});
22 changes: 22 additions & 0 deletions packages/test-typescript-esm-consumer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "es2020",
"module": "node16",
"moduleResolution": "node16",
"noEmit": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
// "emitDecoratorMetadata": true,
// "experimentalDecorators": true,
// "forceConsistentCasingInFileNames": true,
// "incremental": true,
// "noFallthroughCasesInSwitch": false,
// "noImplicitAny": false,
// "outDir": "./dist",
// "removeComments": true,
// "skipLibCheck": true,
// "sourceMap": true,
// "strictBindCallApply": true,
// "strictNullChecks": false,
}
}
Loading

0 comments on commit aef5f67

Please sign in to comment.