@@ -3,8 +3,8 @@ import * as fs from "fs";
33import { generateTypes } from ".." ;
44import { CliArgumentsMap } from "../types" ;
55import { sanitizeFileName } from "../utils" ;
6- import path = require ( "path" ) ;
76import { resolvePackageJSONPath } from "@rigor789/resolve-package-path" ;
7+ import path = require ( "path" ) ;
88
99const args = process . argv . slice ( 2 ) ;
1010const workDir = process . cwd ( ) ;
@@ -26,6 +26,7 @@ Options:
2626 -c | --core: Generate types for @nativescript/core.
2727 -a | --all: Generate types for all packages based on project dependencies.
2828 -d | --directory: Generate types from a directory.
29+ -r | --reset: Reset lock files
2930 -n | --filename: Name of the output file.
3031 -h | --help: Show help.
3132` ) ;
@@ -54,6 +55,10 @@ function parseArgs(args: any[]) {
5455 case "--output" :
5556 parsedArgs . output = _args [ i + 1 ] ;
5657 break ;
58+ case "-r" :
59+ case "--reset" :
60+ parsedArgs . resetLockFiles = true ;
61+ break ;
5762 case "-f" :
5863 case "--framework" :
5964 parsedArgs . framework = _args [ i + 1 ] ;
@@ -82,23 +87,23 @@ function parseArgs(args: any[]) {
8287const parsedArgs = parseArgs ( args ) ;
8388
8489const isNativeScriptInstalled = resolvePackageJSONPath ( "@nativescript/core" , {
85- paths : [ workDir ]
86- } )
90+ paths : [ workDir ] ,
91+ } ) ;
8792
8893if ( ! isNativeScriptInstalled ) {
8994 console . error ( "Please install @nativescript/core in your project." ) ;
9095 process . exit ( 1 ) ;
9196}
9297
93- if ( ! parsedArgs . package && ! parsedArgs . core && ! parsedArgs . all ) {
94- console . error ( "Please provide a package to generate types for." ) ;
95- process . exit ( 1 ) ;
96- }
98+ // if (!parsedArgs.package && !parsedArgs.core && !parsedArgs.all) {
99+ // console.error("Please provide a package to generate types for.");
100+ // process.exit(1);
101+ // }
97102
98103if ( parsedArgs . package ) {
99104 const isPackageInstalled = resolvePackageJSONPath ( parsedArgs . package , {
100- paths : [ workDir ]
101- } ) ;
105+ paths : [ workDir ] ,
106+ } ) ;
102107 if ( ! isPackageInstalled ) {
103108 console . error (
104109 `Package ${ parsedArgs . package } is not installed in your project.`
@@ -107,40 +112,106 @@ if (parsedArgs.package) {
107112 }
108113}
109114
110- if ( ! parsedArgs . framework ) {
111- console . error (
112- "Invalid framework provided. The framework to generate the types for. (solidjs | vue | svelte | angular | react)."
113- ) ;
114- process . exit ( 1 ) ;
115- }
115+ const typegenLockDir = path . join ( workDir , "node_modules" , ".ns-type-gen" ) ;
116+ const lockFilePath = path . join ( typegenLockDir , ".ns-type-gen.lock.json" ) ;
116117
117- export async function startCliTypeGenerator ( ) {
118+ async function generateTypesForArgs ( parsedArgs : CliArgumentsMap ) {
118119 const types = await generateTypes ( parsedArgs ) ;
119120 if ( types ?. length ) {
120121 for ( let type of types ) {
121122 const filename = parsedArgs . filename
122123 ? `${ parsedArgs . filename } `
123124 : `${
124- parsedArgs . package
125- ? sanitizeFileName ( parsedArgs . package )
126- : parsedArgs . core
125+ parsedArgs . core
127126 ? "core"
128- : "ns"
127+ : parsedArgs . package
128+ ? sanitizeFileName ( parsedArgs . package )
129+ : "ns"
129130 } _${ parsedArgs . framework } _${ type . nameSuffix } .${ type . format } `;
130131
131-
132132 if ( parsedArgs . output && ! fs . existsSync ( parsedArgs . output ) ) {
133133 fs . mkdirSync ( parsedArgs . output , {
134- recursive : true
135- } )
134+ recursive : true ,
135+ } ) ;
136136 }
137137
138138 fs . writeFileSync (
139- parsedArgs . output ? `${ parsedArgs . output } /${ filename } ` : `types/${ filename } ` ,
139+ parsedArgs . output
140+ ? `${ parsedArgs . output } /${ filename } `
141+ : `types/${ filename } ` ,
140142 type . data
141143 ) ;
142144 }
143145 }
144146}
145147
148+ export async function startCliTypeGenerator ( ) {
149+ if ( parsedArgs . resetLockFiles ) {
150+ if ( fs . existsSync ( lockFilePath ) ) {
151+ fs . unlinkSync ( lockFilePath ) ;
152+ console . log ( "Lock files have been reset." ) ;
153+ }
154+ }
155+
156+ if (
157+ ! parsedArgs . all &&
158+ ! parsedArgs . core &&
159+ ! parsedArgs . directory &&
160+ ! parsedArgs . package
161+ ) {
162+ return ;
163+ }
164+
165+
166+ if ( ! parsedArgs . framework ) {
167+ console . error (
168+ "Invalid framework provided. The framework to generate the types for. (solid | vue | svelte | angular | react)."
169+ ) ;
170+ process . exit ( 1 ) ;
171+ }
172+
173+ if ( parsedArgs . all ) {
174+ const packageJsonPath = path . join ( workDir , "package.json" ) ;
175+ if ( ! fs . existsSync ( packageJsonPath ) ) {
176+ console . log (
177+ "Could not find a valid package.json file at the current directory to generate types"
178+ ) ;
179+ return ;
180+ }
181+ const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , "utf-8" ) ) ;
182+
183+ let lockFileJson : Record < string , string > = undefined ;
184+ try {
185+ if ( fs . existsSync ( lockFilePath ) ) {
186+ lockFileJson = JSON . parse ( fs . readFileSync ( lockFilePath , "utf-8" ) ) ;
187+ }
188+ } catch ( e ) { }
189+
190+ const packages = packageJson . dependencies as Record < string , string > ;
191+ if ( ! packages ) {
192+ console . log (
193+ "The provided package.json file is invalid or does not have a dependencies key."
194+ ) ;
195+ return ;
196+ }
197+ const packageNames = Object . keys ( packages ) ;
198+ for ( const pkgName of packageNames ) {
199+ if ( ! lockFileJson || lockFileJson [ pkgName ] !== packages [ pkgName ] ) {
200+ await generateTypesForArgs ( {
201+ ...parsedArgs ,
202+ package : pkgName ,
203+ core : pkgName === "@nativescript/core" ,
204+ } ) ;
205+ }
206+ }
207+
208+ if ( ! fs . existsSync ( typegenLockDir ) ) {
209+ fs . mkdirSync ( typegenLockDir ) ;
210+ }
211+ fs . writeFileSync ( lockFilePath , JSON . stringify ( packages ) ) ;
212+ } else {
213+ await generateTypesForArgs ( parsedArgs ) ;
214+ }
215+ }
216+
146217startCliTypeGenerator ( ) ;
0 commit comments