// // Copyright 2021 DXOS.org // import assert from 'assert'; import chalk from 'chalk'; import columnify from 'columnify'; import { randomBytes } from 'crypto'; import { readdir } from 'fs/promises'; import { join } from 'path'; import { Argv, CommandModule } from 'yargs'; import { asyncExec, deleteFile, getProjects, spawn, writeFile } from '../helpers'; /** * Rush commands. */ export const rushModule: CommandModule = { command: 'rush', aliases: ['m'], describe: 'Rush monorepo utils.', handler: (argv: any) => {}, builder: (yargs: Argv) => yargs .demandCommand() .command({ command: 'list [search]', describe: 'List packages.', builder: (yargs: Argv) => yargs, handler: async ({ search, verbose, json }: any) => { const projects = await getProjects(search); if (verbose || json) { console.log(JSON.stringify(projects, undefined, 2)); } else { const rows = columnify(projects.map(({ name, path, shouldPublish }) => ({ package: chalk.green(name), path, public: (shouldPublish ?? true) ? chalk.green('✔') : chalk.red('✗') })), { columns: ['package', 'path', 'public'] }); console.log(rows); } } }) .command({ command: 'cd [name]', describe: 'Change to the directory of the first package matching the name.', builder: (yargs: Argv) => yargs, handler: async ({ shellCommand, name }: any) => { deleteFile(shellCommand); const projects = await getProjects(name); if (!projects.length) { return; } writeFile(shellCommand, `cd ${projects[0].fullPath}`); } }) .command({ command: 'exec [name]', aliases: ['x'], describe: 'Run command in package.', builder: (yargs: Argv) => yargs .option('cmd', { describe: 'Command to execute', default: 'pwd' }), handler: async ({ verbose, name, cmd }: any) => { const projects = await getProjects(name); for await (const { path, fullPath } of projects) { console.log(chalk.green(`\n${path}\n>`), cmd, '\n'); await spawn(cmd, fullPath).run({ verbose, log: true }); } } }) .command({ command: 'pack', describe: 'Pack local packages for linking.', handler: async () => { console.log('NOTE: You should build before packing.'); await asyncExec('rush publish --include-all --pack -p', true); const randomId = randomBytes(6).toString('hex'); console.log(`Saving to common/temp/artifacts/local-${randomId}`); await asyncExec(`rm -r common/temp/artifacts/local-*`, true).catch(); await asyncExec(`cp -r common/temp/artifacts/packages common/temp/artifacts/local-${randomId}`, true); } }) // TODO(burdon): ncu -u --deep '@dxos/*' .command({ command: 'link', describe: 'Update local package updates.', handler: async () => { // TODO(burdon): Until implemented call rush update (uses braneframe/common/config/run.pnpmfile.cjs) throw new Error('Not implemented.'); const linkedPath = process.env.LINKED_PATH; assert(linkedPath, 'LINKED_PATH not set.'); // common/temp/node_modules/.pnpm/local++Users+dmytro+Projects+work+dxos+protocols+c_07e3f1b6dabe1ca3beadc4b47699aca7 const storePath = 'common/temp/node_modules/.pnpm'; const files = await readdir(storePath) for (const file of files) { if (!file.startsWith('local')) { continue; } const innerPackages = await readdir(join(storePath, file, 'node_modules/@dxos')); } } }) };