Skip to content

Commit

Permalink
feat(scripts): add projecType filtering to isConvergedPkg helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell committed Jun 16, 2022
1 parent 7adc9bb commit dd64082
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function getCodesandboxBabelOptions() {
const allPackageInfo = getAllPackageInfo();

return Object.values(allPackageInfo).reduce((acc, cur) => {
if (isConvergedPackage(cur.packageJson)) {
if (isConvergedPackage({ packagePathOrJson: cur.packageJson, projectType: 'library' })) {
const prereleaseTags = semver.prerelease(cur.packageJson.version);
const isNonRcPrerelease = prereleaseTags && !prereleaseTags[0].includes('rc');
acc[cur.packageJson.name] = isNonRcPrerelease
Expand Down
2 changes: 1 addition & 1 deletion scripts/beachball/tagVNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getPackagesToTag() {
const packageInfos: AllPackageInfo = getAllPackageInfo();
return Object.values(packageInfos)
.map(packageInfo => {
if (!packageInfo.packageJson.private && isConvergedPackage(packageInfo.packageJson)) {
if (!packageInfo.packageJson.private && isConvergedPackage({ packagePathOrJson: packageInfo.packageJson })) {
return {
name: packageInfo.packageJson.name,
version: packageInfo.packageJson.version,
Expand Down
2 changes: 1 addition & 1 deletion scripts/beachball/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function getConfig({ version }: { version: 'v8' | 'vNext' }) {
function getVNextPackagePaths(allPackageInfo: AllPackageInfo) {
return Object.values(allPackageInfo)
.map(packageInfo => {
if (isConvergedPackage(packageInfo.packageJson)) {
if (isConvergedPackage({ packagePathOrJson: packageInfo.packageJson })) {
return packageInfo.packagePath;
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/create-component/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { names, WorkspaceJsonConfiguration } from '@nrwl/devkit';
const convergedComponentPackages = Object.entries(getAllPackageInfo())
.filter(
([pkgName, info]) =>
isConvergedPackage(info.packageJson) &&
isConvergedPackage({ packagePathOrJson: info.packageJson }) &&
pkgName.startsWith('@fluentui/react-') &&
info.packagePath.startsWith('packages') &&
!!info.packageJson.scripts?.start &&
Expand Down
4 changes: 2 additions & 2 deletions scripts/just.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function preset() {
task('ts', () => {
return series(
'ts:compile',
condition('copy-compiled', () => isConvergedPackage()),
condition('copy-compiled', () => isConvergedPackage({ projectType: 'library' })),
'ts:postprocess',
condition('babel:postprocess', () => fs.existsSync(path.join(process.cwd(), '.babelrc.json'))),
);
Expand Down Expand Up @@ -118,7 +118,7 @@ export function preset() {
'sass',
'ts',
// v9 needs to run api-extractor which generates rolluped .d.ts files that are shipped to npm
condition('api-extractor', () => isConvergedPackage() || !args.min),
condition('api-extractor', () => isConvergedPackage({ projectType: 'library' }) || !args.min),
),
).cached();

Expand Down
4 changes: 4 additions & 0 deletions scripts/monorepo/findGitRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const path = require('path');
let cwdForGitRoot;
let gitRoot;

/**
*
* @returns {string}
*/
function findGitRoot() {
let cwd = process.cwd();

Expand Down
24 changes: 19 additions & 5 deletions scripts/monorepo/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface PackageInfo {

export type AllPackageInfo = { [packageName: string]: PackageInfo };

export declare function findGitRoot(cwd?: string): string;
export declare function findGitRoot(): string;

/**
* Find all the dependencies (and their dependencies) within the repo for a specific package
Expand All @@ -37,11 +37,19 @@ export declare function getAllPackageInfo(): AllPackageInfo;

/**
* Determines whether a package is converged, based on its version.
* @param packagePathOrJson optional different package path to run in OR previously-read package.json
*
* @param {Object} [options]
* @param {PathOrPackageJson} [options.packagePathOrJson] - optional different package path to run in OR previously-read package.json
* (defaults to reading package.json from `process.cwd()`)
* @returns true if it's a converged package (version >= 9)
* @param {'library' | 'application' | 'all'} [options.projectType] - filter for what project types you wanna apply the condition
*
* @returns {boolean} true if it's a converged package (version >= 9)
*/
export declare function isConvergedPackage(packagePathOrJson?: string | PackageJson): boolean;

export declare function isConvergedPackage(option?: {
packagePathOrJson?: string | PackageJson;
projectType?: 'library' | 'application' | 'all';
}): boolean;

/**
* @param since - Commit to compare against
Expand All @@ -62,6 +70,12 @@ export declare function getNthCommit(n = 1, ref = 'HEAD'): string;
* Gets project metadata from monorepo source of truth which is `workspace.json`
*/
export declare function getProjectMetadata(options: {
root: string;
/**
* repo root path
*/
root?: string;
/**
* package name
*/
name: string;
}): import('@nrwl/devkit').ProjectConfiguration;
23 changes: 20 additions & 3 deletions scripts/monorepo/isConvergedPackage.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
// @ts-check
const semver = require('semver');
const { readConfig } = require('../read-config');
const { getProjectMetadata } = require('./utils');

/**
* @typedef {string | import('./index').PackageJson} PathOrPackageJson
*/

/**
* Determines whether a package is converged, based on its version.
* @param {PathOrPackageJson} [packagePathOrJson] optional different package path to run in OR previously-read package.json
*
* @param {Object} [options]
* @param {PathOrPackageJson} [options.packagePathOrJson] - optional different package path to run in OR previously-read package.json
* (defaults to reading package.json from `process.cwd()`)
* @param {'library' | 'application' | 'all'} [options.projectType] - filter for what project types you wanna apply the condition
*
* @returns {boolean} true if it's a converged package (version >= 9)
*/
function isConvergedPackage(packagePathOrJson) {
function isConvergedPackage(options = {}) {
const { packagePathOrJson, projectType = 'all' } = options;
const packageJson =
!packagePathOrJson || typeof packagePathOrJson === 'string'
? readConfig('package.json', /** @type {string|undefined} */ (packagePathOrJson))
: packagePathOrJson;
return !!packageJson && (semver.major(packageJson.version) >= 9 || isNightlyVersion(packageJson.version));

if (!packageJson) {
throw new Error(`package.json doesn't exist`);
}

const metadata = getProjectMetadata({ name: packageJson.name });

if (projectType !== 'all' && metadata.projectType !== projectType) {
return false;
}

return semver.major(packageJson.version) >= 9 || isNightlyVersion(packageJson.version);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions scripts/monorepo/runPublished.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const beachballPackageScopes = Object.entries(getAllPackageInfo())
}

if (process.env.RELEASE_VNEXT) {
return isConvergedPackage(packageJson) && packageJson.private !== true;
} else if (!isConvergedPackage(packageJson)) {
return isConvergedPackage({ packagePathOrJson: packageJson }) && packageJson.private !== true;
} else if (!isConvergedPackage({ packagePathOrJson: packageJson })) {
// v8 scope
return packageJson.private !== true || websitePackages.includes(packageJson.name);
}
Expand Down
7 changes: 5 additions & 2 deletions scripts/monorepo/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
const fs = require('fs');
const path = require('path');

const findGitRoot = require('./findGitRoot');

/**
* Gets project metadata from monorepo source of truth which is `workspace.json`
* @param {{root:string;name:string}} options
* @param {{root?:string;name:string}} options
* @returns {import('@nrwl/devkit').ProjectConfiguration}
*/
function getProjectMetadata(options) {
const { root = findGitRoot() } = options;
/**@type {import('@nrwl/devkit').WorkspaceJsonConfiguration} */
const nxWorkspace = JSON.parse(fs.readFileSync(path.join(options.root, 'workspace.json'), 'utf-8'));
const nxWorkspace = JSON.parse(fs.readFileSync(path.join(root, 'workspace.json'), 'utf-8'));

return nxWorkspace.projects[options.name];
}
Expand Down

0 comments on commit dd64082

Please sign in to comment.