From 2c08328fb2aa163d46d9e2e71c2b378c061b10bf Mon Sep 17 00:00:00 2001 From: Sonny T Date: Thu, 11 Jan 2024 12:07:28 -0500 Subject: [PATCH] fixed the paths (#4) * fixed the paths * updated readme --- README.md | 22 +++++++++++++++------- src/libs/server.ts | 24 +++++++++++++++++++++--- src/libs/typescript.ts | 3 +-- tests/libs/server.test.ts | 7 ------- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index d349dd1..9ebdfe3 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ node -r @sonnyt/just/register myscript.ts Please note that runner does not support type checking. ## Default Config File -Just automatically finds and loads `tsconfig.json` or `jsconfig.json`. By default, this search is performed relative to the entrypoint script. If neither file is found nor the file is not provided as an argument. Just falls back to using default settings. +Just automatically finds and loads `tsconfig.json` or `jsconfig.json`. By default, this search is performed relative to the entrypoint script. If neither file is found nor the file is not provided as an argument. Just falls back to using default settings shown below. ```JSON { @@ -110,6 +110,7 @@ Just automatically finds and loads `tsconfig.json` or `jsconfig.json`. By defaul "moduleResolution": "Node", "inlineSourceMap": true, "strict": true, + "baseUrl": "./", "experimentalDecorators": true, "emitDecoratorMetadata": true, "esModuleInterop": true, @@ -118,8 +119,12 @@ Just automatically finds and loads `tsconfig.json` or `jsconfig.json`. By defaul "outDir": "dist", "paths": {} }, - "include": ["./"], - "exclude": ["node_modules"] + "include": [ + "./" + ], + "exclude": [ + "node_modules" + ] } ``` @@ -140,12 +145,15 @@ Just REPL enables you to execute TypeScript files on Node.js directly without pr Out of the box, Just supports build and runtime path aliases. All output file alias imports are replaced with relative paths. ### What happens to my non-JavaScript/TypeScript files? -If your source directory includes noncompilable files (i.e., JSON, SVG, etc.), Just automatically copies them to your output directory. +If your source directory includes non-compilable files (i.e., JSON, SVG, etc.), Just automatically copies them into your output directory. + +### How can I help? +If you would like to contribute, please see the issues labeled as [help-wanted](https://github.com/sonnyt/just/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22). ## Roadmap -- Build watch option -- Init option - copy over the default config file to the working directory -- [TypeScript ESLint](https://typescript-eslint.io/) support +- Build watch option [#7](https://github.com/sonnyt/just/issues/7) +- Init option - copy over the default config file to the working directory [#5](https://github.com/sonnyt/just/issues/5) +- [TypeScript ESLint](https://typescript-eslint.io/) support [#6](https://github.com/sonnyt/just/issues/6) - [Prettier](https://www.npmjs.com/package/prettier-eslint) support - REPL ES module support - ~~`jsconfig.json` support~~ diff --git a/src/libs/server.ts b/src/libs/server.ts index 5b1f227..42e05ed 100644 --- a/src/libs/server.ts +++ b/src/libs/server.ts @@ -40,6 +40,22 @@ export function getOptions(JUST_TSCONFIG: string, port?: string | number) { return options; } +/** + * Loads the package.json file from the current working directory. + * @returns The content of the package.json file, or undefined if it cannot be loaded. + */ +export function loadPackageJson() { + const path = resolve(process.cwd(), 'package.json'); + + let content; + + try { + content = require(path); + } catch (error) { } + + return content; +} + /** * Resolves the entry path for the server. * @@ -53,9 +69,11 @@ export function resolveEntryPath(path?: string) { return resolve(process.cwd(), path); } - if (process.env.npm_package_main) { - log.debug(`using main entry file from package.json: ${process.env.npm_package_main}`); - return resolve(process.cwd(), process.env.npm_package_main); + const packageJson = loadPackageJson(); + + if (packageJson?.main) { + log.debug(`using main entry file from package.json: ${packageJson.main}`); + return resolve(process.cwd(), packageJson.main); } log.error('entry path is not provided'); diff --git a/src/libs/typescript.ts b/src/libs/typescript.ts index c91c18f..f28d2fd 100644 --- a/src/libs/typescript.ts +++ b/src/libs/typescript.ts @@ -1,6 +1,5 @@ import ts from 'typescript'; -import { dirname } from 'path'; import * as log from '../utils/logger'; /** @@ -11,7 +10,7 @@ import * as log from '../utils/logger'; */ export function loadTSConfig(path: string) { const { config } = ts.readConfigFile(path, ts.sys.readFile); - const { options: compilerOptions, fileNames, errors } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(path)); + const { options: compilerOptions, fileNames, errors } = ts.parseJsonConfigFileContent(config, ts.sys, process.cwd()); if (errors.length) { log.error('failed to load tsconfig.json'); diff --git a/tests/libs/server.test.ts b/tests/libs/server.test.ts index dc5e251..3fa0c1e 100644 --- a/tests/libs/server.test.ts +++ b/tests/libs/server.test.ts @@ -37,13 +37,6 @@ describe('server', () => { expect(result).toEqual('/path/to/project/path/to/entry.js'); }); - it('returns the resolved main entry path from package.json when entry path is not provided', () => { - process.env.npm_package_main = 'src/index.js'; - const result = resolveEntryPath(); - expect(result).toEqual('/path/to/project/src/index.js'); - delete process.env.npm_package_main; - }); - it('throws an error when entry path is not provided and JUST_DEBUG environment variable is set', () => { process.env.JUST_DEBUG = 'TRUE';