def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--tsconfig_path', required=True) args = parser.parse_args(argv) result = node.RunNode([node_modules.PathToTypescript()] + ['--project', args.tsconfig_path]) if len(result) != 0: raise RuntimeError('Failed to compile Typescript: \n%s' % result)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--deps', nargs='*') parser.add_argument('--gen_dir', required=True) parser.add_argument('--path_mappings', nargs='*') parser.add_argument('--root_dir', required=True) parser.add_argument('--out_dir', required=True) parser.add_argument('--in_files', nargs='*') parser.add_argument('--definitions', nargs='*') args = parser.parse_args(argv) root_dir = os.path.relpath(args.root_dir, args.gen_dir) out_dir = os.path.relpath(args.out_dir, args.gen_dir) with open(os.path.join(_HERE_DIR, 'tsconfig_base.json')) as root_tsconfig: tsconfig = json.loads(root_tsconfig.read()) tsconfig['files'] = [] if args.in_files is not None: # Source .ts files are always resolved as being relative to |root_dir|. tsconfig['files'].extend( [os.path.join(root_dir, f) for f in args.in_files]) if args.definitions is not None: tsconfig['files'].extend(args.definitions) tsconfig['compilerOptions']['rootDir'] = root_dir tsconfig['compilerOptions']['outDir'] = out_dir # Handle custom path mappings, for example chrome://resources/ URLs. if args.path_mappings is not None: path_mappings = {} for m in args.path_mappings: mapping = m.split('|') if not path_mappings.has_key(mapping[0]): path_mappings[mapping[0]] = [] path_mappings[mapping[0]].append(os.path.join('./', mapping[1])) tsconfig['compilerOptions']['paths'] = path_mappings if args.deps is not None: tsconfig['references'] = [{'path': dep} for dep in args.deps] _write_tsconfig_json(args.gen_dir, tsconfig) node.RunNode([ node_modules.PathToTypescript(), '--project', os.path.join(args.gen_dir, 'tsconfig.json') ]) if args.in_files is not None: with open(os.path.join(args.gen_dir, 'tsconfig.manifest'), 'w') \ as manifest_file: manifest_data = {} manifest_data['base_dir'] = args.out_dir manifest_data['files'] = \ [re.sub(r'\.ts$', '.js', f) for f in args.in_files] json.dump(manifest_data, manifest_file)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--gen_dir', required=True) parser.add_argument('--out_dir', required=True) parser.add_argument('--root_dir', required=True) parser.add_argument('--js_files', nargs='*', required=True) parser.add_argument('--path_mappings', nargs='*') args = parser.parse_args(argv) with open(os.path.join(_HERE_DIR, _TSCONFIG_BASE)) as root_tsconfig: tsconfig = json.loads(root_tsconfig.read()) root_dir = os.path.relpath(args.root_dir, args.gen_dir) out_dir = os.path.relpath(args.out_dir, args.gen_dir) tsconfig['files'] = [os.path.join(root_dir, f) for f in args.js_files] tsconfig['compilerOptions']['rootDir'] = root_dir tsconfig['compilerOptions']['outDir'] = out_dir # Handle custom path mappings, for example chrome://resources/ URLs. if args.path_mappings is not None: path_mappings = {} for m in args.path_mappings: mapping = m.split('|') if not path_mappings.has_key(mapping[0]): path_mappings[mapping[0]] = [] path_mappings[mapping[0]].append(os.path.join('./', mapping[1])) tsconfig['compilerOptions']['paths'] = path_mappings _write_tsconfig_json(args.gen_dir, tsconfig) if (args.root_dir == args.out_dir): # Delete .d.ts files if they already exist, otherwise TypeScript compiler # throws "error TS5055: Cannot write file ... because it would overwrite # input file" errors. for f in args.js_files: to_delete = os.path.join(args.out_dir, re.sub(r'\.js$', '.d.ts', f)) if os.path.exists(to_delete): os.remove(to_delete) node.RunNode([ node_modules.PathToTypescript(), '--project', os.path.join(args.gen_dir, _TSCONFIG_GEN) ])
def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--filelist', required=True) args = parser.parse_args(argv) files = [] with open(args.filelist) as filelist_file: for line in filelist_file: for f in line.split(): files.append(os.path.join(os.getcwd(), f)) file_paths = ' '.join(files) result = node.RunNode([node_modules.PathToTypescript()] + [ "--target 'es6'", "--module 'es6'", "--lib 'es6, esnext.bigint'", "--strict", file_paths ]) if len(result) != 0: raise RuntimeError('Failed to compile Typescript: \n%s' % result)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--deps', nargs='*') parser.add_argument('--gen_dir', required=True) parser.add_argument('--path_mappings', nargs='*') parser.add_argument('--root_dir', required=True) parser.add_argument('--sources', nargs='*', required=True) args = parser.parse_args(argv) root_dir = os.path.relpath(args.root_dir, args.gen_dir) sources = [os.path.join(root_dir, f) for f in args.sources] with open(os.path.join(_HERE_DIR, 'tsconfig_base.json')) as root_tsconfig: tsconfig = json.loads(root_tsconfig.read()) tsconfig['files'] = sources tsconfig['compilerOptions']['rootDir'] = root_dir # Handle custom path mappings, for example chrome://resources/ URLs. if args.path_mappings is not None: path_mappings = {} for m in args.path_mappings: mapping = m.split('|') path_mappings[mapping[0]] = [os.path.join('./', mapping[1])] tsconfig['compilerOptions']['paths'] = path_mappings if args.deps is not None: tsconfig['references'] = [{'path': dep} for dep in args.deps] _write_tsconfig_json(args.gen_dir, tsconfig) node.RunNode([ node_modules.PathToTypescript(), '--project', os.path.join(args.gen_dir, 'tsconfig.json') ]) with open(os.path.join(args.gen_dir, 'tsconfig.manifest'), 'w') \ as manifest_file: manifest_data = {} manifest_data['base_dir'] = args.gen_dir manifest_data['files'] = [re.sub(r'\.ts$', '.js', f) for f in args.sources] json.dump(manifest_data, manifest_file)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--gen_dir', required=True) parser.add_argument('--root_dir', required=True) parser.add_argument('--js_files', nargs='*', required=True) args = parser.parse_args(argv) js_files = [os.path.join(args.root_dir, f) for f in args.js_files] node.RunNode([ node_modules.PathToTypescript(), '--declaration', '--allowJs', '--emitDeclarationOnly', '--removeComments', '--noResolve', '--rootDir', args.root_dir, '--outDir', args.gen_dir, ] + js_files)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--deps', nargs='*') parser.add_argument('--gen_dir', required=True) parser.add_argument('--path_mappings', nargs='*') parser.add_argument('--root_dir', required=True) parser.add_argument('--out_dir', required=True) parser.add_argument('--tsconfig_base') parser.add_argument('--in_files', nargs='*') parser.add_argument('--definitions', nargs='*') parser.add_argument('--composite', action='store_true') args = parser.parse_args(argv) root_dir = os.path.relpath(args.root_dir, args.gen_dir) out_dir = os.path.relpath(args.out_dir, args.gen_dir) TSCONFIG_BASE_PATH = os.path.join(_HERE_DIR, 'tsconfig_base.json') tsconfig = collections.OrderedDict() tsconfig['extends'] = args.tsconfig_base \ if args.tsconfig_base is not None \ else os.path.relpath(TSCONFIG_BASE_PATH, args.gen_dir) tsconfig['compilerOptions'] = collections.OrderedDict() tsconfig['compilerOptions']['rootDir'] = root_dir tsconfig['compilerOptions']['outDir'] = out_dir if args.composite: tsbuildinfo_name = 'tsconfig.tsbuildinfo' tsconfig['compilerOptions']['composite'] = True tsconfig['compilerOptions']['declaration'] = True tsconfig['compilerOptions']['tsBuildInfoFile'] = tsbuildinfo_name tsconfig['files'] = [] if args.in_files is not None: # Source .ts files are always resolved as being relative to |root_dir|. tsconfig['files'].extend( [os.path.join(root_dir, f) for f in args.in_files]) if args.definitions is not None: tsconfig['files'].extend(args.definitions) # Handle custom path mappings, for example chrome://resources/ URLs. if args.path_mappings is not None: path_mappings = collections.defaultdict(list) for m in args.path_mappings: mapping = m.split('|') path_mappings[mapping[0]].append(os.path.join('./', mapping[1])) tsconfig['compilerOptions']['paths'] = path_mappings if args.deps is not None: tsconfig['references'] = [{'path': dep} for dep in args.deps] _write_tsconfig_json(args.gen_dir, tsconfig) # Delete any obsolete .ts files (from previous builds) corresponding to .js # |in_files| in the |root_dir| folder, as they would cause the following error # to be thrown: # # "error TS5056: Cannot write file '...' because it would be overwritten by # multiple input files." # # This can happen when a ts_library() is migrating JS to TS one file at a time # and a bot is switched from building a later CL to building an earlier CL. if args.in_files is not None: for f in args.in_files: [pathname, extension] = os.path.splitext(f) if extension == '.js': to_check = os.path.join(args.root_dir, pathname + '.ts') if os.path.exists(to_check): os.remove(to_check) node.RunNode([ node_modules.PathToTypescript(), '--project', os.path.join(args.gen_dir, 'tsconfig.json') ]) if args.composite: # `.tsbuildinfo` is generated by TypeScript for incremenetal compilation # freshness checks. Since GN already decides which ts_library() targets are # dirty, `.tsbuildinfo` is not needed for our purposes and is deleted. # # Moreover `.tsbuildinfo` can cause flakily failing builds since the TS # compiler checks the `.tsbuildinfo` file and sees that none of the source # files are changed and does not regenerate any output, without checking # whether output files have been modified/deleted, which can lead to bad # builds (missing files or picking up obsolete generated files). os.remove(os.path.join(args.gen_dir, tsbuildinfo_name)) if args.in_files is not None: with open(os.path.join(args.gen_dir, 'tsconfig.manifest'), 'w') \ as manifest_file: manifest_data = {} manifest_data['base_dir'] = args.out_dir manifest_data['files'] = \ [re.sub(r'\.ts$', '.js', f) for f in args.in_files] json.dump(manifest_data, manifest_file)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument('--gen_dir', required=True) parser.add_argument('--out_dir', required=True) parser.add_argument('--root_dir', required=True) parser.add_argument('--js_files', nargs='*', required=True) parser.add_argument('--path_mappings', nargs='*') args = parser.parse_args(argv) with open(os.path.join(_HERE_DIR, _TSCONFIG_BASE)) as root_tsconfig: tsconfig = json.loads(root_tsconfig.read()) root_dir = os.path.relpath(args.root_dir, args.gen_dir) out_dir = os.path.relpath(args.out_dir, args.gen_dir) tsconfig['files'] = [os.path.join(root_dir, f) for f in args.js_files] tsconfig['compilerOptions']['rootDir'] = root_dir tsconfig['compilerOptions']['outDir'] = out_dir # Handle custom path mappings, for example chrome://resources/ URLs. if args.path_mappings is not None: path_mappings = collections.defaultdict(list) for m in args.path_mappings: mapping = m.split('|') path_mappings[mapping[0]].append(os.path.join('./', mapping[1])) tsconfig['compilerOptions']['paths'] = path_mappings _write_tsconfig_json(args.gen_dir, tsconfig) if (args.root_dir == args.out_dir): # Delete .d.ts files if they already exist, otherwise TypeScript compiler # throws "error TS5055: Cannot write file ... because it would overwrite # input file" errors. for f in args.js_files: to_delete = os.path.join(args.out_dir, re.sub(r'\.js$', '.d.ts', f)) if os.path.exists(to_delete): os.remove(to_delete) stdout = node.RunNode([ node_modules.PathToTypescript(), '--project', os.path.join(args.gen_dir, _TSCONFIG_GEN) ]) # Verify that that no unexpected .d.ts files were generated. lines = stdout.splitlines() token = 'TSFILE: ' generated_files = [] for l in lines: if token in l: generated_files.append( os.path.normpath(os.path.relpath(l[len(token):], args.out_dir))) generated_files.sort() args.js_files.sort() unexpected_file = None for i, _js_file in enumerate(args.js_files): js_file = os.path.normpath(_js_file) if os.path.dirname(js_file) != os.path.dirname(generated_files[i]): unexpected_file = generated_files[i] break base = os.path.splitext(os.path.basename(js_file))[0] if base + '.d.ts' != os.path.basename(generated_files[i]): unexpected_file = generated_files[i] break # Delete all generated files to not pollute the gen/ folder with any invalid # files, which could cause problems on subsequent builds. if unexpected_file is not None: for f in generated_files: os.remove(os.path.join(args.out_dir, f)) raise Exception(\ 'Unexpected file \'%s\' generated, deleting all generated files.' \ % unexpected_file)