chore: add eslint and other corrections

Signed-off-by: Pranav C <pranavxc@gmail.com>
This commit is contained in:
Pranav C
2024-10-07 00:21:01 +05:30
parent 2ae0049b9e
commit 9258dbdeca
9 changed files with 240 additions and 114 deletions

View File

@@ -3,3 +3,5 @@ webpack.config.js
src
node_modules
scripts
.eslintrc.js
.prettierrc

View File

@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}

File diff suppressed because one or more lines are too long

View File

@@ -9,7 +9,8 @@
"dev": "cross-env NC_DB=\"pg://localhost:5432?u=postgres&p=password&d=meta_2024_09_07\" nodemon --watch 'src/**/*.ts' --exec 'ts-node --project tsconfig.json' src/index.ts -- a b --nc-db abc",
"test": "mocha --require ts-node/register src/**/*.spec.ts",
"build:pkg": "npx pkg . --out-path dist --compress GZip",
"publish": "npm publish ."
"publish": "npm publish .",
"lint": "eslint src/**/*.ts --fix"
},
"pkg": {
"assets": [
@@ -28,7 +29,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^5.3.0",
"chalk": "^4.1.0",
"commander": "^12.1.0",
"enquirer": "^2.4.1",
"figlet": "^1.7.0",
@@ -46,6 +47,12 @@
"cross-env": "^7.0.3",
"mocha": "^10.3.0",
"nodemon": "^3.0.3",
"pkg": "^5.8.0"
"pkg": "^5.8.0",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-functional": "^5.0.8",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.8"
}
}

View File

@@ -1,7 +1,12 @@
import * as path from 'path';
import fs from 'fs';
import { promisify } from 'util';
const { DriverClient, getToolDir, metaUrlToDbConfig, prepareEnv } = require( '../nocodb/cli');
const {
DriverClient,
getToolDir,
metaUrlToDbConfig,
prepareEnv,
} = require('../nocodb/cli');
export class NcConfig {
meta: {
@@ -31,8 +36,7 @@ export class NcConfig {
};
secret?: string;
}): Promise<NcConfig> {
const { meta, secret } =
param;
const { meta } = param;
const ncConfig = new NcConfig();
@@ -43,20 +47,19 @@ export class NcConfig {
);
}
if (meta?.metaUrl) {
ncConfig.meta.db = await metaUrlToDbConfig(meta.metaUrl);
} else if (meta?.metaJson) {
ncConfig.meta.db = JSON.parse(meta.metaJson);
} else if (meta?.metaJsonFile) {
if (!(await promisify(fs.exists)(meta.metaJsonFile))) {
throw new Error(`NC_DB_JSON_FILE not found: ${meta.metaJsonFile}`);
}
const fileContent = await promisify(fs.readFile)(meta.metaJsonFile, {
encoding: 'utf8',
});
ncConfig.meta.db = JSON.parse(fileContent);
if (meta?.metaUrl) {
ncConfig.meta.db = await metaUrlToDbConfig(meta.metaUrl);
} else if (meta?.metaJson) {
ncConfig.meta.db = JSON.parse(meta.metaJson);
} else if (meta?.metaJsonFile) {
if (!(await promisify(fs.exists)(meta.metaJsonFile))) {
throw new Error(`NC_DB_JSON_FILE not found: ${meta.metaJsonFile}`);
}
const fileContent = await promisify(fs.readFile)(meta.metaJsonFile, {
encoding: 'utf8',
});
ncConfig.meta.db = JSON.parse(fileContent);
}
return ncConfig;
}
@@ -73,18 +76,26 @@ export class NcConfig {
}
}
export const getNocoConfig = async (options: {
ncDb?: string;
ncDbJson?: string;
ncDbJsonFile?: string;
databaseUrl?: string;
databaseUrlFile?: string;
} ={}) =>{
export const getNocoConfig = async (
options: {
ncDb?: string;
ncDbJson?: string;
ncDbJsonFile?: string;
databaseUrl?: string;
databaseUrlFile?: string;
} = {},
) => {
// check for JDBC url specified in env or options
await prepareEnv({
databaseUrl: options.databaseUrl || process.env.NC_DATABASE_URL || process.env.DATABASE_URL,
databaseUrlFile: options.databaseUrlFile || process.env.NC_DATABASE_URL_FILE || process.env.DATABASE_URL_FILE,
})
databaseUrl:
options.databaseUrl ||
process.env.NC_DATABASE_URL ||
process.env.DATABASE_URL,
databaseUrlFile:
options.databaseUrlFile ||
process.env.NC_DATABASE_URL_FILE ||
process.env.DATABASE_URL_FILE,
});
// create NocoConfig using utility method which works similar to Nocodb NcConfig with only meta db config
return NcConfig.create({
@@ -92,6 +103,6 @@ export const getNocoConfig = async (options: {
metaUrl: process.env.NC_DB || options.ncDb,
metaJson: process.env.NC_DB_JSON || options.ncDbJson,
metaJsonFile: process.env.NC_DB_JSON_FILE || options.ncDbJsonFile,
}
},
});
}
};

View File

@@ -1,3 +1 @@
export class NcError extends Error {
}
export class NcError extends Error {}

View File

@@ -1,34 +1,46 @@
import {NcError} from "./NcError";
import * as logger from "./logger";
import { existsSync } from 'fs';
import { NcError } from './NcError';
import * as logger from './logger';
const { SqlClientFactory, MetaTable, decryptPropIfRequired, encryptPropIfRequired } = require('../nocodb/cli')
const {
SqlClientFactory,
MetaTable,
decryptPropIfRequired,
encryptPropIfRequired,
} = require('../nocodb/cli');
export class SecretManager {
private sqlClient;
constructor(private prevSecret: string, private newSecret: string, private config: any) {
constructor(
private prevSecret: string,
private newSecret: string,
private config: any,
) {
this.sqlClient = SqlClientFactory.create(this.config.meta.db);
}
// validate config by checking if database config is valid
async validateConfig() {
// if sqlite then check the file exist in provided path
if (this.config.meta.db.client === 'sqlite3') {
if (!existsSync(this.config.meta.db.connection.filename)) {
throw new NcError('SQLite database file not found at path: ' + this.config.meta.db.connection.filename);
throw new NcError(
'SQLite database file not found at path: ' +
this.config.meta.db.connection.filename,
);
}
}
// use the sqlClientFactory to create a new sql client and then use testConnection to test the connection
const isValid = await this.sqlClient.testConnection();
if (!isValid) {
throw new NcError('Invalid database configuration. Please verify your database settings and ensure the database is reachable.');
throw new NcError(
'Invalid database configuration. Please verify your database settings and ensure the database is reachable.',
);
}
}
async validateAndExtract() {
// check if tables are present in the database
if (!(await this.sqlClient.knex.schema.hasTable(MetaTable.SOURCES))) {
@@ -40,15 +52,23 @@ export class SecretManager {
}
// if is_encrypted column is not present in the sources table then throw an error
if(
!(await this.sqlClient.knex.schema.hasColumn(MetaTable.SOURCES, 'is_encrypted')) ||
!(await this.sqlClient.knex.schema.hasColumn(MetaTable.INTEGRATIONS, 'is_encrypted'))){
throw new NcError('Looks like you are using an older version of NocoDB. Please upgrade to the latest version and try again.');
if (
!(await this.sqlClient.knex.schema.hasColumn(
MetaTable.SOURCES,
'is_encrypted',
)) ||
!(await this.sqlClient.knex.schema.hasColumn(
MetaTable.INTEGRATIONS,
'is_encrypted',
))
) {
throw new NcError(
'Looks like you are using an older version of NocoDB. Please upgrade to the latest version and try again.',
);
}
const sources = await this.sqlClient.knex(MetaTable.SOURCES).where(qb => {
qb.where('is_meta', false).orWhere('is_meta', null)
const sources = await this.sqlClient.knex(MetaTable.SOURCES).where((qb) => {
qb.where('is_meta', false).orWhere('is_meta', null);
});
const integrations = await this.sqlClient.knex(MetaTable.INTEGRATIONS);
@@ -56,19 +76,18 @@ export class SecretManager {
const sourcesToUpdate: Record<string, any>[] = [];
const integrationsToUpdate: Record<string, any>[] = [];
let isValid = false;
for (const source of sources) {
try {
const decrypted = decryptPropIfRequired({
data: source,
secret: this.prevSecret,
prop: 'config'
prop: 'config',
});
isValid = true;
sourcesToUpdate.push({ ...source, config: decrypted });
} catch (e) {
logger.error('Failed to decrypt source configuration : ' + e.message);
logger.error('Failed to decrypt source configuration : ' + (e as Error).message);
}
}
@@ -77,7 +96,7 @@ export class SecretManager {
const decrypted = decryptPropIfRequired({
data: integration,
secret: this.prevSecret,
prop: 'config'
prop: 'config',
});
isValid = true;
integrationsToUpdate.push({ ...integration, config: decrypted });
@@ -91,15 +110,12 @@ export class SecretManager {
throw new NcError('Invalid old secret or no sources/integrations found');
}
return { sourcesToUpdate, integrationsToUpdate };
}
async updateSecret(
sourcesToUpdate: Record<string, any>[],
integrationsToUpdate: Record<string, any>[]
integrationsToUpdate: Record<string, any>[],
) {
// start transaction
const transaction = await this.sqlClient.transaction();
@@ -107,30 +123,33 @@ export class SecretManager {
try {
// update sources
for (const source of sourcesToUpdate) {
await transaction(MetaTable.SOURCES).update({
config: encryptPropIfRequired({
data: source,
secret: this.newSecret,
prop: 'config'
await transaction(MetaTable.SOURCES)
.update({
config: encryptPropIfRequired({
data: source,
secret: this.newSecret,
prop: 'config',
}),
})
}).where('id', source.id);
.where('id', source.id);
}
// update integrations
for (const integration of integrationsToUpdate) {
await transaction(MetaTable.INTEGRATIONS).update({
config: encryptPropIfRequired({
data: integration,
secret: this.newSecret,
prop: 'config'
await transaction(MetaTable.INTEGRATIONS)
.update({
config: encryptPropIfRequired({
data: integration,
secret: this.newSecret,
prop: 'config',
}),
})
}).where('id', integration.id);
.where('id', integration.id);
}
await transaction.commit();
} catch (e) {
logger.error('Failed to decrypt integration configuration: ' + e.message);
logger.error('Failed to decrypt integration configuration: ' + (e as Error).message);
await transaction.rollback();
throw e;
}

View File

@@ -1,25 +1,25 @@
import chalk from 'chalk';
const chalk = require('chalk');
export function log(message: string) {
console.log(chalk.white(message));
}
export function log(message: string) {
console.log(chalk.white(message));
}
export function error(message: string) {
console.error(chalk.red('Error: ' + message));
}
export function error(message: string) {
console.error(chalk.red('Error: ' + message));
}
export function warn(message: string) {
console.warn(chalk.yellow('Warning: ' + message));
}
export function warn(message: string) {
console.warn(chalk.yellow('Warning: ' + message));
}
export function info(message: string) {
console.info(chalk.green('Info: ' + message));
}
export function info(message: string) {
console.info(chalk.green('Info: ' + message));
}
export function success(message: string) {
console.log(chalk.green('Success: ' + message));
}
export function success(message: string) {
console.log(chalk.green('Success: ' + message));
}
export function debug(message: string) {
console.debug(chalk.blue('Debug: ' + message));
}
export function debug(message: string) {
console.debug(chalk.blue('Debug: ' + message));
}

129
pnpm-lock.yaml generated
View File

@@ -493,8 +493,8 @@ importers:
packages/nc-secret-mgr:
dependencies:
chalk:
specifier: ^5.3.0
version: 5.3.0
specifier: ^4.1.0
version: 4.1.2
commander:
specifier: ^12.1.0
version: 12.1.0
@@ -535,6 +535,21 @@ importers:
cross-env:
specifier: ^7.0.3
version: 7.0.3
eslint-config-prettier:
specifier: ^8.10.0
version: 8.10.0(eslint@8.56.0)
eslint-plugin-eslint-comments:
specifier: ^3.2.0
version: 3.2.0(eslint@8.56.0)
eslint-plugin-functional:
specifier: ^5.0.8
version: 5.0.8(eslint@8.56.0)
eslint-plugin-import:
specifier: ^2.29.1
version: 2.29.1(eslint@8.56.0)
eslint-plugin-prettier:
specifier: ^4.2.1
version: 4.2.1(eslint-config-prettier@8.10.0)(eslint@8.56.0)(prettier@2.8.8)
mocha:
specifier: ^10.3.0
version: 10.3.0
@@ -544,6 +559,9 @@ importers:
pkg:
specifier: ^5.8.0
version: 5.8.1
prettier:
specifier: ^2.8.8
version: 2.8.8
packages/nocodb:
dependencies:
@@ -4254,7 +4272,7 @@ packages:
debug: 4.3.4(supports-color@5.5.0)
espree: 9.6.1
globals: 13.21.0
ignore: 5.3.1
ignore: 5.3.2
import-fresh: 3.3.0
js-yaml: 4.1.0
minimatch: 3.1.2
@@ -6400,7 +6418,7 @@ packages:
defu: 6.1.4
globby: 14.0.2
hash-sum: 2.0.0
ignore: 5.3.1
ignore: 5.3.2
jiti: 1.21.0
knitwork: 1.0.0
mlly: 1.6.1
@@ -6427,7 +6445,7 @@ packages:
defu: 6.1.4
globby: 14.0.1
hash-sum: 2.0.0
ignore: 5.3.1
ignore: 5.3.2
jiti: 1.21.0
knitwork: 1.0.0
mlly: 1.6.1
@@ -6454,7 +6472,7 @@ packages:
defu: 6.1.4
globby: 14.0.1
hash-sum: 2.0.0
ignore: 5.3.1
ignore: 5.3.2
jiti: 1.21.0
knitwork: 1.1.0
mlly: 1.6.1
@@ -6514,7 +6532,7 @@ packages:
defu: 6.1.4
globby: 13.2.2
hash-sum: 2.0.0
ignore: 5.3.1
ignore: 5.3.2
jiti: 1.21.0
knitwork: 1.0.0
lodash.template: 4.5.0
@@ -6541,7 +6559,7 @@ packages:
defu: 6.1.4
globby: 14.0.0
hash-sum: 2.0.0
ignore: 5.3.1
ignore: 5.3.2
jiti: 1.21.0
knitwork: 1.0.0
mlly: 1.5.0
@@ -6796,7 +6814,7 @@ packages:
'@nrwl/devkit': 16.7.4(nx@16.7.4)
ejs: 3.1.10
enquirer: 2.3.6
ignore: 5.3.1
ignore: 5.3.2
nx: 16.7.4
semver: 7.5.3
tmp: 0.2.1
@@ -10500,7 +10518,7 @@ packages:
debug: 4.3.4(supports-color@5.5.0)
eslint: 8.56.0
graphemer: 1.4.0
ignore: 5.3.1
ignore: 5.3.2
natural-compare-lite: 1.4.0
semver: 7.6.2
tsutils: 3.21.0(typescript@5.3.2)
@@ -10813,7 +10831,7 @@ packages:
eslint: ^6.0.0 || ^7.0.0 || ^8.0.0
dependencies:
'@eslint-community/eslint-utils': 4.4.0(eslint@8.33.0)
'@types/json-schema': 7.0.12
'@types/json-schema': 7.0.15
'@types/semver': 7.5.0
'@typescript-eslint/scope-manager': 5.62.0
'@typescript-eslint/types': 5.62.0
@@ -10833,7 +10851,7 @@ packages:
eslint: ^6.0.0 || ^7.0.0 || ^8.0.0
dependencies:
'@eslint-community/eslint-utils': 4.4.0(eslint@8.56.0)
'@types/json-schema': 7.0.12
'@types/json-schema': 7.0.15
'@types/semver': 7.5.0
'@typescript-eslint/scope-manager': 5.62.0
'@typescript-eslint/types': 5.62.0
@@ -16298,7 +16316,7 @@ packages:
dependencies:
escape-string-regexp: 1.0.5
eslint: 8.33.0
ignore: 5.3.0
ignore: 5.3.2
dev: true
/eslint-plugin-eslint-comments@3.2.0(eslint@8.56.0):
@@ -16328,7 +16346,28 @@ packages:
escape-string-regexp: 4.0.0
eslint: 8.33.0
is-immutable-type: 1.2.9(eslint@8.33.0)
semver: 7.5.4
semver: 7.6.3
transitivePeerDependencies:
- supports-color
dev: true
/eslint-plugin-functional@5.0.8(eslint@8.56.0):
resolution: {integrity: sha512-rXC5THzqqSXUrbTBG+dLLYn10Af0C9Df+N4TT3onPrOz+kgInshLJdRAvEcV+8HHNsZyDrNLcgWh5jzVaAnleQ==}
engines: {node: '>=16.10.0'}
peerDependencies:
eslint: ^8.0.0
typescript: latest
peerDependenciesMeta:
typescript:
optional: true
dependencies:
'@typescript-eslint/type-utils': 5.62.0(eslint@8.56.0)(typescript@5.3.2)
'@typescript-eslint/utils': 5.62.0(eslint@8.56.0)(typescript@5.3.2)
deepmerge-ts: 5.1.0
escape-string-regexp: 4.0.0
eslint: 8.56.0
is-immutable-type: 1.2.9(eslint@8.56.0)
semver: 7.6.3
transitivePeerDependencies:
- supports-color
dev: true
@@ -16414,7 +16453,7 @@ packages:
eslint: 8.56.0
eslint-import-resolver-node: 0.3.9
eslint-module-utils: 2.8.0(@typescript-eslint/parser@5.62.0)(eslint-import-resolver-node@0.3.9)(eslint@8.56.0)
hasown: 2.0.0
hasown: 2.0.1
is-core-module: 2.13.1
is-glob: 4.0.3
minimatch: 3.1.2
@@ -16464,6 +16503,40 @@ packages:
- supports-color
dev: true
/eslint-plugin-import@2.29.1(eslint@8.56.0):
resolution: {integrity: sha512-BbPC0cuExzhiMo4Ff1BTVwHpjjv28C5R+btTOGaCRC7UEz801up0JadwkeSk5Ued6TG34uaczuVuH6qyy5YUxw==}
engines: {node: '>=4'}
peerDependencies:
'@typescript-eslint/parser': '*'
eslint: ^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8
peerDependenciesMeta:
'@typescript-eslint/parser':
optional: true
dependencies:
array-includes: 3.1.7
array.prototype.findlastindex: 1.2.3
array.prototype.flat: 1.3.2
array.prototype.flatmap: 1.3.2
debug: 3.2.7
doctrine: 2.1.0
eslint: 8.56.0
eslint-import-resolver-node: 0.3.9
eslint-module-utils: 2.8.0(@typescript-eslint/parser@6.21.0)(eslint-import-resolver-node@0.3.9)(eslint@8.56.0)
hasown: 2.0.1
is-core-module: 2.13.1
is-glob: 4.0.3
minimatch: 3.1.2
object.fromentries: 2.0.7
object.groupby: 1.0.1
object.values: 1.1.7
semver: 6.3.1
tsconfig-paths: 3.15.0
transitivePeerDependencies:
- eslint-import-resolver-typescript
- eslint-import-resolver-webpack
- supports-color
dev: true
/eslint-plugin-json@3.1.0:
resolution: {integrity: sha512-MrlG2ynFEHe7wDGwbUuFPsaT2b1uhuEFhJ+W1f1u+1C2EkXmTYJp4B1aAdQQ8M+CC3t//N/oRKiIVw14L2HR1g==}
engines: {node: '>=12.0'}
@@ -16510,7 +16583,7 @@ packages:
eslint: 8.56.0
eslint-plugin-es: 4.1.0(eslint@8.56.0)
eslint-utils: 3.0.0(eslint@8.56.0)
ignore: 5.3.1
ignore: 5.3.2
is-core-module: 2.13.1
minimatch: 3.1.2
resolve: 1.22.4
@@ -18084,7 +18157,7 @@ packages:
dir-glob: 3.0.1
fast-glob: 3.3.2
glob: 7.2.3
ignore: 5.3.1
ignore: 5.3.2
merge2: 1.4.1
slash: 3.0.0
dev: true
@@ -18107,7 +18180,7 @@ packages:
dependencies:
dir-glob: 3.0.1
fast-glob: 3.3.2
ignore: 5.3.1
ignore: 5.3.2
merge2: 1.4.1
slash: 4.0.0
@@ -18117,7 +18190,7 @@ packages:
dependencies:
'@sindresorhus/merge-streams': 1.0.0
fast-glob: 3.3.2
ignore: 5.3.1
ignore: 5.3.2
path-type: 5.0.0
slash: 5.1.0
unicorn-magic: 0.1.0
@@ -18128,7 +18201,7 @@ packages:
dependencies:
'@sindresorhus/merge-streams': 2.2.1
fast-glob: 3.3.2
ignore: 5.3.1
ignore: 5.3.2
path-type: 5.0.0
slash: 5.1.0
unicorn-magic: 0.1.0
@@ -18738,12 +18811,12 @@ packages:
/ignore@5.3.1:
resolution: {integrity: sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw==}
engines: {node: '>= 4'}
dev: true
/ignore@5.3.2:
resolution: {integrity: sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==}
engines: {node: '>= 4'}
requiresBuild: true
dev: false
/image-meta@0.2.0:
resolution: {integrity: sha512-ZBGjl0ZMEMeOC3Ns0wUF/5UdUmr3qQhBSCniT0LxOgGGIRHiNFOkMtIHB7EOznRU47V2AxPgiVP+s+0/UCU0Hg==}
@@ -19226,6 +19299,18 @@ packages:
- supports-color
dev: true
/is-immutable-type@1.2.9(eslint@8.56.0):
resolution: {integrity: sha512-DDx04RjLpGNT4vtF49vGW5CECP6lAx8SL2keq99ogIxwLvJPBvgThdhb43ED5uYO4nq0kZ51tMj7VdCCQgdZ5Q==}
peerDependencies:
eslint: '*'
typescript: latest
dependencies:
'@typescript-eslint/type-utils': 5.62.0(eslint@8.56.0)(typescript@5.3.2)
eslint: 8.56.0
transitivePeerDependencies:
- supports-color
dev: true
/is-immutable-type@1.2.9(eslint@8.56.0)(typescript@5.3.2):
resolution: {integrity: sha512-DDx04RjLpGNT4vtF49vGW5CECP6lAx8SL2keq99ogIxwLvJPBvgThdhb43ED5uYO4nq0kZ51tMj7VdCCQgdZ5Q==}
peerDependencies:
@@ -23052,7 +23137,7 @@ packages:
flat: 5.0.2
fs-extra: 11.2.0
glob: 7.1.4
ignore: 5.3.1
ignore: 5.3.2
js-yaml: 4.1.0
jsonc-parser: 3.2.0
lines-and-columns: 2.0.3