Skip to content

Commit

Permalink
Only enable typescript when detected
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Jul 4, 2019
1 parent d6ac11e commit f0992b4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
3 changes: 0 additions & 3 deletions lib/base-config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict';

const path = require('path');

const baseConfig = {
extends: [
'airbnb-base',
path.resolve(__dirname, 'typescript.js'),
'plugin:node/recommended',
'plugin:promise/recommended',
'plugin:jest/recommended',
Expand Down
20 changes: 12 additions & 8 deletions lib/eslint-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

const path = require('path');
const featuresEnabled = require('./utils/features-enabled');
const eslintConfig = require('./base-config');

let eslintConfig = null;
function addPresetToExtends(pathname) {
const currentExtends = eslintConfig.extends;
const prettierLocation = currentExtends.indexOf('prettier');

if (eslintConfig === null) {
eslintConfig = {
extends: [path.resolve(__dirname, 'base-config.js')],
};
currentExtends.splice(prettierLocation, 0, pathname);
}

if (featuresEnabled.typescript === true) {
addPresetToExtends(path.resolve(__dirname, 'typescript.js'));
}

if (featuresEnabled.flow === true) {
eslintConfig.extends.push(path.resolve(__dirname, 'flow.js'));
}
if (featuresEnabled.flow === true) {
addPresetToExtends(path.resolve(__dirname, 'flow.js'));
}

module.exports = eslintConfig;
2 changes: 2 additions & 0 deletions lib/utils/features-enabled.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const flowEnabled = require('./flow-enabled');
const typescriptEnabled = require('./typescript-enabled');

const featuresEnabled = {
flow: flowEnabled,
typescript: typescriptEnabled,
};

module.exports = featuresEnabled;
13 changes: 13 additions & 0 deletions lib/utils/typescript-enabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const fs = require('fs');
const path = require('path');

const typescriptEnabled = (function checkForTypescript() {
const typescriptConfigFile = path.resolve(process.cwd(), 'tsconfig.json');
const typescriptExists = fs.existsSync(typescriptConfigFile);

return typescriptExists;
})();

module.exports = typescriptEnabled;
34 changes: 34 additions & 0 deletions lib/utils/typescript-enabled.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const { TempSandbox } = require('temp-sandbox');

const sandbox = new TempSandbox();
const cwd = process.cwd();

beforeEach(() => {
process.chdir(sandbox.dir);
// Remove all files/directories inside sandbox
sandbox.cleanSync();
});

afterAll(() => {
// delete sandbox and sandbox instance
sandbox.destroySandboxSync();
process.chdir(cwd);
});

describe('detectTypescript', () => {
test('detects typescript with tsconfig.json', () => {
sandbox.createFileSync('tsconfig.json');

const typescriptEnabled = require('./typescript-enabled');

expect(typescriptEnabled).toEqual(true);
});

test('no tsconfig.json found', () => {
const typescriptEnabled = require('./typescript-enabled');

expect(typescriptEnabled).toEqual(false);
});
});

0 comments on commit f0992b4

Please sign in to comment.