@@ -2,36 +2,56 @@ const path = require("path");
2
2
const { copyFileSync, emptyDirSync } = require ( "fs-extra" ) ;
3
3
const { readdirSync, lstatSync } = require ( "fs" ) ;
4
4
const { spawnProcess } = require ( "./spawn-process" ) ;
5
- const { CODE_GEN_ROOT , CODE_GEN_INPUT_DIR } = require ( "./code-gen-dir" ) ;
5
+ const { CODE_GEN_ROOT , TEMP_CODE_GEN_INPUT_DIR } = require ( "./code-gen-dir" ) ;
6
+ const Glob = require ( "glob" ) ;
6
7
7
8
async function generateClients ( models ) {
8
- console . info ( "models directory: " , models ) ;
9
- if ( models === CODE_GEN_INPUT_DIR ) {
10
- // This script will clean the CODE_GEN_INPUT_DIR in execution.
11
- // throw to avoid input model being removed
12
- throw new Error (
13
- `models directory cannot be the same as ${ CODE_GEN_INPUT_DIR } `
14
- ) ;
15
- } else {
16
- console . log ( `clearing code gen input folder...` ) ;
17
- emptyDirSync ( CODE_GEN_INPUT_DIR ) ;
18
- console . log ( `copying models from ${ models } to ${ CODE_GEN_INPUT_DIR } ...` ) ;
9
+ let designatedModels = false ;
10
+ if ( typeof models === "string" ) {
11
+ //`models` is a folder path
12
+ designatedModels = true ;
13
+ emptyDirSync ( TEMP_CODE_GEN_INPUT_DIR ) ;
14
+ console . log ( `preparing models from ${ models } ...` ) ;
19
15
for ( const modelFileName of readdirSync ( models ) ) {
20
16
const modelPath = path . join ( models , modelFileName ) ;
21
17
if ( ! lstatSync ( modelPath ) . isFile ( ) ) continue ;
22
18
console . log ( `copying model ${ modelFileName } ...` ) ;
23
- copyFileSync ( modelPath , path . join ( CODE_GEN_INPUT_DIR , modelFileName ) , {
24
- overwrite : true
25
- } ) ;
19
+ copyFileSync (
20
+ modelPath ,
21
+ path . join ( TEMP_CODE_GEN_INPUT_DIR , modelFileName ) ,
22
+ {
23
+ overwrite : true
24
+ }
25
+ ) ;
26
26
}
27
+ } else if ( Array . isArray ( models ) ) {
28
+ //`models` is a list of globs
29
+ designatedModels = true ;
30
+ emptyDirSync ( TEMP_CODE_GEN_INPUT_DIR ) ;
31
+ models . forEach ( pattern => {
32
+ const files = Glob . sync ( pattern , {
33
+ realpath : true ,
34
+ absolute : true
35
+ } ) ;
36
+ files . forEach ( file => {
37
+ if ( ! lstatSync ( file ) . isFile ( ) ) return ;
38
+ const name = path . basename ( file ) ;
39
+ console . log ( `copying model ${ name } ...` ) ;
40
+ copyFileSync ( file , path . join ( TEMP_CODE_GEN_INPUT_DIR , name ) , {
41
+ overwrite : true
42
+ } ) ;
43
+ } ) ;
44
+ } ) ;
45
+ } else {
46
+ console . log ( "no model supplied, generating all AWS clients" ) ;
27
47
}
28
- await spawnProcess (
29
- "./gradlew" ,
30
- [ ":sdk-codegen:clean" , ":sdk-codegen:build" ] ,
31
- {
32
- cwd : CODE_GEN_ROOT
33
- }
34
- ) ;
48
+ const options = [ ":sdk-codegen:clean" , ":sdk-codegen:build" ] ;
49
+ if ( designatedModels ) {
50
+ options . push ( `-PmodelsDirProp= ${ TEMP_CODE_GEN_INPUT_DIR } ` ) ;
51
+ }
52
+ await spawnProcess ( "./gradlew" , options , {
53
+ cwd : CODE_GEN_ROOT
54
+ } ) ;
35
55
}
36
56
37
57
module . exports = {
0 commit comments