Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Nov 25, 2021
1 parent 0633c69 commit 1844878
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
37 changes: 19 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Ts = require('typescript')
const Path = require('path')
const Fs = require('fs')
const { resolve } = require('./utils')
const ts = require('typescript')

// ---------------------------------------------------------------------------------------------------------------------
// typedefs
Expand Down Expand Up @@ -89,26 +89,27 @@ function makeConfig () {

/**
* Logs out an error resulting from parsing a json file, with some pretty colors.
*
* @param {Error} error
*
* @param {Error} error
* @param {string} path
*/
function logJsonError (error) {
function logJsonError (error, path) {
require('colors')
const file = Path.basename(path)
const message = (
'\n Error! ' + error.message.replace('JSON', `"${file}"`)
+ '\n\n Edit the file to fix this, then try again'
+ '\n').red
'\n Error! ' + error.message.replace('JSON', `"${file}"`) +
'\n\n Edit the file to fix this, then try again' +
'\n').red

// CLI
if (global['ALIAS_CLI']) {
if (global.ALIAS_CLI) {
console.warn(message)
process.exit(0)
}

// API
console.warn(`\n [Alias HQ]\n${message}\n`.red)
throw(error)
throw (error)
}

// ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -124,11 +125,11 @@ function logJsonError (error) {
*/
function loadJson (path) {
const text = Fs.readFileSync(path, 'utf8').toString()
const {config, error} = ts.parseConfigFileTextToJson(path, text)
if (error) {
logJsonError(error)
} else {
return config;
try {
return JSON.parse(text)
}
catch (err) {
logJsonError(err, path)
}
}

Expand All @@ -155,15 +156,15 @@ function loadSettings () {
function loadConfig (path) {
// load
const text = Fs.readFileSync(path, 'utf8').toString()
const {config: json, error} = ts.parseConfigFileTextToJson(path, text)
const { config: json, error } = Ts.parseConfigFileTextToJson(path, text)
if (error) {
logJsonError(error)
logJsonError(error, path)
}

// extract config
const compilerOptions = json && ts.parseJsonConfigFileContent(json, ts.sys, Path.dirname(path)).options
const compilerOptions = json && Ts.parseJsonConfigFileContent(json, Ts.sys, Path.dirname(path)).options
if (compilerOptions) {
config.rootUrl = compilerOptions.pathsBasePath || Path.dirname(path);
config.rootUrl = compilerOptions.pathsBasePath || Path.dirname(path)
// compilerOptions.baseUrl is an absolute path, we want relative from root
config.baseUrl = Path.relative(config.rootUrl, compilerOptions.baseUrl) || ''
config.paths = compilerOptions.paths || {}
Expand Down
8 changes: 4 additions & 4 deletions tests/specs/load.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ describe('calling load and passing', function () {
describe('config with comments and trailing commas', () => {
it('should load the config file', () => {
const getConfig = () => hq.load('demo/tsconfig-fancy.json')
expect(getConfig).not.toThrowError();
});
expect(getConfig).not.toThrowError()
})
})

describe('config extending another config', () => {
it('should extract paths from the base config', () => {
const received = hq.load('demo/tsconfig-fancy.json')
const expected = config.ts
expect(received.config.paths).toEqual(expected);
});
expect(received.config.paths).toEqual(expected)
})
})

describe('any other value', function () {
Expand Down

0 comments on commit 1844878

Please sign in to comment.