Skip to content

Commit

Permalink
feat: improve open file
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay committed Aug 10, 2024
1 parent 0dd006f commit 50626b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
28 changes: 22 additions & 6 deletions cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { execSync } from 'node:child_process'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { platform } from 'node:os'
import { getRegistry, setRegistry, getConfigPath } from './config.mjs'
import { REGISTRIES, speedTest } from './registry.mjs'
import c, { printRegistries } from './utils.mjs'
Expand Down Expand Up @@ -148,13 +149,28 @@ async function test(timeoutLimit) {
async function rc() {
const filePath = await getConfigPath(local)
try {
// try vscode first
execSync(`code ${filePath}`)
} catch {
console.error(
`You do not have vscode installed!\nPlease open ${c.gray(
filePath
)} manually.`
)
process.exit(-1)
/** @type {Record<string, string>} */
const map = { win32: 'start', darwin: 'open', linux: 'xdg-open' }
const cmd = map[platform()]

let shouldRunEditor = false
try {
if (cmd) execSync(`${cmd} "${filePath}"`)
else shouldRunEditor = true
} catch {
shouldRunEditor = true
}

try {
if (shouldRunEditor) execSync(`editor "${filePath}"`)
} catch {
console.error(
`Failed to open file, please open ${c.gray(filePath)} manually.`
)
process.exit(-1)
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"nrml": "cli.mjs"
},
"devDependencies": {
"@types/node": "^20.14.2",
"typescript": "^5.4.5"
"@types/node": "^22.2.0",
"typescript": "^5.5.4"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
Expand Down
2 changes: 1 addition & 1 deletion registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function setRegistryFromStream(stream, registryUrl) {
}

/**
* Returns `Infinity` when execeed timeout, and `null` when network error
* Returns `Infinity` when exceed timeout, and `null` when network error
* @param {string} url
* @param {number} timeoutLimit - in milliseconds
*/
Expand Down

0 comments on commit 50626b5

Please sign in to comment.