Skip to content

Commit

Permalink
Merge pull request #110 from NullVoxPopuli/support-pnpm-workspace-pro…
Browse files Browse the repository at this point in the history
…tocol

Support the workspace protocol (or other workflows that update references out of band)
  • Loading branch information
rwjblue committed Jan 15, 2024
2 parents ea8cad1 + 0f550ac commit 7c24eeb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
39 changes: 34 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function rejectAfter(ms, error) {
function discoverWorkspaces() {
let { publishConfig, workspaces } = JSON.parse(fs.readFileSync(path.resolve(ROOT_MANIFEST_PATH)));

if (!workspaces && fs.existsSync(PNPM_WORKSPACE_PATH)) {
if (!workspaces && hasPnpm()) {
({ packages: workspaces } = YAML.parse(
fs.readFileSync(path.resolve(PNPM_WORKSPACE_PATH), { encoding: 'utf-8' })
));
Expand All @@ -46,6 +46,10 @@ function discoverWorkspaces() {
return { publishConfig, workspaces };
}

function hasPnpm() {
return fs.existsSync('./pnpm-lock.yaml') || fs.existsSync(PNPM_WORKSPACE_PATH);
}

function resolveWorkspaces(workspaces) {
if (Array.isArray(workspaces)) {
return workspaces;
Expand Down Expand Up @@ -275,6 +279,16 @@ export default class WorkspacesPlugin extends Plugin {
}
};

/**
* In workflows where publishing is handled on C.I.,
* and not be release-it, the environment will often require
* that the lockfile be updated -- this is usually done by
* running the install command for the package manager.
*/
if (hasPnpm()) {
await this.exec(`pnpm install`);
}

return this.spinner.show({ task, label: 'npm version' });
}

Expand Down Expand Up @@ -309,14 +323,29 @@ export default class WorkspacesPlugin extends Plugin {
}

_buildReplacementDepencencyVersion(existingVersion, newVersion) {
let firstChar = existingVersion[0];
let prefix = '';
let firstChar = '';
let range = existingVersion;
let suffix = newVersion;

// preserve workspace protocol prefix,
// tools that use this replace with a real version during the publish the process
if (existingVersion.startsWith('workspace:')) {
prefix = 'workspace:';
range = existingVersion.slice(prefix.length);
suffix = range.length > 1 ? newVersion : '';
}

// preserve existing floating constraint
if (['^', '~'].includes(firstChar)) {
return `${firstChar}${newVersion}`;
if (['^', '~'].includes(range[0])) {
firstChar = range[0];
}

if ('*' === range) {
return `${prefix}*`;
}

return newVersion;
return `${prefix}${firstChar}${suffix}`;
}

_updateDependencies(pkgInfo, newVersion) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"scripts": {
"lint:js": "eslint .",
"test": "npm-run-all lint:js test:vitest",
"test:watch": "vitest --no-threads",
"test:vitest": "vitest run --no-threads"
},
"husky": {
Expand Down
34 changes: 34 additions & 0 deletions tests/plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,33 @@ describe('@release-it-plugins/workspaces', () => {
`);
});

it('supports the workspace protocol as used in the glimmer-vm repo', async () => {
setupProject(['packages/@glimmer/*']);
setupWorkspace({ name: '@glimmer/interfaces', version: '1.0.0' });
setupWorkspace({
name: '@glimmer/runtime',
version: '1.0.0',
dependencies: { '@glimmer/interfaces': 'workspace:*' },
});

let plugin = buildPlugin();

await runTasks(plugin);

// dist was updated
expect(JSON.parse(dir.readText('packages/@glimmer/interfaces/package.json'))).toEqual({
license: 'MIT',
name: '@glimmer/interfaces',
version: '1.0.1',
});
expect(JSON.parse(dir.readText('packages/@glimmer/runtime/package.json'))).toEqual({
license: 'MIT',
name: '@glimmer/runtime',
version: '1.0.1',
dependencies: { '@glimmer/interfaces': 'workspace:*' },
});
});

it('skips registry checks with skipChecks', async () => {
let plugin = buildPlugin({ skipChecks: true });

Expand Down Expand Up @@ -1120,5 +1147,12 @@ describe('@release-it-plugins/workspaces', () => {
updatesTo({ existing: '^1.0.0', new: '2.0.0-beta.1', expected: '^2.0.0-beta.1' });
updatesTo({ existing: '^1.0.0-beta.1', new: '1.0.0-beta.2', expected: '^1.0.0-beta.2' });
updatesTo({ existing: '^1.0.0-beta.1', new: '1.0.0', expected: '^1.0.0' });

updatesTo({ existing: 'workspace:^1.0.0', new: '2.0.0', expected: 'workspace:^2.0.0' });
updatesTo({ existing: 'workspace:~1.0.0', new: '2.0.0', expected: 'workspace:~2.0.0' });
updatesTo({ existing: 'workspace:1.0.0', new: '2.0.0', expected: 'workspace:2.0.0' });
updatesTo({ existing: 'workspace:^', new: '2.0.0', expected: 'workspace:^' });
updatesTo({ existing: 'workspace:~', new: '2.0.0', expected: 'workspace:~' });
updatesTo({ existing: 'workspace:*', new: '2.0.0', expected: 'workspace:*' });
});
});

0 comments on commit 7c24eeb

Please sign in to comment.