Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Make yarn run faster #6188

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
12 changes: 4 additions & 8 deletions packages/plugin-constraints/sources/constraintUtils.ts
@@ -1,9 +1,5 @@
import {Configuration, formatUtils, Manifest, miscUtils, nodeUtils, Project, treeUtils, Workspace} from '@yarnpkg/core';
import {PortablePath} from '@yarnpkg/fslib';
import get from 'lodash/get';
import set from 'lodash/set';
import toPath from 'lodash/toPath';
import unset from 'lodash/unset';

export type ProcessResult = {
manifestUpdates: Map<PortablePath, Map<string, Map<any, Set<nodeUtils.Caller>>>>;
Expand Down Expand Up @@ -125,7 +121,7 @@ function isKnownDict(parts: Array<string>, index: number) {
export function normalizePath(p: Array<string> | string) {
const parts = Array.isArray(p)
? p
: toPath(p);
: miscUtils.toPath(p);

const normalizedParts = parts.map((part, t) => {
if (numberRegExp.test(part))
Expand Down Expand Up @@ -202,7 +198,7 @@ export function applyEngineReport(project: Project, {manifestUpdates, reportedEr
} else {
const [[newValue]] = newValues;

const currentValue = get(manifest, fieldPath);
const currentValue = miscUtils.get(manifest, fieldPath);
if (JSON.stringify(currentValue) === JSON.stringify(newValue))
continue;

Expand All @@ -218,9 +214,9 @@ export function applyEngineReport(project: Project, {manifestUpdates, reportedEr
}

if (typeof newValue === `undefined`)
unset(manifest, fieldPath);
miscUtils.unset(manifest, fieldPath);
else
set(manifest, fieldPath, newValue);
miscUtils.set(manifest, fieldPath, newValue);

changedWorkspace = true;
}
Expand Down
13 changes: 6 additions & 7 deletions packages/plugin-constraints/sources/tauModule.ts
@@ -1,10 +1,9 @@
/// <reference path="./tauProlog.d.ts"/>

import {Project, structUtils} from '@yarnpkg/core';
import {PortablePath} from '@yarnpkg/fslib';
import getPath from 'lodash/get';
import pl from 'tau-prolog';
import vm from 'vm';
import {Project, miscUtils, structUtils} from '@yarnpkg/core';
import {PortablePath} from '@yarnpkg/fslib';
import pl from 'tau-prolog';
import vm from 'vm';

// eslint-disable-next-line @typescript-eslint/naming-convention
const {is_atom: isAtom, is_variable: isVariable, is_instantiated_list: isInstantiatedList} = pl.type;
Expand Down Expand Up @@ -78,7 +77,7 @@ const tauModule = new pl.type.Module(`constraints`, {
if (workspace == null)
return;

const value = getPath(workspace.manifest.raw!, fieldName.id);
const value = miscUtils.get(workspace.manifest.raw!, miscUtils.toPath(fieldName.id));

// Field is not present => this predicate can never match
if (typeof value === `undefined`)
Expand Down Expand Up @@ -125,7 +124,7 @@ const tauModule = new pl.type.Module(`constraints`, {
if (workspace == null)
return;

const value = getPath(workspace.manifest.raw!, fieldName.id);
const value = miscUtils.get(workspace.manifest.raw!, miscUtils.toPath(fieldName.id));

// Field is not present => this predicate can never match
if (typeof value === `undefined`)
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-essentials/sources/commands/config/get.ts
@@ -1,7 +1,6 @@
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, StreamReport, miscUtils} from '@yarnpkg/core';
import {Command, Option, Usage, UsageError} from 'clipanion';
import getPath from 'lodash/get';
import {inspect} from 'util';

// eslint-disable-next-line arca/no-default-export
Expand Down Expand Up @@ -66,7 +65,7 @@ export default class ConfigGetCommand extends BaseCommand {

const asObject = miscUtils.convertMapsToIndexableObjects(displayedValue);
const requestedObject = path
? getPath(asObject, path)
? miscUtils.get(asObject, miscUtils.toPath(path))
: asObject;

const report = await StreamReport.start({
Expand Down
9 changes: 3 additions & 6 deletions packages/plugin-essentials/sources/commands/config/set.ts
@@ -1,9 +1,6 @@
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, StreamReport, MessageName, miscUtils} from '@yarnpkg/core';
import {Command, Option, Usage, UsageError} from 'clipanion';
import cloneDeep from 'lodash/cloneDeep';
import getPath from 'lodash/get';
import setPath from 'lodash/set';
import {inspect} from 'util';

// eslint-disable-next-line arca/no-default-export
Expand Down Expand Up @@ -84,8 +81,8 @@ export default class ConfigSetCommand extends BaseCommand {

await updateConfiguration(current => {
if (path) {
const clone = cloneDeep(current);
setPath(clone, this.name, value);
const clone = miscUtils.cloneDeep(current);
miscUtils.set(clone, miscUtils.toPath(this.name), value);
return clone;
} else {
return {
Expand All @@ -103,7 +100,7 @@ export default class ConfigSetCommand extends BaseCommand {

const asObject = miscUtils.convertMapsToIndexableObjects(displayedValue);
const requestedObject = path
? getPath(asObject, path)
? miscUtils.get(asObject, miscUtils.toPath(path))
: asObject;

const report = await StreamReport.start({
Expand Down
15 changes: 6 additions & 9 deletions packages/plugin-essentials/sources/commands/config/unset.ts
@@ -1,9 +1,6 @@
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, StreamReport, MessageName} from '@yarnpkg/core';
import {Command, Option, Usage, UsageError} from 'clipanion';
import cloneDeep from 'lodash/cloneDeep';
import hasPath from 'lodash/has';
import unsetPath from 'lodash/unset';
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, StreamReport, MessageName, miscUtils} from '@yarnpkg/core';
import {Command, Option, Usage, UsageError} from 'clipanion';

// eslint-disable-next-line arca/no-default-export
export default class ConfigUnsetCommand extends BaseCommand {
Expand Down Expand Up @@ -63,17 +60,17 @@ export default class ConfigUnsetCommand extends BaseCommand {
}, async report => {
let bailedOutEarly = false;
await updateConfiguration(current => {
if (!hasPath(current, this.name)) {
if (!miscUtils.hasPath(current, miscUtils.toPath(this.name))) {
report.reportWarning(MessageName.UNNAMED, `Configuration doesn't contain setting ${this.name}; there is nothing to unset`);
bailedOutEarly = true;
return current;
}

const clone = path
? cloneDeep(current)
? miscUtils.cloneDeep(current)
: {...current};

unsetPath(clone, this.name);
miscUtils.unset(clone, miscUtils.toPath(this.name));
return clone;
});

Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-git/sources/gitUtils.ts
Expand Up @@ -2,7 +2,6 @@ import {Configuration, Hooks, Locator, Project, execUtils, httpUtils, miscUtils,
import {Filename, npath, PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import {UsageError} from 'clipanion';
import GitUrlParse from 'git-url-parse';
import capitalize from 'lodash/capitalize';
import querystring from 'querystring';
import semver from 'semver';

Expand Down Expand Up @@ -393,7 +392,7 @@ async function git(message: string, args: Array<string>, opts: Omit<execUtils.Ex

const label = errorName === `error`
? `Error`
: `${capitalize(errorName)} Error`;
: `${miscUtils.capitalize(errorName)} Error`;

report.reportError(MessageName.EXCEPTION, ` ${formatUtils.prettyField(configuration, {
label,
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-init/sources/commands/init.ts
Expand Up @@ -215,7 +215,7 @@ export default class InitCommand extends BaseCommand {
changedPaths.push(gitattributesPath);
}

const editorConfigProperties = {
const initialEditorConfigProperties = {
[`*`]: {
endOfLine: `lf`,
insertFinalNewline: true,
Expand All @@ -227,7 +227,10 @@ export default class InitCommand extends BaseCommand {
},
};

miscUtils.mergeIntoTarget(editorConfigProperties, configuration.get(`initEditorConfig`));
const editorConfigProperties = miscUtils.toMerged(
initialEditorConfigProperties,
configuration.get(`initEditorConfig`),
);

let editorConfigBody = `root = true\n`;
for (const [selector, props] of Object.entries(editorConfigProperties)) {
Expand Down
17 changes: 13 additions & 4 deletions packages/plugin-interactive-tools/sources/algolia.ts
@@ -1,14 +1,22 @@
import algoliasearch from 'algoliasearch';
import type {SearchIndex} from 'algoliasearch';

const algolia = {
appId: `OFCNCOG2CU`,
apiKey: `6fe4476ee5a1832882e326b506d14126`,
indexName: `npm-search`,
};

const client = algoliasearch(algolia.appId, algolia.apiKey).initIndex(
algolia.indexName,
);
let searchIndex: SearchIndex | undefined;

export const getSearchIndex = async () => {
if (typeof searchIndex !== `undefined`)
return searchIndex;

const {default: algoliasearch} = await import(`algoliasearch`);
searchIndex = algoliasearch(algolia.appId, algolia.apiKey).initIndex(algolia.indexName);

return searchIndex;
};

export interface AlgoliaPackage {
objectID: string;
Expand All @@ -34,6 +42,7 @@ export const search = async (
query: string,
page: number = 0,
) => {
const client = await getSearchIndex();
const res = await client.search<AlgoliaPackage>(
query,
{
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-npm/sources/npmHttpUtils.ts
@@ -1,8 +1,6 @@
import {Configuration, Ident, formatUtils, httpUtils, nodeUtils, StreamReport, structUtils, hashUtils, Project, miscUtils, Cache} from '@yarnpkg/core';
import {MessageName, ReportError} from '@yarnpkg/core';
import {Filename, PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import {prompt} from 'enquirer';
import pick from 'lodash/pick';
import semver from 'semver';

import {Hooks} from './index';
Expand Down Expand Up @@ -285,7 +283,7 @@ function pickPackageMetadata(metadata: PackageMetadata): PackageMetadata {
'dist-tags': metadata[`dist-tags`],
versions: Object.fromEntries(Object.entries(metadata.versions).map(([key, value]) => [
key,
pick(value, CACHED_FIELDS) as any,
miscUtils.pick(value, CACHED_FIELDS) as any,
])),
};
}
Expand Down Expand Up @@ -510,6 +508,8 @@ async function askForOtp(error: any, {configuration}: {configuration: Configurat
if (!process.env.YARN_IS_TEST_ENV) {
const autoOpen = notice.match(/open (https?:\/\/\S+)/i);
if (autoOpen && nodeUtils.openUrl) {
const {prompt} = await import(`enquirer`);

const {openNow} = await prompt<{openNow: boolean}>({
type: `confirm`,
name: `openNow`,
Expand All @@ -535,6 +535,8 @@ async function askForOtp(error: any, {configuration}: {configuration: Configurat
if (process.env.YARN_IS_TEST_ENV)
return process.env.YARN_INJECT_NPM_2FA_TOKEN || ``;

const {prompt} = await import(`enquirer`);

const {otp} = await prompt<{otp: string}>({
type: `password`,
name: `otp`,
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-npm/sources/npmPublishUtils.ts
Expand Up @@ -3,7 +3,6 @@ import {Workspace, structUtils} from '@yarnpkg/core';
import {PortablePath, xfs, npath} from '@yarnpkg/fslib';
import {packUtils} from '@yarnpkg/plugin-pack';
import {createHash} from 'crypto';
import ssri from 'ssri';

import {normalizeRegistry} from './npmConfigUtils';

Expand All @@ -21,6 +20,8 @@ export async function makePublishBody(workspace: Workspace, buffer: Buffer, {acc
const name = structUtils.stringifyIdent(ident);

const shasum = createHash(`sha1`).update(buffer).digest(`hex`);

const {default: ssri} = await import(`ssri`);
const integrity = ssri.fromData(buffer).toString();

const publishAccess = access ?? getPublishAccess(workspace, ident);
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-typescript/sources/typescriptUtils.ts
@@ -1,7 +1,6 @@
import {Request, Requester, Response} from '@algolia/requester-common';
import {Configuration, Descriptor} from '@yarnpkg/core';
import {httpUtils, structUtils} from '@yarnpkg/core';
import algoliasearch from 'algoliasearch';

// Note that the appId and appKey are specific to Yarn's plugin-typescript - please
// don't use them anywhere else without asking Algolia's permission
Expand Down Expand Up @@ -32,6 +31,8 @@ export const hasDefinitelyTyped = async (
};

const createAlgoliaClient = (configuration: Configuration) => {
const algoliasearch = require(`algoliasearch`) as typeof import('algoliasearch').default;

const requester: Requester = {
async send(request: Request): Promise<Response> {
try {
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-version/sources/versionUtils.ts
Expand Up @@ -3,7 +3,6 @@ import {PortablePath, npath, ppath, xfs}
import {parseSyml, stringifySyml} from '@yarnpkg/parsers';
import {gitUtils} from '@yarnpkg/plugin-git';
import {UsageError} from 'clipanion';
import omit from 'lodash/omit';
import semver from 'semver';

// Basically we only support auto-upgrading the ranges that are very simple (^x.y.z, ~x.y.z, >=x.y.z, and of course x.y.z)
Expand All @@ -27,7 +26,7 @@ export function validateReleaseDecision(decision: unknown): string {
if (semverDecision)
return semverDecision;

return miscUtils.validateEnum(omit(Decision, `UNDECIDED`), decision as string);
return miscUtils.validateEnum(miscUtils.omit(Decision, [`UNDECIDED`]), decision as string);
}

export type VersionFile = {
Expand Down