Skip to content

Commit

Permalink
adding main operations
Browse files Browse the repository at this point in the history
  • Loading branch information
NAMEER242 committed Feb 17, 2024
1 parent d8362df commit 010bd4e
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 2 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions package.backup
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -53,4 +62,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
3 changes: 3 additions & 0 deletions repackify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
add: { test: 'test' },
};
7 changes: 7 additions & 0 deletions src/common/dto/config.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class ConfigDto {
remove: string[];
replace: Record<string, any>[];
add: Record<string, any>[];
replaceText: { text: string; replace: string }[];
customOperation: ((record: Record<string, any>) => Record<string, any>)[];
}
1 change: 1 addition & 0 deletions src/common/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './config.dto';
26 changes: 26 additions & 0 deletions src/common/utils/get-config-object.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>,
): ConfigDto | null => {
if (Object.keys(record).includes('repackify')) {
return record['repackify'];
}
return null;
};

export const getConfig = (record: Record<string, any>): ConfigDto | null => {
const jsConfig = getConfigFromJSFile();
const packageJsonConfig = getConfigFromRecord(record);
return { ...packageJsonConfig, ...jsConfig };
};
1 change: 1 addition & 0 deletions src/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './get-config-object';
52 changes: 52 additions & 0 deletions src/features/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { filterPackageJson } from '../common/package-utils';

export const add = (
packProps: Record<string, any>,
props: Record<string, any>,
): Record<string, any> => {
for (const key in props) {
if (!(key in packProps)) {
packProps[key] = props[key];
}
}
return filterPackageJson(packProps);
};

export const remove = (
packProps: Record<string, any>,
prop: string,
): Record<string, any> => {
if (prop in packProps) {
delete packProps[prop];
}
return packProps;
};

export const replace = (
packProps: Record<string, any>,
props: Record<string, any>,
): Record<string, any> => {
for (const key in props) {
if (key in packProps) {
packProps[key] = props[key];
}
}
return packProps;
};

export const replaceText = (
packProps: Record<string, any>,
text: string,
replace: string,
): Record<string, any> => {
let resultString = JSON.stringify(packProps);
resultString = resultString.replaceAll(text, replace);
return JSON.parse(resultString);
};

export const customOperation = (
packProps: Record<string, any>,
operation: (packProps: Record<string, any>) => Record<string, any>,
): Record<string, any> => {
return operation(packProps);
};
40 changes: 40 additions & 0 deletions src/features/operationsRunner.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>,
config: ConfigDto,
): Record<string, any> => {
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);
};
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);

0 comments on commit 010bd4e

Please sign in to comment.