changesets-gitlab
Version:
[](https://github.com/un-ts/changesets-gitlab/actions/workflows/ci.yml?query=branch%3Amain) [![CodeRabbit Pull Request Revie
135 lines • 4.84 kB
JavaScript
import fs from 'node:fs/promises';
import nodePath from 'node:path';
import assembleReleasePlan from '@changesets/assemble-release-plan';
import { parse as parseConfig } from '@changesets/config';
import parseChangeset from '@changesets/parse';
import micromatch from 'micromatch';
import { parse } from 'yaml';
import { getAllFiles } from './utils.js';
function fetchFile(path) {
return fs.readFile(path, 'utf8');
}
export const getChangedPackages = async ({ changedFiles: changedFilesPromise, }) => {
let hasErrored = false;
async function fetchJsonFile(path) {
try {
const x = await fetchFile(path);
return JSON.parse(x);
}
catch (err) {
hasErrored = true;
console.error(err);
return {};
}
}
async function fetchTextFile(path) {
try {
return await fetchFile(path);
}
catch (err) {
hasErrored = true;
console.error(err);
return '';
}
}
async function getPackage(pkgPath) {
const jsonContent = await fetchJsonFile(pkgPath + '/package.json');
return {
packageJson: jsonContent,
dir: pkgPath,
};
}
const rootPackageJsonContentsPromise = fetchJsonFile('package.json');
const configPromise = fetchJsonFile('.changeset/config.json');
const tree = await getAllFiles(process.cwd());
let preStatePromise;
const changesetPromises = [];
const potentialWorkspaceDirectories = [];
let isPnpm = false;
const changedFiles = await changedFilesPromise;
for (const item of tree) {
if (item.endsWith('/package.json')) {
const dirPath = nodePath.dirname(item);
potentialWorkspaceDirectories.push(dirPath);
}
else if (item === 'pnpm-workspace.yaml') {
isPnpm = true;
}
else if (item === '.changeset/pre.json') {
preStatePromise = fetchJsonFile('.changeset/pre.json');
}
else if (item !== '.changeset/README.md' &&
item.startsWith('.changeset') &&
item.endsWith('.md') &&
changedFiles.includes(item)) {
const res = /\.changeset\/([^.]+)\.md/.exec(item);
if (!res) {
throw new Error('could not get name from changeset filename');
}
const id = res[1];
changesetPromises.push(fetchTextFile(item).then(text => ({
...parseChangeset(text),
id,
})));
}
}
let tool;
if (isPnpm) {
tool = {
tool: 'pnpm',
globs: parse(await fetchTextFile('pnpm-workspace.yaml')).packages,
};
}
else {
const rootPackageJsonContent = await rootPackageJsonContentsPromise;
if (rootPackageJsonContent.workspaces) {
tool = {
tool: 'yarn',
globs: Array.isArray(rootPackageJsonContent.workspaces)
? rootPackageJsonContent.workspaces
: rootPackageJsonContent.workspaces.packages,
};
}
else if (rootPackageJsonContent.bolt?.workspaces) {
tool = {
tool: 'bolt',
globs: rootPackageJsonContent.bolt.workspaces,
};
}
}
const rootPackageJsonContent = await rootPackageJsonContentsPromise;
const packages = {
root: {
dir: '/',
packageJson: rootPackageJsonContent,
},
tool: tool ? tool.tool : 'root',
packages: [],
};
if (tool) {
if (!Array.isArray(tool.globs) ||
!tool.globs.every(x => typeof x === 'string')) {
throw new Error('globs are not valid: ' + JSON.stringify(tool.globs));
}
const matches = micromatch(potentialWorkspaceDirectories, tool.globs);
packages.packages = await Promise.all(matches.map(dir => getPackage(dir)));
}
else {
packages.packages.push(packages.root);
}
if (hasErrored) {
throw new Error('an error occurred when fetching files');
}
const config = await configPromise.then(rawConfig => parseConfig(rawConfig, packages));
const releasePlan = assembleReleasePlan(await Promise.all(changesetPromises), packages, config, await preStatePromise);
return {
changedPackages: (packages.tool === 'root'
? packages.packages
: packages.packages.filter(pkg => changedFiles.some(changedFile => changedFile.includes(pkg.dir))))
.filter(pkg => pkg.packageJson.private !== true &&
!config.ignore.includes(pkg.packageJson.name))
.map(x => x.packageJson.name),
releasePlan,
};
};
//# sourceMappingURL=get-changed-packages.js.map