Skip to content

Commit 13033fa

Browse files
author
Will Vedder
committed
Merge branch 'master' of https://github.com/auth0/auth0-deploy-cli into applying-prettier-existing-code
2 parents b0e7072 + 39c48c8 commit 13033fa

File tree

15 files changed

+388
-277
lines changed

15 files changed

+388
-277
lines changed

src/args.ts

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
import yargs from 'yargs';
2+
import { Config } from './types'
23

3-
function getParams() {
4+
type SharedParams = {
5+
_: ['export' | 'import' | 'deploy' | 'dump']
6+
proxy_url?: string
7+
debug: boolean
8+
config_file?: string
9+
env: boolean
10+
format: 'yaml' | 'directory'
11+
secret?: string
12+
base_path?: string // Necessary when package imported as Node module
13+
config?: Partial<Config>,
14+
}
15+
16+
type ImportSpecificParams = {
17+
input_file: string
18+
}
19+
20+
type ExportSpecificParams = {
21+
output_folder: string
22+
export_ids: boolean
23+
}
24+
25+
export type ExportParams = ExportSpecificParams & SharedParams
26+
export type ImportParams = ImportSpecificParams & SharedParams
27+
28+
function getParams(): ExportParams | ImportParams {
429
const args = yargs
530
.demandCommand(1, 'A command is required')
631
.usage('Auth0 Deploy CLI')
@@ -15,7 +40,7 @@ function getParams() {
1540
describe: 'A url for proxying requests, only set this if you are behind a proxy.',
1641
type: 'string'
1742
})
18-
.command([ 'import', 'deploy' ], 'Deploy Configuration', {
43+
.command(['import', 'deploy'], 'Deploy Configuration', {
1944
input_file: {
2045
alias: 'i',
2146
describe: 'The updates to deploy. Either a JSON file, or directory that contains the correct file layout. See README and online for more info.',
@@ -38,7 +63,7 @@ function getParams() {
3863
type: 'string'
3964
}
4065
})
41-
.command([ 'export', 'dump' ], 'Export Auth0 Tenant Configuration', {
66+
.command(['export', 'dump'], 'Export Auth0 Tenant Configuration', {
4267
output_folder: {
4368
alias: 'o',
4469
describe: 'The output directory.',
@@ -49,7 +74,7 @@ function getParams() {
4974
alias: 'f',
5075
describe: 'The output format.',
5176
type: 'string',
52-
choices: [ 'yaml', 'directory' ],
77+
choices: ['yaml', 'directory'],
5378
demandOption: true
5479
},
5580
config_file: {
@@ -84,6 +109,7 @@ function getParams() {
84109
.example('$0 deploy -c config.json -i path/to/files', 'Deploy Auth0 via Path')
85110
.epilogue('See README (https://github.com/auth0/auth0-deploy-cli) for more in-depth information on configuration and setup.')
86111
.wrap(null);
112+
//@ts-ignore because we know these types are either ExportParams or ImportParams. TODO: fix more native way of inferring types from yargs
87113
return args.argv;
88114
}
89115

src/commands/export.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@ import log from '../logger';
66
import { isDirectory } from '../utils';
77
import { setupContext } from '../context/index';
88
import { Config } from '../types'
9-
10-
type ExportParams = {
11-
output_folder: string,
12-
base_path?: string,
13-
config_file: string,
14-
config?: Partial<Config>,
15-
export_ids: boolean,
16-
secret?: string
17-
format: 'yaml' | 'directory'
18-
debug: boolean
19-
env: boolean
20-
}
9+
import { ExportParams } from '../args'
2110

2211
export default async function exportCMD(params: ExportParams) {
2312
const {
@@ -38,7 +27,7 @@ export default async function exportCMD(params: ExportParams) {
3827
nconf.file(configFile);
3928
}
4029

41-
const overrides: Partial<Config> = {
30+
const overrides: Partial<Config> = {
4231
AUTH0_INPUT_FILE: outputFolder,
4332
AUTH0_BASE_PATH: basePath,
4433
...configObj || {}

src/commands/import.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@ import { configFactory } from '../configFactory';
33
import { deploy as toolsDeploy } from '../tools';
44
import log from '../logger';
55
import { setupContext } from '../context';
6-
import { Config } from '../types'
7-
8-
type ImportParams = {
9-
input_file: string,
10-
base_path: string,
11-
config_file: string,
12-
env: boolean,
13-
secret: string,
14-
config?: Partial<Config>,
15-
}
6+
import { ImportParams } from '../args'
167

178
export default async function importCMD(params: ImportParams) {
189
const {

src/context/directory/handlers/tenant.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from 'path';
22
import {
3-
existsMustBeDir, isFile, dumpJSON, loadJSON, hoursAsInteger, clearTenantFlags
3+
existsMustBeDir, isFile, dumpJSON, loadJSON, clearTenantFlags
44
} from '../../../utils';
5+
import { sessionDurationsToMinutes } from '../../../sessionDurationsToMinutes'
56
import { DirectoryHandler } from '.'
67
import DirectoryContext from '..'
78

@@ -25,18 +26,22 @@ function parse(context: DirectoryContext): ParsedTenant {
2526
session_lifetime,
2627
idle_session_lifetime,
2728
...tenant
29+
}: {
30+
session_lifetime?: number
31+
idle_session_lifetime?: number,
32+
[key: string]: any
2833
} = loadJSON(tenantFile, context.mappings);
2934

3035
clearTenantFlags(tenant);
3136

37+
const sessionDurations = sessionDurationsToMinutes({ session_lifetime, idle_session_lifetime })
38+
3239
return {
3340
tenant: {
3441
...tenant,
35-
session_lifetime_in_minutes: hoursAsInteger('session_lifetime', session_lifetime)['session_lifetime_in_minutes'],
36-
idle_session_lifetime_in_minutes: hoursAsInteger('idle_session_lifetime', idle_session_lifetime)['idle_session_lifetime_in_minutes'],
37-
}
42+
...sessionDurations
43+
},
3844
};
39-
/* eslint-enable camelcase */
4045
}
4146

4247
return {};

src/context/yaml/handlers/tenant.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { hoursAsInteger, clearTenantFlags } from '../../../utils';
1+
import { clearTenantFlags } from '../../../utils';
2+
import { sessionDurationsToMinutes } from '../../../sessionDurationsToMinutes'
23
import { YAMLHandler } from '.'
34
import YAMLContext from '..'
45

@@ -15,18 +16,22 @@ async function parse(context: YAMLContext): Promise<ParsedTenant> {
1516
session_lifetime,
1617
idle_session_lifetime,
1718
...tenant
19+
}: {
20+
session_lifetime?: number
21+
idle_session_lifetime?: number,
22+
[key: string]: any
1823
} = context.assets.tenant;
1924

2025
clearTenantFlags(tenant);
2126

27+
const sessionDurations = sessionDurationsToMinutes({ session_lifetime, idle_session_lifetime })
28+
2229
return {
23-
tenant: Object.assign(
24-
tenant,
25-
session_lifetime && hoursAsInteger('session_lifetime', session_lifetime),
26-
idle_session_lifetime && hoursAsInteger('idle_session_lifetime', idle_session_lifetime)
27-
)
30+
tenant: {
31+
...tenant,
32+
...sessionDurations
33+
},
2834
};
29-
/* eslint-enable camelcase */
3035
}
3136

3237
async function dump(context: YAMLContext): Promise<ParsedTenant> {

src/index.js src/index.ts

+30-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#!/usr/bin/env node
22
import { bootstrap } from 'global-agent';
33

4-
import { getParams } from './args';
5-
import commands from './commands';
4+
import { getParams, ExportParams, ImportParams } from './args';
65
import log from './logger';
76
import tools from './tools';
7+
import { Stage } from './tools/auth0'
88

9-
async function run(params) {
9+
import importCMD from './commands/import'
10+
import exportCMD from './commands/export'
11+
12+
async function run(params: ImportParams | ExportParams): Promise<void> {
1013
// Run command
11-
const cmd = commands[params._[0]];
14+
const command = params._[0];
15+
1216
const proxy = params.proxy_url;
1317

1418
if (proxy) {
@@ -23,9 +27,14 @@ async function run(params) {
2327
bootstrap();
2428
}
2529

26-
log.debug(`Start command ${params._[0]}`);
27-
await cmd(params);
28-
log.debug(`Finished command ${params._[0]}`);
30+
log.debug(`Start command ${command}`);
31+
if (['deploy', 'import'].includes(command) && 'input_file' in params) {
32+
await importCMD(params)
33+
}
34+
if (['dump', 'export'].includes(command) && 'output_folder' in params) {
35+
await exportCMD(params)
36+
}
37+
log.debug(`Finished command ${command}`);
2938
}
3039

3140
// Only run if from command line
@@ -35,8 +44,6 @@ if (require.main === module) {
3544

3645
log.debug('Starting Auth0 Deploy CLI Tool');
3746

38-
// Set log level
39-
log.transports.console.level = params.level;
4047
if (params.debug) {
4148
log.transports.console.level = 'debug';
4249
// Set for tools
@@ -46,17 +53,23 @@ if (require.main === module) {
4653

4754
run(params)
4855
.then(() => process.exit(0))
49-
.catch((error) => {
56+
.catch((error: {
57+
type?: string
58+
stage?: Stage
59+
message?: string
60+
stack?: string
61+
}) => {
62+
const command = params._[0]
5063
if (error.type || error.stage) {
51-
log.error(`Problem running command ${params._[0]} during stage ${error.stage} when processing type ${error.type}`);
64+
log.error(`Problem running command ${command} during stage ${error.stage} when processing type ${error.type}`);
5265
} else {
53-
log.error(`Problem running command ${params._[0]}`);
66+
log.error(`Problem running command ${command}`);
5467
}
5568

5669
const msg = error.message || error.toString();
5770
log.error(msg);
5871

59-
if (process.env.AUTH0_DEBUG === 'true') {
72+
if (process.env.AUTH0_DEBUG === 'true' && error.stack) {
6073
log.debug(error.stack);
6174
}
6275

@@ -69,9 +82,9 @@ if (require.main === module) {
6982

7083
// Export commands to be used programmatically
7184
module.exports = {
72-
deploy: commands.import,
73-
dump: commands.export,
74-
import: commands.import,
75-
export: commands.export,
85+
deploy: importCMD,
86+
dump: exportCMD,
87+
import: importCMD,
88+
export: exportCMD,
7689
tools: tools
7790
};

src/sessionDurationsToMinutes.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function hoursToMinutes(hours: number): number {
2+
return Math.round(hours * 60)
3+
}
4+
5+
export const sessionDurationsToMinutes = (
6+
{ session_lifetime, idle_session_lifetime }: { session_lifetime?: number, idle_session_lifetime?: number }): { session_lifetime_in_minutes?: number, idle_session_lifetime_in_minutes?: number } => {
7+
const sessionDurations: {
8+
session_lifetime_in_minutes?: number
9+
idle_session_lifetime_in_minutes?: number
10+
} = {};
11+
12+
if (!!session_lifetime) sessionDurations.session_lifetime_in_minutes = hoursToMinutes(session_lifetime)
13+
if (!!idle_session_lifetime) sessionDurations.idle_session_lifetime_in_minutes = hoursToMinutes(idle_session_lifetime)
14+
15+
return sessionDurations
16+
}

src/tools/auth0/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import { Assets, Auth0APIClient } from '../../types'
88
import APIHandler from './handlers/default';
99
import { ConfigFunction } from '../../configFactory';
1010

11-
12-
13-
type Stage = 'load' | 'validate' | 'processChanges'
11+
export type Stage = 'load' | 'validate' | 'processChanges'
1412

1513
type StageFunction = APIHandler['load'] // Using `load` method as a template for what type stage functions resemble
1614

0 commit comments

Comments
 (0)