Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade faker dependency and add website type #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
},
"bugs": "https://github.com/rap2hpoutre/pg-anonymizer/issues",
"dependencies": {
"@faker-js/faker": "^6.3.1",
"@oclif/command": "^1.8.16",
"@oclif/config": "^1.18.2",
"@oclif/plugin-help": "^5.1.10",
"@types/faker": "^5.5.9",
"faker": "^5.2.0",
"tslib": "^2.3.1"
},
"devDependencies": {
Expand Down
23 changes: 14 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command, flags } from "@oclif/command";
import { spawn } from "child_process";
const faker = require("faker");
import faker from "@faker-js/faker";
const fs = require("fs");
const path = require("path");
const readline = require("readline");
Expand Down Expand Up @@ -33,7 +33,8 @@ class PgAnonymizer extends Command {
help: flags.help({ char: "h" }),
list: flags.string({
char: "l",
description: "[default: email,name,description,address,city,country,phone,comment,birthdate] list of columns to anonymize",
description:
"[default: email,name,description,address,city,country,phone,comment,birthdate,website] list of columns to anonymize",
}),
configFile: flags.string({
char: "c",
Expand Down Expand Up @@ -83,20 +84,22 @@ class PgAnonymizer extends Command {
pg.stdout.setEncoding("utf8");

if (!(flags.list || flags.configFile)) {
flags.list = "email,name,description,address,city,country,phone,comment,birthdate";
flags.list =
"email,name,description,address,city,country,phone,comment,birthdate";
}

let list: { col: string; replacement: string | null; }[];
let list: { col: string; replacement: string | null }[];
if (flags.configFile) {
list = fs.readFileSync(flags.configFile, "utf8")
list = fs
.readFileSync(flags.configFile, "utf8")
.split(/\r?\n/)
.map((l: string) => l.trim())
.map((l: string) => {
if (l === "") return null;
if (l.startsWith("#")) return null;
return {
col: l.replace(/:(?:.*)$/, "").toLowerCase(),
replacement: l.includes(":") ? l.replace(/^(?:.*):/, "") : null
replacement: l.includes(":") ? l.replace(/^(?:.*):/, "") : null,
};
})
.filter(Boolean);
Expand Down Expand Up @@ -154,7 +157,7 @@ class PgAnonymizer extends Command {
cols.filter((v, k) => indices.includes(k)).join(", ")
);
else console.error("No columns to anonymize");
} else if (table && line.trim() && (line !== "\\.")) {
} else if (table && line.trim() && line !== "\\.") {
line = line
.split("\t")
.map((v, k) => {
Expand All @@ -172,8 +175,8 @@ class PgAnonymizer extends Command {
const [_one, two, three] = replacement.split(".");
if (!(two && three)) return replacement;
if (two === "date")
return postgreSQLDate(faker.date[three]());
return faker[two][three]();
return postgreSQLDate((faker.date as any)[three]());
return (faker as any)[two][three]();
}
if (replacement.startsWith("extension.")) {
const functionPath = replacement.split(".");
Expand All @@ -193,6 +196,8 @@ class PgAnonymizer extends Command {
if (cols[k] === "city") return faker.address.city();
if (cols[k] === "country") return faker.address.country();
if (cols[k] === "phone") return faker.phone.phoneNumber();
if (cols[k] === "website" || cols[k] === "url")
return faker.internet.url();
if (cols[k] === "comment") return faker.random.words(3);
if (cols[k] === "birthdate")
return postgreSQLDate(faker.date.past());
Expand Down
Loading