2
2
const yargs = require ( "yargs" ) ;
3
3
4
4
const { promises : fsPromises } = require ( "fs" ) ;
5
- const { join } = require ( "path" ) ;
6
- const { spawnProcess } = require ( "../utils/spawn-process" ) ;
5
+ const { join, resolve } = require ( "path" ) ;
7
6
8
- const { models } = yargs ( process . argv . slice ( 2 ) )
7
+ const { models, protocols } = yargs ( process . argv . slice ( 2 ) )
9
8
. alias ( "m" , "models" )
10
9
. string ( "m" )
11
10
. describe ( "m" , "The path to directory with aws-models." )
12
11
. demandOption ( [ "models" ] )
12
+ . alias ( "p" , "protocols" )
13
+ . string ( "p" )
14
+ . describe (
15
+ "p" ,
16
+ "List of protocols to copy e.g. awsJson1,restXml, default all, list: \n" +
17
+ `awsJson1_1,
18
+ restJson1,
19
+ awsJson1_0,
20
+ awsQuery,
21
+ restXml,
22
+ ec2Query`
23
+ )
13
24
. help ( ) . argv ;
14
25
26
+ const getSdkId = ( model ) => {
27
+ const { shapes } = model ;
28
+ const service = Object . values ( shapes ) . find ( ( shape ) => shape . type === "service" ) || { } ;
29
+ const { traits } = service ;
30
+ for ( const [ trait , value ] of Object . entries ( traits ) ) {
31
+ if ( trait === "aws.api#service" ) {
32
+ return value . sdkId ;
33
+ }
34
+ }
35
+ throw new Error ( "unable to find SDK ID in model file" ) ;
36
+ } ;
37
+
38
+ const getWireProtocol = ( model ) => {
39
+ const { shapes } = model ;
40
+ const service = Object . values ( shapes ) . find ( ( shape ) => shape . type === "service" ) || { } ;
41
+ for ( const trait of Object . keys ( service . traits || { } ) ) {
42
+ if ( trait . startsWith ( "aws.protocols#" ) ) {
43
+ const parts = trait . split ( "aws.protocols#" ) ;
44
+ if ( parts . length !== 0 ) {
45
+ return parts . pop ( ) ;
46
+ }
47
+ }
48
+ }
49
+ throw new Error ( "unable to determine wire protocol in model file" ) ;
50
+ } ;
51
+
15
52
( async ( ) => {
16
53
const OUTPUT_DIR = join ( __dirname , ".." , ".." , "codegen" , "sdk-codegen" , "aws-models" ) ;
17
54
@@ -23,34 +60,42 @@ const { models } = yargs(process.argv.slice(2))
23
60
. filter ( ( file ) => file . isDirectory ( ) )
24
61
. map ( ( dir ) => join ( models . toString ( ) , dir . name , `smithy/model.json` ) ) ;
25
62
63
+ const observedProtocols = new Set ( ) ;
64
+
26
65
for ( const smithyModelsFile of smithyModelsFiles ) {
27
66
try {
28
67
// Test if file exists.
29
68
await fsPromises . stat ( smithyModelsFile ) ;
30
69
// File exists, copy it.
70
+ const absolutePath = resolve ( smithyModelsFile ) ;
71
+
31
72
try {
32
- const fileContent = ( await fsPromises . readFile ( smithyModelsFile ) ) . toString ( ) ;
73
+ const model = require ( absolutePath ) ;
74
+ const sdkId = getSdkId ( model ) . toLowerCase ( ) . replace ( / \s / g, "-" ) ;
75
+ const protocol = getWireProtocol ( model ) ;
33
76
34
- const sdkIdRE = / " s d k I d " : " ( [ ^ " ] * ) " / ;
35
- const sdkId = fileContent . match ( sdkIdRE ) [ 1 ] . toLowerCase ( ) . replace ( / \s / g, "-" ) ;
77
+ observedProtocols . add ( protocol ) ;
36
78
37
- // Copy file.
38
- const outputFile = join ( OUTPUT_DIR , `${ sdkId } .json` ) ;
39
- await fsPromises . writeFile ( outputFile , fileContent ) ;
79
+ if ( ! protocols || protocols . split ( "," ) . includes ( protocol ) ) {
80
+ // Copy file.
81
+ const outputFile = join ( OUTPUT_DIR , `${ sdkId } .json` ) ;
82
+ await fsPromises . writeFile ( outputFile , JSON . stringify ( model , null , 2 ) ) ;
83
+ console . log ( "Copied" , outputFile ) ;
84
+ }
40
85
} catch ( e ) {
41
86
// Copy failed, log.
42
- console . log ( smithyModelsFile ) ;
87
+ console . log ( "Failed to copy" , absolutePath ) ;
43
88
console . log ( e . message ) ;
44
89
}
45
90
} catch ( e ) {
46
91
// File doesn't exist, ignore.
47
- console . log ( e . message ) ;
92
+ console . log ( "File not found" , e . message ) ;
48
93
}
49
94
}
50
95
51
- // Prettify copied models
52
- await spawnProcess ( join ( __dirname , ".." , ".." , "node_modules" , ".bin" , "prettier" ) , [
53
- "--write" ,
54
- ` ${ OUTPUT_DIR } /*.json` ,
55
- ] ) ;
96
+ console . log ( "args:" , {
97
+ models ,
98
+ protocols ,
99
+ observedProtocols ,
100
+ } ) ;
56
101
} ) ( ) ;
0 commit comments