typescript#ExportDeclaration TypeScript Examples

The following examples show how to use typescript#ExportDeclaration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: addDenoExtensions.ts    From date-fns-jalali with MIT License 5 votes vote down vote up
globby('deno')
  .then((files) =>
    files.filter(
      (file) => pattern.test(file) && !ignore.find((p) => p.test(file))
    )
  )
  .then((files) =>
    Promise.all(
      files.map((file) =>
        readFile(file, 'utf8').then(async (content) => {
          const source = ts.createSourceFile(
            file,
            content,
            ts.ScriptTarget.Latest
          )
          const imports: string[] = []

          source.forEachChild((node) => {
            if (
              [
                ts.SyntaxKind.ImportDeclaration,
                ts.SyntaxKind.ExportDeclaration,
              ].includes(node.kind)
            ) {
              const importNode = node as ImportDeclaration | ExportDeclaration
              const specifier = importNode.moduleSpecifier as StringLiteral
              const importPath = specifier.text
              const isLocal = /\.\/.+/
              if (isLocal) imports.push(importPath)
            }
          })

          await Promise.all(
            imports.map(async (importPath) => {
              if (resolvedExtensions[importPath]) return
              const fullPath = resolveFullPath(file, importPath)
              let isTs = false
              try {
                await stat(fullPath + '.ts')
                isTs = true
              } catch (_) {}
              resolvedExtensions[fullPath] = isTs ? '.ts' : '.js'
            })
          )

          return writeFile(
            file,
            imports.reduce((acc, importPath) => {
              const fullPath = resolveFullPath(file, importPath)
              return acc.replace(
                new RegExp(importPath, 'g'),
                importPath + resolvedExtensions[fullPath]
              )
            }, content)
          )
        })
      )
    )
  )