Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: workspace config for run command #1495

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
"micromark-extension-gfm-strikethrough": "^2.0.0",
"micromark-extension-gfm-table": "^2.0.0",
"micromark-extension-gfm-task-list-item": "^2.0.1",
"minimatch": "^9.0.0",
"fast-glob": "^3.3.2",
"mocha": "^10.0.0",
"npm-package-json-lint": "^7.0.0",
"nyc": "^15.1.0",
Expand Down
35 changes: 15 additions & 20 deletions src/check-project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import prompt from 'prompt'
import semver from 'semver'
import yargsParser from 'yargs-parser'
import {
getSubprojectDirectories,
isMonorepoProject,
glob,
usesReleasePlease
} from '../utils.js'
import { checkBuildFiles } from './check-build-files.js'
Expand Down Expand Up @@ -114,32 +114,27 @@ async function processMonorepo (projectDir, manifest, branchName, repoUrl, ciFil

const projectDirs = []

for (const workspace of workspaces) {
for await (const subProjectDir of glob('.', workspace, {
cwd: projectDir,
absolute: true
})) {
const stat = await fs.stat(subProjectDir)
for (const subProjectDir of await getSubprojectDirectories(workspaces, projectDir)) {
const stat = await fs.stat(subProjectDir)

if (!stat.isDirectory()) {
continue
}
if (!stat.isDirectory()) {
continue
}

const manfest = path.join(subProjectDir, 'package.json')
const manfest = path.join(subProjectDir, 'package.json')

if (!fs.existsSync(manfest)) {
continue
}
if (!fs.existsSync(manfest)) {
continue
}

const pkg = fs.readJSONSync(manfest)
const homePage = `${repoUrl}/tree/${branchName}${subProjectDir.substring(projectDir.length)}`
const pkg = fs.readJSONSync(manfest)
const homePage = `${repoUrl}/tree/${branchName}${subProjectDir.substring(projectDir.length)}`

console.info('Found monorepo project', pkg.name)
console.info('Found monorepo project', pkg.name)

await processModule(subProjectDir, pkg, branchName, repoUrl, homePage, ciFile, manifest)
await processModule(subProjectDir, pkg, branchName, repoUrl, homePage, ciFile, manifest)

projectDirs.push(subProjectDir)
}
projectDirs.push(subProjectDir)
}

await alignMonorepoProjectDependencies(projectDirs)
Expand Down
9 changes: 9 additions & 0 deletions src/cmds/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadUserConfig } from '../config/user.js'
import runCmd from '../run.js'
import { listWorkspaces } from '../utils.js'

/**
* @typedef {import("yargs").Argv} Argv
Expand Down Expand Up @@ -39,6 +40,14 @@ export default {
type: 'number',
describe: 'How many scripts to run at the same time',
default: userConfig.run.concurrency
},

workspaces: {
// an array of strings
array: true,
describe: 'Run the script in a specific workspace',
default: await listWorkspaces(process.cwd()),
alias: ['workspace', 'roots']
}
})
.positional('script', {
Expand Down
2 changes: 1 addition & 1 deletion src/docs/readme-updater-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function load (app) {
let projects = {}

if (isMonorepoParent) {
projects = parseProjects(process.cwd(), pkg.workspaces)
projects = parseProjects(pkg.workspaces)
}

// when rendering has finished, work out which UrlMappings refer to the index
Expand Down
2 changes: 1 addition & 1 deletion src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
async run (ctx) {
const forwardArgs = ctx['--'] ? ctx['--'] : []

await everyMonorepoProject(process.cwd(), async (project) => {
await everyMonorepoProject(async (project) => {
console.info('') // eslint-disable-line no-console
console.info(kleur.grey(`${project.manifest.name}:`), `> ${ctx.command}${forwardArgs.length > 0 ? ` ${forwardArgs.join(' ')}` : ''}`) // eslint-disable-line no-console

Expand Down
4 changes: 2 additions & 2 deletions src/release-rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function releaseMonorepoRcs (commit, ctx) {
/** @type {Record<string, string>} */
const versions = {}

await everyMonorepoProject(process.cwd(), async (project) => {
await everyMonorepoProject(async (project) => {
if (project.manifest.private === true) {
console.info(`Skipping private package ${project.manifest.name}`)
return
Expand All @@ -43,7 +43,7 @@ async function releaseMonorepoRcs (commit, ctx) {
console.info('')

// publish packages
await everyMonorepoProject(process.cwd(), async (project) => {
await everyMonorepoProject(async (project) => {
if (project.manifest.private === true) {
console.info(`Skipping private package ${project.manifest.name}`)
return
Expand Down
27 changes: 11 additions & 16 deletions src/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { execa } from 'execa'
import fs from 'fs-extra'
import Listr from 'listr'
import { calculateSiblingVersion } from './check-project/utils.js'
import { isMonorepoProject, isMonorepoRoot, hasDocs, glob } from './utils.js'
import { isMonorepoProject, isMonorepoRoot, hasDocs, getSubprojectDirectories } from './utils.js'

/**
* @typedef {import("./types").GlobalOptions} GlobalOptions
Expand Down Expand Up @@ -69,7 +69,7 @@ const tasks = new Listr([
const {
siblingVersions,
packageDirs
} = await calculateSiblingVersions(rootDir, workspaces)
} = await calculateSiblingVersions(workspaces, rootDir)

// check these dependency types for monorepo siblings
const dependencyTypes = [
Expand Down Expand Up @@ -142,27 +142,22 @@ const tasks = new Listr([
], { renderer: 'verbose' })

/**
* @param {string} rootDir
* @param {string[]} workspaces
* @param {string} rootDir
*/
async function calculateSiblingVersions (rootDir, workspaces) {
async function calculateSiblingVersions (workspaces, rootDir) {
const packageDirs = []

/** @type {Record<string, string>} */
const siblingVersions = {}

for (const workspace of workspaces) {
for await (const subProjectDir of glob(rootDir, workspace, {
cwd: rootDir,
absolute: true
})) {
const pkg = JSON.parse(fs.readFileSync(path.join(subProjectDir, 'package.json'), {
encoding: 'utf-8'
}))

siblingVersions[pkg.name] = calculateSiblingVersion(pkg.version)
packageDirs.push(subProjectDir)
}
for (const subProjectDir of await getSubprojectDirectories(workspaces, rootDir)) {
const pkg = JSON.parse(fs.readFileSync(path.join(subProjectDir, 'package.json'), {
encoding: 'utf-8'
}))

siblingVersions[pkg.name] = calculateSiblingVersion(pkg.version)
packageDirs.push(subProjectDir)
}

return {
Expand Down
3 changes: 2 additions & 1 deletion src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {

const forwardArgs = ctx['--'] == null ? [] : ['--', ...ctx['--']]

await everyMonorepoProject(process.cwd(), async (project) => {
await everyMonorepoProject(async (project) => {
for (const script of scripts) {
if (project.manifest.scripts[script] == null) {
continue
Expand All @@ -45,6 +45,7 @@ export default {
}
}
}, {
workspaces: ctx.workspaces,
concurrency: ctx.concurrency
})
}
Expand Down
9 changes: 3 additions & 6 deletions src/test-dependant/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import os from 'os'
import path from 'path'
import fs from 'fs-extra'
import {
exec,
glob
exec, getSubprojectDirectories
} from '../utils.js'

/**
Expand Down Expand Up @@ -197,10 +196,8 @@ const testMonoRepo = async (targetDir, deps, scriptName) => {
}

// test each package that depends on passed deps
for (const pattern of config.workspaces) {
for await (const match of glob(targetDir, pattern)) {
await testModule(path.join(targetDir, match), deps, scriptName)
}
for (const match of await getSubprojectDirectories(config.workspaces, targetDir)) {
await testModule(path.join(targetDir, match), deps, scriptName)
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ interface GlobalOptions {
* Full config from configuration file
*/
fileConfig: Options

}

interface BuildOptions {
Expand Down Expand Up @@ -391,9 +392,11 @@ interface ExecOptions {
* Run commands in parallel up to this limit
*/
concurrency?: number

}

interface RunOptions {

/**
* If false, the script will continue to be run in other packages
*/
Expand All @@ -408,6 +411,12 @@ interface RunOptions {
* Run scripts in parallel up to this limit
*/
concurrency?: number

/**
* Workspaces to run the command in
*/
workspaces?: string[]

}

export type {
Expand Down
Loading
Loading