Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


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

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

8 changes: 8 additions & 0 deletions .yarn/versions/36e6a0e0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
releases:
"@yarnpkg/plugin-npm": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/cli"
- "@yarnpkg/core"
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"clipanion": "^3.2.0-rc.4",
"eslint": "^8.2.0",
"github-api": "^3.2.2",
"globby": "^11.0.1",
"jest": "^26.0.1",
"jest-environment-node": "^26.0.1",
"jest-junit": "^10.0.0",
Expand Down
11 changes: 8 additions & 3 deletions packages/gatsby/gatsby-plugin-yarn-introspection/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {execute} = require(`@yarnpkg/monorepo/scripts/extract-hooks`);
const {execute, checkForInvalidHookLocations} = require(`@yarnpkg/monorepo/scripts/extract-hooks`);

const fs = require(`fs`);
const path = require(`path`);
Expand All @@ -19,9 +19,14 @@ exports.sourceNodes = async ({actions, createNodeId, createContentDigest}, opts)
}
});

const data = await execute([
require.resolve(`@yarnpkg/monorepo/packages/yarnpkg-core/sources/Plugin.ts`),
const files = [
path.join(packageDirectory, `yarnpkg-core/sources/Plugin.ts`),
...indexList,
];

const [data] = await Promise.all([
execute(files),
checkForInvalidHookLocations(`${packageDirectory}/**/*.{ts,tsx}`, files),
]);

createNode({
Expand Down
8 changes: 0 additions & 8 deletions packages/plugin-npm/sources/Hooks.ts

This file was deleted.

33 changes: 22 additions & 11 deletions packages/plugin-npm/sources/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import {Plugin, SettingsType, miscUtils} from '@yarnpkg/core';
import {Plugin, SettingsType, miscUtils, Configuration, Ident} from '@yarnpkg/core';

import {Hooks} from './Hooks';
import {NpmHttpFetcher} from './NpmHttpFetcher';
import {NpmRemapResolver} from './NpmRemapResolver';
import {NpmSemverFetcher} from './NpmSemverFetcher';
import {NpmSemverResolver} from './NpmSemverResolver';
import {NpmTagResolver} from './NpmTagResolver';
import * as npmConfigUtils from './npmConfigUtils';
import * as npmHttpUtils from './npmHttpUtils';
import * as npmPublishUtils from './npmPublishUtils';
import {NpmHttpFetcher} from './NpmHttpFetcher';
import {NpmRemapResolver} from './NpmRemapResolver';
import {NpmSemverFetcher} from './NpmSemverFetcher';
import {NpmSemverResolver} from './NpmSemverResolver';
import {NpmTagResolver} from './NpmTagResolver';
import * as npmConfigUtils from './npmConfigUtils';
import * as npmHttpUtils from './npmHttpUtils';
import * as npmPublishUtils from './npmPublishUtils';

export {npmConfigUtils};
export {npmHttpUtils};
export {npmPublishUtils};
export type {Hooks};

export interface Hooks {
/**
* Called when getting the authentication header for a request to the npm registry.
* You can use this mechanism to dynamically query a CLI for the credentials for a
* specific registry.
*/
getNpmAuthenticationHeader?: (currentHeader: string | undefined, registry: string, {
configuration,
ident,
}: { configuration: Configuration, ident?: Ident }) => Promise<string | undefined>;
}


const authSettings = {
npmAlwaysAuth: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-npm/sources/npmHttpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {MessageName, ReportError} from '@yarnpkg/core';
import {prompt} from 'enquirer';
import {URL} from 'url';

import {Hooks} from './Hooks';
import {Hooks} from './index';
import * as npmConfigUtils from './npmConfigUtils';
import {MapLike} from './npmConfigUtils';

Expand Down
28 changes: 27 additions & 1 deletion scripts/extract-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Cli, Command, Option, UsageError} from 'clipanion';
import fs from 'fs';
import globby from 'globby';
import path from 'path';
import ts from 'typescript';

Expand All @@ -21,15 +22,25 @@ type HookDefinition = {
comment?: string;
};

async function processFile(file: ts.SourceFile) {
async function processFile(file: ts.SourceFile, {allowHooks = true}: {allowHooks?: boolean} = {}) {
const hooks: Array<HookDefinition> = [];
let isInsideInterface = false;

const processNode = (node: ts.Node) => {
switch (node.kind) {
case ts.SyntaxKind.TypeAliasDeclaration: {
const typeAliasNode = node as ts.TypeAliasDeclaration;
if (typeAliasNode.name.getText() === `Hooks`) {
throw new UsageError(`Hooks should only be declared using interfaces, not type aliases (in ${file.fileName})`);
}
} break;

case ts.SyntaxKind.InterfaceDeclaration: {
const interfaceNode = node as ts.InterfaceDeclaration;
if (interfaceNode.name.getText() === `Hooks`) {
if (!allowHooks)
throw new UsageError(`Hooks aren't allowed in ${file.fileName}`);

isInsideInterface = true;
ts.forEachChild(node, processNode);
isInsideInterface = false;
Expand Down Expand Up @@ -110,6 +121,21 @@ async function execute(files: Array<string>) {

exports.execute = execute;

async function checkForInvalidHookLocations(patterns: string | Array<string>, ignore: Array<string>) {
const allFiles = await globby(patterns, {
absolute: true,
ignore,
});

const processPromises = [];
for (const absolutePath of allFiles)
processPromises.push(Promise.resolve().then(() => parseFile(absolutePath).then(file => processFile(file, {allowHooks: false}))));

await Promise.all(processPromises);
}

exports.checkForInvalidHookLocations = checkForInvalidHookLocations;

if (require.main === module) {
Cli.from([
class extends Command {
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5850,6 +5850,7 @@ __metadata:
clipanion: ^3.2.0-rc.4
eslint: ^8.2.0
github-api: ^3.2.2
globby: ^11.0.1
jest: ^26.0.1
jest-environment-node: ^26.0.1
jest-junit: ^10.0.0
Expand Down