Skip to content

Commit

Permalink
refactor: semicolons opt
Browse files Browse the repository at this point in the history
  • Loading branch information
aldis-ameriks committed Aug 12, 2023
1 parent 84a83bf commit 14a7f50
Show file tree
Hide file tree
Showing 9 changed files with 2,769 additions and 2,385 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ Usage: pg-typegen [options] <connection>
Options:
-V, --version output the version number
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-e, --exclude <exclude> excluded tables and enums as comma separated string e.g. knex_migrations,knex_migrations_lock (default: [])
--type use type definitions instead of interfaces in generated output (default: false)
--noSemi, --no-semicolons omit semicolons in generated types (default: false)
--semicolons use semicolons in generated types (default: false)
--ssl use ssl (default: false)
--optionals use optionals "?" instead of null (default: false)
--comments generate table and column comments (default: false)
Expand Down
15 changes: 8 additions & 7 deletions cjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const options = {
ssl: { type: 'boolean' },
optionals: { type: 'boolean' },
comments: { type: 'boolean' },
semicolons: { type: 'boolean' },
bigint: { type: 'boolean' },
noSemi: { type: 'boolean' },
'no-semicolons': { type: 'boolean' },
Expand All @@ -35,7 +36,7 @@ const defaultOptions = {
output: 'stdout',
exclude: [],
type: false,
semicolons: true,
semicolons: false,
ssl: false,
optionals: false,
comments: false,
Expand All @@ -52,13 +53,13 @@ const help = `Usage: pg-typegen [options] <connection>
Options:
-V, --version output the version number
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-e, --exclude <exclude> excluded tables and enums as comma separated string e.g. knex_migrations,knex_migrations_lock (default: [])
--type use type definitions instead of interfaces in generated output (default: false)
--noSemi, --no-semicolons omit semicolons in generated types (default: false)
--semicolons use semicolons in generated types (default: false)
--ssl use ssl (default: false)
--optionals use optionals "?" instead of null (default: false)
--comments generate table and column comments (default: false)
Expand Down Expand Up @@ -122,7 +123,7 @@ if (require.main === module) {
output: opts.output,
exclude: opts.exclude ? opts.exclude.split(',').map(e => e.trim()).filter(Boolean) : [],
type: opts.type,
semicolons: !(opts.noSemi === true || opts['no-semicolons'] === true),
semicolons: opts.semicolons === true || !(opts.noSemi === true || opts['no-semicolons'] === true),
ssl: opts.ssl,
optionals: opts.optionals,
comments: opts.comments,
Expand Down
20 changes: 20 additions & 0 deletions cjs/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,26 @@ t.test('generates types with no-semicolons option', t => {
})
})

t.test('generates types with semicolons option', t => {
t.plan(1)

const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--semicolons'], {
cwd: __dirname,
env: process.env,
stdio: ['ignore', 'pipe', 'pipe'],
detached: false
})

const result = { data: '' }
child.stdout.on('data', data => {
result.data += data.toString()
})

child.on('close', () => {
t.matchSnapshot(result.data)
})
})

t.test('generates types with bigint option', t => {
t.plan(1)

Expand Down
15 changes: 8 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const options = {
ssl: { type: 'boolean' },
optionals: { type: 'boolean' },
comments: { type: 'boolean' },
semicolons: { type: 'boolean' },
bigint: { type: 'boolean' },
noSemi: { type: 'boolean' },
'no-semicolons': { type: 'boolean' },
Expand All @@ -36,7 +37,7 @@ const defaultOptions = {
output: 'stdout',
exclude: [],
type: false,
semicolons: true,
semicolons: false,
ssl: false,
optionals: false,
comments: false,
Expand All @@ -53,13 +54,13 @@ const help = `Usage: pg-typegen [options] <connection>
Options:
-V, --version output the version number
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-e, --exclude <exclude> excluded tables and enums as comma separated string e.g. knex_migrations,knex_migrations_lock (default: [])
--type use type definitions instead of interfaces in generated output (default: false)
--noSemi, --no-semicolons omit semicolons in generated types (default: false)
--semicolons use semicolons in generated types (default: false)
--ssl use ssl (default: false)
--optionals use optionals "?" instead of null (default: false)
--comments generate table and column comments (default: false)
Expand Down Expand Up @@ -123,7 +124,7 @@ if (import.meta.url === pathToFileURL(process.argv[1]).href) {
output: opts.output,
exclude: opts.exclude ? opts.exclude.split(',').map(e => e.trim()).filter(Boolean) : [],
type: opts.type,
semicolons: !(opts.noSemi === true || opts['no-semicolons'] === true),
semicolons: opts.semicolons === true || !(opts.noSemi === true || opts['no-semicolons'] === true),
ssl: opts.ssl,
optionals: opts.optionals,
comments: opts.comments,
Expand Down
191 changes: 181 additions & 10 deletions tap-snapshots/cjs/test/cli.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,177 @@ export interface UserEntity {
};
`

exports['cjs/test/cli.js TAP generates types with semicolons option > must match snapshot 1'] = `
export enum DeliciousKebab {
'big-mix' = 'big-mix',
mix = 'mix',
}
export enum Fruits {
apple = 'apple',
banana = 'banana',
orange = 'orange',
}
export enum SnakesOn {
a_plane = 'a_plane',
}
export interface AddressEntity {
id: number;
};
export interface HistoryEntity {
id: number;
};
export interface KebabTestEntity {
id: number;
};
export interface MaterializedItemEntity {
test: number | null;
test_array: Array<number> | null;
test_text: string | null;
test_timestamp: Date | null;
};
export interface MaterializedOtherItemEntity {
test: number | null;
test_text: string | null;
};
export interface SnakeTestEntity {
id: number;
};
export interface SomeViewEntity {
test: number | null;
test_text: string | null;
};
export interface TypeEntity {
avatar_darren: Array<number> | null;
avatar_ernestina: string;
avatar_mark: Array<any> | null;
avatar_myah: Array<string>;
avatar_rozella: Array<string>;
camelCase: boolean | null;
category_amari: string;
category_april: Array<string>;
category_buddy: Array<string> | null;
category_clementine: number | null;
category_marcelle: Date | null;
category_roberta: boolean | null;
category_trent: any | null;
category_viola: string;
comment_cali: string;
comment_delilah: Array<string> | null;
comment_easter: Date | null;
comment_ella: string;
comment_myles: string;
comment_rocio: string | null;
createdat_hulda: any;
createdat_pansy: Array<number> | null;
email_andres: Date;
email_cleveland: string | null;
email_keaton: Array<number> | null;
email_lucio: string | null;
email_paris: string;
email_paula: string | null;
email_ressie: string;
fruit_a: Fruits | null;
fruit_b: Fruits;
group_abigayle: Array<string>;
group_gabe: Array<Date> | null;
group_jay: Array<string>;
group_jedediah: Array<Date>;
group_shanny: Date;
group_toby: string;
group_ulices: Array<string> | null;
id_gay: string;
id_hailee: any;
id_helen: Array<number> | null;
id_ike: Array<string>;
id_joan: Array<string> | null;
id_karelle: string | null;
id_lavern: Date;
id_margarita: string | null;
id_maximilian: Array<string>;
id_william: Array<string>;
id_wilmer: Array<string> | null;
'kebab-a': DeliciousKebab | null;
'kebab-b': DeliciousKebab;
name_amara: string | null;
name_brionna: number;
name_enoch: string;
name_jermain: Array<string> | null;
name_marielle: string | null;
name_myrtle: Array<string> | null;
name_santos: Array<string>;
name_skye: string | null;
name_stephanie: boolean;
password_alessia: Array<string> | null;
password_camylle: Array<string>;
password_elenora: number | null;
password_felton: Array<Date> | null;
password_korey: number;
password_murphy: Array<any> | null;
password_vladimir: number | null;
phone_angelo: string;
phone_colten: number;
phone_erling: Array<string> | null;
phone_johanna: Array<Date> | null;
phone_kendall: Array<number>;
phone_keyshawn: string | null;
phone_maryam: string | null;
phone_osvaldo: Array<Date>;
phone_rupert: any | null;
snakes_on_a: SnakesOn | null;
snakes_on_b: SnakesOn;
status_amalia: number;
status_angelica: Array<number> | null;
status_cade: Array<number>;
status_lori: string | null;
status_ricky: number;
status_sid: Array<boolean> | null;
title_aidan: Date | null;
title_alexzander: string;
title_haylee: Array<string> | null;
title_ilene: Array<any>;
title_vicenta: string | null;
title_vivienne: Array<number>;
token_adella: Array<Date>;
token_hermann: Array<string>;
token_kenyon: Array<string>;
token_marianna: Array<string> | null;
token_rubye: string;
token_ryley: string | null;
token_zora: Array<number>;
updatedat_aaliyah: number | null;
updatedat_abe: Array<string> | null;
updatedat_brett: Array<string> | null;
updatedat_cedrick: Array<boolean>;
updatedat_derick: Array<any>;
updatedat_eli: Array<string>;
updatedat_ewell: Array<string>;
updatedat_laura: Array<string> | null;
updatedat_melody: Array<number>;
updatedat_rossie: number | null;
};
export interface UserEntity {
id: number;
name: string | null;
name2: string;
name3: string;
other_id: number;
other_primary_id: number;
};
`

exports['cjs/test/cli.js TAP generates types with types option > must match snapshot 1'] = `
Expand Down Expand Up @@ -1915,13 +2086,13 @@ Usage: pg-typegen [options] <connection>
Options:
-V, --version output the version number
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-e, --exclude <exclude> excluded tables and enums as comma separated string e.g. knex_migrations,knex_migrations_lock (default: [])
--type use type definitions instead of interfaces in generated output (default: false)
--noSemi, --no-semicolons omit semicolons in generated types (default: false)
--semicolons use semicolons in generated types (default: false)
--ssl use ssl (default: false)
--optionals use optionals "?" instead of null (default: false)
--comments generate table and column comments (default: false)
Expand All @@ -1942,13 +2113,13 @@ Usage: pg-typegen [options] <connection>
Options:
-V, --version output the version number
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-f, --suffix <suffix> suffix to append to generated table type, e.g. item -> ItemEntity (default: "Entity")
-s, --schema <schema> schema (default: "public")
-h, --header <header> header content (default: "")
-o, --output <output> file output path (default: "stdout")
-e, --exclude <exclude> excluded tables and enums as comma separated string e.g. knex_migrations,knex_migrations_lock (default: [])
--type use type definitions instead of interfaces in generated output (default: false)
--noSemi, --no-semicolons omit semicolons in generated types (default: false)
--semicolons use semicolons in generated types (default: false)
--ssl use ssl (default: false)
--optionals use optionals "?" instead of null (default: false)
--comments generate table and column comments (default: false)
Expand Down
Loading

0 comments on commit 14a7f50

Please sign in to comment.