|
| 1 | +import fs from 'fs'; |
| 2 | + |
| 3 | +// Org where the packages are pushed |
| 4 | +const NPM_ORG = '@experimental-api-clients-automation/'; |
| 5 | + |
| 6 | +// Output formats |
| 7 | +const BROWSER_FORMATS = ['umd-browser', 'esm-browser', 'cjs-browser']; |
| 8 | +const NODE_FORMATS = ['cjs-node', 'esm-node']; |
| 9 | + |
| 10 | +// Utils package with default options |
| 11 | +const UTILS = { |
| 12 | + 'client-common': { |
| 13 | + dependencies: [], |
| 14 | + }, |
| 15 | + 'requester-browser-xhr': { |
| 16 | + external: ['dom'], |
| 17 | + dependencies: [`${NPM_ORG}client-common`], |
| 18 | + }, |
| 19 | + 'requester-node-http': { |
| 20 | + external: ['https', 'http', 'url'], |
| 21 | + dependencies: [`${NPM_ORG}client-common`], |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Returns the `UTILS` packages configuration with their default bundler options. |
| 27 | + */ |
| 28 | +function getUtilConfigs() { |
| 29 | + const commonOptions = { |
| 30 | + input: 'index.ts', |
| 31 | + formats: NODE_FORMATS, |
| 32 | + external: [], |
| 33 | + }; |
| 34 | + |
| 35 | + return Object.entries(UTILS).map(([key, utilOptions]) => { |
| 36 | + return { |
| 37 | + ...commonOptions, |
| 38 | + ...utilOptions, |
| 39 | + output: key, |
| 40 | + package: key, |
| 41 | + name: `${NPM_ORG}${key}`, |
| 42 | + }; |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Whether to build the given `utilClient` or not. |
| 48 | + */ |
| 49 | +function shouldBuildUtil(utilClient) { |
| 50 | + if (process.env.SKIP_UTILS === 'true') { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + if (!process.env.CI) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + // Checking existence of `dist` folder doesn't really guarantee the built files are up-to-date. |
| 59 | + // However, on the CI, it's very likely. |
| 60 | + return !fs.existsSync(path.resolve('packages', utilClient, 'dist')); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Reads available packages in the monorepo. |
| 65 | + */ |
| 66 | +function getAvailableClients(client) { |
| 67 | + const availableClients = fs |
| 68 | + .readdirSync('packages/') |
| 69 | + .filter((packageName) => !Object.keys(UTILS).includes(packageName)); |
| 70 | + |
| 71 | + return client === 'all' |
| 72 | + ? availableClients |
| 73 | + : availableClients.filter((availableClient) => availableClient === client); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Returns the packages to bundled based on environment variables and run conditions. |
| 78 | + */ |
| 79 | +export function getPackageConfigs() { |
| 80 | + const UTIL_CONFIGS = getUtilConfigs(); |
| 81 | + const CLIENT = process.env.CLIENT.replace(NPM_ORG, ''); |
| 82 | + |
| 83 | + if (CLIENT === 'utils') { |
| 84 | + return UTIL_CONFIGS; |
| 85 | + } |
| 86 | + |
| 87 | + if (Object.keys(UTILS).includes(CLIENT)) { |
| 88 | + return UTIL_CONFIGS.filter((config) => config.package === CLIENT); |
| 89 | + } |
| 90 | + |
| 91 | + const availableClients = getAvailableClients(CLIENT); |
| 92 | + |
| 93 | + if (availableClients.length === 0) { |
| 94 | + throw new Error(`No clients matches '${CLIENT}'.`); |
| 95 | + } |
| 96 | + |
| 97 | + const configs = availableClients.flatMap((packageName) => { |
| 98 | + const isAlgoliasearchClient = packageName === 'algoliasearch'; |
| 99 | + const commonConfig = { |
| 100 | + package: packageName, |
| 101 | + name: `${NPM_ORG}${packageName}`, |
| 102 | + output: packageName, |
| 103 | + dependencies: [`${NPM_ORG}client-common`], |
| 104 | + external: [], |
| 105 | + }; |
| 106 | + |
| 107 | + // This non-generated client is an aggregation of client, hence does not follow |
| 108 | + // the same build process. |
| 109 | + if (isAlgoliasearchClient) { |
| 110 | + commonConfig.name = packageName; |
| 111 | + commonConfig.dependencies = [ |
| 112 | + `${NPM_ORG}client-analytics`, |
| 113 | + `${NPM_ORG}client-common`, |
| 114 | + `${NPM_ORG}client-personalization`, |
| 115 | + `${NPM_ORG}client-search`, |
| 116 | + ]; |
| 117 | + } |
| 118 | + |
| 119 | + return [ |
| 120 | + // Browser build |
| 121 | + { |
| 122 | + ...commonConfig, |
| 123 | + input: 'builds/browser.ts', |
| 124 | + formats: BROWSER_FORMATS, |
| 125 | + external: ['dom'], |
| 126 | + dependencies: [ |
| 127 | + ...commonConfig.dependencies, |
| 128 | + `${NPM_ORG}/requester-browser-xhr`, |
| 129 | + ], |
| 130 | + globals: { |
| 131 | + [packageName]: packageName, |
| 132 | + }, |
| 133 | + }, |
| 134 | + // Node build |
| 135 | + { |
| 136 | + ...commonConfig, |
| 137 | + input: 'builds/node.ts', |
| 138 | + formats: NODE_FORMATS, |
| 139 | + dependencies: [ |
| 140 | + ...commonConfig.dependencies, |
| 141 | + `${NPM_ORG}/requester-node-http`, |
| 142 | + ], |
| 143 | + }, |
| 144 | + ]; |
| 145 | + }); |
| 146 | + |
| 147 | + return [ |
| 148 | + ...UTIL_CONFIGS.filter((config) => shouldBuildUtil(config.package)), |
| 149 | + ...configs, |
| 150 | + ]; |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Returns the license at the top of the UMD bundled file. |
| 155 | + */ |
| 156 | +export function createLicense(name, version) { |
| 157 | + return `/*! ${name}.umd.js | ${version} | © Algolia, inc. | https://github.com/algolia/algoliasearch-client-javascript */`; |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Bundlers with their output format and file name for the given client. |
| 162 | + */ |
| 163 | +export function createBundlers({ output, clientPath }) { |
| 164 | + return { |
| 165 | + 'esm-node': { |
| 166 | + file: `${clientPath}/dist/${output}.esm.node.js`, |
| 167 | + format: 'es', |
| 168 | + }, |
| 169 | + 'esm-browser': { |
| 170 | + file: `${clientPath}/dist/${output}.esm.browser.js`, |
| 171 | + format: 'es', |
| 172 | + }, |
| 173 | + 'umd-browser': { |
| 174 | + file: `${clientPath}/dist/${output}.umd.browser.js`, |
| 175 | + format: 'umd', |
| 176 | + }, |
| 177 | + 'cjs-node': { |
| 178 | + file: `${clientPath}/dist/${output}.cjs.node.js`, |
| 179 | + format: 'cjs', |
| 180 | + }, |
| 181 | + 'cjs-browser': { |
| 182 | + file: `${clientPath}/dist/${output}.cjs.browser.js`, |
| 183 | + format: 'cjs', |
| 184 | + }, |
| 185 | + }; |
| 186 | +} |
0 commit comments