Skip to content

Commit b94b175

Browse files
authored
fix root package access (#49)
* fix root package access * fix root package access * eslint fixes * eslint fixes * add filename to dictionary * add filename to dictionary
1 parent 9ea67c0 commit b94b175

10 files changed

Lines changed: 53 additions & 25 deletions

File tree

.dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Javascript
2222
Jira
2323
LinkedIn
2424
MSSQL
25+
package-lock.json
2526
Matomo
2627
Microservice
2728
Middleware

language/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ response_not_success: 'The response was not success, %1% field was %2%'
4343
response_not_failure: 'The response was not failure, %1% field was %2%'
4444
no_errors_warnings: No errors or warnings found.
4545
impossible_include: Can't include %1%'s export %2%.
46+
missing_package_lock: Can't find a package-lock.json.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@idrinth-api-bench/framework",
33
"description": "A library to benchmark apis, no matter if rest or soap",
44
"license": "MIT",
5-
"version": "1.0.3",
5+
"version": "1.0.5",
66
"type": "module",
77
"main": "index.js",
88
"homepage": "https://idrinth-api-ben.ch",

src/helper/root-package.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
readFileSync,
3+
existsSync,
4+
} from 'fs';
5+
import language from './language.js';
6+
7+
export interface Versioned {
8+
name: string;
9+
version: string;
10+
}
11+
export interface Lock extends Versioned{
12+
packages: {[lib: string]: Versioned};
13+
}
14+
15+
let lock: Lock;
16+
17+
export const set = (cwd: string,) => {
18+
if (! existsSync(cwd + '/package-lock.json',)) {
19+
throw new Error(language('missing_package_lock',),);
20+
}
21+
lock = JSON.parse(readFileSync(cwd + '/package-lock.json', 'utf8',),);
22+
};
23+
24+
export default (): Lock => {
25+
if (! lock) {
26+
set(process.cwd(),);
27+
}
28+
return lock;
29+
};

src/helper/user-agent.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import reqlib from 'app-root-path';
2-
interface Versioned {
3-
name: string;
4-
version: string;
5-
}
6-
interface Lock extends Versioned{
7-
packages: {[lib: string]: Versioned};
8-
}
1+
import getLock, {
2+
Versioned,
3+
Lock,
4+
} from '../helper/root-package.js';
95

106
const formatVersion = (ob: Versioned,): string => {
117
if (! ob) {
@@ -29,7 +25,7 @@ const getVersion = (name: string, lock: Lock,): string => {
2925
return '0.0';
3026
};
3127

32-
const lock: Lock = reqlib.require('/package-lock.json',) as Lock;
28+
const lock = getLock();
3329
const main = `${ lock.name }/${ formatVersion(lock,) }`;
3430
const needle = `needle/${ getVersion('needle', lock,) }`;
3531
const name = '@idrinth-api-bench/framework';

src/load/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ const loadUp = async(config: Config,) => {
4949
threads += config.increment;
5050
} while (threads <= config.maximum);
5151
}
52-
defaultReporter(runs, config.cwd);
52+
defaultReporter(runs, config.cwd,);
5353
};
5454
export default loadUp;

src/main.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import {
2525
import blacklist from './routes/blacklist.js';
2626
import validateTasks from './routes/validate-tasks.js';
2727
import Executions from './executions.js';
28+
import {
29+
set,
30+
} from './helper/root-package.js';
2831

2932
// eslint-disable-next-line complexity
3033
export const run = async(
@@ -46,7 +49,11 @@ export const run = async(
4649
job?: Job|Array<Task>|undefined,
4750
// eslint-disable-next-line max-params
4851
): Promise<void> => {
52+
if (! configuration.cwd) {
53+
configuration.cwd = process.cwd();
54+
}
4955
await locale(configuration.language || DEFAULT_LANGUAGE,);
56+
set(configuration.cwd,);
5057
if (typeof configuration.logger === 'undefined') {
5158
configuration.logger = new NullLogger();
5259
}
@@ -64,12 +71,12 @@ export const run = async(
6471
}
6572
if (typeof configuration.blacklist === 'undefined') {
6673
configuration.blacklist = blacklist(
67-
process.cwd(),
74+
configuration.cwd,
6875
configuration.mode || 'benchmarking',
6976
);
7077
}
7178
if (typeof job === 'undefined') {
72-
job = await jobCreator(configuration.cwd || process.cwd(),);
79+
job = await jobCreator(configuration.cwd,);
7380
} else if (typeof job === 'object' && Array.isArray(job,)) {
7481
job = {
7582
before: [],

src/storage/mssql-storage.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import Storage from './storage.js';
22
import FinishedSet from '../messaging/finished-set.js';
33
import mssql from 'msnodesqlv8';
4-
import reqlib from 'app-root-path';
54
import {
65
MONTH_OFFSET,
76
TEN,
87
} from '../constants.js';
8+
import getLock from '../helper/root-package.js';
99

10-
const project: string = reqlib
11-
.require('/package-lock.json',)
12-
.name.replace(/[^a-z0-9\-_]/gu, '_',);
10+
const project: string = getLock().name.replace(/[^a-z0-9\-_]/gu, '_',);
1311

1412
export class MssqlStorage implements Storage {
1513
private readonly connection: string;

src/storage/mysql-storage.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import {
44
createConnection,
55
Connection,
66
} from 'mysql2';
7-
import reqlib from 'app-root-path';
87
import {
98
MONTH_OFFSET,
109
TEN,
1110
} from '../constants.js';
11+
import getLock from '../helper/root-package.js';
1212

13-
const project: string = reqlib
14-
.require('/package-lock.json',)
15-
.name.replace(/[^a-z0-9\-_]/gu, '_',);
13+
const project: string = getLock().name.replace(/[^a-z0-9\-_]/gu, '_',);
1614

1715
export class MysqlStorage implements Storage {
1816
private connection: Connection;

src/storage/postgres-storage.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import Storage from './storage.js';
22
import FinishedSet from '../messaging/finished-set.js';
33
import postgres from 'postgres';
4-
import reqlib from 'app-root-path';
4+
import getLock from '../helper/root-package.js';
55
import {
66
MONTH_OFFSET,
77
TEN,
88
} from '../constants.js';
99

10-
const project: string = reqlib
11-
.require('/package-lock.json',)
12-
.name.replace(/[^a-z0-9\-_]/gu, '_',);
10+
const project: string = getLock().name.replace(/[^a-z0-9\-_]/gu, '_',);
1311

1412
export class PostgresStorage implements Storage {
1513
private connection: postgres.Sql<Record<string, unknown>>;

0 commit comments

Comments
 (0)