@@ -23,20 +23,17 @@ const templateBaseDir = path.join(__dirname, '../leetcode/templates')
23
23
const templateExtension = 'mustache'
24
24
const questionList = lc . data . problemsetQuestionList
25
25
26
- const getQuestionNumber : ( ) => number = ( ) => {
27
- program . parse ( )
28
-
29
- const question : string = program . args [ 0 ] || ''
26
+ const getQuestionNumber : ( questionNumberAsString : string ) => number = ( questionNumberAsString : string = '' ) => {
30
27
const regex = new RegExp ( '\\d+' )
31
28
32
- if ( ! regex . test ( question ) ) {
33
- throw new InvalidArgumentError ( `Input must be number: ${ program . args [ 0 ] } ` )
29
+ if ( ! regex . test ( questionNumberAsString ) ) {
30
+ throw new InvalidArgumentError ( `Input must be number: ${ questionNumberAsString } ` )
34
31
}
35
32
36
- const questionNumber = Number ( question )
33
+ const questionNumber = Number ( questionNumberAsString )
37
34
38
35
if ( questionNumber < 0 ) {
39
- throw new InvalidArgumentError ( `Input must be a positive integer: ${ program . args [ 0 ] } ` )
36
+ throw new InvalidArgumentError ( `Input must be a positive integer: ${ questionNumberAsString } ` )
40
37
}
41
38
42
39
return questionNumber
@@ -99,8 +96,7 @@ const render: (fileName: string, templateData: object) => string = (fileName: st
99
96
}
100
97
101
98
// Main program
102
- const generateNewPost : ( ) => void = async ( ) => {
103
- const questionNumber : number = getQuestionNumber ( )
99
+ const generateNewPost : ( questionNumber : number ) => void = async ( questionNumber : number ) => {
104
100
const question = getQuestion ( questionNumber )
105
101
106
102
if ( question === undefined ) {
@@ -122,4 +118,13 @@ const generateNewPost: () => void = async () => {
122
118
writeToFile ( path . join ( __dirname , '../content/leetcode-solutions' , `${ slug } .md` ) , output )
123
119
}
124
120
125
- generateNewPost ( )
121
+ program . name ( 'pnpm lc:new' )
122
+ . description ( 'Utility to generate new blog post' )
123
+ . version ( '1.0.0' )
124
+ . argument ( '<leetcode-question-number>' , 'Leetcode question number' )
125
+ . action ( ( questionNumberAsString , _ ) => {
126
+ const questionNumber : number = getQuestionNumber ( questionNumberAsString )
127
+ generateNewPost ( questionNumber )
128
+ } ) ;
129
+
130
+ program . parse ( )
0 commit comments