-
Notifications
You must be signed in to change notification settings - Fork 31
/
task.cfc
234 lines (194 loc) · 8.99 KB
/
task.cfc
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
component accessors="true" {
property name="formatterUtil" inject="Formatter";
function run() {
organizeResources();
organizeMetadata();
buildReference();
}
function organizeResources() {
var resourcePath = resolvePath( 'lib/resources/' );
var resources = directoryList( resourcePath, true, 'path', '*.cfc' ).sort( 'text' );
for ( var srcPath in resources ) {
var relativePath = srcPath.replace( resourcePath, '' ).replace( '\', '/', 'all' );
var srcCode = fileRead( srcPath, 'utf-8' );
var meta = loadMetadata( srcCode );
if ( !isNull( meta ) ) {
print.line( 'Organizing #relativePath#' );
fileWrite( srcPath, organize( srcCode, meta ), 'utf-8' );
}
}
}
function organizeMetadata() {
var metadataPath = resolvePath( './metadata/' );
var md = directoryList( metadataPath, true, 'path', '*.json' ).sort( 'text' );
for ( var srcPath in md ) {
var relativePath = srcPath.replace( metadataPath, '' ).replace( '\', '/', 'all' );
print.line( 'Organizing #relativePath#' );
var src = fileRead( srcPath, 'utf-8' );
var formatted = formatterUtil.formatJson( json = src, spaceAfterColon = true, sortKeys = 'text' );
fileWrite( srcPath, formatted & server.separator.line, 'utf-8' );
}
}
function buildReference() {
var resourcePath = resolvePath( 'lib/resources/' );
var resources = directoryList( resourcePath, true, 'path', '*.cfc' ).sort( 'text' );
var ref = [
'## stripe-cfml reference',
'For documentation on which arguments can be passed to each method please see the [Stripe API Reference](https://stripe.com/docs/api). The arguments that are listed here are named arguments that ultimately appear in the URL of an API HTTP request. Since the Node library passes these arguments by position, you can refer to this page to see the names to use when passing these arguments by name.'
];
for ( var srcPath in resources ) {
var relativePath = srcPath.replace( resourcePath, '' ).replace( '\', '/', 'all' );
var srcCode = fileRead( srcPath, 'utf-8' );
var meta = loadMetadata( srcCode );
ref.append( reference( relativePath, srcCode, meta ) );
}
print.line( 'Writing reference.md' );
fileWrite(
resolvePath( './reference.md' ),
ref.toList( server.separator.line & server.separator.line ) & server.separator.line,
'utf-8'
);
}
function compare( string stripeNodePath ) {
var jsPath = resolvePath( stripeNodePath ).replace( '\', '/', 'all' ) & 'lib/resources/';
var resourcePath = resolvePath( './lib/resources/' );
var jsResources = directoryList( jsPath, true, 'path', '*.js' ).sort( 'text' );
var resources = directoryList( resourcePath, true, 'path', '*.cfc' ).sort( 'text' );
var jsMap = jsResources.reduce( ( r, p ) => {
var key = p
.replace( '\', '/', 'all' )
.replace( jsPath, '' )
.lcase()
.left( -3 );
r[ key ] = p;
return r;
}, { } );
var resourceMap = resources.reduce( ( r, p ) => {
var key = p
.replace( resourcePath, '' )
.replace( '\', '/', 'all' )
.lcase()
.left( -4 );
r[ key ] = p;
return r;
}, { } );
var count = 0;
for ( var rp in jsMap.keyArray().sort( 'text' ) ) {
if ( resourceMap.keyExists( rp ) ) {
var jsSrcCode = fileRead( jsMap[ rp ], 'utf-8' );
var srcCode = fileRead( resourceMap[ rp ], 'utf-8' );
var jsMethods = findJSMethods( jsSrcCode );
var methods = loadMetadata( srcCode ).data.methods.keyArray();
for ( var m in jsMethods ) {
// we use `delete` not `del`
if ( m == 'del' ) m = 'delete';
if ( !methods.findNoCase( m ) ) {
print.redLine( 'Resource `#rp#` is missing method `#m#`.' )
}
}
for ( var m in methods ) {
// we use `delete` not `del`
if ( m == 'delete' ) m = 'del';
if ( !jsMethods.findNoCase( m ) ) {
print.redLine( 'Resource `#rp#` has extra method `#m#`.' )
}
}
} else {
print.redLine( 'Resource `#rp#` is missing.' );
}
}
}
function organize( srcCode, meta ) {
var patternClass = createObject( 'java', 'java.util.regex.Pattern' );
var quoteKeyRegex = patternClass.compile( '"([A-Za-z_]+)"\s*:' );
var methodNameRegex = patternClass.compile( '\n\s{8}([A-Za-z]+):' );
var formattedMeta = formatterUtil.formatJson( json = meta.data, spaceAfterColon = true, sortKeys = 'text' );
formattedMeta = quoteKeyRegex
.matcher( formattedMeta )
.replaceAll( '$1:' )
.replace( '"', '''', 'all' );
formattedMeta = methodNameRegex
.matcher( formattedMeta )
.replaceAll( '#chr( 10 )# ''$1'':' )
.replace( '#chr( 10 )#', '#chr( 10 )# ', 'all' );
return srcCode.replace( meta.raw, formattedMeta );
}
function reference( relativePath, srcCode, meta ) {
var patternClass = createObject( 'java', 'java.util.regex.Pattern' );
var urlKeyRegex = patternClass.compile( '\{([a-z_]+)\}' );
var lf = server.separator.line;
var cfcName = relativePath.replace( '/', '.', 'all' ).left( -4 );
var cfcDoc = meta.doc.len() ? '#lf##lf##meta.doc#' : '';
var methods = [ ];
for ( var methodName in meta.data.methods ) {
var pathVariables = [ ];
if ( meta.data.methods[ methodName ].keyExists( 'positionalArgs' ) ) {
pathVariables = meta.data.methods[ methodName ].positionalArgs;
} else {
var urlKeyMatcher = urlKeyRegex.matcher( meta.data.methods[ methodName ].path );
while ( urlKeyMatcher.find() ) {
pathVariables.append( urlKeyMatcher.group( 1 ) );
}
}
methods.append( 'stripe.#cfcName#.#methodName#(#pathVariables.toList( ', ' )#);' );
}
// add override for accounts.retrieve();
if ( cfcName == 'accounts' ) {
methods.append( 'stripe.accounts.retrieve();' );
}
return '#### stripe.#cfcName##cfcDoc##lf##lf#```cfc#lf##methods.sort( 'text' ).toList( lf )##lf#```';
}
function loadMetadata( srcCode ) {
var patternClass = createObject( 'java', 'java.util.regex.Pattern' );
var unquoteKeyRegex = patternClass.compile( '\b([A-Za-z_]+)\s*:' );
var metaRegex = patternClass.compile( 'this\.metadata\s*=\s*(\{.*?\n\s{4}\})', 32 );
var docRegex = patternClass.compile( '^/\*\*(.*?)\*/', 32 );
var metaMatcher = metaRegex.matcher( srcCode );
if ( metaMatcher.find() ) {
var rawMeta = metaMatcher.group( 1 );
var meta = unquoteKeyRegex
.matcher( rawMeta )
.replaceAll( '"$1":' )
.replace( '''', '"', 'all' );
var docMatcher = docRegex.matcher( srcCode );
var doc = '';
if ( docMatcher.find() ) {
doc = docMatcher
.group( 1 )
.reReplace( '(\n)\s*\*\s*', '\1', 'all' )
.trim();
}
return {
raw: rawMeta,
data: deserializeJSON( meta ),
doc: doc
}
}
}
function findJSMethods( srcCode ) {
var patternClass = createObject( 'java', 'java.util.regex.Pattern' );
var basicRegex = patternClass.compile( 'includeBasic:\s*\[([^\]]+)\]', 2 );
var stripeMethodRegex = patternClass.compile( '^\s*(\w+):\s*stripeMethod\(', 10 );
var methodRegex = patternClass.compile( '^ (\w+)\(', 10 );
var methods = [ ];
var basicMethodsMatcher = basicRegex.matcher( srcCode );
if ( basicMethodsMatcher.find() ) {
methods.append(
basicMethodsMatcher
.group( 1 )
.listToArray()
.map( ( m ) => m.trim().replace( '''', '', 'all' ) ),
true
);
}
var stripeMethodsMatcher = stripeMethodRegex.matcher( srcCode );
while ( stripeMethodsMatcher.find() ) {
methods.append( stripeMethodsMatcher.group( 1 ) );
}
var methodsMatcher = methodRegex.matcher( srcCode );
while ( methodsMatcher.find() ) {
methods.append( methodsMatcher.group( 1 ) );
}
return methods;
}
}