jsii-rosetta 5.3.4 → 5.3.5-dev.1
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/lib/fixtures.js +1 -1
- package/lib/fixtures.js.map +1 -1
- package/lib/support.js.map +1 -1
- package/lib/translate_all.js +2 -3
- package/lib/translate_all.js.map +1 -1
- package/package.json +1 -1
package/lib/fixtures.js
CHANGED
|
@@ -52,7 +52,7 @@ function loadLiterateSource(directory, literateFileName) {
|
|
|
52
52
|
// This couldn't really happen in practice, but do the check anyway
|
|
53
53
|
throw new Error(`Sample uses literate source ${literateFileName}, but not found: ${fullPath}`);
|
|
54
54
|
}
|
|
55
|
-
return fs.readFileSync(fullPath
|
|
55
|
+
return fs.readFileSync(fullPath, { encoding: 'utf-8' });
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
58
|
* Load the fixture with the given name, and substitute the source into it
|
package/lib/fixtures.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fixtures.js","sourceRoot":"","sources":["../src/fixtures.ts"],"names":[],"mappings":";;;AAAA,8BAA8B;AAC9B,kCAAkC;AAClC,2CAAoF;AAEpF,uCAA8E;AAE9E;;GAEG;AACH,SAAgB,SAAS,CAAC,OAA0B,EAAE,KAAK,GAAG,KAAK;IACjE,IAAI,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IACnC,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,EAAE,CAAC;IAE5C,MAAM,SAAS,GAAG,UAAU,CAAC,2BAAiB,CAAC,kBAAkB,CAAC,CAAC;IACnE,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,MAAM,cAAc,GAAG,UAAU,CAAC,2BAAiB,CAAC,eAAe,CAAC,CAAC;IACrE,IAAI,cAAc,EAAE,CAAC;QACnB,mEAAmE;QACnE,uEAAuE;QACvE,IAAI,CAAC;YACH,MAAM,GAA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CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,SAAS,MAAM,CAAC,EAAY;QAC1B,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACpC,CAAC;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,eAAe,CAAC,MAAc;IAIrC,IAAI,OAAO,GAAG,EAAE,CAAC;IACjB,IAAI,UAAU,GAAG,EAAE,CAAC;IAEpB,MAAM,UAAU,GAAG,IAAA,6BAAgB,EAAC,UAAU,EAAE,MAAM,EAAE,yBAAY,CAAC,MAAM,EAAE,IAAI,EAAE,uBAAU,CAAC,EAAE,CAAC,CAAC;IAClG,KAAK,MAAM,SAAS,IAAI,UAAU,CAAC,UAAU,EAAE,CAAC;QAC9C,IACE,SAAS,CAAC,IAAI,KAAK,uBAAU,CAAC,iBAAiB;YAC/C,SAAS,CAAC,IAAI,KAAK,uBAAU,CAAC,uBAAuB;YACrD,CAAC,SAAS,CAAC,IAAI,KAAK,uBAAU,CAAC,iBAAiB;gBAC9C,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,uBAAU,CAAC,cAAc,CAAC,EAC3E,CAAC;YACD,OAAO,IAAI,SAAS,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QAC/C,CAAC;aAAM,CAAC;YACN,UAAU,IAAI,SAAS,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AACjC,CAAC","sourcesContent":["import * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport { createSourceFile, ScriptKind, ScriptTarget, SyntaxKind } from 'typescript';\n\nimport { TypeScriptSnippet, SnippetParameters, ApiLocation } from './snippet';\n\n/**\n * Complete snippets with fixtures, if required\n */\nexport function fixturize(snippet: TypeScriptSnippet, loose = false): TypeScriptSnippet {\n let source = snippet.visibleSource;\n const parameters = snippet.parameters ?? {};\n\n const directory = parameters[SnippetParameters.$PROJECT_DIRECTORY];\n if (!directory) {\n return snippet;\n }\n\n const literateSource = parameters[SnippetParameters.LITERATE_SOURCE];\n if (literateSource) {\n // Compatibility with the \"old school\" example inclusion mechanism.\n // Completely load this file and attach a parameter with its directory.\n try {\n source = loadLiterateSource(directory, literateSource);\n } catch (ex) {\n // In loose mode, we ignore this failure and stick to the visible source.\n if (!loose) {\n throw ex;\n }\n }\n parameters[SnippetParameters.$COMPILATION_DIRECTORY] = path.join(directory, path.dirname(literateSource));\n } else if (parameters[SnippetParameters.FIXTURE]) {\n // Explicitly requested fixture must exist, unless we are operating in loose mode\n source = loadAndSubFixture(directory, snippet.location.api, parameters.fixture, source, !loose);\n } else if (parameters[SnippetParameters.NO_FIXTURE] === undefined) {\n // Don't explicitly request no fixture, load the default.\n source = loadAndSubFixture(directory, snippet.location.api, 'default', source, false);\n }\n\n return {\n ...snippet,\n completeSource: source,\n parameters,\n };\n}\n\nfunction loadLiterateSource(directory: string, literateFileName: string) {\n const fullPath = path.join(directory, literateFileName);\n const exists = fs.existsSync(fullPath);\n if (!exists) {\n // This couldn't really happen in practice, but do the check anyway\n throw new Error(`Sample uses literate source ${literateFileName}, but not found: ${fullPath}`);\n }\n return fs.readFileSync(fullPath).toString('utf-8');\n}\n\n/**\n * Load the fixture with the given name, and substitute the source into it\n *\n * If no fixture could be found and `mustExist` is true, and error will be thrown.\n *\n * In principle, the fixture we're looking for is `rosetta/FIXTURE.ts-fixture`.\n * However, we want to support an automatic transform of many small packages\n * combined into a single large package, perhaps into submodules (i.e., we want\n * to support monocdk), and in those cases the names of fixtures might conflict.\n * For example, all of them will have a `default.ts-fixture`, and there won't be\n * any explicit reference to that file anywhere... yet in the combined\n * monopackage we have to distinguish those fixtures.\n *\n * Therefore, we will consider submodule names as subdirectories, based on the\n * API location of the snippet we're fixturizing.\n *\n * (For example, the fixtures for a type called `monocdk.aws_s3.Bucket` will be\n * searched both in `rosetta/aws_s3/default.ts-fixture` as well as\n * `rosetta/default.ts-fixture`).\n */\nfunction loadAndSubFixture(\n directory: string,\n location: ApiLocation,\n fixtureName: string,\n source: string,\n mustExist: boolean,\n) {\n const candidates = fixtureCandidates(directory, fixtureName, location);\n const fixtureFileName = candidates.find((n) => fs.existsSync(n));\n\n if (!fixtureFileName) {\n if (mustExist) {\n throw new Error(`Sample uses fixture ${fixtureName}, but not found: ${JSON.stringify(candidates)}`);\n }\n return source;\n }\n\n const fixtureContents = fs.readFileSync(fixtureFileName, {\n encoding: 'utf-8',\n });\n\n const subRegex = /[/]{3}[ \\t]*here[ \\t]*$/im;\n if (!subRegex.test(fixtureContents)) {\n throw new Error(`Fixture does not contain '/// here': ${fixtureFileName}`);\n }\n\n const { imports, statements } = sidelineImports(source);\n const show = '/// !show';\n const hide = '/// !hide';\n\n const result = fixtureContents.replace(\n subRegex,\n [\n '// Code snippet begins after !show marker below',\n show,\n statements,\n hide,\n '// Code snippet ended before !hide marker above',\n ].join('\\n'),\n );\n\n return imports\n ? [\n '// Hoisted imports begin after !show marker below',\n show,\n imports,\n hide,\n '// Hoisted imports ended before !hide marker above',\n result,\n ].join('\\n')\n : result;\n}\n\nfunction fixtureCandidates(directory: string, fixtureName: string, location: ApiLocation): string[] {\n const ret = new Array<string>();\n const fileName = `${fixtureName}.ts-fixture`;\n const mods = submodules(location);\n\n ret.push(path.join(directory, 'rosetta', fileName));\n for (let i = 0; i < mods.length; i++) {\n ret.push(path.join(directory, 'rosetta', ...mods.slice(0, i + 1), fileName));\n }\n\n // Most specific one up front\n ret.reverse();\n return ret;\n}\n\n/**\n * Return the submodule parts from a given ApiLocation\n */\nfunction submodules(location: ApiLocation): string[] {\n switch (location.api) {\n case 'file':\n return [];\n case 'initializer':\n case 'member':\n case 'type':\n case 'parameter':\n return middle(location.fqn.split('.'));\n case 'moduleReadme':\n return location.moduleFqn.split('.').slice(1);\n }\n\n function middle(xs: string[]) {\n return xs.slice(1, xs.length - 1);\n }\n}\n\n/**\n * When embedding code fragments in a fixture, \"import\" statements must be\n * hoisted up to the top of the resulting document, as TypeScript only allows\n * those to be present in the top-level context of an ESM.\n *\n * @param source a block of TypeScript source\n *\n * @returns an object containing the import statements on one end, and the rest\n * on the other hand.\n */\nfunction sidelineImports(source: string): {\n imports: string;\n statements: string;\n} {\n let imports = '';\n let statements = '';\n\n const sourceFile = createSourceFile('index.ts', source, ScriptTarget.Latest, true, ScriptKind.TS);\n for (const statement of sourceFile.statements) {\n if (\n statement.kind === SyntaxKind.ImportDeclaration ||\n statement.kind === SyntaxKind.ImportEqualsDeclaration ||\n (statement.kind === SyntaxKind.VariableStatement &&\n statement.getChildAt(0).getChildAt(0).kind === SyntaxKind.DeclareKeyword)\n ) {\n imports += statement.getFullText(sourceFile);\n } else {\n statements += statement.getFullText(sourceFile);\n }\n }\n\n return { imports, statements };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"fixtures.js","sourceRoot":"","sources":["../src/fixtures.ts"],"names":[],"mappings":";;;AAAA,8BAA8B;AAC9B,kCAAkC;AAClC,2CAAoF;AAEpF,uCAA8E;AAE9E;;GAEG;AACH,SAAgB,SAAS,CAAC,OAA0B,EAAE,KAAK,GAAG,KAAK;IACjE,IAAI,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IACnC,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,EAAE,CAAC;IAE5C,MAAM,SAAS,GAAG,UAAU,CAAC,2BAAiB,CAAC,kBAAkB,CAAC,CAAC;IACnE,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,MAAM,cAAc,GAAG,UAAU,CAAC,2BAAiB,CAAC,eAAe,CAAC,CAAC;IACrE,IAAI,cAAc,EAAE,CAAC;QACnB,mEAAmE;QACnE,uEAAuE;QACvE,IAAI,CA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cAAc;YACjB,OAAO,QAAQ,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,SAAS,MAAM,CAAC,EAAY;QAC1B,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACpC,CAAC;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,eAAe,CAAC,MAAc;IAIrC,IAAI,OAAO,GAAG,EAAE,CAAC;IACjB,IAAI,UAAU,GAAG,EAAE,CAAC;IAEpB,MAAM,UAAU,GAAG,IAAA,6BAAgB,EAAC,UAAU,EAAE,MAAM,EAAE,yBAAY,CAAC,MAAM,EAAE,IAAI,EAAE,uBAAU,CAAC,EAAE,CAAC,CAAC;IAClG,KAAK,MAAM,SAAS,IAAI,UAAU,CAAC,UAAU,EAAE,CAAC;QAC9C,IACE,SAAS,CAAC,IAAI,KAAK,uBAAU,CAAC,iBAAiB;YAC/C,SAAS,CAAC,IAAI,KAAK,uBAAU,CAAC,uBAAuB;YACrD,CAAC,SAAS,CAAC,IAAI,KAAK,uBAAU,CAAC,iBAAiB;gBAC9C,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,uBAAU,CAAC,cAAc,CAAC,EAC3E,CAAC;YACD,OAAO,IAAI,SAAS,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QAC/C,CAAC;aAAM,CAAC;YACN,UAAU,IAAI,SAAS,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AACjC,CAAC","sourcesContent":["import * as fs from 'node:fs';\nimport * as path from 'node:path';\nimport { createSourceFile, ScriptKind, ScriptTarget, SyntaxKind } from 'typescript';\n\nimport { TypeScriptSnippet, SnippetParameters, ApiLocation } from './snippet';\n\n/**\n * Complete snippets with fixtures, if required\n */\nexport function fixturize(snippet: TypeScriptSnippet, loose = false): TypeScriptSnippet {\n let source = snippet.visibleSource;\n const parameters = snippet.parameters ?? {};\n\n const directory = parameters[SnippetParameters.$PROJECT_DIRECTORY];\n if (!directory) {\n return snippet;\n }\n\n const literateSource = parameters[SnippetParameters.LITERATE_SOURCE];\n if (literateSource) {\n // Compatibility with the \"old school\" example inclusion mechanism.\n // Completely load this file and attach a parameter with its directory.\n try {\n source = loadLiterateSource(directory, literateSource);\n } catch (ex) {\n // In loose mode, we ignore this failure and stick to the visible source.\n if (!loose) {\n throw ex;\n }\n }\n parameters[SnippetParameters.$COMPILATION_DIRECTORY] = path.join(directory, path.dirname(literateSource));\n } else if (parameters[SnippetParameters.FIXTURE]) {\n // Explicitly requested fixture must exist, unless we are operating in loose mode\n source = loadAndSubFixture(directory, snippet.location.api, parameters.fixture, source, !loose);\n } else if (parameters[SnippetParameters.NO_FIXTURE] === undefined) {\n // Don't explicitly request no fixture, load the default.\n source = loadAndSubFixture(directory, snippet.location.api, 'default', source, false);\n }\n\n return {\n ...snippet,\n completeSource: source,\n parameters,\n };\n}\n\nfunction loadLiterateSource(directory: string, literateFileName: string) {\n const fullPath = path.join(directory, literateFileName);\n const exists = fs.existsSync(fullPath);\n if (!exists) {\n // This couldn't really happen in practice, but do the check anyway\n throw new Error(`Sample uses literate source ${literateFileName}, but not found: ${fullPath}`);\n }\n return fs.readFileSync(fullPath, { encoding: 'utf-8' });\n}\n\n/**\n * Load the fixture with the given name, and substitute the source into it\n *\n * If no fixture could be found and `mustExist` is true, and error will be thrown.\n *\n * In principle, the fixture we're looking for is `rosetta/FIXTURE.ts-fixture`.\n * However, we want to support an automatic transform of many small packages\n * combined into a single large package, perhaps into submodules (i.e., we want\n * to support monocdk), and in those cases the names of fixtures might conflict.\n * For example, all of them will have a `default.ts-fixture`, and there won't be\n * any explicit reference to that file anywhere... yet in the combined\n * monopackage we have to distinguish those fixtures.\n *\n * Therefore, we will consider submodule names as subdirectories, based on the\n * API location of the snippet we're fixturizing.\n *\n * (For example, the fixtures for a type called `monocdk.aws_s3.Bucket` will be\n * searched both in `rosetta/aws_s3/default.ts-fixture` as well as\n * `rosetta/default.ts-fixture`).\n */\nfunction loadAndSubFixture(\n directory: string,\n location: ApiLocation,\n fixtureName: string,\n source: string,\n mustExist: boolean,\n) {\n const candidates = fixtureCandidates(directory, fixtureName, location);\n const fixtureFileName = candidates.find((n) => fs.existsSync(n));\n\n if (!fixtureFileName) {\n if (mustExist) {\n throw new Error(`Sample uses fixture ${fixtureName}, but not found: ${JSON.stringify(candidates)}`);\n }\n return source;\n }\n\n const fixtureContents = fs.readFileSync(fixtureFileName, {\n encoding: 'utf-8',\n });\n\n const subRegex = /[/]{3}[ \\t]*here[ \\t]*$/im;\n if (!subRegex.test(fixtureContents)) {\n throw new Error(`Fixture does not contain '/// here': ${fixtureFileName}`);\n }\n\n const { imports, statements } = sidelineImports(source);\n const show = '/// !show';\n const hide = '/// !hide';\n\n const result = fixtureContents.replace(\n subRegex,\n [\n '// Code snippet begins after !show marker below',\n show,\n statements,\n hide,\n '// Code snippet ended before !hide marker above',\n ].join('\\n'),\n );\n\n return imports\n ? [\n '// Hoisted imports begin after !show marker below',\n show,\n imports,\n hide,\n '// Hoisted imports ended before !hide marker above',\n result,\n ].join('\\n')\n : result;\n}\n\nfunction fixtureCandidates(directory: string, fixtureName: string, location: ApiLocation): string[] {\n const ret = new Array<string>();\n const fileName = `${fixtureName}.ts-fixture`;\n const mods = submodules(location);\n\n ret.push(path.join(directory, 'rosetta', fileName));\n for (let i = 0; i < mods.length; i++) {\n ret.push(path.join(directory, 'rosetta', ...mods.slice(0, i + 1), fileName));\n }\n\n // Most specific one up front\n ret.reverse();\n return ret;\n}\n\n/**\n * Return the submodule parts from a given ApiLocation\n */\nfunction submodules(location: ApiLocation): string[] {\n switch (location.api) {\n case 'file':\n return [];\n case 'initializer':\n case 'member':\n case 'type':\n case 'parameter':\n return middle(location.fqn.split('.'));\n case 'moduleReadme':\n return location.moduleFqn.split('.').slice(1);\n }\n\n function middle(xs: string[]) {\n return xs.slice(1, xs.length - 1);\n }\n}\n\n/**\n * When embedding code fragments in a fixture, \"import\" statements must be\n * hoisted up to the top of the resulting document, as TypeScript only allows\n * those to be present in the top-level context of an ESM.\n *\n * @param source a block of TypeScript source\n *\n * @returns an object containing the import statements on one end, and the rest\n * on the other hand.\n */\nfunction sidelineImports(source: string): {\n imports: string;\n statements: string;\n} {\n let imports = '';\n let statements = '';\n\n const sourceFile = createSourceFile('index.ts', source, ScriptTarget.Latest, true, ScriptKind.TS);\n for (const statement of sourceFile.statements) {\n if (\n statement.kind === SyntaxKind.ImportDeclaration ||\n statement.kind === SyntaxKind.ImportEqualsDeclaration ||\n (statement.kind === SyntaxKind.VariableStatement &&\n statement.getChildAt(0).getChildAt(0).kind === SyntaxKind.DeclareKeyword)\n ) {\n imports += statement.getFullText(sourceFile);\n } else {\n statements += statement.getFullText(sourceFile);\n }\n }\n\n return { imports, statements };\n}\n"]}
|
package/lib/support.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"support.js","sourceRoot":"","sources":["../src/support.ts"],"names":[],"mappings":";;;AAAA,qCAAuC;AACvC,2CAAiC;AACjC,yCAAiC;AACjC,+BAA+B;AAC/B,2CAA+C;AAE/C,MAAM,eAAe,GAAG,8BAA8B,CAAC;AACvD,MAAM,qBAAqB,GAAG,UAAa,CAAC;AA4B5C;;;;;;;;GAQG;AACI,KAAK,UAAU,4BAA4B;IAChD,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,CAAC;QACjC,OAAO;IACT,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,mBAAmB,EAAE,CAAC;IAEzC,IAAI,IAAI,CAAC,OAAO,IAAI,8BAAiB,EAAE,CAAC;QACtC,8CAA8C;QAC9C,OAAO;IACT,CAAC;IAED,MAAM,gBAAgB,GAAG,IA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MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACpE,CAAC;IAED,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1D,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IAC9C,MAAM,MAAM,GAAG,SAAS,CAAC,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAEvD,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACtB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/D,CAAC;IACD,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AACxB,CAAC","sourcesContent":["import { readFileSync } from 'node:fs';\nimport { get } from 'node:https';\nimport { join } from 'node:path';\nimport * as chalk from 'chalk';\nimport { versionMajorMinor } from 'typescript';\n\nconst SILENCE_ENV_VAR = 'JSII_SILENCE_SUPPORT_WARNING';\nconst THIRTY_DAYS_IN_MILLIS = 2_592_000_000;\n\n/** @internal */\nexport interface ReleasesDocument {\n /**\n * The release line that occupies the 'Current' stage.\n */\n readonly current: ReleaseLine;\n\n /**\n * The release line that occupies the 'Current' stage.\n */\n readonly currentMinVersionNumber: VersionNumber;\n /**\n * Release lines currently in 'Maintenance' with the date at which they are\n * planned to go into the 'End-of-Support' stage. This date should always be\n * set a minimum of 6 months in the future when a release line is added to the\n * list.\n */\n readonly maintenance: { readonly [release: ReleaseLine]: Date };\n /**\n * Release lines that are currently out-of-support. This is semantically\n * equivalent to being in the `maintenance` list with a past date, but offers\n * slightly faster look-up.\n */\n readonly endOfSupport?: readonly ReleaseLine[];\n}\n\n/**\n * Checks whether the current release line is close to End-of-Support (within\n * 30 days), or already in End-of-Support, and if that is the case, emits a\n * warning to call the user to action.\n *\n * It is possible for users to opt out of these notifications by setting the\n * `JSII_SILENCE_SUPPORT_WARNING` environment variable to any truthy value (that\n * is, any non-empty value).\n */\nexport async function emitSupportPolicyInformation() {\n if (process.env[SILENCE_ENV_VAR]) {\n return;\n }\n\n const data = await getReleasesDocument();\n\n if (data.current == versionMajorMinor) {\n // Current release is not close to deprecation\n return;\n }\n\n const endOfSupportDate = data.endOfSupport?.includes(versionMajorMinor)\n ? new Date(0)\n : data.maintenance[versionMajorMinor];\n if (endOfSupportDate == null) {\n // Don't know the status, so don't say anything...\n return;\n }\n\n const now = new Date();\n const inThirtyDays = new Date(now.getTime() + THIRTY_DAYS_IN_MILLIS);\n const alternatives = Object.entries(data.maintenance)\n .flatMap(([release, dateStr]) => {\n const date = new Date(dateStr);\n if (date <= inThirtyDays) {\n return [];\n }\n return [{ release, date }];\n })\n .reduce((acc, { release, date }) => {\n if (acc.length === 0) {\n acc.push('', 'Other actively supported release lines include:');\n }\n acc.push(`- ${release} (planned End-of-Support date: ${date.toISOString()})`);\n return acc;\n }, new Array<string>());\n if (endOfSupportDate <= now) {\n // End-of-Support already!\n veryVisibleMessage(\n chalk.bgRed.white.bold,\n `The ${versionMajorMinor} release line of jsii has reached End-of-Support.`,\n `We strongly recommend you upgrade to the current release line (${data.current}) at your earliest convenience.`,\n ...alternatives,\n );\n } else if (endOfSupportDate <= inThirtyDays) {\n // End-of-Support within 30 days!\n veryVisibleMessage(\n chalk.bgYellow.black,\n `The ${versionMajorMinor} release line of jsii will reach End-of-Support soon, on ${endOfSupportDate.toISOString()}.`,\n `We strongly recommend you upgrade to the current release line (${data.current}) at your earliest convenience.`,\n ...alternatives,\n );\n }\n}\n\n/**\n * Downloads the latest `releases.json` document from 'https://raw.githubusercontent.com/aws/jsii-rosetta/main/releases.json'\n * if possible, or falls back to the built-in version of that file if that fails in any way.\n */\nasync function getReleasesDocument(): Promise<ReleasesDocument> {\n const downloaded = await new Promise<string | undefined>((ok, ko) => {\n const request = get(\n new URL('https://raw.githubusercontent.com/aws/jsii-rosetta/main/releases.json'),\n (response) => {\n if (response.statusCode === 404) {\n return ok(undefined);\n }\n if (response.statusCode !== 200) {\n return ko(`received error response: HTTP ${response.statusCode} - ${response.statusMessage}`);\n }\n response.once('error', ko);\n const chunks = new Array<Buffer>();\n response.on('data', (chunk) => chunks.push(Buffer.from(chunk)));\n response.once('end', () => ok(Buffer.concat(chunks).toString('utf-8')));\n },\n );\n request.once('abort', () => ko('request aborted'));\n request.once('timeout', () => ko('request timed out'));\n request.once('error', ko);\n request.end();\n }).catch((cause) => {\n if (process.env.JSII_DEBUG) {\n console.error(`Could not download releases.json from GitHub: ${cause}`);\n }\n undefined;\n });\n\n return JSON.parse(downloaded ?? readFileSync(join(__dirname, '..', 'releases.json'), 'utf-8'), (key, value) => {\n if (key !== 'maintenance') {\n return value;\n }\n return Object.fromEntries(Object.entries(value).map(([release, date]) => [release, new Date(date as string)]));\n }) as ReleasesDocument;\n}\n\nfunction veryVisibleMessage(formatter: chalk.Chalk, ...lines: readonly string[]): void {\n if (lines.length === 0) {\n throw new Error(`At least one line of message must be provided!`);\n }\n\n const len = Math.max(...lines.map((line) => line.length));\n const border = formatter('!'.repeat(len + 8));\n const spacer = formatter(`!! ${' '.repeat(len)} !!`);\n\n console.error(border);\n console.error(spacer);\n for (const line of lines) {\n console.error(formatter(`!! ${line.padEnd(len, ' ')} !!`));\n }\n console.error(spacer);\n console.error(border);\n}\n\ntype ReleaseLine = `${number}.${number}`;\ntype VersionNumber = `${number}.${number}.${number}`;\n"]}
|
|
1
|
+
{"version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CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AACxB,CAAC","sourcesContent":["import { readFileSync } from 'node:fs';\nimport { get } from 'node:https';\nimport { join } from 'node:path';\nimport * as chalk from 'chalk';\nimport { versionMajorMinor } from 'typescript';\n\nconst SILENCE_ENV_VAR = 'JSII_SILENCE_SUPPORT_WARNING';\nconst THIRTY_DAYS_IN_MILLIS = 2_592_000_000;\n\n/** @internal */\nexport interface ReleasesDocument {\n /**\n * The release line that occupies the 'Current' stage.\n */\n readonly current: ReleaseLine;\n /**\n * Release lines currently in 'Maintenance' with the date at which they are\n * planned to go into the 'End-of-Support' stage. This date should always be\n * set a minimum of 6 months in the future when a release line is added to the\n * list.\n */\n readonly maintenance: { readonly [release: ReleaseLine]: Date };\n /**\n * Release lines that are currently out-of-support. This is semantically\n * equivalent to being in the `maintenance` list with a past date, but offers\n * slightly faster look-up.\n */\n readonly endOfSupport?: readonly ReleaseLine[];\n}\n\n/**\n * Checks whether the current release line is close to End-of-Support (within\n * 30 days), or already in End-of-Support, and if that is the case, emits a\n * warning to call the user to action.\n *\n * It is possible for users to opt out of these notifications by setting the\n * `JSII_SILENCE_SUPPORT_WARNING` environment variable to any truthy value (that\n * is, any non-empty value).\n */\nexport async function emitSupportPolicyInformation() {\n if (process.env[SILENCE_ENV_VAR]) {\n return;\n }\n\n const data = await getReleasesDocument();\n\n if (data.current == versionMajorMinor) {\n // Current release is not close to deprecation\n return;\n }\n\n const endOfSupportDate = data.endOfSupport?.includes(versionMajorMinor)\n ? new Date(0)\n : data.maintenance[versionMajorMinor];\n if (endOfSupportDate == null) {\n // Don't know the status, so don't say anything...\n return;\n }\n\n const now = new Date();\n const inThirtyDays = new Date(now.getTime() + THIRTY_DAYS_IN_MILLIS);\n const alternatives = Object.entries(data.maintenance)\n .flatMap(([release, dateStr]) => {\n const date = new Date(dateStr);\n if (date <= inThirtyDays) {\n return [];\n }\n return [{ release, date }];\n })\n .reduce((acc, { release, date }) => {\n if (acc.length === 0) {\n acc.push('', 'Other actively supported release lines include:');\n }\n acc.push(`- ${release} (planned End-of-Support date: ${date.toISOString()})`);\n return acc;\n }, new Array<string>());\n if (endOfSupportDate <= now) {\n // End-of-Support already!\n veryVisibleMessage(\n chalk.bgRed.white.bold,\n `The ${versionMajorMinor} release line of jsii has reached End-of-Support.`,\n `We strongly recommend you upgrade to the current release line (${data.current}) at your earliest convenience.`,\n ...alternatives,\n );\n } else if (endOfSupportDate <= inThirtyDays) {\n // End-of-Support within 30 days!\n veryVisibleMessage(\n chalk.bgYellow.black,\n `The ${versionMajorMinor} release line of jsii will reach End-of-Support soon, on ${endOfSupportDate.toISOString()}.`,\n `We strongly recommend you upgrade to the current release line (${data.current}) at your earliest convenience.`,\n ...alternatives,\n );\n }\n}\n\n/**\n * Downloads the latest `releases.json` document from 'https://raw.githubusercontent.com/aws/jsii-rosetta/main/releases.json'\n * if possible, or falls back to the built-in version of that file if that fails in any way.\n */\nasync function getReleasesDocument(): Promise<ReleasesDocument> {\n const downloaded = await new Promise<string | undefined>((ok, ko) => {\n const request = get(\n new URL('https://raw.githubusercontent.com/aws/jsii-rosetta/main/releases.json'),\n (response) => {\n if (response.statusCode === 404) {\n return ok(undefined);\n }\n if (response.statusCode !== 200) {\n return ko(`received error response: HTTP ${response.statusCode} - ${response.statusMessage}`);\n }\n response.once('error', ko);\n const chunks = new Array<Buffer>();\n response.on('data', (chunk) => chunks.push(Buffer.from(chunk)));\n response.once('end', () => ok(Buffer.concat(chunks).toString('utf-8')));\n },\n );\n request.once('abort', () => ko('request aborted'));\n request.once('timeout', () => ko('request timed out'));\n request.once('error', ko);\n request.end();\n }).catch((cause) => {\n if (process.env.JSII_DEBUG) {\n console.error(`Could not download releases.json from GitHub: ${cause}`);\n }\n undefined;\n });\n\n return JSON.parse(downloaded ?? readFileSync(join(__dirname, '..', 'releases.json'), 'utf-8'), (key, value) => {\n if (key !== 'maintenance') {\n return value;\n }\n return Object.fromEntries(Object.entries(value).map(([release, date]) => [release, new Date(date as string)]));\n }) as ReleasesDocument;\n}\n\nfunction veryVisibleMessage(formatter: chalk.Chalk, ...lines: readonly string[]): void {\n if (lines.length === 0) {\n throw new Error(`At least one line of message must be provided!`);\n }\n\n const len = Math.max(...lines.map((line) => line.length));\n const border = formatter('!'.repeat(len + 8));\n const spacer = formatter(`!! ${' '.repeat(len)} !!`);\n\n console.error(border);\n console.error(spacer);\n for (const line of lines) {\n console.error(formatter(`!! ${line.padEnd(len, ' ')} !!`));\n }\n console.error(spacer);\n console.error(border);\n}\n\ntype ReleaseLine = `${number}.${number}`;\n"]}
|
package/lib/translate_all.js
CHANGED
|
@@ -21,9 +21,8 @@ async function translateAll(snippets, includeCompilerDiagnostics) {
|
|
|
21
21
|
// help that much, or we become I/O-bound at some point. On my machine, using
|
|
22
22
|
// more than half the cores actually makes it slower.
|
|
23
23
|
// Cap to a reasonable top-level limit to prevent thrash on machines with many, many cores.
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
: Math.min(16, Math.max(1, Math.ceil(os.cpus().length / 2)));
|
|
24
|
+
const maxWorkers = parseInt(process.env.JSII_ROSETTA_MAX_WORKER_COUNT ?? '16');
|
|
25
|
+
const N = Math.min(maxWorkers, Math.max(1, Math.ceil(os.cpus().length / 2)));
|
|
27
26
|
const snippetArr = Array.from(snippets);
|
|
28
27
|
logging.info(`Translating ${snippetArr.length} snippets using ${N} workers`);
|
|
29
28
|
const pool = workerpool.pool(path.join(__dirname, 'translate_all_worker.js'), {
|
package/lib/translate_all.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"translate_all.js","sourceRoot":"","sources":["../src/translate_all.ts"],"names":[],"mappings":";;;AAAA,8BAA8B;AAC9B,kCAAkC;AAClC,yCAAyC;AAEzC,qCAAqC;AAErC,+CAAsD;AAItD;;;;;;;;;GASG;AACI,KAAK,UAAU,YAAY,CAChC,QAA6B,EAC7B,0BAAmC;I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;QAEF,MAAM,WAAW,GAAG,IAAI,KAAK,EAAqB,CAAC;QACnD,MAAM,kBAAkB,GAAG,IAAI,KAAK,EAAqB,CAAC;QAE1D,kBAAkB;QAClB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,WAAW,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC;YAC1C,kBAAkB,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,iBAAiB,CAAC,GAAG,CAAC,2BAAiB,CAAC,UAAU,CAAC,CAAC,CAAC;QAC3F,CAAC;QACD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,CAAC;IAC7C,CAAC;YAAS,CAAC;QACT,yBAAyB;QACzB,KAAK,IAAI,CAAC,SAAS,EAAE,CAAC;IACxB,CAAC;AACH,CAAC;AArCD,oCAqCC;AAED,SAAS,aAAa,CACpB,QAA6B,EAC7B,0BAAmC,EACnC,SAAS,GAAG,EAAE;IAEd,MAAM,GAAG,GAAG,EAAE,CAAC;IAEf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,SAAS,EAAE,CAAC;QACpD,GAAG,CAAC,IAAI,CAAC;YACP,QAAQ,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC;YAC1C,0BAA0B;SAC3B,CAAC,CAAC;IACL,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import * as os from 'node:os';\nimport * as path from 'node:path';\nimport * as workerpool from 'workerpool';\n\nimport * as logging from './logging';\nimport { TypeScriptSnippet } from './snippet';\nimport { TranslatedSnippet } from './tablets/tablets';\nimport { RosettaDiagnostic } from './translate';\nimport type { TranslateBatchRequest, TranslateBatchResponse } from './translate_all_worker';\n\n/**\n * Divide the work evenly over all processors by running 'translate_all_worker' in Worker Threads, then combine results\n *\n * The workers are fed small queues of work each. We used to divide the entire queue into N\n * but since the work is divided unevenly that led to some workers stopping early, idling while\n * waiting for more work.\n *\n * Never include 'translate_all_worker' directly, only do TypeScript type references (so that in\n * the script we may assume that 'worker_threads' successfully imports).\n */\nexport async function translateAll(\n snippets: TypeScriptSnippet[],\n includeCompilerDiagnostics: boolean,\n): Promise<TranslateAllResult> {\n // Use about half the advertised cores because hyperthreading doesn't seem to\n // help that much, or we become I/O-bound at some point. On my machine, using\n // more than half the cores actually makes it slower.\n // Cap to a reasonable top-level limit to prevent thrash on machines with many, many cores.\n const maxWorkers = parseInt(process.env.JSII_ROSETTA_MAX_WORKER_COUNT ?? '16');\n const N = Math.min(maxWorkers, Math.max(1, Math.ceil(os.cpus().length / 2)));\n const snippetArr = Array.from(snippets);\n logging.info(`Translating ${snippetArr.length} snippets using ${N} workers`);\n\n const pool = workerpool.pool(path.join(__dirname, 'translate_all_worker.js'), {\n maxWorkers: N,\n });\n\n try {\n const requests = batchSnippets(snippetArr, includeCompilerDiagnostics);\n\n const responses: TranslateBatchResponse[] = await Promise.all(\n requests.map((request) => pool.exec('translateBatch', [request])),\n );\n\n const diagnostics = new Array<RosettaDiagnostic>();\n const translatedSnippets = new Array<TranslatedSnippet>();\n\n // Combine results\n for (const response of responses) {\n diagnostics.push(...response.diagnostics);\n translatedSnippets.push(...response.translatedSchemas.map(TranslatedSnippet.fromSchema));\n }\n return { diagnostics, translatedSnippets };\n } finally {\n // Not waiting on purpose\n void pool.terminate();\n }\n}\n\nfunction batchSnippets(\n snippets: TypeScriptSnippet[],\n includeCompilerDiagnostics: boolean,\n batchSize = 10,\n): TranslateBatchRequest[] {\n const ret = [];\n\n for (let i = 0; i < snippets.length; i += batchSize) {\n ret.push({\n snippets: snippets.slice(i, i + batchSize),\n includeCompilerDiagnostics,\n });\n }\n\n return ret;\n}\n\nexport interface TranslateAllResult {\n translatedSnippets: TranslatedSnippet[];\n diagnostics: RosettaDiagnostic[];\n}\n"]}
|