Skip to content

Commit

Permalink
clean updates and recompile
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Mar 1, 2023
1 parent 3c4e7f5 commit 8685745
Show file tree
Hide file tree
Showing 24 changed files with 223 additions and 51 deletions.
4 changes: 3 additions & 1 deletion packages/ts-scripts-yarn3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"lint-profile": "dist/cjs/bin/lint-profile.js",
"package-clean": "dist/cjs/bin/package/clean.js",
"package-compile": "dist/cjs/bin/package/compile.js",
"package-recompile": "dist/cjs/bin/package/recompile.js",
"package-compile-cjs": "dist/cjs/bin/package/compile-cjs.js",
"package-compile-esm": "dist/cjs/bin/package/compile-esm.js",
"package-copy-assets-cjs": "dist/cjs/bin/package/copy-assets-cjs.js",
Expand All @@ -39,6 +40,7 @@
"package-tsconfig-gen-cjs": "dist/cjs/bin/package/tsconfig-gen-cjs.js",
"package-tsconfig-gen-esm": "dist/cjs/bin/package/tsconfig-gen-esm.js",
"rebuild": "dist/cjs/bin/rebuild.js",
"recompile": "dist/cjs/bin/recompile.js",
"reinstall": "dist/cjs/bin/reinstall.js",
"relint": "dist/cjs/bin/relint.js",
"sonar": "dist/cjs/bin/sonar.js",
Expand Down Expand Up @@ -145,4 +147,4 @@
},
"sideEffects": false,
"version": "2.15.0"
}
}
12 changes: 12 additions & 0 deletions packages/ts-scripts-yarn3/src/actions/clean-eslint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import chalk from 'chalk'
import { rimrafSync } from 'rimraf'

export const cleanESLint = () => {
const pkg = process.env.INIT_CWD
const pkgName = process.env.npm_package_name
console.log(chalk.green(`Cleaning ESLint [${pkgName}]`))

rimrafSync(`${pkg}/.eslintcache`)

return 0
}
7 changes: 7 additions & 0 deletions packages/ts-scripts-yarn3/src/actions/clean-jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { runSteps } from '../lib'

export const cleanJest = (): number => {
const pkgName = process.env.npm_package_name

return runSteps(`Cleaning Jest [${pkgName}]`, [['jest', ['--clearCache']]])
}
4 changes: 3 additions & 1 deletion packages/ts-scripts-yarn3/src/actions/clean.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { runSteps } from '../lib'
import { cleanESLint } from './clean-eslint'
import { cleanJest } from './clean-jest'

export interface CleanParams {
pkg?: string
}

export const clean = () => {
return runSteps('Clean', [['yarn', 'workspaces foreach -pA run package-clean']])
return cleanJest() + cleanESLint() + runSteps('Clean', [['yarn', 'workspaces foreach -pA run package-clean']])
}
1 change: 1 addition & 0 deletions packages/ts-scripts-yarn3/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './lint-clean'
export * from './lint-profile'
export * from './package'
export * from './rebuild'
export * from './recompile'
export * from './reinstall'
export * from './relint'
export * from './sonar'
Expand Down
15 changes: 15 additions & 0 deletions packages/ts-scripts-yarn3/src/actions/package/clean-outputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import chalk from 'chalk'
import { rmSync } from 'fs'

export const packageCleanOutputs = () => {
const pkg = process.env.INIT_CWD
const pkgName = process.env.npm_package_name
console.log(chalk.green(`Cleaning Outputs [${pkgName}]`))

const dist = `${pkg}/dist`
rmSync(dist, { force: true, recursive: true })

const build = `${pkg}/build`
rmSync(build, { force: true, recursive: true })
return 0
}
13 changes: 13 additions & 0 deletions packages/ts-scripts-yarn3/src/actions/package/clean-typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import chalk from 'chalk'
import { rimrafSync } from 'rimraf'

export const packageCleanTypescript = () => {
const pkg = process.env.INIT_CWD
const pkgName = process.env.npm_package_name
console.log(chalk.green(`Cleaning Typescript [${pkgName}]`))

rimrafSync(`${pkg}/*.tsbuildinfo`)
rimrafSync(`${pkg}/.tsconfig.*`)

return 0
}
18 changes: 3 additions & 15 deletions packages/ts-scripts-yarn3/src/actions/package/clean.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import chalk from 'chalk'
import { rmSync } from 'fs'
import { rimrafSync } from 'rimraf'
import { packageCleanOutputs } from './clean-outputs'
import { packageCleanTypescript } from './clean-typescript'

export const packageClean = () => {
const pkg = process.env.INIT_CWD
const pkgName = process.env.npm_package_name
console.log(chalk.green(`Cleaning [${pkgName}]`))

const dist = `${pkg}/dist`
rmSync(dist, { force: true, recursive: true })

const build = `${pkg}/build`
rmSync(build, { force: true, recursive: true })

rimrafSync(`${pkg}/**/*.tsbuildinfo`)
rimrafSync(`${pkg}/**/.tsconfig.*`)
return packageCleanOutputs() + packageCleanTypescript()
}
5 changes: 5 additions & 0 deletions packages/ts-scripts-yarn3/src/actions/package/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import chalk from 'chalk'
import depcheck, { special } from 'depcheck'
import { existsSync, readFileSync } from 'fs'

import { checkResult } from '../../lib'

export const packageDeps = async () => {
const pkg = process.env.INIT_CWD
const pkgName = process.env.npm_package_name
Expand Down Expand Up @@ -93,6 +95,9 @@ export const packageDeps = async () => {
})
console.log(chalk.yellow(message.join('\n')))
}

checkResult(`Deps [${pkgName}]`, errorCount, 'warn', false)

//returning 0 here since we never want deps to be fatal
return 0
}
3 changes: 3 additions & 0 deletions packages/ts-scripts-yarn3/src/actions/package/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export * from './clean'
export * from './clean-outputs'
export * from './clean-typescript'
export * from './compile'
export * from './compile-cjs'
export * from './compile-esm'
export * from './copy-assets'
export * from './deps'
export * from './gen-docs'
export * from './recompile'
export * from './tsconfig-gen-cjs'
export * from './tsconfig-gen-esm'
7 changes: 7 additions & 0 deletions packages/ts-scripts-yarn3/src/actions/package/recompile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { packageClean } from './clean'
import { packageCompileCjs } from './compile-cjs'
import { packageCompileEsm } from './compile-esm'

export const packageRecompile = async () => {
return (await Promise.all([packageClean(), packageCompileEsm(), packageCompileCjs()])).reduce((prev, value) => prev + value, 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFileSync, writeFileSync } from 'fs'

import { createBuildConfig } from '../../lib'

export const packageTsconfigGenEsm = () => {
export const packageTsconfigGenEsm = (quiet = false) => {
const pkg = process.env.INIT_CWD ?? './'
const pkgName = process.env.npm_package_name

Expand All @@ -19,7 +19,9 @@ export const packageTsconfigGenEsm = () => {
currentConfig = undefined
}
if (currentConfig !== config) {
console.log(chalk.gray(`Updating ESM tsconfig [${pkgName}]`))
if (!quiet) {
console.log(chalk.gray(`Updating ESM tsconfig [${pkgName}]`))
}
writeFileSync(`${pkg}/.tsconfig.build.esm.json`, config, { encoding: 'utf8' })
}
}
Expand Down
45 changes: 45 additions & 0 deletions packages/ts-scripts-yarn3/src/actions/recompile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import chalk from 'chalk'

import { runStepsAsync } from '../lib'

export interface RecompileParams {
incremental?: boolean
jobs?: number
pkg?: string
target?: 'esm' | 'cjs'
verbose?: boolean
}

export interface RecompilePackageParams {
pkg: string
target?: 'esm' | 'cjs'
verbose?: boolean
}

export const recompile = async ({ verbose, target, pkg, incremental }: RecompileParams) => {
return pkg ? await recompilePackage({ pkg, target, verbose }) : await recompileAll({ incremental, target, verbose })
}

export const recompilePackage = ({ verbose, target, pkg }: RecompilePackageParams) => {
const verboseOptions = verbose ? ['-v'] : []
const targetOptions = target ? ['-t', target] : []

return runStepsAsync(`Recompile [${pkg}]`, [['yarn', ['workspace', pkg, 'run', 'package-recompile', ...verboseOptions, ...targetOptions]]])
}

export const recompileAll = async ({ jobs, verbose, target, incremental }: RecompileParams) => {
const start = Date.now()
const verboseOptions = verbose ? ['-v'] : []
const targetOptions = target ? ['-t', target] : []
const incrementalOptions = incremental ? ['--since', '-ptA'] : ['-ptA']
const jobsOptions = jobs ? ['-j', `${jobs}`] : []
if (jobs) {
console.log(chalk.blue(`Jobs set to [${jobs}]`))
}

const result = await runStepsAsync(`Recompile${incremental ? '-Incremental' : ''} [All]`, [
['yarn', ['workspaces', 'foreach', ...incrementalOptions, ...jobsOptions, 'run', 'package-recompile', ...verboseOptions, ...targetOptions]],
])
console.log(`${chalk.gray('Recompiled in')} [${chalk.magenta(((Date.now() - start) / 1000).toFixed(2))}] ${chalk.gray('seconds')}`)
return result
}
5 changes: 5 additions & 0 deletions packages/ts-scripts-yarn3/src/bin/package/clean-outputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { packageCleanOutputs } from '../../actions'

packageCleanOutputs()
5 changes: 5 additions & 0 deletions packages/ts-scripts-yarn3/src/bin/package/clean-typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { packageCleanTypescript } from '../../actions'

packageCleanTypescript()
9 changes: 9 additions & 0 deletions packages/ts-scripts-yarn3/src/bin/package/recompile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node

import chalk from 'chalk'
import { packageRecompile } from '../../actions'

packageRecompile().then((value) => process.exitCode = value).catch((reason) => {
console.error(chalk.red(reason))
process.exitCode = 1
})
5 changes: 5 additions & 0 deletions packages/ts-scripts-yarn3/src/bin/recompile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { runXyWithWarning } from "../lib"

runXyWithWarning('recompile')
12 changes: 12 additions & 0 deletions packages/ts-scripts-yarn3/src/lib/checkResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import chalk from 'chalk'

export const checkResult = (name: string, result: number, level: 'error' | 'warn' = 'error', exitOnFail = false) => {
if (result) {
const exiting = exitOnFail ? '[Exiting Process]' : '[Continuing]'
const chalkFunc = level === 'error' ? chalk.red : chalk.yellow
console[level](chalkFunc(`${name} had ${result} failures ${exiting}`))
if (exitOnFail) {
process.exit(result)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ export const detectDuplicateDependencies = (depsFromPackageJSON?: string[], Defa
} catch (e) {
console.error(`Error running yarn why: ${e}`)
exitCode = 1
return
return exitCode
}

if (output) {
exitCode = new DuplicateDetector(output, dependency).detect()
return
return exitCode
} else {
console.log(`${dependency} - N/A`)
if (depsFromPackageJSON) {
console.log(`🚨 Library ${dependency} was requested in package.json but not found`)
}
exitCode = 1
return
return exitCode
}
})
return exitCode
} else {
console.log('🚨 No dependencies where passed')
return exitCode
}
})
}
1 change: 1 addition & 0 deletions packages/ts-scripts-yarn3/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './checkResult'
export * from './createBuildConfig'
export * from './defaultBuildConfig'
export * from './dependencies'
Expand Down
30 changes: 16 additions & 14 deletions packages/ts-scripts-yarn3/src/lib/runSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import chalk from 'chalk'
import { spawnSync, SpawnSyncOptionsWithBufferEncoding } from 'child_process'
import { existsSync } from 'fs'

import { checkResult } from './checkResult'
import { safeExit } from './safeExit'

export type ScriptStep =
| [/*command*/ 'yarn' | 'node' | 'ts-node-script' | 'tsc', /*arg*/ string | string[]]
| [/*command*/ 'yarn' | 'node' | 'ts-node-script' | 'tsc' | 'jest', /*arg*/ string | string[]]
| [/*command*/ string, /*arg*/ string | string[], /*config*/ SpawnSyncOptionsWithBufferEncoding]

export const runSteps = (name: string, steps: ScriptStep[], exitOnFail = true, messages?: string[]) => {
export const runSteps = (name: string, steps: ScriptStep[], exitOnFail = true, messages?: string[]): number => {
return safeExit(() => {
const pkgName = process.env.npm_package_name
console.log(chalk.green(`${name} [${pkgName}]`))
let totalStatus = 0
for (let i = 0; i < steps.length; i++) {
const [command, args, config] = steps[i]
if (messages?.[i]) {
Expand All @@ -21,17 +23,17 @@ export const runSteps = (name: string, steps: ScriptStep[], exitOnFail = true, m
if (command === 'node' && !existsSync(argList[0])) {
throw Error(`File not found [${argList[0]}]`)
}
const status = spawnSync(command, Array.isArray(args) ? args : args.split(' '), {
...config,
encoding: 'utf8',
env: { FORCE_COLOR: '3', ...process.env },
shell: true,
stdio: 'inherit',
}).status
if (status && exitOnFail) {
return status
}
const status =
spawnSync(command, Array.isArray(args) ? args : args.split(' '), {
...config,
encoding: 'utf8',
env: { FORCE_COLOR: '3', ...process.env },
shell: true,
stdio: 'inherit',
}).status ?? 0
checkResult(name, status, 'error', exitOnFail)
totalStatus += status ?? 0
}
return 0
})
return totalStatus
}, !!exitOnFail)
}
17 changes: 7 additions & 10 deletions packages/ts-scripts-yarn3/src/lib/runStepsAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import chalk from 'chalk'
import { spawn } from 'child_process'
import { existsSync } from 'fs'

import { checkResult } from './checkResult'
import { ScriptStep } from './runSteps'
import { safeExitAsync } from './safeExit'

export const runStepAsync = (step: ScriptStep, exitOnFail = true, message?: string) => {
export const runStepAsync = (name: string, step: ScriptStep, exitOnFail = true, message?: string) => {
return new Promise<number>((resolve) => {
const [command, args, config] = step
if (message) {
Expand All @@ -28,9 +30,7 @@ export const runStepAsync = (step: ScriptStep, exitOnFail = true, message?: stri
)}`,
),
)
if (exitOnFail) {
process.exit(code)
}
checkResult(name, code, 'error', exitOnFail)
resolve(code)
} else {
resolve(0)
Expand All @@ -40,16 +40,13 @@ export const runStepAsync = (step: ScriptStep, exitOnFail = true, message?: stri
}

export const runStepsAsync = async (name: string, steps: ScriptStep[], exitOnFail = true, messages?: string[]) => {
try {
return await safeExitAsync(async () => {
const pkgName = process.env.npm_package_name
console.log(chalk.green(`${name} [${pkgName}]`))
let result = 0
for (let i = 0; i < steps.length; i++) {
result += await runStepAsync(steps[i], exitOnFail, messages?.[i])
result += await runStepAsync(name, steps[i], exitOnFail, messages?.[i])
}
return result
} catch (ex) {
console.error(chalk.red(ex))
process.exit(-1)
}
})
}
Loading

0 comments on commit 8685745

Please sign in to comment.