-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
165 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const blacklistProperties = [ | ||
'readme', | ||
'readmeFilename', | ||
'gitHead', | ||
'_id', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}, | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters