Skip to content

Commit

Permalink
Replace TypeScript dependency with JSON5 (#65)
Browse files Browse the repository at this point in the history
* Replace TypeScript dependency with JSON5

* Update version and changelog
  • Loading branch information
davestewart authored Apr 25, 2023
1 parent 309d23a commit 447b2c7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [6.2.0] - 2023-04-25
### Changed
- Replace TypeScript dependency with JSON5 (#65) - closes #41

## [6.1.0] - 2022-12-06
### Changed
- Add Babel support (#60) - closes #56
Expand Down
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alias-hq",
"version": "6.1.0",
"version": "6.2.0",
"description": "The end-to-end solution for configuring, refactoring, maintaining and using path aliases",
"bin": "bin/alias-hq",
"exports": {
Expand Down Expand Up @@ -65,6 +65,7 @@
"glob": "^7.1.6",
"inquirer": "^7.3.3",
"jscodeshift": "^0.10.0",
"json5": "^2.2.3",
"module-alias": "^2.2.2",
"node-fetch": "^2.6.0",
"open": "^7.0.0",
Expand Down
110 changes: 49 additions & 61 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Ts = require('typescript')
const Path = require('path')
const Fs = require('fs')
const Path = require('path')
const JSON5 = require('json5')
const { resolve } = require('./utils')

// ---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -83,35 +83,6 @@ function makeConfig () {
}
}

// ---------------------------------------------------------------------------------------------------------------------
// helpers
// ---------------------------------------------------------------------------------------------------------------------

/**
* Logs out an error resulting from parsing a json file, with some pretty colors.
*
* @param {Error} error
* @param {string} path
*/
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

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

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

// ---------------------------------------------------------------------------------------------------------------------
// loaders
// ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -124,12 +95,29 @@ function logJsonError (error, path) {
* @throws An error if the JSON could not be parsed
*/
function loadJson (path) {
const text = Fs.readFileSync(path, 'utf8').toString()
try {
return JSON.parse(text)
}
catch (err) {
logJsonError(err, path)
const text = Fs.readFileSync(path, 'utf8')
if (text) {
try {
return JSON5.parse(text)
}
catch (err) {
require('colors')
const file = Path.basename(path)
const message = (
'\n Error! ' + err.message.replace('JSON', `"${file}"`) +
'\n\n Edit the file to fix this, then try again' +
'\n').red

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

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

Expand All @@ -154,30 +142,31 @@ function loadSettings () {
* @param {string} path The absolute path to the config file
*/
function loadConfig (path) {
// load
const text = Fs.readFileSync(path, 'utf8').toString()
const { config: json, error } = Ts.parseConfigFileTextToJson(path, text)
if (error) {
logJsonError(error, path)
}

// extract config
const compilerOptions = json && Ts.parseJsonConfigFileContent(json, Ts.sys, Path.dirname(path)).options
if (compilerOptions) {
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 || {}

// settings
settings.configFile = path
/**
* @type {TSConfig}
*/
const json = loadJson(path)
if (json) {
const { compilerOptions } = json
if (compilerOptions) {
const { baseUrl, paths } = compilerOptions

// Note that paths in the extending file take priority over paths in the extended file
// https://stackoverflow.com/questions/53804566/how-to-get-compileroptions-from-tsconfig-json
if (paths) {
settings.configFile = path
config.rootUrl = Path.dirname(path)
config.baseUrl = baseUrl || ''
config.paths = paths
return true
}
}

// done
return true
// if no paths, check extends
if (json.extends) {
return loadConfig(resolve(Path.dirname(path), json.extends))
}
}

// normal file
config.paths = {}
}

// ---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -207,12 +196,11 @@ function load (value = undefined) {
}

// no value: default or configured config file
else if (typeof value === 'undefined') {
else if (value === undefined) {
// variables
let found = false
const files = [
'jsconfig.json',
'tsconfig.base.json',
'tsconfig.json',
]

Expand Down

0 comments on commit 447b2c7

Please sign in to comment.