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