« get me outta code hell

repl.js « build-modes « write « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write/build-modes/repl.js
blob: 20985595b938de131f06c2e321eeb9649173ddf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
export const description = `Provide command-line interactive access to wiki data objects`;

export const config = {
  fileSizes: {
    default: 'skip',
  },

  languageReloading: {
    default: true,
  },

  mediaValidation: {
    default: 'skip',
  },

  thumbs: {
    applicable: false,
  },
};

export function getCLIOptions() {
  return {
    'no-repl-history': {
      help: `Disable locally logging commands entered into the REPL in your home directory`,
      type: 'flag',
    },
  };
}

import * as os from 'node:os';
import * as path from 'node:path';
import * as repl from 'node:repl';

import _find, {bindFind} from '#find';
import CacheableObject from '#cacheable-object';
import {logWarn} from '#cli';
import {debugComposite} from '#composite';
import * as serialize from '#serialize';
import * as sort from '#sort';
import * as sugar from '#sugar';
import thingConstructors from '#things';
import * as wikiDataUtils from '#wiki-data';

export async function getContextAssignments({
  dataPath,
  mediaPath,
  mediaCachePath,

  defaultLanguage,
  languages,
  missingImagePaths,
  thumbsCache,
  urls,
  wikiData,

  getSizeOfAdditionalFile,
  getSizeOfImagePath,
  niceShowAggregate,
}) {
  let find;
  try {
    find = bindFind(wikiData);
  } catch (error) {
    console.error(error);
    logWarn`Failed to prepare wikiData-bound find() functions`;
    logWarn`\`find\` variable will be missing`;
  }

  const replContext = {
    dataPath,
    mediaPath,
    mediaCachePath,

    languages,
    defaultLanguage,
    language: defaultLanguage,

    missingImagePaths,
    thumbsCache,
    urls,

    wikiData,
    ...wikiData,
    WD: wikiData,

    ...thingConstructors,
    CacheableObject,
    debugComposite,

    ...sort,
    ...sugar,
    ...wikiDataUtils,

    serialize,
    S: serialize,

    _find,
    find,
    bindFind,

    getSizeOfAdditionalFile,
    getSizeOfImagePath,
    showAggregate: niceShowAggregate,
  };

  replContext.replContext = replContext;

  return replContext;
}

export async function go(buildOptions) {
  const {
    cliOptions,
    closeLanguageWatchers,
  } = buildOptions;

  const disableHistory = cliOptions['no-repl-history'] ?? false;

  console.log('HSMusic data REPL');

  const replServer = repl.start();

  Object.assign(replServer.context,
    await getContextAssignments(buildOptions));

  if (disableHistory) {
    console.log(`\rInput history disabled (--no-repl-history provided)`);
  } else {
    const historyFile = path.join(os.homedir(), '.hsmusic_repl_history');
    await new Promise(resolve => {
      replServer.setupHistory(historyFile, (err) => {
        if (err) {
          console.error(`\rFailed to begin locally logging input history to ${historyFile} (provide --no-repl-history to disable)`);
        } else {
          console.log(`\rLogging input history to ${historyFile} (provide --no-repl-history to disable)`);
        }
        resolve();
      });
    });
  }

  replServer.displayPrompt(true);

  let resolveDone;

  replServer.on('exit', () => {
    closeLanguageWatchers();
    resolveDone();
  });

  await new Promise(resolve => {
    resolveDone = resolve;
  });

  return true;
}