-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarnpnp.js
115 lines (102 loc) · 4.13 KB
/
yarnpnp.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const https = require('https');
const fs = require('fs');
const path = require('path');
const getPluginUrl = plugin => `https://raw.githubusercontent.com/yarnpkg/berry/master/packages/plugin-${plugin}/bin/%40yarnpkg/plugin-${plugin}.js`;
const packageData = require('./package.json');
if (!packageData.packman) {
console.error('You must provide the `pacman` field in your package.json file!');
process.exit(0);
}
const REQUESTED_VERSION = require('./package.json').packman.split('@')[1];
const YARNRC_YML_PATH = path.join(__dirname, '.yarnrc.yml');
const PLUGIN_LIST = !fs.existsSync(YARNRC_YML_PATH) ? [] : fs.readFileSync(YARNRC_YML_PATH, 'utf-8')
.split('\n')
.filter(line => line.includes('.yarn/plugins/@yarnpkg/plugin-'))
.map(line => line.match(/.*plugin-(.*?)\.cjs/)[1]);
const YARN_URL = /^[0,1]\..*$/.test(REQUESTED_VERSION) ?
`https://github.com/yarnpkg/yarn/releases/download/v${REQUESTED_VERSION}/yarn-${REQUESTED_VERSION}.js` :
`https://raw.githubusercontent.com/yarnpkg/berry/%40yarnpkg/cli/${REQUESTED_VERSION}/packages/yarnpkg-cli/bin/yarn.js`;
const YARN_DIR = path.join(__dirname, '.yarn');
const RELEASES_DIR = path.join(YARN_DIR, 'releases');
const PLUGIN_DIR = path.join(YARN_DIR, 'plugins');
const YARN_BINARY = path.join(RELEASES_DIR, `yarn-${REQUESTED_VERSION}.cjs`);
let stats;
try {
stats = fs.statSync(RELEASES_DIR);
} catch (e) {
// Only catch to avoid crash
// It is valid to continue
}
const CURRENT_YARN_BINARYNAME = !stats ? null : fs.readdirSync(RELEASES_DIR)[0];
const CURRENT_VERSION = !stats ? null : path.basename(CURRENT_YARN_BINARYNAME).slice(0, -path.extname(CURRENT_YARN_BINARYNAME).length).replace('yarn-', '');
const launchBerry = () => {
const args = [];
for (const arg of process.argv) {
if (arg.indexOf('--production') === 0 || arg.indexOf('--ignore-engines') === 0)
continue;
else if (arg.indexOf('--frozen-lockfile') === 0)
args.push('--immutable');
else
args.push(arg);
}
process.argv = args;
delete process.env.YARN_PRODUCTION;
require(YARN_BINARY);
};
const downloadFile = async (filePath, url) => {
return new Promise((resolve, reject) => {
https.get(url, res => {
if (res.statusCode === 301 || res.statusCode === 302) {
downloadFile(filePath, res.headers.location).then(resolve).catch(reject);
} else if (res.statusCode !== 200) {
reject(`Error downloading ${url}, status: ${res.statusCode}`);
} else {
const file = fs.createWriteStream(filePath);
res.pipe(file);
file.on('finish', () => {
resolve();
});
}
}).on('error', err => {
console.error(err);
fs.unlink(filePath);
reject(err);
});
});
};
const promises = [];
if (CURRENT_VERSION !== REQUESTED_VERSION) {
if (CURRENT_YARN_BINARYNAME) {
fs.rmdirSync(RELEASES_DIR, { recursive: true });
fs.rmdirSync(PLUGIN_DIR, { recursive: true });
}
fs.mkdirSync(RELEASES_DIR, { recursive: true });
console.log('Downloading Yarn Berry...');
promises.push(downloadFile(YARN_BINARY, YARN_URL));
}
for (const plugin of PLUGIN_LIST) {
const pluginPath = path.join(PLUGIN_DIR, '@yarnpkg', `plugin-${plugin}.cjs`);
if (!fs.existsSync(pluginPath)) {
fs.mkdirSync(path.join(PLUGIN_DIR, '@yarnpkg'), { recursive: true });
console.log(`Downloading Yarn plugin ${plugin}...`);
promises.push(downloadFile(pluginPath, getPluginUrl(plugin)));
}
}
if (PLUGIN_LIST.length === 0) {
fs.rmdirSync(PLUGIN_DIR, { recursive: true });
} else {
const entries = fs.readdirSync(path.join(PLUGIN_DIR, '@yarnpkg'));
for (const entry of entries) {
const pluginName = entry.replace(/plugin-(.*)\.cjs/, '$1');
if (!PLUGIN_LIST.includes(pluginName))
fs.unlinkSync(path.join(PLUGIN_DIR, '@yarnpkg', `plugin-${pluginName}.cjs`));
}
}
(async () => {
try {
await Promise.all(promises);
launchBerry();
} catch (err) {
console.error(err);
}
})();