Skip to content

Commit

Permalink
Adds support for nightlies
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison committed Nov 13, 2018
1 parent f835c34 commit 76f2964
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
57 changes: 40 additions & 17 deletions src/cli/commands/self.js
Expand Up @@ -83,13 +83,11 @@ async function fetchReleases(
return releases;
}

async function fetchBundle(config: Config, asset: ReleaseAsset): Promise<string> {
const data: Buffer = await config.requestManager.request({
url: asset.browser_download_url,
function fetchBundle(config: Config, url: string): Promise<Buffer> {
return config.requestManager.request({
url,
buffer: true,
});

return data.toString();
}

export function hasWrapper(flags: Object, args: Array<string>): boolean {
Expand All @@ -98,26 +96,51 @@ export function hasWrapper(flags: Object, args: Array<string>): boolean {

const {run, setFlags, examples} = buildSubCommands('self', {
async set(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
const releases = await fetchReleases(config);
let range = args[0] || 'latest';
let allowRc = flags.rc;

const release = releases.find(release => {
// $FlowFixMe
return semver.satisfies(release.version, args[0]);
});
if (range === 'rc') {
range = 'latest';
allowRc = true;
}

if (!release) {
throw new Error(`Release not found: ${args[0]}`);
if (range === 'latest') {
range = '*';
}

const asset = getBundleAsset(release);
invariant(asset, 'The bundle asset should exist');
let bundleUrl;
let bundleVersion;

if (range === 'nightly' || range === 'nightlies') {
bundleUrl = 'https://nightly.yarnpkg.com/latest.js';
bundleVersion = 'nightly';
} else {
const releases = await fetchReleases(config, {
includePrereleases: allowRc,
});

const release = releases.find(release => {
// $FlowFixMe
return semver.satisfies(release.version, range);
});

if (!release) {
throw new Error(`Release not found: ${range}`);
}

const asset = getBundleAsset(release);
invariant(asset, 'The bundle asset should exist');

bundleUrl = asset.browser_download_url;
bundleVersion = release.version.version;
}

reporter.log(`Downloading ${chalk.green(asset.name)}...`);
reporter.log(`Downloading ${chalk.green(bundleUrl)}...`);

const bundle = await fetchBundle(config, asset);
const bundle = await fetchBundle(config, bundleUrl);
const rc = getRcConfigForCwd(config.lockfileFolder, []);

const yarnPath = path.resolve(config.lockfileFolder, `.yarn/releases/${release.version.version}.js`);
const yarnPath = path.resolve(config.lockfileFolder, `.yarn/releases/yarn-${bundleVersion}.js`);
reporter.log(`Saving it into ${chalk.magenta(yarnPath)}...`);
await fs.mkdirp(path.dirname(yarnPath));
await fs.writeFile(yarnPath, bundle);
Expand Down
4 changes: 3 additions & 1 deletion src/util/fs.js
Expand Up @@ -28,7 +28,9 @@ export const lockQueue = new BlockingQueue('fs lock');

export const readFileBuffer = promisify(fs.readFile);
export const open: (path: string, flags: string, mode?: number) => Promise<Array<string>> = promisify(fs.open);
export const writeFile: (path: string, data: string, options?: Object) => Promise<void> = promisify(fs.writeFile);
export const writeFile: (path: string, data: string | Buffer, options?: Object) => Promise<void> = promisify(
fs.writeFile,
);
export const readlink: (path: string, opts: void) => Promise<string> = promisify(fs.readlink);
export const realpath: (path: string, opts: void) => Promise<string> = promisify(fs.realpath);
export const readdir: (path: string, opts: void) => Promise<Array<string>> = promisify(fs.readdir);
Expand Down

0 comments on commit 76f2964

Please sign in to comment.