Skip to content

Commit

Permalink
fixed the paths (#4)
Browse files Browse the repository at this point in the history
* fixed the paths

* updated readme
  • Loading branch information
sonnyt authored Jan 11, 2024
1 parent f5cdde6 commit 2c08328
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
Expand All @@ -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"
]
}
```

Expand All @@ -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~~
Expand Down
24 changes: 21 additions & 3 deletions src/libs/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions src/libs/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ts from 'typescript';

import { dirname } from 'path';
import * as log from '../utils/logger';

/**
Expand All @@ -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');
Expand Down
7 changes: 0 additions & 7 deletions tests/libs/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 2c08328

Please sign in to comment.