Skip to content

Commit

Permalink
[now dev] Install builders with yarn instead of npm (#2272)
Browse files Browse the repository at this point in the history
* [now dev] Install builders with `yarn` instead of `npm`

Also invokes via `process.execPath` so that the pkg'd node is used
instead of a global one.

Depends on #2270.

* Install `yarn` to the builder cache dir

Not the `node_modules/.bin` dir, because `yarn` cleans up that directory
when installing modules, so it deletes itself.

* Remove unnecessary unit test

* Always install `yarn`

* Pass in the `yarnPath` to `installBuilders()`

* Restore unit test

* Remove unused `delimiter` import

Co-Authored-By: TooTallNate <n@n8.io>
  • Loading branch information
TooTallNate committed Apr 30, 2019
1 parent 37964c1 commit 5ec4545
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 27 deletions.
12 changes: 7 additions & 5 deletions src/commands/dev/lib/builder-cache.ts
Expand Up @@ -119,7 +119,7 @@ export async function cleanCacheDir(output: Output): Promise<void> {
/**
* Install a list of builders to the cache directory.
*/
export async function installBuilders(packagesSet: Set<string>): Promise<void> {
export async function installBuilders(packagesSet: Set<string>, yarnDir: string): Promise<void> {
const packages = Array.from(packagesSet);
if (
packages.length === 0 || (
Expand All @@ -130,6 +130,7 @@ export async function installBuilders(packagesSet: Set<string>): Promise<void> {
return;
}
const cacheDir = await builderDirPromise;
const yarnPath = join(yarnDir, 'yarn');
const buildersPkg = join(cacheDir, 'package.json');

// Pull the same version of `@now/build-utils` that now-cli is using
Expand All @@ -140,11 +141,12 @@ export async function installBuilders(packagesSet: Set<string>): Promise<void> {
);
try {
await execa(
'npm',
process.execPath,
[
'install',
'--save-exact',
'--no-package-lock',
yarnPath,
'add',
'--exact',
'--no-lockfile',
`${buildUtils}@${buildUtilsVersion}`,
...packages.filter(p => p !== '@now/static')
],
Expand Down
8 changes: 5 additions & 3 deletions src/commands/dev/lib/dev-server.ts
Expand Up @@ -54,7 +54,7 @@ export default class DevServer {
public env: EnvConfig;
public buildEnv: EnvConfig;
public files: BuilderInputs;
public yarnPath?: string;
public yarnPath: string;

private cachedNowJson: NowConfig | null;
private server: http.Server;
Expand All @@ -71,7 +71,9 @@ export default class DevServer {
this.env = {};
this.buildEnv = {};
this.files = {};
this.yarnPath = undefined;

// This gets updated when `start()` is invoked
this.yarnPath = '/';

this.cachedNowJson = null;
this.server = http.createServer(this.devServerHandler);
Expand Down Expand Up @@ -425,7 +427,7 @@ export default class DevServer {
(nowJson.builds || []).map((b: BuildConfig) => b.use)
);

await installBuilders(builders);
await installBuilders(builders, this.yarnPath);
await this.updateBuildMatches(nowJson);

// Now Builders that do not define a `shouldServe()` function need to be
Expand Down
20 changes: 2 additions & 18 deletions src/commands/dev/lib/yarn-installer.ts
@@ -1,6 +1,5 @@
import execa from 'execa';
import { tmpdir } from 'os';
import which from 'which-promise';
import { createHash } from 'crypto';
import {
mkdirp,
Expand All @@ -19,17 +18,6 @@ const YARN_VERSION = '1.15.2';
const YARN_SHA = '97efd1871117e60c24f157289d61a7595e142070';
const YARN_URL = `https://github.com/yarnpkg/yarn/releases/download/v${YARN_VERSION}/yarn-${YARN_VERSION}.js`;

async function whichYarn(output: Output): Promise<string | null> {
try {
const yarnPath = await which('yarn');
output.debug(`Found yarn in current $PATH at "${yarnPath}"`);
return yarnPath;
} catch (error) {
output.debug('Did not find yarn in current $PATH');
return null;
}
}

function plusxSync(file: string): void {
const s = statSync(file);
const newMode = s.mode | 64 | 8 | 1;
Expand Down Expand Up @@ -60,7 +48,7 @@ function getSha1(filePath: string): Promise<string | null> {

async function installYarn(output: Output): Promise<string> {
// Loosely based on https://yarnpkg.com/install.sh
const dirName = join(await builderDirPromise, 'node_modules', '.bin');
const dirName = await builderDirPromise;
const yarnBin = join(dirName, 'yarn');
const sha1 = await getSha1(yarnBin);

Expand Down Expand Up @@ -91,10 +79,6 @@ async function installYarn(output: Output): Promise<string> {
return dirName;
}

export async function getYarnPath(output: Output): Promise<string | undefined> {
const path = await whichYarn(output);
if (path) {
return dirname(path);
}
export async function getYarnPath(output: Output): Promise<string> {
return installYarn(output);
}
1 change: 0 additions & 1 deletion test/dev-server.unit.js
Expand Up @@ -57,4 +57,3 @@ test('do not install builders if there are no builds', async t => {

t.pass();
})

0 comments on commit 5ec4545

Please sign in to comment.