jsii-rosetta 1.65.1 → 1.66.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.
@@ -56,9 +56,9 @@ export interface ExtractOptions {
56
56
  */
57
57
  readonly allowDirtyTranslations?: boolean;
58
58
  /**
59
- * Compress the implicit tablet files
59
+ * Compress the implicit tablet files.
60
60
  *
61
- * @default false
61
+ * @default - preserves the original compression status of each individual implicit tablet file.
62
62
  */
63
63
  readonly compressTablet?: boolean;
64
64
  /**
@@ -69,12 +69,14 @@ async function extractSnippets(assemblyLocations, options = {}) {
69
69
  // Save to individual tablet files
70
70
  if (options.writeToImplicitTablets ?? true) {
71
71
  await Promise.all(Object.entries(snippetsPerAssembly).map(async ([location, snips]) => {
72
- const asmTabletFile = path.join(location, options.compressTablet ? tablets_1.DEFAULT_TABLET_NAME_COMPRESSED : tablets_1.DEFAULT_TABLET_NAME);
72
+ // Compress the implicit tablet if explicitly asked to, otherwise compress only if the original tablet was compressed.
73
+ const compressedTablet = options.compressTablet ?? (0, assemblies_1.compressedTabletExists)(location);
74
+ const asmTabletFile = path.join(location, compressedTablet ? tablets_1.DEFAULT_TABLET_NAME_COMPRESSED : tablets_1.DEFAULT_TABLET_NAME);
73
75
  logging.debug(`Writing ${snips.length} translations to ${asmTabletFile}`);
74
76
  const translations = snips.map(({ key }) => translator.tablet.tryGetSnippet(key)).filter(util_1.isDefined);
75
77
  const asmTablet = new tablets_1.LanguageTablet();
76
78
  asmTablet.addSnippets(...translations);
77
- await asmTablet.save(asmTabletFile, options.compressTablet);
79
+ await asmTablet.save(asmTabletFile, compressedTablet);
78
80
  }));
79
81
  }
80
82
  // optionally append to the output file
@@ -37,6 +37,13 @@ export declare function loadAssemblies(assemblyLocations: readonly string[], val
37
37
  * Returns a map of { directory -> tablet }.
38
38
  */
39
39
  export declare function loadAllDefaultTablets(asms: readonly LoadedAssembly[]): Promise<Record<string, LanguageTablet>>;
40
+ /**
41
+ * Returns the location of the tablet file, either .jsii.tabl.json or .jsii.tabl.json.gz.
42
+ * Assumes that a tablet exists in the directory and if not, the ensuing behavior is
43
+ * handled by the caller of this function.
44
+ */
45
+ export declare function guessTabletLocation(directory: string): string;
46
+ export declare function compressedTabletExists(directory: string): boolean;
40
47
  export declare type AssemblySnippetSource = {
41
48
  type: 'markdown';
42
49
  markdown: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findContainingSubmodule = exports.findTypeLookupAssembly = exports.allTypeScriptSnippets = exports.allSnippetSources = exports.loadAllDefaultTablets = exports.loadAssemblies = exports.EXAMPLE_METADATA_JSDOCTAG = void 0;
3
+ exports.findContainingSubmodule = exports.findTypeLookupAssembly = exports.allTypeScriptSnippets = exports.allSnippetSources = exports.compressedTabletExists = exports.guessTabletLocation = exports.loadAllDefaultTablets = exports.loadAssemblies = exports.EXAMPLE_METADATA_JSDOCTAG = void 0;
4
4
  const spec = require("@jsii/spec");
5
5
  const spec_1 = require("@jsii/spec");
6
6
  const fs_1 = require("fs");
@@ -52,14 +52,24 @@ exports.loadAssemblies = loadAssemblies;
52
52
  * Returns a map of { directory -> tablet }.
53
53
  */
54
54
  async function loadAllDefaultTablets(asms) {
55
- return (0, util_1.mkDict)(await Promise.all(asms.map(async (a) => [a.directory, await tablets_1.LanguageTablet.fromOptionalFile(guessTabletLocation(a))])));
56
- function guessTabletLocation(a) {
57
- const defaultTablet = path.join(a.directory, tablets_1.DEFAULT_TABLET_NAME);
58
- const compDefaultTablet = path.join(a.directory, tablets_1.DEFAULT_TABLET_NAME_COMPRESSED);
59
- return fs.existsSync(defaultTablet) ? defaultTablet : compDefaultTablet;
60
- }
55
+ return (0, util_1.mkDict)(await Promise.all(asms.map(async (a) => [a.directory, await tablets_1.LanguageTablet.fromOptionalFile(guessTabletLocation(a.directory))])));
61
56
  }
62
57
  exports.loadAllDefaultTablets = loadAllDefaultTablets;
58
+ /**
59
+ * Returns the location of the tablet file, either .jsii.tabl.json or .jsii.tabl.json.gz.
60
+ * Assumes that a tablet exists in the directory and if not, the ensuing behavior is
61
+ * handled by the caller of this function.
62
+ */
63
+ function guessTabletLocation(directory) {
64
+ return compressedTabletExists(directory)
65
+ ? path.join(directory, tablets_1.DEFAULT_TABLET_NAME_COMPRESSED)
66
+ : path.join(directory, tablets_1.DEFAULT_TABLET_NAME);
67
+ }
68
+ exports.guessTabletLocation = guessTabletLocation;
69
+ function compressedTabletExists(directory) {
70
+ return fs.existsSync(path.join(directory, tablets_1.DEFAULT_TABLET_NAME_COMPRESSED));
71
+ }
72
+ exports.compressedTabletExists = compressedTabletExists;
63
73
  /**
64
74
  * Return all markdown and example snippets from the given assembly
65
75
  */
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Rosetta = exports.RosettaTabletReader = exports.UnknownSnippetMode = void 0;
4
- const path = require("path");
5
4
  const assemblies_1 = require("./jsii/assemblies");
6
5
  const logging = require("./logging");
7
6
  const markdown_1 = require("./markdown/markdown");
@@ -93,7 +92,7 @@ class RosettaTabletReader {
93
92
  * pacmak sends our way later on is not going to be enough to do that.
94
93
  */
95
94
  async addAssembly(assembly, assemblyDir) {
96
- const defaultTablet = path.join(assemblyDir, tablets_1.DEFAULT_TABLET_NAME);
95
+ const defaultTablet = (0, assemblies_1.guessTabletLocation)(assemblyDir);
97
96
  if (await (0, util_1.pathExists)(defaultTablet)) {
98
97
  try {
99
98
  await this.loadTabletFromFile(defaultTablet);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsii-rosetta",
3
- "version": "1.65.1",
3
+ "version": "1.66.0",
4
4
  "description": "Translate TypeScript code snippets to other languages",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
@@ -19,14 +19,14 @@
19
19
  "@types/commonmark": "^0.27.5",
20
20
  "@types/mock-fs": "^4.13.1",
21
21
  "@types/workerpool": "^6.1.0",
22
- "@types/semver": "^7.3.10",
23
- "jsii-build-tools": "1.65.1",
22
+ "@types/semver": "^7.3.12",
23
+ "jsii-build-tools": "1.66.0",
24
24
  "memory-streams": "^0.1.3",
25
25
  "mock-fs": "^5.1.4"
26
26
  },
27
27
  "dependencies": {
28
- "@jsii/check-node": "1.65.1",
29
- "@jsii/spec": "1.65.1",
28
+ "@jsii/check-node": "1.66.0",
29
+ "@jsii/spec": "1.66.0",
30
30
  "commonmark": "^0.30.0",
31
31
  "typescript": "~3.9.10",
32
32
  "@xmldom/xmldom": "^0.8.2",
@@ -35,7 +35,7 @@
35
35
  "semver": "^7.3.7",
36
36
  "semver-intersect": "^1.4.0",
37
37
  "fast-glob": "^3.2.11",
38
- "jsii": "1.65.1"
38
+ "jsii": "1.66.0"
39
39
  },
40
40
  "license": "Apache-2.0",
41
41
  "author": {
@@ -81,6 +81,57 @@ test('extract can compress implicit tablet file', async () => {
81
81
  await tablet.load(compImplicitTablet);
82
82
  expect(tablet.snippetKeys.length).toEqual(1);
83
83
  });
84
+ describe('extract behavior regarding compressing implicit tablets', () => {
85
+ function hasCompressedDefaultTablet(directory) {
86
+ return (fs.existsSync(path.join(directory, lib_1.DEFAULT_TABLET_NAME_COMPRESSED)) &&
87
+ !fs.existsSync(path.join(directory, lib_1.DEFAULT_TABLET_NAME)));
88
+ }
89
+ function hasUncompressedDefaultTablet(directory) {
90
+ return (!fs.existsSync(path.join(directory, lib_1.DEFAULT_TABLET_NAME_COMPRESSED)) &&
91
+ fs.existsSync(path.join(directory, lib_1.DEFAULT_TABLET_NAME)));
92
+ }
93
+ function hasBothDefaultTablets(directory) {
94
+ return (fs.existsSync(path.join(directory, lib_1.DEFAULT_TABLET_NAME_COMPRESSED)) &&
95
+ fs.existsSync(path.join(directory, lib_1.DEFAULT_TABLET_NAME)));
96
+ }
97
+ test('preserve uncompressed tablet by default', async () => {
98
+ // Run extract with compressTablet = false to create .jsii.tabl.json file
99
+ await extract.extractSnippets([assembly.moduleDirectory], {
100
+ ...defaultExtractOptions,
101
+ compressTablet: false,
102
+ });
103
+ expect(hasUncompressedDefaultTablet(assembly.moduleDirectory)).toBeTruthy();
104
+ await extract.extractSnippets([assembly.moduleDirectory], {
105
+ ...defaultExtractOptions,
106
+ });
107
+ expect(hasUncompressedDefaultTablet(assembly.moduleDirectory)).toBeTruthy();
108
+ });
109
+ test('preserve compressed tablet by default', async () => {
110
+ // Run extract with compressTablet = true to create .jsii.tabl.json.gz file
111
+ await extract.extractSnippets([assembly.moduleDirectory], {
112
+ ...defaultExtractOptions,
113
+ compressTablet: true,
114
+ });
115
+ expect(hasCompressedDefaultTablet(assembly.moduleDirectory)).toBeTruthy();
116
+ await extract.extractSnippets([assembly.moduleDirectory], {
117
+ ...defaultExtractOptions,
118
+ });
119
+ expect(hasCompressedDefaultTablet(assembly.moduleDirectory)).toBeTruthy();
120
+ });
121
+ test('respects the explicit call to compress the tablet', async () => {
122
+ // Run extract with compressTablet = false to create .jsii.tabl.json file
123
+ await extract.extractSnippets([assembly.moduleDirectory], {
124
+ ...defaultExtractOptions,
125
+ compressTablet: false,
126
+ });
127
+ expect(hasUncompressedDefaultTablet(assembly.moduleDirectory)).toBeTruthy();
128
+ await extract.extractSnippets([assembly.moduleDirectory], {
129
+ ...defaultExtractOptions,
130
+ compressTablet: true,
131
+ });
132
+ expect(hasBothDefaultTablets(assembly.moduleDirectory)).toBeTruthy();
133
+ });
134
+ });
84
135
  test('extract works from compressed test assembly', async () => {
85
136
  const compressedAssembly = testutil_1.TestJsiiModule.fromSource({
86
137
  'index.ts': `
@@ -1316,6 +1316,36 @@ test('will read translations from cache even if they are dirty', async () => {
1316
1316
  infusedAssembly.cleanup();
1317
1317
  }
1318
1318
  });
1319
+ test('will read translations from cache when tablet is compressed', async () => {
1320
+ const infusedAssembly = testutil_1.TestJsiiModule.fromSource({
1321
+ 'index.ts': `
1322
+ /**
1323
+ * ClassA
1324
+ *
1325
+ * @example x
1326
+ */
1327
+ export class ClassA {
1328
+ public someMethod() {
1329
+ }
1330
+ }
1331
+ `,
1332
+ }, {
1333
+ name: 'my_assembly',
1334
+ jsii: testutil_1.DUMMY_JSII_CONFIG,
1335
+ });
1336
+ try {
1337
+ // Run an extract and tell it to compress the tablet
1338
+ await (0, extract_1.extractSnippets)([infusedAssembly.moduleDirectory], {
1339
+ compressTablet: true,
1340
+ });
1341
+ await (0, transliterate_1.transliterateAssembly)([infusedAssembly.moduleDirectory], [target_language_1.TargetLanguage.PYTHON]);
1342
+ const translated = JSON.parse(await fs.promises.readFile(path.join(infusedAssembly.moduleDirectory, '.jsii.python'), 'utf-8'));
1343
+ expect(translated.types?.['my_assembly.ClassA'].docs?.example).toEqual('x');
1344
+ }
1345
+ finally {
1346
+ infusedAssembly.cleanup();
1347
+ }
1348
+ });
1319
1349
  test('will output to specified directory', async () => (0, testutil_1.withTemporaryDirectory)(async (tmpDir) => {
1320
1350
  // GIVEN
1321
1351
  const compilationResult = jsii.compileJsiiForTest({