Skip to content

Commit

Permalink
feat: add semantic release - make package sync
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinAgha committed Sep 25, 2019
1 parent 54009dd commit e5765a2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2
jobs:
build:
docker:
- image: 'circleci/node:latest'
steps:
- checkout
- run:
name: install
command: npm install
- run:
name: release
command: npm run semantic-release || true
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tsconfig-paths-jest-mapper",
"version": "1.0.0",
"version": "0.0.0-development",
"main": "dist/index.js",
"license": "MIT",
"repository": {
Expand All @@ -15,7 +15,8 @@
"prepare": "npm run compile",
"precompile": "rimraf dist",
"compile": "tsc",
"test": "jest"
"test": "jest",
"semantic-release": "semantic-release"
},
"keywords": [
"TypeScript",
Expand All @@ -34,7 +35,8 @@
"@types/node": "^12.7.7",
"jest": "^24.9.0",
"rimraf": "^3.0.0",
"typescript": "^3.6.3"
"typescript": "^3.6.3",
"semantic-release": "^15.13.24"
},
"dependencies": {
"tsconfig": "^7.0.0"
Expand Down
28 changes: 14 additions & 14 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ import tsconfigPathsJestMapper from './index';
import path from 'path';

describe('test lib', () => {
it('should load tsconfig', async () => {
await tsconfigPathsJestMapper(path.resolve(__dirname, '../test/right-config'));
it('should load tsconfig', () => {
tsconfigPathsJestMapper(path.resolve(__dirname, '../test/right-config'));
})

it('should not accept configs with no paths field', async () => {
it('should convert paths to jest moduleNameMapper', () => {
const mappers = tsconfigPathsJestMapper(path.resolve(__dirname, '../test/right-config'));
expect(mappers).toEqual({
"\\$components/(.*)": "<rootDir>/src/components/$1",
"@app/(.*)": "<rootDir>/src/app/$1"
})
})

it('should not accept configs with no paths field', () => {
const config = path.resolve(__dirname, '../test/no-paths-field');
expect(tsconfigPathsJestMapper(config)).rejects.toThrow();
expect(() => tsconfigPathsJestMapper(config)).toThrowError();
})

it('should only accept array paths', async () => {
it('should only accept array paths', () => {
try {
await tsconfigPathsJestMapper(path.resolve(__dirname, '../test/wrong-path-value'));
tsconfigPathsJestMapper(path.resolve(__dirname, '../test/wrong-path-value'));
} catch (e) {
expect(e.message).toContain('Check value of: @app/*')
}
})

it('should convert paths to jest moduleNameMapper', async () => {
const mappers = await tsconfigPathsJestMapper(path.resolve(__dirname, '../test/right-config'));
expect(mappers).toEqual({
"\\$components/(.*)": "<rootDir>/src/components/$1",
"@app/(.*)": "<rootDir>/src/app/$1"
})
})
})
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { load } from 'tsconfig';
import { loadSync } from 'tsconfig';

function escapeRegExp(text: string) {
// we don't excape * in tsconfig paths
// but we should escape other special characters
return text.replace(/[-[\]{}()+?.,\\^$|#\s]/g, '\\$&');
}

export default async (tsconfigPath: string) => {
const { config } = await load('', tsconfigPath);
export const getJestMappersFromTSConfig = (tsconfigPath: string) => {
const { config } = loadSync('', tsconfigPath);
if (!config.compilerOptions || !config.compilerOptions.paths) {
throw new Error('paths field not found in tsconfig compiler options');
}
Expand All @@ -27,3 +27,6 @@ export default async (tsconfigPath: string) => {

return moduleNameMapper;
}

export default getJestMappersFromTSConfig;
module.exports = getJestMappersFromTSConfig;
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"include": ["./src/**/*"],
"exclude": ["./**/*.test.ts"],
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"skipLibCheck": true,
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand Down Expand Up @@ -41,7 +43,7 @@

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
Expand Down

0 comments on commit e5765a2

Please sign in to comment.