[READ-ONLY] Mirror of https://github.com/danielroe/siroc. Zero-config build tooling for Node
bundle node package rollup typescript
0

Configure Feed

Select the types of activity you want to include in your feed.

feat(cli): allow custom commands

Daniel Roe (Jun 16, 2020, 9:49 AM +0100) 8a2a2324 50940039

+53 -36
+22 -14
packages/cli/src/index.ts
··· 1 1 import 'v8-compile-cache' 2 - import { resolve } from 'path' 3 2 import { PerformanceObserver, performance } from 'perf_hooks' 4 3 4 + import { Package } from '@siroc/core' 5 5 import cac from 'cac' 6 6 import { bold } from 'chalk' 7 7 import consola from 'consola' 8 - import { readJSONSync } from 'fs-extra' 9 8 10 9 import { version } from '../package.json' 11 10 ··· 15 14 import { run as runFile } from './commands/run' 16 15 import { test } from './commands/testing' 17 16 18 - import { time, timeEnd } from './utils' 17 + import { time, timeEnd, RemoveFirst } from './utils' 19 18 20 - time('assign root JSON') 21 - let exampleProject = '@siroc/cli' 19 + time('load root package') 20 + let rootPackage: Package 22 21 try { 23 - // eslint-disable-next-line 24 - const { name } = readJSONSync(resolve(process.cwd(), './package.json')) 25 - if (name) exampleProject = name 26 - // eslint-disable-next-line 27 - } catch {} 28 - timeEnd('assign root JSON') 22 + rootPackage = new Package() 23 + } catch (e) { 24 + throw new Error(`Couldn't load package: ${e}`) 25 + } 26 + timeEnd('load root package') 27 + 28 + const exampleProject = rootPackage.pkg.name || '@siroc/cli' 29 29 30 30 const obs = new PerformanceObserver(items => { 31 31 const { duration, name } = items.getEntries()[0] ··· 38 38 time('load CLI') 39 39 const cli = cac('siroc') 40 40 41 - const run = async <A extends (...args: any[]) => Promise<void>>( 41 + const run = async < 42 + A extends (pkg: Package, ...args: any[]) => void | Promise<void> 43 + >( 42 44 type: string, 43 45 action: A, 44 - ...args: Parameters<A> 46 + ...args: RemoveFirst<Parameters<A>> 45 47 ) => { 46 48 performance.mark(`Start ${type}`) 47 - await action(...args).catch(err => { 49 + await Promise.resolve(action(rootPackage, ...args)).catch(err => { 48 50 consola.error(err) 49 51 process.exit(1) 50 52 }) ··· 102 104 .action(packages => 103 105 run('starting eslint', test, { packages, command: 'eslint' }) 104 106 ) 107 + 108 + Object.entries(rootPackage.options.commands).forEach(([command, action]) => { 109 + cli 110 + .command(`${command}`, `Custom command (${bold(rootPackage.pkg.name)})`) 111 + .action(() => run(command, action)) 112 + }) 105 113 106 114 cli.version(version) 107 115 cli.help()
+4
packages/cli/src/utils.ts
··· 4 4 export const timeEnd = (id: string) => { 5 5 if (process.env.TIME) console.timeEnd(id) 6 6 } 7 + 8 + export type RemoveFirst<T extends Array<any>> = T[1] extends undefined 9 + ? never[] 10 + : [T[1]]
+4 -4
packages/cli/src/commands/build.ts
··· 11 11 packages: string[] 12 12 } 13 13 14 - export async function build({ packages, ...options }: BuildCommandOptions) { 15 - // Read package at current directory 16 - const rootPackage = new Package() 17 - 14 + export async function build( 15 + rootPackage: Package, 16 + { packages, ...options }: BuildCommandOptions 17 + ) { 18 18 const workspacePackages = await rootPackage.getWorkspacePackages( 19 19 packages.length ? packages : undefined 20 20 )
+1 -2
packages/cli/src/commands/changelog.ts
··· 1 1 import { writeFile } from 'fs-extra' 2 2 import { getChangelog, Package } from '@siroc/core' 3 3 4 - export async function changelog() { 5 - const rootPackage = new Package() 4 + export async function changelog(rootPackage: Package) { 6 5 const changelog = await getChangelog(rootPackage) 7 6 8 7 process.stdout.write('\n\n' + changelog + '\n\n')
+4 -4
packages/cli/src/commands/dev.ts
··· 4 4 packages: string[] 5 5 } 6 6 7 - export async function dev({ packages }: DevCommandOptions) { 8 - // Read package at current directory 9 - const rootPackage = new Package() 10 - 7 + export async function dev( 8 + rootPackage: Package, 9 + { packages }: DevCommandOptions 10 + ) { 11 11 const workspacePackages = await rootPackage.getWorkspacePackages( 12 12 packages.length ? packages : undefined 13 13 )
+4 -7
packages/cli/src/commands/run.ts
··· 14 14 } 15 15 } 16 16 17 - export async function run({ 18 - file, 19 - args, 20 - options: { workspaces, sequential }, 21 - }: RunCommandOptions) { 17 + export async function run( 18 + rootPackage: Package, 19 + { file, args, options: { workspaces, sequential } }: RunCommandOptions 20 + ) { 22 21 const fullCommand = `${file} ${args.join()}`.trim() 23 22 const filepath = resolve(process.cwd(), file) 24 23 const isLocalFile = ··· 41 40 consola.error(`Error running ${bold(fullCommand)}\n`, gray(e)) 42 41 } 43 42 } 44 - 45 - const rootPackage = new Package() 46 43 47 44 const packages = workspaces 48 45 ? await rootPackage.getWorkspacePackages()
+4 -3
packages/cli/src/commands/testing.ts
··· 62 62 options?: Record<string, any> 63 63 } 64 64 65 - export async function test({ packages, command }: CommandOptions) { 66 - const rootPackage = new Package() 67 - 65 + export async function test( 66 + rootPackage: Package, 67 + { packages, command }: CommandOptions 68 + ) { 68 69 if (packages.length) { 69 70 const workspacePackages = await rootPackage.getWorkspacePackages(packages) 70 71 workspacePackages.forEach(async pkg => commands[command](pkg))
+5
packages/core/src/build/hooks.ts
··· 23 23 } 24 24 25 25 export type PackageHooks = Hooks<PackageHookOptions> 26 + 27 + export type PackageCommands = Record< 28 + string, 29 + (pkg: Package) => void | Promise<void> 30 + >
+5 -2
packages/core/src/package/index.ts
··· 16 16 17 17 import type { 18 18 BuildConfigOptions, 19 + PackageCommands, 19 20 PackageHookOptions, 20 21 PackageHooks, 21 22 } from '../build' ··· 34 35 build: boolean 35 36 suffix: string 36 37 hooks: PackageHooks 37 - pkg?: PackageJson 38 + commands: PackageCommands 38 39 linkedDependencies?: string[] 39 - sortDependencies?: boolean 40 + pkg?: PackageJson 40 41 rollup?: BuildConfigOptions & RollupOptions 42 + sortDependencies?: boolean 41 43 } 42 44 43 45 export type PackageOptions = Partial<DefaultPackageOptions> ··· 60 62 build: true, 61 63 suffix: process.env.PACKAGE_SUFFIX ? `-${process.env.PACKAGE_SUFFIX}` : '', 62 64 hooks: {}, 65 + commands: {}, 63 66 } 64 67 65 68 export class Package {