|
| 1 | +/** |
| 2 | + * Workaround for failing `yarn add -D -W typescript@next` |
| 3 | + * See https://github.com/yarnpkg/yarn/issues/7935 |
| 4 | + * |
| 5 | + * Given an environment variable called `TYPESCRIPT_DIST_TAG` fetch the corresponding |
| 6 | + * version and make sure this version used in the worktree as well as in dtslint. |
| 7 | + * |
| 8 | + * If you work on this file: |
| 9 | + * WARNING: This script can only use built-in modules since it has to run before |
| 10 | + * `yarn install` |
| 11 | + */ |
| 12 | +const childProcess = require('child_process'); |
| 13 | +const fs = require('fs'); |
| 14 | +const os = require('os'); |
| 15 | +const path = require('path'); |
| 16 | +const { promisify } = require('util'); |
| 17 | + |
| 18 | +const exec = promisify(childProcess.exec); |
| 19 | + |
| 20 | +async function main(distTag) { |
| 21 | + if (typeof distTag !== 'string') { |
| 22 | + throw new TypeError(`expected distTag: string but got '${distTag}'`); |
| 23 | + } |
| 24 | + |
| 25 | + if (distTag === 'stable') { |
| 26 | + // eslint-disable-next-line no-console |
| 27 | + console.log('nothing to do with stable'); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const { stdout: versions } = await exec(`npm dist-tag ls typescript ${distTag}`); |
| 32 | + const tagMapping = versions.split('\n').find(mapping => { |
| 33 | + return mapping.startsWith(`${distTag}: `); |
| 34 | + }); |
| 35 | + if (tagMapping === undefined) { |
| 36 | + throw new Error(`Could not find '${distTag}' in "${versions}"`); |
| 37 | + } |
| 38 | + |
| 39 | + const version = tagMapping.replace(`${distTag}: `, ''); |
| 40 | + |
| 41 | + const packageJsonPath = path.resolve(__dirname, '../package.json'); |
| 42 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' })); |
| 43 | + |
| 44 | + packageJson.devDependencies.typescript = version; |
| 45 | + packageJson.resolutions['**/dtslint/typescript'] = version; |
| 46 | + |
| 47 | + // CircleCI seemingly times out if it has a newline diff at the end |
| 48 | + fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}${os.EOL}`); |
| 49 | +} |
| 50 | + |
| 51 | +main(process.env.TYPESCRIPT_DIST_TAG).catch(error => { |
| 52 | + console.error(error); |
| 53 | + process.exit(1); |
| 54 | +}); |
0 commit comments