generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
628 lines (544 loc) · 24.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
**
** Copyright (c) 2024, Oracle and/or its affiliates.
** All rights reserved
** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
*/
import { input, password, select } from '@inquirer/prompts';
import { Args, Command, Flags } from '@oclif/core';
import { exec } from 'node:child_process';
import fs, { constants } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { promisify } from 'node:util';
import untildify from 'untildify';
import { generateDatabaseApp } from '../generators/run-generator.js';
const __filename = fileURLToPath( import.meta.url );
const __dirname = path.dirname( __filename );
/* eslint-disable perfectionist/sort-objects */
// The reason why we need extract-zip is because zlib doesn't support .zip formats and can only decompress 1 file rather than
// an archive. extract-zip seems to be the better option.
import extract from 'extract-zip';
let walletDirectory:string;
// For some reason, using createReadStream causes the inquirer to bug a bit.
// Function to retrieve the connection details from the ora file.
const retrieveConnectionStringDetailsFromORAFile = ( oraFilePath: string ) => {
const data = fs.readFileSync( oraFilePath, 'utf8' );
const protocol = data.slice( ( data.indexOf( 'protocol=' ) + 9 ), ( data.indexOf( ')(port' ) ) );
const hostname = data.slice( ( data.indexOf( 'host=' ) + 5 ), ( data.indexOf( '))(' ) ) );
const port = data.slice( ( data.indexOf( 'port=' ) + 5 ), ( data.indexOf( ')(host' ) ) );
const serviceName = data.slice( ( data.indexOf( 'service_name=' ) + 13 ), ( data.indexOf( '))(security' ) ) );
return { protocol, hostname, port, serviceName };
};
const generateConnectionString = ( protocol: string, hostname: string, port: string, serviceName: string ) => `${ protocol }://${ hostname }:${ port }/${ serviceName }`;
// Function to check if the supplied path is that of a Directory or a Zip file.
const checkIfDirectoryOrZip = ( directoryOrZip: string ) => {
let isDirectoryOrZip = true;
const resolvedPath = untildify( directoryOrZip );
if ( resolvedPath.length === 0 ) {
return 'This field cannot be empty!';
}
try {
fs.accessSync( resolvedPath, constants.R_OK );
} catch {
return 'WARNING: File access not permitted.';
}
if ( resolvedPath.slice( resolvedPath.trim().lastIndexOf( '.' ) ) === '.zip' ) {
try {
isDirectoryOrZip = fs.lstatSync( resolvedPath ).isFile();
} catch ( error: any ) {
return error.message;
}
} else {
try {
isDirectoryOrZip = fs.lstatSync( resolvedPath ).isDirectory();
if ( fs.readdirSync( resolvedPath ).filter( file => file.includes( '.sso' ) ).length === 0 ) {
return 'This is not a Wallet file, it does not contain the sso files!';
}
retrieveConnectionStringDetailsFromORAFile( path.join( resolvedPath, 'tnsnames.ora' ) );
} catch ( error: any ) {
return error.message;
}
}
return isDirectoryOrZip;
};
const unzipWalletFile = async ( walletZip: string ) => {
const downloadPath = path.normalize( walletZip );
if ( downloadPath.slice( walletZip.lastIndexOf( '.' ) ) === '.zip' ) {
try {
const destination = path.normalize( path.join( walletDirectory, path.basename( walletZip, '.zip' ) ) );
fs.mkdirSync( destination );
await extract( downloadPath, { dir: destination } );
} catch ( error ) {
console.log( error );
return false;
}
return true;
}
return false;
};
// Function to get the wallets that are within the wallet directory (/database)
const getWalletFiles = async ( givenPath?: string ) => {
const walletFiles: string[] = [];
if ( !givenPath ) return;
const downloadPath = path.normalize( givenPath );
if ( await unzipWalletFile( downloadPath ) ) {
walletFiles.push( downloadPath );
} else if ( checkIfDirectoryOrZip( givenPath ) ) {
fs.readdirSync( givenPath ).some( file => {
if ( file.slice( file.lastIndexOf( '.' ) ) === '.sso' ) {
walletFiles.push( givenPath );
return true;
}
return false;
} );
}
return walletFiles;
};
// Create the wallet directory (/database) in the working directory where the process was invoked.
const createWalletDirectory = ( walletDirectoryPath: string ) => {
const walletDestination = path.normalize( path.join( '.', 'database' ) );
if ( !fs.existsSync( walletDestination ) ) fs.mkdirSync( walletDestination );
const pasteDirectory = path.normalize( path.join( walletDestination, path.basename( walletDirectoryPath, '.zip' ) ) );
try {
// Check if we have read access to the supplied path
fs.accessSync( walletDirectoryPath, constants.R_OK );
} catch {
console.log( '\u001B[33m%s\u001B[0m', `WARNING: ${ walletDirectoryPath } File access not permitted. We couldn't copy the wallet file into the projects folder. Please run the following command to do so: \n cp ${ walletDirectoryPath } ./<YOUR_PROJECT>/utils/db/<WALLET_UNZIPPED_FOLDER> ` );
}
// If it's a zip file, the we unzip it.
if ( walletDirectoryPath.includes( '.zip' ) ) {
unzipWalletFile( walletDirectoryPath );
} else {
// Otherwise, it's the unzipped wallet directory, since we already check whether sso files are inside of the directory first.
fs.mkdirSync( pasteDirectory );
fs.cpSync( walletDirectoryPath, pasteDirectory, { recursive: true } );
}
return path.join( walletDestination, path.basename( walletDirectoryPath, '.zip' ) );
};
type Choice = {
name: string,
value: string,
};
// In the case where there's multiple wallets in the wallet directory, this function creates the choices that are needed
// by inquirer so that the user can pick which wallet to use.
const getWalletFileChoices = async () => {
const wallets = await getWalletFiles();
if ( wallets === undefined ) return [];
const choices: Choice[] = [];
for ( const wallet of wallets ) {
choices.push( {
name: wallet,
value: path.normalize( path.join( '/database', wallet ) ),
} );
}
return choices;
};
// Prompts
const promptWalletPath = async () => {
console.log( '\u001B[33m%s\u001B[0m', 'In order to unzip and copy the Wallet file, please ensure that you have file access permissions. \n' );
return await input(
{
// TODO: Cannot use tab to autocomplete, cannot use ~ for $HOME
message: 'Please enter the path of your Oracle Wallet:',
validate ( input ) {
return checkIfDirectoryOrZip( input );
}
}
);
}
// Here we promisify the exec function from child_process to use it with async/await
const execAsync = promisify( exec );
export default class Generate extends Command {
static args = {
name: Args.string( { name:'name', required: false, description: 'Name of the project' } ),
};
static description = 'generate template';
static examples = [
'<%= config.bin %> <%= command.id %>',
];
static flags = {
'template': Flags.string({
char: 't',
description: 'Template to use',
options: ['node-vanilla', 'node-react', 'node-vue', 'node-react-todo', 'node-jet', 'node-angular', 'ords-remix-jwt-sample', 'mle-js-basic'],
multiple: false
}),
'connection-type': Flags.string({
char: 'c',
description: 'Connection type',
options:['basic', 'walletPath'],
multiple: false
}),
'db-username': Flags.string({
description: 'Database username',
multiple: false
}),
// basic connection flags
'db-protocol': Flags.string({
description: 'Database protocol',
multiple: false,
relationships:[{
type:'none', flags:[
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'}
]
}
]}),
'db-hostname': Flags.string( {
description: 'Database host name',
multiple: false,
relationships:[{
type:'none', flags:[
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'}
]
}]
}),
'db-port': Flags.integer({
description: 'Database port number',
max: 65535,
min: 0,
multiple: false,
relationships:[{
type:'none', flags:[
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'}
]
}
]
}),
'db-service-type': Flags.string({
description: 'Database service type. Only can be sid or service name',
options: ['sid', 'serviceName'],
multiple: false,
relationships:[{
type:'none', flags:[
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'}
]
}
]
}),
'db-sid': Flags.string({
description: 'Database service ID, only needed if service type was sid',
multiple: false,
relationships:[{
type:'none', flags:[
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'},
{name: 'db-service-type', when: async (flags) => flags['db-service-type'] === 'serviceName'}
]
}
]
}),
'db-service-name': Flags.string({
description: 'Database service name, only needed if service type was serviceName',
multiple: false,
relationships:[{
type:'none', flags:[
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'walletPath'},
{name: 'db-service-type', when: async (flags) => flags['db-service-type'] === 'sid'}
]
}
]
}),
'wallet-path': Flags.string({
description: 'Cloud wallet path',
dependsOn: ['connection-type'],
multiple: false,
relationships:[{
type:'none', flags:[
{name: 'connection-type', when: async (flags) => flags['connection-type'] === 'basic'}
]
}
]
})
};
public async run (): Promise<void> {
const { args, flags } = await this.parse( Generate );
const name = args.name ?? '';
const template = flags['template'] ?? '';
const connectionType = flags['connection-type'] ?? '';
const databaseProtocol = flags['db-protocol'] ?? '';
const databaseHostName = flags['db-hostname'] ?? '';
const databasePort = flags['db-port']?.toString() ?? '';
const databaseServiceType = flags['db-service-type'] ?? '';
const databaseSID = flags['db-sid'] ?? '';
const databaseServiceName = flags['db-service-name'] ?? '';
const databaseUsername = flags['db-username'] ?? '';
const sqlclPath = flags['sql-cl'] ?? '';
// TODO: Validate and use wallet path
const walletPathDirectory = flags['wallet-path'] ? flags['wallet-path'] : '';
let walletPathValidationResult = undefined;
if (walletPathDirectory !== ''){
walletPathValidationResult = checkIfDirectoryOrZip(walletPathDirectory);
}
// Ask the user for the application name.
const appName = name === '' ? await input(
{
message: 'What would you like your application\'s name to be?',
validate ( input ) {
return /^([\w\\-])+$/.test( input ) ? true : 'Project name may only include letters, numbers, underscores and hashes.';
},
},
) : name;
// Ask the user to choose the template that he wants.
const templateChoice = template === '' ? await select(
{
message: 'Which template would you like to use for your project?',
choices: [
{
name: 'node-vanilla',
value: 'node-vanilla',
description: 'This creates an empty vanilla javascript project and Oracle database connection starter code.',
},
{
name: 'node-react',
value: 'node-react',
description: 'This creates an empty project with React and Oracle database connection starter code.',
},
{
name: 'node-vue',
value: 'node-vue',
description: 'This creates an empty project with Vue and Oracle database connection starter code.',
},
{
name: 'node-jet',
value: 'node-jet',
description: 'This creates an empty project with Oracle JET and Oracle database connection starter code.',
},
{
name: 'node-angular',
value: 'node-angular',
description: 'This creates an empty project with Angular and Oracle database connection starter code.',
},
{
name: 'node-react-todo',
value: 'node-react-todo',
description: 'This creates a simple Todo app made with ExpressJS as the backend, React as the frontend, and an Oracle Database connection that will be created from the details you provide later...',
},
{
name: 'ords-remix-jwt-sample',
value: 'ords-remix-jwt-sample',
description: 'This creates a fullstack Concert Application made with Remix that leverages the Oracle REST Data Services functionalities. You will need to configurate the application yourself following the getting started guide.',
},
{
name: 'mle-js-basic',
value: 'mle-js-basic',
description: 'This creates an empty project with MLE and Oracle database connection starter code.'
},
],
default: 'node-vanilla'
},
) : template;
// This represents the config object that will hold all the information that the user has inputted and selected.
let configObject = {
appName,
templateChoice: path.resolve( path.join( __dirname, '..', '..', 'templates', templateChoice ) ),
};
// Ask the user for the database connection type (Either basic connection or a connection using a cloud wallet).
const databaseConnectionType = connectionType === '' && templateChoice !== 'ords-remix-jwt-sample' ? await select(
{
message: 'Which database connection type would you like to choose?',
choices: [
{
name: 'Cloud Wallet Path',
value: 'walletPath'
},
{
name: 'Basic Connection (Protocol, Hostname, Port, Service Name / SID)',
value: 'basic',
},
],
default: 'walletPath'
}
) : connectionType;
// If the user has chosen the basic connection type, then we ask for the protocol, hostname, port and service name / SID.
if ( databaseConnectionType === 'basic' ) {
const protocol = databaseProtocol === '' ? await input(
{
message: 'What is your database protocol?',
default: 'tcp'
},
) : databaseProtocol;
const hostname = databaseHostName === '' ? await input(
{
message: 'What is your database hostname?',
default: 'localhost'
},
) : databaseHostName;
const port = databasePort === '' ? await input(
{
message: 'What is your database port?',
validate ( input ) {
return !isNaN( Number.parseInt( input ) ) && isFinite( Number.parseInt( input ) ) ? true : 'Port can only be numbers!';
},
default: '1521'
},
) : databasePort;
const serviceType = databaseServiceType === '' ? await select(
{
message: 'Which service type would you like to use?',
choices: [
{
name: 'SID',
value: 'sid',
},
{
name: 'Service name',
value: 'serviceName'
}
]
}
) : databaseServiceType;
let serviceValue;
if (serviceType === 'sid') {
serviceValue = databaseSID === '' ? await input(
{
message: 'Please enter your database SID: ',
validate ( input ) {
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
}
}
) : databaseSID;
} else {
serviceValue = databaseServiceName === '' ? await input(
{
message: 'Please enter your database service name: ',
validate ( input ) {
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
}
}
) : databaseServiceName;
}
// This will be config object for the basic connection type.
Object.assign(configObject, {
connectionString: generateConnectionString( protocol, hostname, port, serviceValue )
});
} else if( databaseConnectionType === 'walletPath' ) {
let walletPath = '';
if(walletPathValidationResult === true){
walletPath = walletPathDirectory;
}
else{
if(walletPathValidationResult !== undefined){
console.error('\u001B[31m%s\u001B[0m', walletPathValidationResult);
}
walletPath = await promptWalletPath();
}
// walletPath = createWalletDirectory( walletPath );
walletPath = path.resolve( untildify( walletPath ) );
walletDirectory = path.dirname( walletPath );
const walletPassword = await password(
{
mask: true,
message: 'Please enter your wallet password:',
validate ( input ) {
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
}
}
);
// This is the config object that represents the wallet connection type.
Object.assign(configObject, {
walletPath: walletPath,
walletPassword: walletPassword
});
}
if(templateChoice !== 'ords-remix-jwt-sample'){
// Ask the user for the database connection username.
Object.assign( configObject, {
connectionUsername: databaseUsername === '' ? await input(
{
message: 'What\'s your database username?',
validate ( input ) {
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
}
},
) : databaseUsername
} );
// Ask the user for the database connection password.
Object.assign( configObject, {
connectionPassword: await password(
{
mask: true,
message: 'What\'s your database password?',
validate ( input ) {
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
}
},
)
} );
}
if(templateChoice == 'mle-js-basic'){
// Ask the user for the path to SQLcl
Object.assign( configObject, {
sqlclPath: sqlclPath === '' ? await input(
{
message: 'Please provide full path to your SQLcl installation: ',
validate ( input ) {
return input.trim().length === 0 ? 'This field cannot be empty!' : true;
}
},
) : sqlclPath
});
}
generateDatabaseApp( configObject );
// TODO: This is the object that holds the application name, template choice, connection details depending on the chosen connection type.
// console.log( JSON.stringify( configObject, null, ' ' ) );
// const emptyReactPath = path.join( 'src', 'templates', 'empty-templates', 'empty-react-app' );
// const emptyVanillaPath = path.join( 'src', 'templates', 'empty-templates', 'empty-vanilla-js-app' );
// const emptyVuePath = path.join( 'src', 'templates', 'empty-templates', 'empty-vue-app' );
// const reactToDoApp = path.join( 'src', 'templates', 'node-react-todo' );
//
//
// switch ( templateChoice ) {
// case 'node-react':
// await this.runNodeAppProject( emptyReactPath );
// break;
// case 'node-vue':
// await this.runNodeAppProject( emptyVuePath );
// break;
// case 'node-vanilla':
// await this.runNodeAppProject( emptyVanillaPath );
// break;
// case 'node-react-todo':
// await this.runNodeAppProject( reactToDoApp );
// break;
//
// default:
// this.runNodeAppProject( emptyReactPath );
//
// }
}
private async runNodeAppProject ( projectPath: string ): Promise<void> {
try {
const serverPath = path.join( 'src', 'templates', 'app' );
// First install dependencies and run the server
this.log( `Installing server dependencies in ${ serverPath }...` );
await execAsync( 'npm install', { cwd: serverPath } );
this.log( `Starting server in ${ serverPath }...` );
const serverProcess = exec( 'node index.js', { cwd: serverPath } );
serverProcess.stdout?.on( 'data', ( data ) => {
this.log( `Server: ${ data }` );
} );
serverProcess.stderr?.on( 'data', ( data ) => {
this.error( `Server Error: ${ data }` );
} );
this.log( `Starting Node.js React app in ${ projectPath }...` );
this.log( `Installing client side dependencies in ${ projectPath }...` );
// check if we need to install dependencies
const shouldInstall = !fs.existsSync( `${ projectPath }/node_modules` ) ||
!fs.existsSync( `${ projectPath }/package-lock.json` );
if ( shouldInstall ) {
this.log( 'Installing dependencies...' );
await execAsync( 'npm install', { cwd: projectPath } );
}
this.log( 'running...' );
const client = exec( 'npm run dev', { cwd: projectPath } );
client.stdout?.on( 'data', ( data ) => {
this.log( `client: ${ data }` );
} );
client.stderr?.on( 'data', ( data ) => {
this.error( `client Error: ${ data }` );
} );
} catch ( error ) {
this.error( `Error running the Node.js React app: ${ error }` );
}
}
}