Skip to content

Commit

Permalink
fix: apply dogfood to this repos workspaces also
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Oct 25, 2022
1 parent ebed330 commit 2ef9995
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 57 deletions.
73 changes: 34 additions & 39 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const deglob = (v) => makePosix(v).replace(/[/*]+$/, '')
const posixDir = (v) => `${v === '.' ? '' : deglob(v).replace(/\/$/, '')}${posix.sep}`
const posixGlob = (str) => `${posixDir(str)}**`

const getCmdPath = (key, { rootConfig, defaultConfig, isRoot, path, root }) => {
const getCmdPath = (key, { rootConfig, defaultConfig, isRoot, pkg, rootPkg }) => {
// Make a path relative from a workspace to the root if we are in a workspace
const wsToRoot = (p) => isRoot ? p : makePosix(join(relative(path, root), p))
const wsToRoot = (p) => isRoot ? p : makePosix(join(relative(pkg.path, rootPkg.path), p))

const rootPath = rootConfig[key]
const defaultPath = defaultConfig[key]
Expand Down Expand Up @@ -82,46 +82,39 @@ const getFiles = (path, rawConfig) => {
}

const getFullConfig = async ({
// the path to the root of the repo
root,
// the path to the package being operated on
// this is the same as root when operating on the root
path,
// the full contents of the package.json for this package
pkgJson,
// an array of all package info {pkgJson,path,config}[]
pkgs,
// an array of all workspaces in this repo
workspaces,
// the config from the package.json in the root
rootConfig: _rootConfig,
// the config from the package.json being operated on
pkgConfig: _pkgConfig,
// everything is an object or an array of objects with the following
// path: the path to the package root
// pkgJson: the package json path
// config: the template oss config from the package root
pkg, // the package currently being operated on
rootPkg, // the root pkg (same as pkg when operating on the root)
pkgs, // an array of all packages to be operated on
wsPkgs, // an array of all workspaces being operated
}) => {
const isRoot = root === path
const isRoot = rootPkg.path === pkg.path
const isWorkspace = !isRoot
const isMono = !!workspaces.length
const isMono = !!wsPkgs.length
const isRootMono = isRoot && isMono

const isLatest = _pkgConfig.version === LATEST_VERSION
const isDogFood = pkgJson.name === NAME
const isLatest = pkg.config.version === LATEST_VERSION
const isDogFood = rootPkg.pkgJson.name === NAME
const isForce = process.argv.includes('--force')

// These config items are merged betweent the root and child workspaces and only come from
// the package.json because they can be used to read configs from other the content directories
const mergedConfig = mergeConfigs(_rootConfig, _pkgConfig)
const mergedConfig = mergeConfigs(rootPkg.config, pkg.config)

const defaultConfig = getConfig(DEFAULT_CONTENT)
const [defaultFiles, defaultDir] = getFiles(DEFAULT_CONTENT, mergedConfig)
const useDefault = mergedConfig.defaultContent && defaultConfig

const rootConfig = getConfig(_rootConfig.content, _rootConfig)
const [rootFiles, rootDir] = getFiles(_rootConfig.content, mergedConfig)
const rootConfig = getConfig(rootPkg.config.content, rootPkg.config)
const [rootFiles, rootDir] = getFiles(rootPkg.config.content, mergedConfig)

// The content config only gets set from the package we are in, it doesn't inherit
// anything from the root
const rootPkgConfig = merge(useDefault, rootConfig)
const pkgConfig = merge(useDefault, getConfig(_pkgConfig.content, _pkgConfig))
const pkgConfig = merge(useDefault, getConfig(pkg.config.content, pkg.config))
const [pkgFiles, pkgDir] = getFiles(mergedConfig.content, mergedConfig)

// Files get merged in from the default content (that template-oss provides) as well
Expand All @@ -139,23 +132,23 @@ const getFullConfig = async ({
...getAddedFiles(repoFiles),
// and allow all workspace repo level files in the root
...pkgs
.filter(p => p.path !== root && p.config.workspaceRepo !== false)
.filter(p => p.path !== rootPkg.path && p.config.workspaceRepo !== false)
.flatMap(() => getAddedFiles(files.workspaceRepo)),
] : [],
]

// root only configs
const npmPath = getCmdPath('npm', { rootConfig, defaultConfig, isRoot, path, root })
const npxPath = getCmdPath('npx', { rootConfig, defaultConfig, isRoot, path, root })
const npmPath = getCmdPath('npm', { rootConfig, defaultConfig, isRoot, pkg, rootPkg })
const npxPath = getCmdPath('npx', { rootConfig, defaultConfig, isRoot, pkg, rootPkg })

// these are written to ci yml files so it needs to always use posix
const pkgPath = makePosix(relative(root, path)) || '.'
const pkgPath = makePosix(relative(rootPkg.path, pkg.path)) || '.'

// we use the raw paths from the package.json workspaces as ignore patterns in
// some cases. the workspaces passed in have already been run through map workspaces
const workspacePaths = (pkgJson.workspaces || []).map(deglob)
const workspacePaths = (pkg.pkgJson.workspaces || []).map(deglob)

const isPrivate = !!pkgJson.private
const isPrivate = !!pkg.pkgJson.private
const isPublic = !isPrivate
const publicPkgs = pkgs.filter(p => !p.pkgJson.private)
const allPrivate = pkgs.every(p => p.pkgJson.private)
Expand All @@ -176,23 +169,23 @@ const getFullConfig = async ({
isPrivate,
allPrivate,
// controls whether we are in a monorepo with any public workspaces
isMonoPublic: isMono && !!publicPkgs.filter(p => p.path !== root).length,
isMonoPublic: isMono && !!publicPkgs.filter(p => p.path !== rootPkg.path).length,
// repo
repoDir: root,
repoDir: rootPkg.path,
repoFiles,
applyRepo: !!repoFiles,
// module
moduleDir: path,
moduleDir: pkg.path,
moduleFiles,
applyModule: !!moduleFiles,
// package
pkgName: pkgJson.name,
pkgNameFs: pkgJson.name.replace(/\//g, '-').replace(/@/g, ''),
pkgName: pkg.pkgJson.name,
pkgNameFs: pkg.pkgJson.name.replace(/\//g, '-').replace(/@/g, ''),
// paths
pkgPath,
pkgDir: posixDir(pkgPath),
pkgGlob: posixGlob(pkgPath),
pkgFlags: isWorkspace ? `-w ${pkgJson.name}` : '',
pkgFlags: isWorkspace ? `-w ${pkg.pkgJson.name}` : '',
allFlags: isMono ? '-ws -iwr --if-present' : '',
workspacePaths,
workspaceGlobs: workspacePaths.map(posixGlob),
Expand All @@ -219,7 +212,9 @@ const getFullConfig = async ({
]),
// these cant be sorted since they rely on order
// to allow a previously ignored directoy
...isRoot ? gitignore.allowDir(workspaces.map((p) => makePosix(relative(root, p)))) : [],
...isRoot
? gitignore.allowDir(wsPkgs.map((p) => makePosix(relative(rootPkg.path, p.path))))
: [],
],
// needs update if we are dogfooding this repo, with force argv, or its
// behind the current version
Expand Down Expand Up @@ -256,7 +251,7 @@ const getFullConfig = async ({
derived.engines = pkgConfig.engines || engines
}

const gitUrl = await getGitUrl(root)
const gitUrl = await getGitUrl(rootPkg.path)
if (gitUrl) {
derived.repository = {
type: 'git',
Expand Down
26 changes: 10 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,37 @@ const getWsPkgs = async (root, rootPkg) => {
}
}

return {
pkgs: wsPkgs,
paths: [...rootWorkspaces.values()],
}
return wsPkgs
}

const getPkgs = async (root) => {
log.verbose('get-pkgs', 'root', root)

const rootPkg = await getPkg(root)

const ws = await getWsPkgs(root, rootPkg)
const wsPkgs = await getWsPkgs(root, rootPkg)

return {
rootPkg,
pkgs: [rootPkg].concat(ws.pkgs),
workspaces: ws.paths,
wsPkgs,
pkgs: [rootPkg].concat(wsPkgs),
}
}

const runAll = async (root, checks) => {
const results = []
const { pkgs, workspaces, rootPkg: { config: rootConfig } } = await getPkgs(root)
const { pkgs, rootPkg, wsPkgs } = await getPkgs(root)

for (const { pkgJson, path, config: pkgConfig } of pkgs) {
for (const pkg of pkgs) {
// full config includes original config values
const fullConfig = await getConfig({
root,
path,
pkgJson,
pkg,
pkgs,
workspaces,
rootConfig,
pkgConfig,
rootPkg,
wsPkgs,
})

const options = { root, path, pkg: pkgJson, config: fullConfig }
const options = { root, path: pkg.path, pkg: pkg.pkgJson, config: fullConfig }
log.verbose('run-all', options)

// files can export multiple checks so flatten first
Expand Down
3 changes: 1 addition & 2 deletions workspace/test-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
"node": "^14.17.0 || ^16.13.0 || >=18.0.0"
},
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"version": "4.7.0"
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten."
},
"tap": {
"nyc-arg": [
Expand Down

0 comments on commit 2ef9995

Please sign in to comment.