Skip to content

Commit

Permalink
fix: all around improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vladcos committed Dec 28, 2022
1 parent 916c053 commit cecbecc
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 140 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/dist
*.md
.nvmrc
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16
v18
51 changes: 41 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"dependencies": {
"@npmcli/arborist": "^6.1.5",
"@oclif/core": "^1.22.0",
"@oclif/core": "^1.23.0",
"@oclif/plugin-autocomplete": "^1.3.8",
"@oclif/plugin-help": "^5",
"@oclif/plugin-not-found": "^2.3.11",
Expand All @@ -33,6 +33,7 @@
"log-symbols": "5.1.0",
"node-notifier": "^10.0.1",
"npm-packlist": "^7.0.4",
"promisify-child-process": "^4.1.1",
"tsconfig-paths": "^4.1.1",
"zod": "^3.20.2"
},
Expand All @@ -45,7 +46,7 @@
"@types/node-notifier": "^8.0.2",
"@types/npm-packlist": "^3.0.0",
"@types/npmcli__arborist": "^5.6.0",
"chetzof-lint-config": "^1.0.8",
"chetzof-lint-config": "^1.0.9",
"eslint-config-oclif": "^4",
"eslint-config-oclif-typescript": "^1.0.3",
"eslint-define-config": "^1.12.0",
Expand All @@ -59,7 +60,8 @@
"tslib": "^2.4.1",
"typescript": "^4.9.4",
"vite-tsconfig-paths": "^4.0.3",
"vitest": "0.26.2"
"vitest": "0.26.2",
"vitest-mock-process": "^1.0.4"
},
"oclif": {
"bin": "linktink",
Expand Down Expand Up @@ -90,7 +92,7 @@
"semantic-release": "semantic-release"
},
"engines": {
"node": ">=14.17.0"
"node": ">=16"
},
"bugs": "https://github.com/chetzof/linktink/issues",
"keywords": [
Expand Down
2 changes: 2 additions & 0 deletions src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ export default class Sync extends Command {
syncPaths: path.resolve(inputArguments.from),
},
})
// eslint-disable-next-line no-process-exit,unicorn/no-process-exit
process.exit(0)
}
}
8 changes: 3 additions & 5 deletions src/lib/child-process.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import * as child_process from 'node:child_process'
import * as util from 'node:util'

const execAsync = util.promisify(child_process.exec)
import { exec } from 'promisify-child-process'

export async function execNpm(
command: string,
Expand All @@ -20,6 +17,7 @@ export async function execNpm(
.join(' ')
const compiledCommand = `npm ${compiledOptions} ${command} `
// console.log(compiledCommand)
const output = await execAsync(compiledCommand, { cwd })
const output = await exec(compiledCommand, { cwd })
// console.log(output)
return output.stdout
}
18 changes: 13 additions & 5 deletions src/lib/misc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'

import { copy } from 'fs-extra'
import { read } from 'fs-jetpack'
import jetpack from 'fs-jetpack'

import { execNpm } from '@/lib/child-process'

Expand All @@ -12,10 +12,18 @@ interface PackageJSON {
}

async function readPackageJson(packageDirectory: string): Promise<PackageJSON> {
return (await read(
path.join(packageDirectory, 'package.json'),
'json',
)) as Promise<PackageJSON>
const cwd = jetpack.cwd(packageDirectory)
const contents = (await cwd.readAsync('package.json', 'json')) as
| PackageJSON
| undefined

if (!contents) {
throw new Error(
`Could not find a package.json file in the directory '${packageDirectory}'`,
)
}

return contents
}

export async function getPackageName(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stdin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function listenToQuitKey(callback: () => void): void {
return
}

if (key.name === 'q') {
if (key.name === 'q' || key.name === 'ę') {
process.stdin.removeListener('keypress', handler)
callback()
}
Expand Down
22 changes: 22 additions & 0 deletions src/lib/sync/subtasks/graceful-exit-task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { execNpm } from '@/lib/child-process'
import type { Context } from '@/lib/sync/tasks'

import type { ListrTask } from 'listr2'

export function gracefulExitTask(): ListrTask<Context> {
return {
task: (context, task) => {
task.title = 'Graceful exit'
return task.newListr([
{
title: 'Reverting to the previous package version',
task: async (_context) => {
await execNpm('install', {
cwd: context.targetPackagePath,
})
},
},
])
},
}
}
27 changes: 12 additions & 15 deletions src/lib/sync/subtasks/start-watcher-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,10 @@ export function startWatcherTask(): ListrTask<Context> {
title: 'Starting watching the files',
task: (context, task) => {
const newList = task.newListr([], { exitOnError: false })

let intermediateTask = createIntermediateTask()
newList.add(intermediateTask.task)

listenToQuitKey(() => {
intermediateTask.resolve('Triggered graceful exit')
newList.add([
{
title: 'Graceful',
task: () => {
// eslint-disable-next-line no-process-exit,unicorn/no-process-exit
process.exit(1)
},
},
])
})

watch(context.sourcePackagePath, {
const watcher = watch(context.sourcePackagePath, {
ignoreInitial: true,
persistent: true,
ignored: ['**/.git/**', '**/node_modules/**'],
Expand Down Expand Up @@ -134,6 +120,17 @@ export function startWatcherTask(): ListrTask<Context> {
})
})

listenToQuitKey(() => {
newList.add({
title: 'Close watcher',
task: async (_context) => {
await watcher.close()
},
})

intermediateTask.resolve('Quitting')
})

return newList
},
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/sync/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { checkIfSourcePackageInstalledTask } from '@/lib/sync/subtasks/check-if-
import { checkIfThePathExistsTask } from '@/lib/sync/subtasks/check-if-the-path-exists-task'
import { getFallbackPackList } from '@/lib/sync/subtasks/get-fallback-packlist-task'
import { getPackListTask } from '@/lib/sync/subtasks/get-pack-list-task'
import { gracefulExitTask } from '@/lib/sync/subtasks/graceful-exit-task'
import { installTheDependentPackageTask } from '@/lib/sync/subtasks/install-dependent-package-task'
import { startWatcherTask } from '@/lib/sync/subtasks/start-watcher-task'

Expand Down Expand Up @@ -69,6 +70,7 @@ function getTasks(): Array<ListrTask<Context>> {
}),
},
startWatcherTask(),
gracefulExitTask(),
]
}

Expand All @@ -77,7 +79,7 @@ export async function runTasks<
>(override: O): Promise<Manager<O['ctx'], NonNullable<O['renderer']>>> {
const manager = new Manager({
concurrent: false,
registerSignalListeners: false,
// registerSignalListeners: false,
rendererOptions: {
collapse: false,
collapseSkips: false,
Expand Down
Loading

0 comments on commit cecbecc

Please sign in to comment.