@discord/intl-loader-core 0.22.0-rc.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -3,6 +3,7 @@ const {
3
3
  isMessageDefinitionsFile,
4
4
  isMessageTranslationsFile,
5
5
  IntlCompiledMessageFormat,
6
+ IntlDatabaseInsertStrategy,
6
7
  } = require('@discord/intl-message-database');
7
8
 
8
9
  const { database } = require('./src/database');
@@ -24,6 +25,9 @@ module.exports = {
24
25
  // @ts-expect-error This is a const enum, which TypeScript doesn't like letting you export, even
25
26
  // though it's a tangible object that can be accessed just fine from normal JS.
26
27
  IntlCompiledMessageFormat,
28
+ // @ts-expect-error This is a const enum, which TypeScript doesn't like letting you export, even
29
+ // though it's a tangible object that can be accessed just fine from normal JS.
30
+ IntlDatabaseInsertStrategy,
27
31
  MessageDefinitionsTransformer,
28
32
  database,
29
33
  findAllTranslationFiles,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/intl-loader-core",
3
- "version": "0.22.0-rc.0",
3
+ "version": "0.22.0",
4
4
  "license": "MIT",
5
5
  "description": "Core utilities for writing loaders and transformers using @discord/intl",
6
6
  "author": "Jon Egeland",
@@ -28,7 +28,7 @@
28
28
  "dependencies": {
29
29
  "chokidar": "^3.6.0",
30
30
  "debug": "^4.3.6",
31
- "@discord/intl-message-database": "0.22.0-rc.0"
31
+ "@discord/intl-message-database": "0.22.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/debug": "^4.1.12",
package/src/processing.js CHANGED
@@ -1,7 +1,10 @@
1
1
  const path = require('node:path');
2
2
 
3
3
  const debug = require('debug')('intl:loader-core');
4
- const { IntlCompiledMessageFormat } = require('@discord/intl-message-database');
4
+ const {
5
+ IntlCompiledMessageFormat,
6
+ IntlDatabaseInsertStrategy,
7
+ } = require('@discord/intl-message-database');
5
8
 
6
9
  const { database } = require('./database');
7
10
  const { findAllTranslationFiles, getLocaleFromTranslationsFileName } = require('./util');
@@ -111,10 +114,11 @@ function filterAllMessagesFiles(files, defaultLocale = 'en-US') {
111
114
  * with a `sourceContent` argument.
112
115
  *
113
116
  * @param {IntlMessagesFileDescriptor[]} files
117
+ * @param {IntlDatabaseInsertStrategy} [strategy]
114
118
  * @returns {IntlSourceFileInsertionData[]}
115
119
  */
116
- function processAllMessagesFiles(files) {
117
- return database.processAllMessagesFiles(files);
120
+ function processAllMessagesFiles(files, strategy = IntlDatabaseInsertStrategy.Update) {
121
+ return database.processAllMessagesFiles(files, strategy);
118
122
  }
119
123
 
120
124
  /**
@@ -123,6 +127,7 @@ function processAllMessagesFiles(files) {
123
127
  * @param {{
124
128
  * processTranslations?: boolean,
125
129
  * locale?: string
130
+ * strategy?: IntlDatabaseInsertStrategy
126
131
  * }=} options
127
132
  * @returns {ProcessDefinitionsResult}
128
133
  */
@@ -131,15 +136,16 @@ function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
131
136
  processTranslations = false,
132
137
  // TODO: Make this more configurable/automatically determined.
133
138
  locale = 'en-US',
139
+ strategy = IntlDatabaseInsertStrategy.Update,
134
140
  } = options;
135
141
  debug(`[${sourcePath}] Processing definitions with locale "${locale}"`);
136
142
 
137
143
  /** @type {IntlSourceFileInsertionData} */
138
144
  let result;
139
145
  if (sourceContent != null) {
140
- result = database.processDefinitionsFileContent(sourcePath, sourceContent, locale);
146
+ result = database.processDefinitionsFileContent(sourcePath, sourceContent, locale, strategy);
141
147
  } else {
142
- result = database.processDefinitionsFile(sourcePath, locale);
148
+ result = database.processDefinitionsFile(sourcePath, locale, strategy);
143
149
  }
144
150
  if (result.errors.length > 0) {
145
151
  return { succeeded: false, errors: result.errors };
@@ -162,7 +168,7 @@ function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
162
168
  );
163
169
 
164
170
  if (processTranslations) {
165
- const translationsResult = database.processAllTranslationFiles(translationsLocaleMap);
171
+ const translationsResult = database.processAllTranslationFiles(translationsLocaleMap, strategy);
166
172
  const allErrors = translationsResult.flatMap((result) => result.errors);
167
173
  if (allErrors.length > 0) {
168
174
  return { succeeded: false, errors: allErrors };
@@ -186,18 +192,22 @@ function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
186
192
  * @param {string=} sourceContent
187
193
  * @param {{
188
194
  * locale?: string,
195
+ * strategy?: IntlDatabaseInsertStrategy
189
196
  * }=} options
190
197
  * @returns {ProcessTranslationsResult}
191
198
  */
192
199
  function processTranslationsFile(sourcePath, sourceContent, options = {}) {
193
- const { locale = getLocaleFromTranslationsFileName(sourcePath) } = options;
200
+ const {
201
+ locale = getLocaleFromTranslationsFileName(sourcePath),
202
+ strategy = IntlDatabaseInsertStrategy.Update,
203
+ } = options;
194
204
 
195
205
  /** @type {IntlSourceFileInsertionData} */
196
206
  let result;
197
207
  if (sourceContent) {
198
- result = database.processTranslationFileContent(sourcePath, locale, sourceContent);
208
+ result = database.processTranslationFileContent(sourcePath, locale, sourceContent, strategy);
199
209
  } else {
200
- result = database.processTranslationFile(sourcePath, locale);
210
+ result = database.processTranslationFile(sourcePath, locale, strategy);
201
211
  }
202
212
  if (result.errors.length > 0) {
203
213
  return { succeeded: false, errors: result.errors };
package/src/watcher.js CHANGED
@@ -7,7 +7,6 @@ const {
7
7
  IntlCompiledMessageFormat,
8
8
  } = require('@discord/intl-message-database');
9
9
 
10
- const { database } = require('./database');
11
10
  const {
12
11
  processDefinitionsFile,
13
12
  precompileFileForLocale,
@@ -49,7 +48,7 @@ const DEFAULT_LOCALE = 'en-US';
49
48
  */
50
49
  async function watchMessagesFiles(
51
50
  watchedFolders,
52
- { ignore = [], skipInitial = false, once = true } = {}
51
+ { ignore = [], skipInitial = false, once = true } = {},
53
52
  ) {
54
53
  debug('Pre-initializing database with all discoverable messages files');
55
54
  const files = findAllMessagesFiles(watchedFolders);
@@ -75,7 +74,7 @@ async function watchMessagesFiles(
75
74
 
76
75
  const ignoredPatterns = ignore.concat(ALWAYS_IGNORE_PATTERNS);
77
76
  const globs = watchedFolders.flatMap((folder) =>
78
- MESSAGES_FILE_PATTERNS.map((pattern) => path.join(folder, pattern))
77
+ MESSAGES_FILE_PATTERNS.map((pattern) => path.join(folder, pattern)),
79
78
  );
80
79
 
81
80
  debug(`Initializing watch for patterns:\n- ${globs.join('\n- ')}`);
@@ -94,7 +93,7 @@ async function watchMessagesFiles(
94
93
  const [result] = processAllMessagesFiles(files);
95
94
  const failed = result.errors.length > 0 ? [result.fileKey] : [];
96
95
  const secondResult = processAllMessagesFiles(
97
- filterAllMessagesFiles([...reprocessQueue, ...failed])
96
+ filterAllMessagesFiles([...reprocessQueue, ...failed]),
98
97
  );
99
98
  // For any files that are still failing, keep them in the queue for next time.
100
99
  const secondFailures = secondResult
@@ -144,7 +143,7 @@ async function generateMessageTypes(watcher) {
144
143
  */
145
144
  async function precompileMessageDefinitionsFiles(
146
145
  watcher,
147
- { assetExtension = 'json', precompileOptions = {} } = {}
146
+ { assetExtension = 'json', precompileOptions = {} } = {},
148
147
  ) {
149
148
  watcher.on('all', (_, filePath) => {
150
149
  const { format = IntlCompiledMessageFormat.KeylessJson, bundleSecrets = false } =
@@ -156,7 +155,7 @@ async function precompileMessageDefinitionsFiles(
156
155
  // Convert the file name from `.messages.js` to `.compiled.messages.jsona` for output.
157
156
  const outputPath = filePath.replace(
158
157
  /\.messages\.js$/,
159
- `.compiled.messages.${assetExtension}`
158
+ `.compiled.messages.${assetExtension}`,
160
159
  );
161
160
  const result = processDefinitionsFile(filePath);
162
161
  if (!result.succeeded) {
@@ -165,7 +164,7 @@ async function precompileMessageDefinitionsFiles(
165
164
  return;
166
165
  }
167
166
 
168
- precompileFileForLocale(filePath, result.locale, undefined, {
167
+ precompileFileForLocale(filePath, result.locale, outputPath, {
169
168
  format,
170
169
  bundleSecrets,
171
170
  });
package/types/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { IntlCompiledMessageFormat } from "@discord/intl-message-database";
2
+ import { IntlDatabaseInsertStrategy } from "@discord/intl-message-database";
2
3
  import { MessageDefinitionsTransformer } from "./src/transformer";
3
4
  import { database } from "./src/database";
4
5
  import { findAllTranslationFiles } from "./src/util";
@@ -15,4 +16,4 @@ import { findAllMessagesFiles } from "./src/processing";
15
16
  import { filterAllMessagesFiles } from "./src/processing";
16
17
  import { processAllMessagesFiles } from "./src/processing";
17
18
  import watcher = require("./src/watcher");
18
- export { IntlCompiledMessageFormat, MessageDefinitionsTransformer, database, findAllTranslationFiles, findAllDefinitionsFilesForTranslations, getLocaleFromTranslationsFileName, generateTypeDefinitions, hashMessageKey, isMessageDefinitionsFile, isMessageTranslationsFile, processDefinitionsFile, processTranslationsFile, precompileFileForLocale, findAllMessagesFiles, filterAllMessagesFiles, processAllMessagesFiles, watcher };
19
+ export { IntlCompiledMessageFormat, IntlDatabaseInsertStrategy, MessageDefinitionsTransformer, database, findAllTranslationFiles, findAllDefinitionsFilesForTranslations, getLocaleFromTranslationsFileName, generateTypeDefinitions, hashMessageKey, isMessageDefinitionsFile, isMessageTranslationsFile, processDefinitionsFile, processTranslationsFile, precompileFileForLocale, findAllMessagesFiles, filterAllMessagesFiles, processAllMessagesFiles, watcher };
@@ -45,9 +45,10 @@ export function filterAllMessagesFiles(files: string[], defaultLocale?: string):
45
45
  * with a `sourceContent` argument.
46
46
  *
47
47
  * @param {IntlMessagesFileDescriptor[]} files
48
+ * @param {IntlDatabaseInsertStrategy} [strategy]
48
49
  * @returns {IntlSourceFileInsertionData[]}
49
50
  */
50
- export function processAllMessagesFiles(files: IntlMessagesFileDescriptor[]): IntlSourceFileInsertionData[];
51
+ export function processAllMessagesFiles(files: IntlMessagesFileDescriptor[], strategy?: IntlDatabaseInsertStrategy | undefined): IntlSourceFileInsertionData[];
51
52
  /**
52
53
  * Generate a `.d.ts` file containing TypeScript type definitions for all of the messages defined in
53
54
  * `sourcePath`. This method does not process `sourcePath` at all, meaning it expects the database
@@ -93,12 +94,14 @@ export function precompileFileForLocale(sourcePath: string, locale: string, outp
93
94
  * @param {{
94
95
  * processTranslations?: boolean,
95
96
  * locale?: string
97
+ * strategy?: IntlDatabaseInsertStrategy
96
98
  * }=} options
97
99
  * @returns {ProcessDefinitionsResult}
98
100
  */
99
101
  export function processDefinitionsFile(sourcePath: string, sourceContent?: string | undefined, options?: {
100
102
  processTranslations?: boolean;
101
103
  locale?: string;
104
+ strategy?: IntlDatabaseInsertStrategy;
102
105
  } | undefined): ProcessDefinitionsResult;
103
106
  /**
104
107
  *
@@ -106,14 +109,17 @@ export function processDefinitionsFile(sourcePath: string, sourceContent?: strin
106
109
  * @param {string=} sourceContent
107
110
  * @param {{
108
111
  * locale?: string,
112
+ * strategy?: IntlDatabaseInsertStrategy
109
113
  * }=} options
110
114
  * @returns {ProcessTranslationsResult}
111
115
  */
112
116
  export function processTranslationsFile(sourcePath: string, sourceContent?: string | undefined, options?: {
113
117
  locale?: string;
118
+ strategy?: IntlDatabaseInsertStrategy;
114
119
  } | undefined): ProcessTranslationsResult;
115
120
  import { IntlCompiledMessageFormat } from "@discord/intl-message-database";
116
121
  import type { IntlMessagesFileDescriptor } from '../types';
122
+ import { IntlDatabaseInsertStrategy } from "@discord/intl-message-database";
117
123
  import type { IntlSourceFileInsertionData } from '../types';
118
124
  import type { ProcessDefinitionsResult } from '../types';
119
125
  import type { ProcessTranslationsResult } from '../types';