Просмотр исходного кода

make workspace-scripts relocation work

Adam Shaw 3 лет назад
Родитель
Сommit
ed3c13615b

+ 1 - 0
pnpm-workspace.yaml

@@ -1,4 +1,5 @@
 packages:
+  - ./scripts
   - ./tests
   - ./bundle
   - ./packages/*

+ 1 - 1
scripts/config/postcss.config.cjs

@@ -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

+ 17 - 0
scripts/src/clean.ts

@@ -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 ?
@@ -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'),

+ 3 - 3
scripts/src/pkg/utils/rollup-plugins.ts

@@ -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
 // -------------------------------------------------------------------------------------------------
@@ -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,
   })
 }
 

+ 3 - 3
scripts/src/pkg/utils/rollup-presets.ts

@@ -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,
@@ -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' ?
@@ -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)
 

+ 4 - 2
scripts/src/subrepos/ghost-files.ts

@@ -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,
@@ -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),

+ 46 - 21
scripts/src/utils/monorepo-ts.ts

@@ -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,
   ], {
@@ -19,7 +19,7 @@ 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,
   ], {
@@ -27,21 +27,35 @@ export async function watchTs(dir: string, tscArgs: string[] = []): Promise<() =
   })
 }
 
-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
 
@@ -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),
+  )
 }

+ 23 - 4
scripts/src/utils/script-runner.ts

@@ -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'
 
@@ -9,10 +10,10 @@ 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)
 
@@ -20,6 +21,7 @@ export async function runScript(scriptPkgDir: string): Promise<void> {
     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)
@@ -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 ''
+}

+ 2 - 2
scripts/src/utils/turbo.ts

@@ -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,