Skip to content

Commit

Permalink
feat: update fs-memo and improve config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 16, 2020
1 parent c3f2089 commit 5e4acee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"release": "yarn build && standard-version && npm publish && git push --follow-tags"
},
"dependencies": {
"fs-memo": "^1.1.0"
"fs-memo": "^1.2.0"
},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
Expand Down
36 changes: 16 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,43 @@ import { createServer, AddressInfo } from 'net'
import { getMemo, setMemo } from 'fs-memo'

interface GetPortOptions {
name?: string

random?: boolean
port?: number
ports?: number[]

memoDir?: string
memoName?: string
name: string
random: boolean
port: number
ports: number[]
memoDir: string
memoName: string
}

const defaults = {
name: 'default',

random: false,
port: parseInt(process.env.PORT || '') || 3000,
ports: [4000, 5000, 6000, 7000],

memoDir: undefined, // Default is node_modules/.cache/fs-memo
memoName: 'port'
}

export default async function getPort (options?: GetPortOptions): Promise<number> {
const opts = { ...defaults, ...options }
export default async function getPort (config?: Partial<GetPortOptions>): Promise<number> {
const options = { ...defaults, ...config } as GetPortOptions

const portsToCheck: number[] = []

if (!opts.random) {
if (!options.random) {
// options.port
if (opts.port) {
portsToCheck.push(opts.port)
if (options.port) {
portsToCheck.push(options.port)
}

// options.ports
if (Array.isArray(opts.ports)) {
portsToCheck.push(...opts.ports)
if (Array.isArray(options.ports)) {
portsToCheck.push(...options.ports)
}
}

// Memo
const memoOptions = { name: opts.memoName, dir: opts.memoDir }
const memoKey = 'port_' + opts.name
const memoOptions = { name: options.memoName, dir: options.memoDir! }

const memoKey = 'port_' + options.name
const memo = await getMemo(memoOptions)
if (memo[memoKey]) {
portsToCheck.push(memo[memoKey])
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1433,10 +1433,10 @@ fs-extra@^9.0.1:
jsonfile "^6.0.1"
universalify "^1.0.0"

fs-memo@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/fs-memo/-/fs-memo-1.1.0.tgz#a3870c074090d9425b24cf399986a669c1cf0866"
integrity sha512-p5qUMyLAmnBrDuQj2e59kXQI/dym6w3hDNJISmkqXIMa/xE4j69hwySzMM5HtugoTVvVtmcojSCeCwmkmGhSpA==
fs-memo@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/fs-memo/-/fs-memo-1.2.0.tgz#a2ec3be606b902077adbb37ec529c5ec5fb2e037"
integrity sha512-YEexkCpL4j03jn5SxaMHqcO6IuWuqm8JFUYhyCep7Ao89JIYmB8xoKhK7zXXJ9cCaNXpyNH5L3QtAmoxjoHW2w==

fs.realpath@^1.0.0:
version "1.0.0"
Expand Down

0 comments on commit 5e4acee

Please sign in to comment.