Skip to content

Commit

Permalink
adding package-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
NAMEER242 committed Feb 16, 2024
1 parent 63760ac commit e454317
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs → .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.cjs'],
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand Down
56 changes: 56 additions & 0 deletions package.backup
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "repackify",
"version": "0.0.1",
"description": "___",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "ts-node src/main.ts",
"start:prod": "npm run build && node lib/main.js",
"build": "tsc",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix"
},
"dependencies": {
"read-package-json": "^7.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "repackify",
"version": "0.0.1",
"description": "",
"description": "___",
"author": "",
"private": true,
"license": "UNLICENSED",
"type": "module",
"scripts": {
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "ts-node src/main.ts",
"start:prod": "npm run build && node lib/main.js",
"build": "tsc",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix"
},
"dependencies": {
Expand Down Expand Up @@ -52,4 +53,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
16 changes: 16 additions & 0 deletions src/common/package-utils/backup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as fs from 'fs';
import { promisify } from 'util';
import { filterPackageJson } from './filter-package-json';

const writeFile = promisify(fs.writeFile);

export const backup = async (
record: Record<string, any>,
backupName = 'package.backup',
) => {
record = filterPackageJson(record);
const data = JSON.stringify(record, null, 2);
await writeFile(`${global._projectDir}/${backupName}`, data);

return record;
};
6 changes: 6 additions & 0 deletions src/common/package-utils/blacklist-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const blacklistProperties = [
'readme',
'readmeFilename',
'gitHead',
'_id',
];
11 changes: 11 additions & 0 deletions src/common/package-utils/filter-package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { blacklistProperties } from './blacklist-properties';

export const filterPackageJson = (record: Record<string, any>) => {
for (const key in record) {
if (blacklistProperties.includes(key)) {
delete record[key];
}
}

return record;
};
21 changes: 21 additions & 0 deletions src/common/package-utils/get-package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const readJson = require('read-package-json');

export const getPackageJson = (packageName = 'package.json') => {
return new Promise<Record<string, any>>((resolve, reject) => {
const packageJsonPath = `${global._projectDir}/${packageName}`;

readJson(
packageJsonPath,
console.error,
false,
(er: NodeJS.ErrnoException, data: Record<string, any>) => {
if (er) {
reject(er);
} else {
resolve(data);
}
},
);
});
};
5 changes: 5 additions & 0 deletions src/common/package-utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './get-package-json';
export * from './backup';
export * from './restore';
export * from './blacklist-properties';
export * from './filter-package-json';
11 changes: 11 additions & 0 deletions src/common/package-utils/read-json.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare module 'read-package-json' {
type Callback = (err: Error | null, data?: any) => void;
type LogFunction = (...args: any[]) => void;

export default function readJson(
file: string,
log_?: LogFunction,
strict_?: boolean,
cb_?: Callback,
);
}
14 changes: 14 additions & 0 deletions src/common/package-utils/restore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as fs from 'fs';
import { promisify } from 'util';
import { filterPackageJson } from './filter-package-json';

const writeFile = promisify(fs.writeFile);

export const restore = async (
record: Record<string, any>,
packageName = 'package.json',
) => {
record = filterPackageJson(record);
const data = JSON.stringify(record, null, 2);
await writeFile(`${global._projectDir}/${packageName}`, data);
};
29 changes: 13 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import readJson from 'read-package-json';
import path, { dirname } from 'path';
import { dirname } from 'path';
import { backup, getPackageJson, restore } from './common/package-utils';

const __dirname = path.resolve(dirname('./'), 'src');
const parentDir = path.dirname(__dirname);
readJson(
`${parentDir}/package.json`,
console.error,
false,
function (er: NodeJS.ErrnoException, data: Record<string, any>) {
if (er) {
console.error(er);
return;
}
// setting global variable to the parent directory.
global._projectDir = dirname(__dirname);

console.error('the package data is', data);
},
);
async function main() {
const packageJson = await getPackageJson();
const backupPackage = await backup(packageJson);
console.log('Backup complete');
await restore(backupPackage);
console.log('Restore complete');
}

main();
17 changes: 7 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
{
"ts-node": {
"transpileOnly": true,
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"esModuleInterop": true
},
"esm": true
},
"compilerOptions": {
"module": "commonjs",
"declaration": true,
Expand All @@ -17,7 +8,7 @@
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"outDir": "./lib",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
Expand All @@ -26,5 +17,11 @@
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
},
"ts-node": {
"esm": true,
"compilerOptions": {
"module": "nodenext",
}
}
}

0 comments on commit e454317

Please sign in to comment.