From 010bd4e368d33572abc60e852d60b9632bf5ab93 Mon Sep 17 00:00:00 2001 From: Nameer Haider Date: Sat, 17 Feb 2024 03:40:43 +0300 Subject: [PATCH] adding main operations --- package-lock.json | 17 +++++++++ package.backup | 8 +++++ package.json | 11 +++++- repackify.js | 3 ++ src/common/dto/config.dto.ts | 7 ++++ src/common/dto/index.ts | 1 + src/common/utils/get-config-object.ts | 26 ++++++++++++++ src/common/utils/index.ts | 1 + src/features/operations.ts | 52 +++++++++++++++++++++++++++ src/features/operationsRunner.ts | 40 +++++++++++++++++++++ src/main.ts | 9 ++++- 11 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 repackify.js create mode 100644 src/common/dto/config.dto.ts create mode 100644 src/common/dto/index.ts create mode 100644 src/common/utils/get-config-object.ts create mode 100644 src/common/utils/index.ts create mode 100644 src/features/operations.ts create mode 100644 src/features/operationsRunner.ts diff --git a/package-lock.json b/package-lock.json index 56413ac..512f4df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.1", "license": "UNLICENSED", "dependencies": { + "dotenv": "^16.4.4", "read-package-json": "^7.0.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1" @@ -2794,6 +2795,17 @@ "node": ">=6.0.0" } }, + "node_modules/dotenv": { + "version": "16.4.4", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.4.tgz", + "integrity": "sha512-XvPXc8XAQThSjAbY6cQ/9PcBXmFoWuw1sQ3b8HqUCR6ziGXjkTi//kB9SWa2UwqlgdAIuRqAa/9hVljzPehbYg==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", @@ -8639,6 +8651,11 @@ "esutils": "^2.0.2" } }, + "dotenv": { + "version": "16.4.4", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.4.tgz", + "integrity": "sha512-XvPXc8XAQThSjAbY6cQ/9PcBXmFoWuw1sQ3b8HqUCR6ziGXjkTi//kB9SWa2UwqlgdAIuRqAa/9hVljzPehbYg==" + }, "eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", diff --git a/package.backup b/package.backup index 2b80862..6c6b8fc 100644 --- a/package.backup +++ b/package.backup @@ -5,6 +5,14 @@ "author": "", "private": true, "license": "UNLICENSED", + "repackify": { + "remove": [ + "test" + ], + "operations": { + "test": "test2" + } + }, "scripts": { "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "ts-node src/main.ts", diff --git a/package.json b/package.json index 2b80862..156d511 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,14 @@ "author": "", "private": true, "license": "UNLICENSED", + "repackify": { + "remove": [ + "test" + ], + "add": { + "test": "test2" + } + }, "scripts": { "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "ts-node src/main.ts", @@ -13,6 +21,7 @@ "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix" }, "dependencies": { + "dotenv": "^16.4.4", "read-package-json": "^7.0.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1" @@ -53,4 +62,4 @@ "coverageDirectory": "../coverage", "testEnvironment": "node" } -} \ No newline at end of file +} diff --git a/repackify.js b/repackify.js new file mode 100644 index 0000000..636e07f --- /dev/null +++ b/repackify.js @@ -0,0 +1,3 @@ +module.exports = { + add: { test: 'test' }, +}; diff --git a/src/common/dto/config.dto.ts b/src/common/dto/config.dto.ts new file mode 100644 index 0000000..78fb7da --- /dev/null +++ b/src/common/dto/config.dto.ts @@ -0,0 +1,7 @@ +export class ConfigDto { + remove: string[]; + replace: Record[]; + add: Record[]; + replaceText: { text: string; replace: string }[]; + customOperation: ((record: Record) => Record)[]; +} diff --git a/src/common/dto/index.ts b/src/common/dto/index.ts new file mode 100644 index 0000000..6ab94dc --- /dev/null +++ b/src/common/dto/index.ts @@ -0,0 +1 @@ +export * from './config.dto'; diff --git a/src/common/utils/get-config-object.ts b/src/common/utils/get-config-object.ts new file mode 100644 index 0000000..efe2323 --- /dev/null +++ b/src/common/utils/get-config-object.ts @@ -0,0 +1,26 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { ConfigDto } from '../dto'; + +export const getConfigFromJSFile = (): ConfigDto | null => { + const filePath = path.join(global._projectDir, 'repackify.js'); + if (fs.existsSync(filePath)) { + return require(filePath); + } + return null; +}; + +export const getConfigFromRecord = ( + record: Record, +): ConfigDto | null => { + if (Object.keys(record).includes('repackify')) { + return record['repackify']; + } + return null; +}; + +export const getConfig = (record: Record): ConfigDto | null => { + const jsConfig = getConfigFromJSFile(); + const packageJsonConfig = getConfigFromRecord(record); + return { ...packageJsonConfig, ...jsConfig }; +}; diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts new file mode 100644 index 0000000..aa58714 --- /dev/null +++ b/src/common/utils/index.ts @@ -0,0 +1 @@ +export * from './get-config-object'; diff --git a/src/features/operations.ts b/src/features/operations.ts new file mode 100644 index 0000000..fa84221 --- /dev/null +++ b/src/features/operations.ts @@ -0,0 +1,52 @@ +import { filterPackageJson } from '../common/package-utils'; + +export const add = ( + packProps: Record, + props: Record, +): Record => { + for (const key in props) { + if (!(key in packProps)) { + packProps[key] = props[key]; + } + } + return filterPackageJson(packProps); +}; + +export const remove = ( + packProps: Record, + prop: string, +): Record => { + if (prop in packProps) { + delete packProps[prop]; + } + return packProps; +}; + +export const replace = ( + packProps: Record, + props: Record, +): Record => { + for (const key in props) { + if (key in packProps) { + packProps[key] = props[key]; + } + } + return packProps; +}; + +export const replaceText = ( + packProps: Record, + text: string, + replace: string, +): Record => { + let resultString = JSON.stringify(packProps); + resultString = resultString.replaceAll(text, replace); + return JSON.parse(resultString); +}; + +export const customOperation = ( + packProps: Record, + operation: (packProps: Record) => Record, +): Record => { + return operation(packProps); +}; diff --git a/src/features/operationsRunner.ts b/src/features/operationsRunner.ts new file mode 100644 index 0000000..e30c8fa --- /dev/null +++ b/src/features/operationsRunner.ts @@ -0,0 +1,40 @@ +import { ConfigDto } from '../common/dto'; +import * as operations from './operations'; +import { filterPackageJson } from '../common/package-utils'; + +export const operationsRunner = ( + packProps: Record, + config: ConfigDto, +): Record => { + if (config.add) { + config.add.forEach((prop) => { + packProps = operations.add(packProps, prop); + }); + } + + if (config.remove) { + config.remove.forEach((prop) => { + packProps = operations.remove(packProps, prop); + }); + } + + if (config.replace) { + config.replace.forEach((prop) => { + packProps = operations.replace(packProps, prop); + }); + } + + if (config.replaceText) { + config.replaceText.forEach((prop) => { + packProps = operations.replaceText(packProps, prop.text, prop.replace); + }); + } + + if (config.customOperation) { + config.customOperation.forEach((customOperation) => { + packProps = operations.customOperation(packProps, customOperation); + }); + } + + return filterPackageJson(packProps); +}; diff --git a/src/main.ts b/src/main.ts index f14c3cc..c233717 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,6 @@ import { dirname } from 'path'; import { backup, getPackageJson, restore } from './common/package-utils'; +import { getConfig } from './common/utils'; // setting global variable to the parent directory. global._projectDir = dirname(__dirname); @@ -10,6 +11,12 @@ async function main() { console.log('Backup complete'); await restore(backupPackage); console.log('Restore complete'); + + const config = getConfig(packageJson); + console.log(config); } -main(); +// main(); + +require('dotenv').config({ path: '/custom/path/to/your/.env' }); +console.log(process.env);