|
1 | 1 | const fs = require("fs"); |
2 | 2 | const path = require("path"); |
3 | | -const assert = require("assert"); |
4 | 3 |
|
5 | 4 | const root = path.join(__dirname, ".."); |
6 | 5 |
|
7 | 6 | const pkgJson = require(path.join(root, "package.json")); |
8 | | -const srcFolders = fs.readdirSync(path.join(root, "src")); |
| 7 | +const tsconfigs = { |
| 8 | + cjs: require(path.join(root, "tsconfig.cjs.json")), |
| 9 | + es: require(path.join(root, "tsconfig.es.json")), |
| 10 | + types: require(path.join(root, "tsconfig.types.json")), |
| 11 | +}; |
| 12 | +const submodules = fs.readdirSync(path.join(root, "src", "submodules")); |
9 | 13 |
|
10 | | -assert(pkgJson.exports === undefined, "We cannot support package.json exports yet."); |
| 14 | +const errors = []; |
11 | 15 |
|
| 16 | +for (const submodule of submodules) { |
| 17 | + const submodulePath = path.join(root, "src", "submodules", submodule); |
| 18 | + if (fs.existsSync(submodulePath) && fs.lstatSync(submodulePath).isDirectory()) { |
| 19 | + // package.json metadata. |
| 20 | + if (!pkgJson.exports[`./${submodule}`]) { |
| 21 | + errors.push(`${submodule} submodule is missing exports statement in package.json`); |
| 22 | + pkgJson.exports[`./${submodule}`] = { |
| 23 | + node: `./dist-cjs/submodules/${submodule}/index.js`, |
| 24 | + import: `./dist-es/submodules/${submodule}/index.js`, |
| 25 | + require: `./dist-cjs/submodules/${submodule}/index.js`, |
| 26 | + types: `./dist-types/submodules/${submodule}/index.d.ts`, |
| 27 | + }; |
| 28 | + fs.writeFileSync(path.join(root, "package.json"), JSON.stringify(pkgJson, null, 2) + "\n"); |
| 29 | + } |
| 30 | + if (!pkgJson.files.includes(`./${submodule}.js`)) { |
| 31 | + pkgJson.files.push(`./${submodule}.js`); |
| 32 | + errors.push(`package.json files array missing ${submodule}.js compatibility redirect file.`); |
| 33 | + fs.writeFileSync(path.join(root, "package.json"), JSON.stringify(pkgJson, null, 2) + "\n"); |
| 34 | + } |
| 35 | + // tsconfig metadata. |
| 36 | + for (const [kind, tsconfig] of Object.entries(tsconfigs)) { |
| 37 | + if (!tsconfig.compilerOptions.paths?.[`@aws-sdk/core/${submodule}`]) { |
| 38 | + errors.push(`${submodule} submodule is missing paths entry in tsconfig.${kind}.json`); |
| 39 | + |
| 40 | + tsconfig.compilerOptions.paths[`@aws-sdk/core/${submodule}`] = [`./src/submodules/${submodule}/index.ts`]; |
| 41 | + fs.writeFileSync(path.join(root, `tsconfig.${kind}.json`), JSON.stringify(tsconfig, null, 2) + "\n"); |
| 42 | + } |
| 43 | + } |
| 44 | + // compatibility redirect file. |
| 45 | + const compatibilityRedirectFile = path.join(root, `${submodule}.js`); |
| 46 | + if (!fs.existsSync(compatibilityRedirectFile)) { |
| 47 | + errors.push(`${submodule} is missing compatibility redirect file in the package root folder.`); |
| 48 | + fs.writeFileSync( |
| 49 | + compatibilityRedirectFile, |
| 50 | + ` |
12 | 51 | /** |
13 | | - * We probably can't enable package.json exports until |
14 | | - * dropping support for Node.js 14.x and TypeScript 4.6. |
| 52 | + * Do not edit: |
| 53 | + * This is a compatibility redirect for contexts that do not understand package.json exports field. |
15 | 54 | */ |
16 | | -process.exit(0); |
17 | | - |
18 | | -for (const srcFolder of srcFolders) { |
19 | | - if (fs.lstatSync(path.join(root, "src", srcFolder)).isDirectory()) { |
20 | | - if (!pkgJson.exports["./" + srcFolder]) { |
21 | | - throw new Error(`${srcFolder} is missing exports statement in package.json`); |
| 55 | +module.exports = require("./dist-cjs/submodules/${submodule}/index.js"); |
| 56 | +` |
| 57 | + ); |
22 | 58 | } |
23 | 59 | } |
24 | 60 | } |
| 61 | + |
| 62 | +/** |
| 63 | + * Check for cross-submodule relative imports. |
| 64 | + */ |
| 65 | + |
| 66 | +const walk = require("../../../scripts/utils/walk"); |
| 67 | + |
| 68 | +(async () => { |
| 69 | + for await (const item of walk(path.join(root, "src", "submodules"))) { |
| 70 | + // depth within the submodule where 1 is at the root of the submodule. |
| 71 | + const depth = item.split("core/src/submodules/")[1].split("/").length - 1; |
| 72 | + const sourceCode = fs.readFileSync(item, "utf-8"); |
| 73 | + |
| 74 | + const relativeImports = []; |
| 75 | + relativeImports.push( |
| 76 | + ...new Set( |
| 77 | + [...(sourceCode.toString().match(/(from |import\()"(.*?)";/g) || [])] |
| 78 | + .map((_) => _.replace(/from "/g, "").replace(/";$/, "")) |
| 79 | + .filter((_) => _.startsWith(".")) |
| 80 | + ) |
| 81 | + ); |
| 82 | + |
| 83 | + for (const i of relativeImports) { |
| 84 | + const relativeImportDepth = i.split("..").length - 1; |
| 85 | + if (relativeImportDepth >= depth) { |
| 86 | + errors.push( |
| 87 | + `relative import ${i} in ${item |
| 88 | + .split("packages/") |
| 89 | + .pop()} crosses submodule boundaries. Use @scope/package/submodule import instead.` |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +})().then(() => { |
| 95 | + if (errors.length) { |
| 96 | + throw new Error(errors.join("\n")); |
| 97 | + } |
| 98 | +}); |
0 commit comments