Skip to content

Commit

Permalink
Created a new util called "findPathsToPackages()" that allows searchi…
Browse files Browse the repository at this point in the history
…ng for packages.
  • Loading branch information
pomek committed Oct 10, 2024
1 parent 7c59f38 commit 7105a19
Show file tree
Hide file tree
Showing 14 changed files with 438 additions and 403 deletions.
1 change: 1 addition & 0 deletions packages/ckeditor5-dev-release-tools/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export { default as checkVersionAvailability } from './utils/checkversionavailab
export { default as getNpmTagFromVersion } from './utils/getnpmtagfromversion.js';
export { default as isVersionPublishableForTag } from './utils/isversionpublishablefortag.js';
export { default as provideToken } from './utils/providetoken.js';
export { default as findPathsToPackages } from './utils/findpathstopackages.js';
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fs from 'fs-extra';
import upath from 'upath';
import { glob } from 'glob';
import findPathsToPackages from '../utils/findpathstopackages.js';

/**
* The purpose of the script is to clean all packages prepared for the release. The cleaning consists of two stages:
Expand All @@ -29,12 +30,7 @@ import { glob } from 'glob';
*/
export default async function cleanUpPackages( options ) {
const { packagesDirectory, packageJsonFieldsToRemove, preservePostInstallHook, cwd } = parseOptions( options );

const packageJsonPaths = await glob( '*/package.json', {
cwd: upath.join( cwd, packagesDirectory ),
nodir: true,
absolute: true
} );
const packageJsonPaths = await findPathsToPackages( cwd, packagesDirectory, { includePackageJson: true } );

for ( const packageJsonPath of packageJsonPaths ) {
const packagePath = upath.dirname( packageJsonPath );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

import upath from 'upath';
import fs from 'fs-extra';
import { glob } from 'glob';
import assertNpmAuthorization from '../utils/assertnpmauthorization.js';
import assertPackages from '../utils/assertpackages.js';
import assertNpmTag from '../utils/assertnpmtag.js';
import assertFilesToPublish from '../utils/assertfilestopublish.js';
import executeInParallel from '../utils/executeinparallel.js';
import publishPackageOnNpmCallback from '../utils/publishpackageonnpmcallback.js';
import checkVersionAvailability from '../utils/checkversionavailability.js';
import findPathsToPackages from '../utils/findpathstopackages.js';

/**
* The purpose of the script is to validate the packages prepared for the release and then release them on npm.
Expand Down Expand Up @@ -127,13 +127,6 @@ export default async function publishPackages( options ) {
} );
}

function findPathsToPackages( cwd, packagesDirectory ) {
return glob( '*/', {
cwd: upath.join( cwd, packagesDirectory ),
absolute: true
} );
}

async function removeAlreadyPublishedPackages( packagePaths ) {
for ( const absolutePackagePath of packagePaths ) {
const pkgJson = await fs.readJson( upath.join( absolutePackagePath, 'package.json' ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
*/

import fs from 'fs-extra';
import { glob } from 'glob';
import upath from 'upath';
import findPathsToPackages from '../utils/findpathstopackages.js';

const { normalizeTrim } = upath;

/**
* The purpose of this script is to update all eligible dependencies to a version specified in the `options.version`. The following packages
Expand Down Expand Up @@ -38,15 +40,15 @@ export default async function updateDependencies( options ) {
cwd = process.cwd()
} = options;

const globPatterns = [ 'package.json' ];

if ( packagesDirectory ) {
const packagesDirectoryPattern = upath.join( packagesDirectory, '*', 'package.json' );

globPatterns.push( packagesDirectoryPattern );
}

const pkgJsonPaths = await getPackageJsonPaths( cwd, globPatterns, packagesDirectoryFilter );
const pkgJsonPaths = await findPathsToPackages(
cwd,
packagesDirectory ? normalizeTrim( packagesDirectory ) : null,
{
includePackageJson: true,
includeCwd: true,
packagesDirectoryFilter
}
);

for ( const pkgJsonPath of pkgJsonPaths ) {
const pkgJson = await fs.readJson( pkgJsonPath );
Expand Down Expand Up @@ -78,28 +80,6 @@ function updateVersion( version, callback, dependencies ) {
}
}

/**
* @param {string} cwd
* @param {Array.<string>} globPatterns
* @param {UpdateDependenciesPackagesDirectoryFilter|null} packagesDirectoryFilter
* @returns {Promise.<Array.<string>>}
*/
async function getPackageJsonPaths( cwd, globPatterns, packagesDirectoryFilter ) {
const globOptions = {
cwd,
nodir: true,
absolute: true
};

const pkgJsonPaths = await glob( globPatterns, globOptions );

if ( !packagesDirectoryFilter ) {
return pkgJsonPaths;
}

return pkgJsonPaths.filter( packagesDirectoryFilter );
}

/**
* @callback UpdateVersionCallback
*
Expand Down
48 changes: 10 additions & 38 deletions packages/ckeditor5-dev-release-tools/lib/tasks/updateversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import upath from 'upath';
import fs from 'fs-extra';
import { glob } from 'glob';
import semver from 'semver';
import findPathsToPackages from '../utils/findpathstopackages.js';

const { normalizeTrim } = upath;

Expand Down Expand Up @@ -37,10 +37,16 @@ export default async function updateVersions( options ) {
packagesDirectoryFilter = null,
cwd = process.cwd()
} = options;
const normalizedPackagesDir = packagesDirectory ? normalizeTrim( packagesDirectory ) : null;

const globPatterns = getGlobPatterns( normalizedPackagesDir );
const pkgJsonPaths = await getPackageJsonPaths( cwd, globPatterns, packagesDirectoryFilter );
const pkgJsonPaths = await findPathsToPackages(
cwd,
packagesDirectory ? normalizeTrim( packagesDirectory ) : null,
{
includePackageJson: true,
includeCwd: true,
packagesDirectoryFilter
}
);

checkIfVersionIsValid( version );

Expand All @@ -52,40 +58,6 @@ export default async function updateVersions( options ) {
}
}

/**
* @param {string} cwd
* @param {Array.<string>} globPatterns
* @param {UpdateVersionsPackagesDirectoryFilter|null} packagesDirectoryFilter
* @returns {Promise.<Array.<string>>}
*/
async function getPackageJsonPaths( cwd, globPatterns, packagesDirectoryFilter ) {
const pkgJsonPaths = await glob( globPatterns, {
cwd,
absolute: true,
nodir: true
} );

if ( !packagesDirectoryFilter ) {
return pkgJsonPaths;
}

return pkgJsonPaths.filter( packagesDirectoryFilter );
}

/**
* @param {string|null} packagesDirectory
* @returns {Array.<string>}
*/
function getGlobPatterns( packagesDirectory ) {
const patterns = [ 'package.json' ];

if ( packagesDirectory ) {
patterns.push( packagesDirectory + '/*/package.json' );
}

return patterns;
}

/**
* @param {string} version
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import upath from 'upath';
import os from 'os';
import fs from 'fs/promises';
import { Worker } from 'worker_threads';
import { glob } from 'glob';
import { registerAbortController, deregisterAbortController } from './abortcontroller.js';
import findPathsToPackages from './findpathstopackages.js';

const WORKER_SCRIPT = new URL( './parallelworker.js', import.meta.url );

Expand Down Expand Up @@ -49,15 +49,9 @@ export default async function executeInParallel( options ) {
} = options;

const concurrencyAsInteger = Math.floor( concurrency ) || 1;
const normalizedCwd = upath.toUnix( cwd );
const packages = ( await glob( `${ packagesDirectory }/*/`, {
cwd: normalizedCwd,
absolute: true
} ) ).map( upath.normalize );

const packagesToProcess = packagesDirectoryFilter ?
packages.filter( packagesDirectoryFilter ) :
packages;
const packagesToProcess = await findPathsToPackages( cwd, packagesDirectory, {
packagesDirectoryFilter
} );

const packagesInThreads = getPackagesGroupedByThreads( packagesToProcess, concurrencyAsInteger );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/

import upath from 'upath';
import { glob } from 'glob';

/**
* Returns an array containing paths to packages found in the `packagesDirectory` directory.
*
* @param {string} cwd
* @param {string|null} packagesDirectory
* @param {object} [options={}]
* @param {boolean} [options.includePackageJson=false]
* @param {boolean} [options.includeCwd=false]
* @param {PackagesDirectoryFilter|null} [options.packagesDirectoryFilter=null]
* @returns {Array.<string>}
*/
export default async function findPathsToPackages( cwd, packagesDirectory, options = {} ) {
const {
includePackageJson = false,
includeCwd = false,
packagesDirectoryFilter = null
} = options;

const packagePaths = await getPackages( cwd, packagesDirectory, includePackageJson );

if ( includeCwd ) {
if ( includePackageJson ) {
packagePaths.push( upath.join( cwd, 'package.json' ) );
} else {
packagePaths.push( cwd );
}
}

const normalizedPaths = packagePaths.map( item => upath.normalize( item ) );

if ( packagesDirectoryFilter ) {
return normalizedPaths.filter( item => packagesDirectoryFilter( item ) );
}

return normalizedPaths;
}

/**
* @param {string} cwd
* @param {string|null} packagesDirectory
* @param {boolean} includePackageJson
* @returns {Promise.<Array.<string>>}
*/
function getPackages( cwd, packagesDirectory, includePackageJson ) {
if ( !packagesDirectory ) {
return [];
}

const globOptions = {
cwd: upath.join( cwd, packagesDirectory ),
absolute: true
};

let pattern = '*/';

if ( includePackageJson ) {
pattern += 'package.json';
globOptions.nodir = true;
}

return glob( pattern, globOptions );
}

/**
* @callback PackagesDirectoryFilter
*
* @param {string} packageJsonPath An absolute path to a `package.json` file.
*
* @returns {boolean} Whether to include (`true`) or skip (`false`) processing the given directory/package.
*/
11 changes: 10 additions & 1 deletion packages/ckeditor5-dev-release-tools/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import checkVersionAvailability from '../lib/utils/checkversionavailability.js';
import getNpmTagFromVersion from '../lib/utils/getnpmtagfromversion.js';
import isVersionPublishableForTag from '../lib/utils/isversionpublishablefortag.js';
import provideToken from '../lib/utils/providetoken.js';
import findPathsToPackages from '../lib/utils/findpathstopackages.js';

import * as index from '../lib/index.js';

Expand All @@ -53,7 +54,8 @@ vi.mock( '../lib/utils/changelog' );
vi.mock( '../lib/utils/executeinparallel' );
vi.mock( '../lib/utils/validaterepositorytorelease' );
vi.mock( '../lib/utils/isversionpublishablefortag' );
vi.mock( '../lib/utils/provideToken' );
vi.mock( '../lib/utils/providetoken' );
vi.mock( '../lib/utils/findpathstopackages' );

describe( 'dev-release-tools/index', () => {
describe( 'generateChangelogForSinglePackage()', () => {
Expand Down Expand Up @@ -244,4 +246,11 @@ describe( 'dev-release-tools/index', () => {
expect( index.provideToken ).to.equal( provideToken );
} );
} );

describe( 'findPathsToPackages()', () => {
it( 'should be a function', () => {
expect( findPathsToPackages ).to.be.a( 'function' );
expect( index.findPathsToPackages ).to.equal( findPathsToPackages );
} );
} );
} );
Loading

0 comments on commit 7105a19

Please sign in to comment.