Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 35 additions & 0 deletions .yarn/versions/71a206d9.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
releases:
"@yarnpkg/core": patch
"@yarnpkg/plugin-essentials": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-jsr"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/cli"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ describe(`Commands`, () => {

await run(`plugin`, `import`, pluginUrl);

// Check for post import script output
await expect(xfs.existsPromise(ppath.join(path, `post_import.txt`))).resolves.toEqual(true);

await expect(xfs.existsPromise(ppath.join(path, mockPluginPath))).resolves.toEqual(true);
await expect(fs.readSyml(ppath.join(path, Filename.rc))).resolves.toEqual({
httpsCaFilePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ describe(`Commands`, () => {
await run(`plugin`, `remove`, `@yarnpkg/plugin-hello-world`);

await expect(xfs.existsPromise(helloWorldPlugin)).resolves.toEqual(false);
// Check for pre remove script output
await expect(xfs.existsPromise(ppath.join(path, `pre_remove.txt`))).resolves.toEqual(true);

await expect(fs.readSyml(ppath.join(path, Filename.rc))).resolves.toEqual({
plugins: [{
path: ppath.relative(path, helloUniversePlugin),
Expand Down
7 changes: 4 additions & 3 deletions packages/plugin-essentials/sources/commands/plugin/import.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {BaseCommand} from '@yarnpkg/cli';
import {PluginMeta} from '@yarnpkg/core/sources/Plugin';
import {Hooks, PluginMeta} from '@yarnpkg/core/sources/Plugin';
import {Configuration, MessageName, Project, ReportError, StreamReport, Report} from '@yarnpkg/core';
import {YarnVersion, formatUtils, httpUtils, structUtils, hashUtils} from '@yarnpkg/core';
import {PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
Expand Down Expand Up @@ -54,13 +54,12 @@ export default class PluginImportCommand extends BaseCommand {

async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const {project} = await Project.find(configuration, this.context.cwd);

const report = await StreamReport.start({
configuration,
stdout: this.context.stdout,
}, async report => {
const {project} = await Project.find(configuration, this.context.cwd);

let pluginSpec: string;
let pluginBuffer: Buffer;
if (this.name.match(/^\.{0,2}[\\/]/) || npath.isAbsolute(this.name)) {
Expand Down Expand Up @@ -116,6 +115,8 @@ export default class PluginImportCommand extends BaseCommand {
await savePlugin(pluginSpec, pluginBuffer, {checksum: this.checksum, project, report});
});

await configuration.triggerHook((hooks: Hooks) => hooks.pluginPostImport, project);

return report.exitCode();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {BaseCommand} from '@yarnpkg/cli';
import {structUtils, hashUtils, Report, CommandContext, YarnVersion} from '@yarnpkg/core';
import {Configuration, MessageName, Project, ReportError, StreamReport} from '@yarnpkg/core';
import {PortablePath, npath, ppath, xfs, Filename} from '@yarnpkg/fslib';
import {Command, Option, Usage} from 'clipanion';
import {tmpdir} from 'os';
import {BaseCommand} from '@yarnpkg/cli';
import {structUtils, hashUtils, Report, CommandContext, YarnVersion, Hooks} from '@yarnpkg/core';
import {Configuration, MessageName, Project, ReportError, StreamReport} from '@yarnpkg/core';
import {PortablePath, npath, ppath, xfs, Filename} from '@yarnpkg/fslib';
import {Command, Option, Usage} from 'clipanion';
import {tmpdir} from 'os';

import {prepareRepo, runWorkflow} from '../../set/version/sources';
import {savePlugin} from '../import';
import {getAvailablePlugins} from '../list';
import {prepareRepo, runWorkflow} from '../../set/version/sources';
import {savePlugin} from '../import';
import {getAvailablePlugins} from '../list';

const buildWorkflow = ({pluginName, noMinify}: {noMinify: boolean, pluginName: string}, target: PortablePath) => [
[`yarn`, `build:${pluginName}`, ...noMinify ? [`--no-minify`] : [], `|`],
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class PluginImportSourcesCommand extends BaseCommand {

async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);

const {project} = await Project.find(configuration, this.context.cwd);
const target = typeof this.installPath !== `undefined`
? ppath.resolve(this.context.cwd, npath.toPortablePath(this.installPath))
: ppath.resolve(npath.toPortablePath(tmpdir()), `yarnpkg-sources`, hashUtils.makeHash(this.repository).slice(0, 6) as Filename);
Expand All @@ -69,8 +69,6 @@ export default class PluginImportSourcesCommand extends BaseCommand {
configuration,
stdout: this.context.stdout,
}, async report => {
const {project} = await Project.find(configuration, this.context.cwd);

const ident = structUtils.parseIdent(this.name.replace(/^((@yarnpkg\/)?plugin-)?/, `@yarnpkg/plugin-`));
const identStr = structUtils.stringifyIdent(ident);
const data = await getAvailablePlugins(configuration, YarnVersion);
Expand All @@ -85,6 +83,8 @@ export default class PluginImportSourcesCommand extends BaseCommand {
await buildAndSavePlugin(pluginSpec, this, {project, report, target});
});

await configuration.triggerHook((hooks: Hooks) => hooks.pluginPostImport, project);

return report.exitCode();
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/plugin-essentials/sources/commands/plugin/remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, MessageName, Project, StreamReport, formatUtils, structUtils} from '@yarnpkg/core';
import {PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import {Command, Option, Usage, UsageError} from 'clipanion';
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, MessageName, Project, StreamReport, formatUtils, structUtils, Hooks} from '@yarnpkg/core';
import {PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import {Command, Option, Usage, UsageError} from 'clipanion';

// eslint-disable-next-line arca/no-default-export
export default class PluginRemoveCommand extends BaseCommand {
Expand Down Expand Up @@ -32,6 +32,8 @@ export default class PluginRemoveCommand extends BaseCommand {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const {project} = await Project.find(configuration, this.context.cwd);

await configuration.triggerHook((hooks: Hooks) => hooks.pluginPreRemove, project);

const report = await StreamReport.start({
configuration,
stdout: this.context.stdout,
Expand Down
10 changes: 10 additions & 0 deletions packages/yarnpkg-core/sources/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ export interface Hooks {
cleanGlobalArtifacts?: (
configuration: Configuration,
) => Promise<void>;

/**
* Called after the plugin has been imported.
*/
pluginPostImport?: (project: Project) => Promise<void>;

/**
* Called before the plugin gets removed from the project.
*/
pluginPreRemove?: (project: Project) => Promise<void>;
}

export type Plugin<PluginHooks = any> = {
Expand Down
12 changes: 12 additions & 0 deletions scripts/plugin-hello-world.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ module.exports = {
commands: [
HelloWorldCommand,
],
hooks: {
pluginPostImport: project => {
const {xfs, ppath} = require(`@yarnpkg/fslib`);
const path = ppath.join(project.cwd, `post_import.txt`);
xfs.writeFileSync(path, `Hello, World!`);
},
pluginPreRemove: project => {
const {xfs, ppath} = require(`@yarnpkg/fslib`);
const path = ppath.join(project.cwd, `pre_remove.txt`);
xfs.writeFileSync(path, `Goodbye, World!`);
},
},
};
},
};
Loading