• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / js-slang / 14529259416

18 Apr 2025 03:46AM UTC coverage: 80.538%. Remained the same
14529259416

Pull #1757

github

web-flow
Merge 63eac9783 into fea2b4cad
Pull Request #1757: Language options

3458 of 4687 branches covered (73.78%)

Branch coverage included in aggregate %.

15 of 88 new or added lines in 6 files covered. (17.05%)

84 existing lines in 6 files now uncovered.

10811 of 13030 relevant lines covered (82.97%)

142544.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.36
/src/repl/repl.ts
1
import type fslib from 'fs/promises'
2
import { resolve } from 'path'
1✔
3
import { start } from 'repl'
1✔
4
import { Command } from '@commander-js/extra-typings'
1✔
5

6
import { createContext, type IOptions } from '..'
1✔
7
import { setModulesStaticURL } from '../modules/loader'
1✔
8
import { Chapter, type RecursivePartial, Variant } from '../types'
1✔
9
import { objectValues } from '../utils/misc'
1✔
10
import { runCodeInSource, sourceFilesRunner } from '../runner'
1✔
11
import type { FileGetter } from '../modules/moduleTypes'
12
import {
1✔
13
  chapterParser,
14
  getChapterOption,
15
  getLanguageOption,
16
  getVariantOption,
17
  handleResult,
18
  validChapterVariant
19
} from './utils'
20

21
export const getReplCommand = () =>
1✔
22
  new Command('run')
10✔
23
    .addOption(getChapterOption(Chapter.SOURCE_4, chapterParser))
24
    .addOption(getVariantOption(Variant.DEFAULT, objectValues(Variant)))
25
    .addOption(getLanguageOption())
26
    .option('-v, --verbose', 'Enable verbose errors')
27
    .option('--modulesBackend <backend>')
28
    .option('-r, --repl', 'Start a REPL after evaluating files')
29
    .option('--optionsFile <file>', 'Specify a JSON file to read options from')
30
    .argument('[filename]')
31
    .action(async (filename, { modulesBackend, optionsFile, repl, verbose, ...lang }) => {
32
      if (!validChapterVariant(lang)) {
10!
UNCOV
33
        console.log('Invalid language combination!')
×
UNCOV
34
        return
×
35
      }
36

37
      const fs: typeof fslib = require('fs/promises')
10✔
38

39
      const context = createContext(lang.chapter, lang.variant, lang.languageOptions)
10✔
40

41
      if (modulesBackend !== undefined) {
10!
UNCOV
42
        setModulesStaticURL(modulesBackend)
×
43
      }
44

45
      let options: RecursivePartial<IOptions> = {}
10✔
46
      if (optionsFile !== undefined) {
10!
UNCOV
47
        const rawText = await fs.readFile(optionsFile, 'utf-8')
×
UNCOV
48
        options = JSON.parse(rawText)
×
49
      }
50

51
      const fileGetter: FileGetter = async p => {
10✔
52
        try {
23✔
53
          const text = await fs.readFile(p, 'utf-8')
23✔
54
          return text
19✔
55
        } catch (error) {
56
          if (error.code === 'ENOENT') return undefined
4✔
UNCOV
57
          throw error
×
58
        }
59
      }
60

61
      if (filename !== undefined) {
10✔
62
        const entrypointFilePath = resolve(filename)
6✔
63
        const { result, verboseErrors } = await sourceFilesRunner(
6✔
64
          fileGetter,
65
          entrypointFilePath,
66
          context,
67
          {
68
            ...options,
69
            shouldAddFileName: true
70
          }
71
        )
72

73
        const toLog = handleResult(result, context, verbose ?? verboseErrors)
6✔
74
        console.log(toLog)
6✔
75

76
        if (!repl) return
6✔
77
      }
78

79
      start(
5✔
80
        // the object being passed as argument fits the interface ReplOptions in the repl module.
81
        {
82
          eval: (cmd, unusedContext, unusedFilename, callback) => {
83
            context.errors = []
22✔
84
            runCodeInSource(cmd, context, options, '/default.js', fileGetter)
22✔
85
              .then(obj => {
86
                callback(null, obj)
22✔
87
              })
UNCOV
88
              .catch(err => callback(err, undefined))
×
89
          },
90
          writer: (output: Awaited<ReturnType<typeof runCodeInSource>> | Error) => {
91
            if (output instanceof Error) {
22!
UNCOV
92
              return output.message
×
93
            }
94

95
            return handleResult(output.result, context, verbose ?? output.verboseErrors)
22✔
96
          }
97
        }
98
      )
99
    })
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc