Skip to content

Commit

Permalink
make workspace-scripts relocation work
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Nov 1, 2022
1 parent 3841db9 commit ed3c136
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 36 deletions.
1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
packages:
- ./scripts
- ./tests
- ./bundle
- ./packages/*
2 changes: 1 addition & 1 deletion scripts/config/postcss.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
require('postcss-nesting'),
require('@arshaw/postcss-custom-properties')({ // a fork that does preserveWithFallback
// available to all stylesheets
importFrom: require.resolve('../../standard/packages/core/src/styles/vars.css'),
importFrom: require.resolve('../../packages/core/src/styles/vars.css'),
// keep var statements intact (but still reduce their value in a second statement)
preserve: true,
// the preserved var statements will have a fallback value
Expand Down
17 changes: 17 additions & 0 deletions scripts/src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default async function(this: ScriptContext, ...args: string[]) {
const isAll = args.includes('--all')

await Promise.all([
deleteRootDist(monorepoDir),
deleteRootTsconfig(monorepoDir),
deleteGlobalTurboCache(monorepoDir),
deleteMonorepoArchives(monorepoStruct),
isAll ?
Expand All @@ -20,6 +22,21 @@ export default async function(this: ScriptContext, ...args: string[]) {
])
}

// for deleting archives (only applies to 'standard')
function deleteRootDist(monorepoDir: string): Promise<void> {
return rm(
joinPaths(monorepoDir, 'dist'),
{ force: true },
)
}

function deleteRootTsconfig(monorepoDir: string): Promise<void> {
return rm(
joinPaths(monorepoDir, 'tsconfig.json'),
{ force: true },
)
}

function deleteGlobalTurboCache(monorepoDir: string): Promise<void> {
return rm(
joinPaths(monorepoDir, 'node_modules/.cache/turbo'),
Expand Down
6 changes: 3 additions & 3 deletions scripts/src/pkg/utils/rollup-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Plugin } from 'rollup'
import { execLive } from '../../utils/exec.js'
import { strsToProps } from '../../utils/lang.js'
import { monorepoScriptsDir } from '../../utils/script-runner.js'
import { standardScriptsDir } from '../../utils/script-runner.js'

// Generated Content
// -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -154,12 +154,12 @@ async function minifySeparately(path: string): Promise<void> {
}

return execLive([
joinPaths(monorepoScriptsDir, 'node_modules/.bin/terser'),
joinPaths(standardScriptsDir, 'node_modules/.bin/terser'),
'--config-file', 'config/terser.json',
'--output', pathMatch[1] + '.min' + pathMatch[2],
'--', path,
], {
cwd: monorepoScriptsDir,
cwd: standardScriptsDir,
})
}

Expand Down
6 changes: 3 additions & 3 deletions scripts/src/pkg/utils/rollup-presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { mapProps } from '../../utils/lang.js'
import { MonorepoStruct } from '../../utils/monorepo-struct.js'
import { analyzePkg } from '../../utils/pkg-analysis.js'
import { readPkgJson } from '../../utils/pkg-json.js'
import { monorepoScriptsDir } from '../../utils/script-runner.js'
import { standardScriptsDir } from '../../utils/script-runner.js'
import {
computeExternalPkgs,
computeIifeExternalPkgs,
Expand Down Expand Up @@ -290,7 +290,7 @@ function cssPlugin(options?: { inject?: CssInjector | boolean }): Plugin {

return postcssPlugin({
config: {
path: joinPaths(monorepoScriptsDir, 'config/postcss.config.cjs'),
path: joinPaths(standardScriptsDir, 'config/postcss.config.cjs'),
ctx: {}, // arguments given to config file
},
inject: typeof inject === 'object' ?
Expand Down Expand Up @@ -325,7 +325,7 @@ async function buildBanner(pkgBundleStruct: PkgBundleStruct): Promise<string> {
const fullPkgJson = { ...basePkgJson, ...pkgJson }

// TODO: cache the template
const templatePath = joinPaths(monorepoScriptsDir, 'config/banner.tpl')
const templatePath = joinPaths(standardScriptsDir, 'config/banner.tpl')
const templateText = await readFile(templatePath, 'utf8')
const template = handlebars.compile(templateText)

Expand Down
6 changes: 4 additions & 2 deletions scripts/src/subrepos/ghost-files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join as joinPaths } from 'path'
import { readFile, writeFile, copyFile, rm } from 'fs/promises'
import { monorepoDir } from '../utils/script-runner.js'
import { ScriptContext } from '../utils/script-runner.js'
import {
addFile,
assumeUnchanged,
Expand All @@ -14,7 +14,9 @@ import { querySubrepoSubdirs } from '../utils/git-subrepo.js'
// config
import ghostFileConfigMap, { GhostFileConfig } from '../../config/ghost-files.js'

export default async function(...args: string[]) {
export default async function(this: ScriptContext, ...args: string[]) {
const { monorepoDir } = this.monorepoStruct

await updateGhostFiles(
monorepoDir,
await querySubrepoSubdirs(monorepoDir),
Expand Down
67 changes: 46 additions & 21 deletions scripts/src/utils/monorepo-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { join as joinPaths, relative as relativizePath } from 'path'
import { execLive, spawnLive } from './exec.js'
import { stringifyJson, writeIfDifferent } from './fs.js'
import { MonorepoStruct, PkgStruct, traverseMonorepoGreedy } from './monorepo-struct.js'
import { monorepoScriptsDir } from './script-runner.js'
import { standardScriptsDir } from './script-runner.js'

export async function compileTs(dir: string, tscArgs: string[] = []): Promise<void> {
await execLive([
joinPaths(monorepoScriptsDir, 'node_modules/.bin/tsc'),
joinPaths(standardScriptsDir, 'node_modules/.bin/tsc'),
'-b',
...tscArgs,
], {
Expand All @@ -19,29 +19,43 @@ export async function watchTs(dir: string, tscArgs: string[] = []): Promise<() =
await compileTs(dir, tscArgs)
// for watching, will compile again but will be quick
return spawnLive([
joinPaths(monorepoScriptsDir, 'node_modules/.bin/tsc'),
joinPaths(standardScriptsDir, 'node_modules/.bin/tsc'),
'-b', '--watch',
...tscArgs,
], {
cwd: dir,
})
}

export function writeTsconfigs(
export async function writeTsconfigs(
monorepoStruct: MonorepoStruct,
startPkgDir = '',
): Promise<void> {
return traverseMonorepoGreedy(
const refDirs: string[] = []

await traverseMonorepoGreedy(
monorepoStruct,
(pkgStruct) => writePkgTsconfig(pkgStruct, monorepoStruct),
async (pkgStruct) => {
if (await writePkgTsconfig(pkgStruct, monorepoStruct)) {
refDirs.push(pkgStruct.pkgDir)
}
},
startPkgDir,
)

if (!startPkgDir) {
await writePkgTsconfigWithRefs(
monorepoStruct.monorepoDir,
refDirs,
{ files: [] },
)
}
}

async function writePkgTsconfig(
pkgStruct: PkgStruct,
monorepoStruct: MonorepoStruct,
): Promise<void> {
): Promise<boolean> {
const { pkgDir, pkgJson, localDepDirs } = pkgStruct
const { tsConfig } = pkgJson

Expand All @@ -56,21 +70,32 @@ async function writePkgTsconfig(
}
}

refDirs.sort() // deterministic order
await writePkgTsconfigWithRefs(pkgDir, refDirs, tsConfig)
return true
}

return false
}

const finalTsConfig = {
...tsConfig,
references: [
...(tsConfig.references || []),
...refDirs.map((refDir) => ({
path: relativizePath(pkgDir, refDir),
})),
],
}
async function writePkgTsconfigWithRefs(
pkgDir: string,
refDirs: string[], // gets modified in-place
tsConfigBase: any,
): Promise<void> {
refDirs.sort() // deterministic order

await writeIfDifferent(
joinPaths(pkgDir, 'tsconfig.json'),
stringifyJson(finalTsConfig),
)
const finalTsConfig = {
...tsConfigBase,
references: [
...(tsConfigBase.references || []),
...refDirs.map((refDir) => ({
path: relativizePath(pkgDir, refDir),
})),
],
}

await writeIfDifferent(
joinPaths(pkgDir, 'tsconfig.json'),
stringifyJson(finalTsConfig),
)
}
27 changes: 23 additions & 4 deletions scripts/src/utils/script-runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join as joinPaths } from 'path'
import { join as joinPaths, sep as pathSep } from 'path'
import { fileURLToPath } from 'url'
import { fileExists } from './fs.js'
import { MonorepoStruct, readMonorepo } from './monorepo-struct.js'
// import { compileTs, writeTsconfigs } from './monorepo-ts.js'

Expand All @@ -9,17 +10,18 @@ export interface ScriptContext {
scriptName: string
}

export const monorepoScriptsDir = joinPaths(fileURLToPath(import.meta.url), '../../..')
export const monorepoDir = joinPaths(monorepoScriptsDir, '../..')
export const standardScriptsDir = joinPaths(fileURLToPath(import.meta.url), '../../..')

export async function runScript(scriptPkgDir: string): Promise<void> {
const cwd = process.cwd()
const scriptName = process.argv[2]
const scriptArgs = process.argv.slice(3)

if (!scriptName) {
throw new Error('Must provide a script name')
}

const monorepoDir = await findNearestMonorepoRoot(cwd)
const monorepoStruct = await readMonorepo(monorepoDir)
// await writeTsconfigs(monorepoStruct, scriptPkgDir)
// await compileTs(scriptPkgDir)
Expand All @@ -33,10 +35,27 @@ export async function runScript(scriptPkgDir: string): Promise<void> {
}

const scriptContext: ScriptContext = {
cwd: process.cwd(),
cwd,
monorepoStruct,
scriptName,
}

await scriptMain.apply(scriptContext, scriptArgs)
}

// TODO: cleanup
async function findNearestMonorepoRoot(currentDir: string): Promise<string> {
const parts = currentDir.split(pathSep)

while (parts.length) {
const dir = parts.join(pathSep)

if (await fileExists(joinPaths(dir, 'pnpm-workspace.yaml'))) {
return dir
}

parts.pop()
}

return ''
}
4 changes: 2 additions & 2 deletions scripts/src/utils/turbo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { join as joinPaths } from 'path'
import { execLive } from './exec.js'
import { monorepoScriptsDir } from './script-runner.js'
import { standardScriptsDir } from './script-runner.js'

export function runTurboTasks(monorepoDir: string, turboRunArgs: string[]): Promise<void> {
return execLive([
joinPaths(monorepoScriptsDir, 'node_modules/.bin/turbo'),
joinPaths(standardScriptsDir, 'node_modules/.bin/turbo'),
'run', ...turboRunArgs,
], {
cwd: monorepoDir,
Expand Down

0 comments on commit ed3c136

Please sign in to comment.