Skip to content

Commit

Permalink
feat: print runtime digest
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Sep 12, 2020
1 parent e1339e2 commit 631897e
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 326 deletions.
29 changes: 28 additions & 1 deletion src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,37 @@ import {join} from 'path'
import findCacheDir from 'find-cache-dir'
import chalk from 'chalk'
import {invoke, formatFlags, getSymlinkType, getWorkspaces, getYarn, getNpm, readJson} from './util'
import {sync as pkgDir} from 'pkg-dir'

type TContext = { cwd: string, temp: string, flags: Record<string, any>, manifest: Record<string, any>}

type TCallback = (cxt: TContext) => void | Promise<void>

type TStage = [string, ...TCallback[]]

/**
* Print runtime context digest.
*/
const printRuntimeDigest: TCallback = ({temp, flags, manifest}) => {
if (flags.silent) {
return
}

const isMonorepo = !!manifest.workspaces
const npmPath = getNpm(isMonorepo, flags['npm-v7'])
const npmVersion = invoke(npmPath, ['--version'], temp, true, false)
const nodeVersion = invoke('node', ['--version'], temp, true, false)
const yarnAuditFixVersion = readJson(join(pkgDir(__dirname) + '', 'package.json')).version

console.log(JSON.stringify({
isMonorepo,
npmPath,
npmVersion,
nodeVersion,
yarnAuditFixVersion,
}, null, 2).replace(/[":,{}]/g, ''))
}

/**
* Prepare temp assets.
* @param {TContext} cxt
Expand Down Expand Up @@ -79,7 +103,6 @@ const npmAuditFix: TCallback = ({temp, flags, cwd, manifest}) => {
`--prefix=${temp}`,
]

invoke(npm, ['--version'], temp, flags.silent)
invoke(npm, auditArgs, temp, flags.silent)
}

Expand Down Expand Up @@ -112,6 +135,10 @@ const clear: TCallback = ({temp}) =>
fs.emptyDirSync(temp)

export const stages: TStage[] = [
[
'Runtime digest',
printRuntimeDigest,
],
[
'Preparing temp assets...',
clear,
Expand Down
9 changes: 6 additions & 3 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import cp from 'child_process'
import cp, {StdioOptions} from 'child_process'
import chalk from 'chalk'
import {FsSymlinkType, readFileSync} from 'fs-extra'
import minimist from 'minimist'
import {resolve} from 'path'
import glob, {Options as GlobOptions} from 'bash-glob'
import {sync as pkgDir} from 'pkg-dir'

export const invoke = (cmd: string, args: string[], cwd: string, silent= false) => {
export const invoke = (cmd: string, args: string[], cwd: string, silent= false, inherit = true) => {
!silent && console.log(chalk.bold('invoke'), cmd, ...args)

const result = cp.spawnSync(cmd, args, {cwd, stdio: ['inherit', 'inherit', 'inherit']})
const stdio: StdioOptions = inherit ? ['inherit', 'inherit', 'inherit'] : [null, null, null]
const result = cp.spawnSync(cmd, args, {cwd, stdio})

if (result.error || result.status) {
throw result
}

return result.stdout?.toString().trim()
}

export const parseFlags = (argv: string[]) => minimist(argv, {'--': true})
Expand Down
Loading

0 comments on commit 631897e

Please sign in to comment.