@@ -5,7 +5,7 @@ import { promises as asyncfs } from 'fs';
55import ts from 'typescript' ;
66import * as tsickle from 'tsickle' ;
77
8- const DEBUG = true ;
8+ const DEBUG = false ;
99
1010function getExecRoot ( ) : string {
1111 return process . env . JS_BINARY__EXECROOT || '.' ;
@@ -16,12 +16,6 @@ function getBazelBinDir(): string {
1616 return path . join ( getExecRoot ( ) , binDir ) ;
1717}
1818
19- function getOutDir ( execRoot : string ) : string {
20- const pkgdir = process . env . BAZEL_PACKAGE ! ;
21- const bindir = process . env . BAZEL_BINDIR ! ;
22- return path . join ( execRoot , bindir , pkgdir ) ;
23- }
24-
2519async function listFiles ( dir : string ) : Promise < string [ ] > {
2620 return asyncfs . readdir ( dir , { recursive : true } ) ;
2721}
@@ -40,12 +34,7 @@ async function listExecrootFiles() {
4034}
4135
4236function getInputFiles ( execRoot : string , args : string [ ] ) : string [ ] {
43- const inputFiles = args . map ( arg => `${ execRoot } /${ arg } ` ) ;
44- if ( inputFiles . length === 0 ) {
45- console . error ( 'Usage: tsicklecompiler <input.ts> [input2.ts] ...' ) ;
46- process . exit ( 1 ) ;
47- }
48- return inputFiles ;
37+ return args . map ( arg => `${ execRoot } /${ arg } ` ) ;
4938}
5039
5140function getTsCompilerOptions ( ) : ts . CompilerOptions {
@@ -59,79 +48,20 @@ function getTsCompilerOptions(): ts.CompilerOptions {
5948 } ;
6049}
6150
62- /**
63- * Determine the lowest-level common parent directory of the given list of files.
64- */
65- function getCommonParentDirectory ( fileNames : string [ ] ) : string {
66- const pathSplitter = / [ \/ \\ ] + / ;
67- const commonParent = fileNames [ 0 ] . split ( pathSplitter ) ;
68- for ( let i = 1 ; i < fileNames . length ; i ++ ) {
69- const thisPath = fileNames [ i ] . split ( pathSplitter ) ;
70- let j = 0 ;
71- while ( thisPath [ j ] === commonParent [ j ] ) {
72- j ++ ;
73- }
74- commonParent . length = j ; // Truncate without copying the array
75- }
76- if ( commonParent . length === 0 ) {
77- return '/' ;
78- } else {
79- return commonParent . join ( path . sep ) ;
80- }
81- }
82-
83- async function main ( ) {
84- const execRoot = getExecRoot ( ) ;
85- const outDir = getOutDir ( execRoot ) ;
86- const args = process . argv . slice ( 2 ) ;
87- const inputFiles = getInputFiles ( execRoot , args ) ;
88- // const inputFiles = args;
89- const compilerOptions = getTsCompilerOptions ( ) ;
90-
91- if ( DEBUG ) {
92- const cwd = process . cwd ( )
93- // console.log('env:', process.env);
94- console . log ( 'pwd:' , cwd ) ;
95- console . log ( 'args:' , args ) ;
96- console . log ( 'files:' , inputFiles ) ;
97- console . log ( 'compilerOptions:' , compilerOptions ) ;
98- }
99-
100- const result = run ( compilerOptions , inputFiles , ( filePath : string , contents : string ) => {
101- console . log ( 'emitted file:' , filePath ) ;
102- fs . mkdirSync ( path . dirname ( filePath ) , { recursive : true } ) ;
103- fs . writeFileSync ( filePath , contents , { encoding : 'utf-8' } ) ;
104- } ) ;
105-
106- if ( result . diagnostics . length ) {
107- console . error ( ts . formatDiagnostics ( result . diagnostics , ts . createCompilerHost ( compilerOptions ) ) ) ;
108- return 1 ;
109- }
110-
111- // if (settings.externsPath) {
112- // fs.mkdirSync(path.dirname(settings.externsPath), { recursive: true });
113- // fs.writeFileSync(
114- // settings.externsPath,
115- // tsickle.getGeneratedExterns(result.externs, config.options.rootDir || ''));
116- // }
117-
118- return 0 ;
119-
120- }
121-
12251function run (
12352 options : ts . CompilerOptions ,
124- absoluteFileNames : string [ ] ,
53+ fileNames : string [ ] ,
12554 writeFile : ts . WriteFileCallback ,
12655) : tsickle . EmitResult {
12756 // Use absolute paths to determine what files to process since files may be imported using
12857 // relative or absolute paths
12958 // const absoluteFileNames = fileNames.map(i => path.resolve(i));
13059
13160 const compilerHost = ts . createCompilerHost ( options ) ;
132- const program = ts . createProgram ( absoluteFileNames , options , compilerHost ) ;
133- const filesToProcess = new Set ( absoluteFileNames ) ;
134- const rootModulePath = options . rootDir || getCommonParentDirectory ( absoluteFileNames ) ;
61+ const program = ts . createProgram ( fileNames , options , compilerHost ) ;
62+ const filesToProcess = new Set ( fileNames ) ;
63+ const rootModulePath = options . rootDir ! ;
64+
13565 const transformerHost : tsickle . TsickleHost = {
13666 rootDirsRelative : ( f : string ) => f ,
13767 shouldSkipTsickleProcessing : ( fileName : string ) => {
@@ -150,9 +80,9 @@ function run(
15080 console . error ( ts . formatDiagnostics ( [ warning ] , compilerHost ) ) ,
15181 options,
15282 generateExtraSuppressions : true ,
153- // transformDynamicImport: 'nodejs',
15483 moduleResolutionHost : compilerHost ,
15584 } ;
85+
15686 const diagnostics = ts . getPreEmitDiagnostics ( program ) ;
15787 if ( diagnostics . length > 0 ) {
15888 return {
@@ -162,10 +92,56 @@ function run(
16292 externs : { } ,
16393 emitSkipped : true ,
16494 emittedFiles : [ ] ,
165- // fileSummaries: new Map(),
16695 } ;
16796 }
97+
16898 return tsickle . emit ( program , transformerHost , writeFile ) ;
16999}
170100
101+ async function main ( ) {
102+ const execRoot = getExecRoot ( ) ;
103+
104+ const args = process . argv . slice ( 2 ) ;
105+ const inputFiles = getInputFiles ( execRoot , args ) ;
106+ if ( inputFiles . length === 0 ) {
107+ console . error ( 'Usage: tsicklecompiler <input.ts> [input2.ts] ...' ) ;
108+ process . exit ( 1 ) ;
109+ }
110+
111+ const compilerOptions = getTsCompilerOptions ( ) ;
112+
113+ if ( DEBUG ) {
114+ const cwd = process . cwd ( )
115+ console . log ( 'env:' , process . env ) ;
116+ console . log ( 'pwd:' , cwd ) ;
117+ console . log ( 'args:' , args ) ;
118+ console . log ( 'inputFiles:' , inputFiles ) ;
119+ console . log ( 'execRoot:' , execRoot ) ;
120+ console . log ( 'compilerOptions:' , compilerOptions ) ;
121+ }
122+
123+ const result = run ( compilerOptions , inputFiles , ( filePath : string , contents : string ) => {
124+ if ( DEBUG ) {
125+ console . log ( 'emitted file:' , filePath ) ;
126+ }
127+ fs . mkdirSync ( path . dirname ( filePath ) , { recursive : true } ) ;
128+ fs . writeFileSync ( filePath , contents , { encoding : 'utf-8' } ) ;
129+ } ) ;
130+
131+ if ( result . diagnostics . length ) {
132+ console . error ( ts . formatDiagnostics ( result . diagnostics , ts . createCompilerHost ( compilerOptions ) ) ) ;
133+ return 1 ;
134+ }
135+
136+ // if (settings.externsPath) {
137+ // fs.mkdirSync(path.dirname(settings.externsPath), { recursive: true });
138+ // fs.writeFileSync(
139+ // settings.externsPath,
140+ // tsickle.getGeneratedExterns(result.externs, config.options.rootDir || ''));
141+ // }
142+
143+ return 0 ;
144+
145+ }
146+
171147void main ( )
0 commit comments