Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yandeu committed May 21, 2023
1 parent 9ec0ac0 commit 038dddd
Show file tree
Hide file tree
Showing 24 changed files with 204 additions and 192 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/client/**.js
/lib
/node_modules
/tmp
/tmp
/tmp*
3 changes: 3 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
3 changes: 2 additions & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
coverageReporters: ['html', 'lcov', 'text'],
maxConcurrency: 1,
maxWorkers: 1,
testTimeout: 30_000
testTimeout: 30_000,
testMatch: ['**/?(*.)+(spec|test).?(m)[jt]s?(x)']
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Development Server with Live Reload Capability. (Maintained Fork of Live Server)",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"type": "module",
"type": "commonjs",
"bin": {
"live-server": "./lib/bin.js",
"five-server": "./lib/bin.js"
Expand Down Expand Up @@ -39,7 +39,7 @@
"lint": "eslint src --ext .ts,.tsx",
"lint:fix": "eslint --fix src --ext .ts,.tsx",
"extract": "extract version name src/const.ts",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --forceExit --silent",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --runInBand --detectOpenHandles --silent --no-cache acc",
"test:full": "npm i && npm run clean && npm run build && npm run format:write && npm run lint:fix && npm run test",
"publish": "publish",
"push": "npm run publish",
Expand Down Expand Up @@ -95,4 +95,4 @@
"funding": {
"url": "https://github.com/sponsors/yandeu"
}
}
}
320 changes: 162 additions & 158 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,176 +24,180 @@ console.clear()
const liveServer = new LiveServer()
let isTesting = false

const opts: any = await getConfigFile()
opts._cli = true
const main = async () => {
const opts: any = await getConfigFile()
opts._cli = true

for (let i = process.argv.length - 1; i >= 2; --i) {
const arg = process.argv[i]
if (arg.indexOf('--root=') > -1) {
const root = arg.substring(7)
opts.root = root
process.argv.splice(i, 1)
} else if (arg.indexOf('--useLocalIp') > -1) {
opts.useLocalIp = true
process.argv.splice(i, 1)
} else if (arg.indexOf('--php=') > -1) {
const php = arg.substring(6)
opts.php = php
process.argv.splice(i, 1)
} else if (arg.indexOf('--phpIni=') > -1) {
const phpIni = arg.substring(9)
opts.phpIni = phpIni
process.argv.splice(i, 1)
} else if (arg.indexOf('--remoteLogs=') > -1) {
const log = arg.substring(13)
if (log === 'true') opts.remoteLogs = true
else if (log === 'false') opts.remoteLogs = false
else opts.remoteLogs = log
process.argv.splice(i, 1)
} else if (arg.indexOf('--port=') > -1) {
const portString = arg.substring(7)
const portNumber = parseInt(portString, 10)
if (portNumber === +portString) {
opts.port = portNumber
for (let i = process.argv.length - 1; i >= 2; --i) {
const arg = process.argv[i]
if (arg.indexOf('--root=') > -1) {
const root = arg.substring(7)
opts.root = root
process.argv.splice(i, 1)
}
} else if (arg.indexOf('--host=') > -1) {
opts.host = arg.substring(7)
process.argv.splice(i, 1)
} else if (arg.indexOf('--open=') > -1) {
const open = arg.substring(7)
} else if (arg.indexOf('--useLocalIp') > -1) {
opts.useLocalIp = true
process.argv.splice(i, 1)
} else if (arg.indexOf('--php=') > -1) {
const php = arg.substring(6)
opts.php = php
process.argv.splice(i, 1)
} else if (arg.indexOf('--phpIni=') > -1) {
const phpIni = arg.substring(9)
opts.phpIni = phpIni
process.argv.splice(i, 1)
} else if (arg.indexOf('--remoteLogs=') > -1) {
const log = arg.substring(13)
if (log === 'true') opts.remoteLogs = true
else if (log === 'false') opts.remoteLogs = false
else opts.remoteLogs = log
process.argv.splice(i, 1)
} else if (arg.indexOf('--port=') > -1) {
const portString = arg.substring(7)
const portNumber = parseInt(portString, 10)
if (portNumber === +portString) {
opts.port = portNumber
process.argv.splice(i, 1)
}
} else if (arg.indexOf('--host=') > -1) {
opts.host = arg.substring(7)
process.argv.splice(i, 1)
} else if (arg.indexOf('--open=') > -1) {
const open = arg.substring(7)

if (open === 'true') opts.open = true
else if (open === 'false') opts.open = false
else if (/,/.test(open)) opts.open = open.split(',').map(opn => (opn === '/' ? '' : opn))
else opts.open = open
if (open === 'true') opts.open = true
else if (open === 'false') opts.open = false
else if (/,/.test(open)) opts.open = open.split(',').map(opn => (opn === '/' ? '' : opn))
else opts.open = open

process.argv.splice(i, 1)
} else if (arg.indexOf('--watch=') > -1) {
// Will be modified later when cwd is known
opts.watch = arg.substring(8).split(',')
process.argv.splice(i, 1)
} else if (arg.indexOf('--ignore=') > -1) {
// Will be modified later when cwd is known
opts.ignore = arg.substring(9).split(',')
process.argv.splice(i, 1)
} else if (arg.indexOf('--ignorePattern=') > -1) {
opts.ignorePattern = new RegExp(arg.substring(16))
process.argv.splice(i, 1)
} else if (arg === '--no-css-inject') {
opts.injectCss = false
process.argv.splice(i, 1)
} else if (arg === '--no-browser') {
opts.open = false
process.argv.splice(i, 1)
} else if (arg.indexOf('--browser=') > -1) {
opts.browser = arg.substring(10).split(',')
process.argv.splice(i, 1)
} else if (arg.indexOf('--entry-file=') > -1) {
const file = arg.substring(13)
if (file.length) {
opts.file = file
process.argv.splice(i, 1)
}
} else if (arg === '--spa') {
opts.middleware.push('spa')
process.argv.splice(i, 1)
} else if (arg === '--quiet' || arg === '-q') {
opts.logLevel = 0
process.argv.splice(i, 1)
} else if (arg === '--verbose' || arg === '-V') {
opts.logLevel = 3
process.argv.splice(i, 1)
} else if (arg.indexOf('--mount=') > -1) {
// e.g. "--mount=/components:./node_modules" will be ['/components', '<process.cwd()>/node_modules']
// split only on the first ":", as the path may contain ":" as well (e.g. C:\file.txt)
const match = arg.substring(8).match(/([^:]+):(.+)$/)
if (!match) message.error('Option --mount is wrong', null, false)
else {
match[2] = path.resolve(process.cwd(), match[2])
opts.mount.push([match[1], match[2]])
} else if (arg.indexOf('--watch=') > -1) {
// Will be modified later when cwd is known
opts.watch = arg.substring(8).split(',')
process.argv.splice(i, 1)
}
} else if (arg.indexOf('--wait=') > -1) {
const waitString = arg.substring(7)
const waitNumber = parseInt(waitString, 10)
if (waitNumber === +waitString) {
opts.wait = waitNumber
} else if (arg.indexOf('--ignore=') > -1) {
// Will be modified later when cwd is known
opts.ignore = arg.substring(9).split(',')
process.argv.splice(i, 1)
}
} else if (arg === '--version' || arg === '-v') {
message.log(NAME, VERSION)
process.exit()
} else if (arg.indexOf('--htpasswd=') > -1) {
opts.htpasswd = arg.substring(11)
process.argv.splice(i, 1)
} else if (arg === '--cors') {
opts.cors = true
process.argv.splice(i, 1)
} else if (arg === '--no-cors') {
opts.cors = false
process.argv.splice(i, 1)
} else if (arg.indexOf('--https=') > -1) {
const https = arg.substring(8)
if (https === 'true') opts.https = true
else opts.https = https
process.argv.splice(i, 1)
} else if (arg.indexOf('--https-module=') > -1) {
opts.httpsModule = arg.substring(15)
process.argv.splice(i, 1)
} else if (arg.indexOf('--proxy=') > -1) {
// split only on the first ":", as the URL will contain ":" as well
const match = arg.substring(8).match(/([^:]+):(.+)$/)
if (!match) message.error('Option --proxy is wrong', null, false)
else {
opts.proxy.push([match[1], match[2]])
} else if (arg.indexOf('--ignorePattern=') > -1) {
opts.ignorePattern = new RegExp(arg.substring(16))
process.argv.splice(i, 1)
} else if (arg === '--no-css-inject') {
opts.injectCss = false
process.argv.splice(i, 1)
} else if (arg === '--no-browser') {
opts.open = false
process.argv.splice(i, 1)
} else if (arg.indexOf('--browser=') > -1) {
opts.browser = arg.substring(10).split(',')
process.argv.splice(i, 1)
} else if (arg.indexOf('--entry-file=') > -1) {
const file = arg.substring(13)
if (file.length) {
opts.file = file
process.argv.splice(i, 1)
}
} else if (arg === '--spa') {
opts.middleware.push('spa')
process.argv.splice(i, 1)
} else if (arg === '--quiet' || arg === '-q') {
opts.logLevel = 0
process.argv.splice(i, 1)
} else if (arg === '--verbose' || arg === '-V') {
opts.logLevel = 3
process.argv.splice(i, 1)
} else if (arg.indexOf('--mount=') > -1) {
// e.g. "--mount=/components:./node_modules" will be ['/components', '<process.cwd()>/node_modules']
// split only on the first ":", as the path may contain ":" as well (e.g. C:\file.txt)
const match = arg.substring(8).match(/([^:]+):(.+)$/)
if (!match) message.error('Option --mount is wrong', null, false)
else {
match[2] = path.resolve(process.cwd(), match[2])
opts.mount.push([match[1], match[2]])
process.argv.splice(i, 1)
}
} else if (arg.indexOf('--wait=') > -1) {
const waitString = arg.substring(7)
const waitNumber = parseInt(waitString, 10)
if (waitNumber === +waitString) {
opts.wait = waitNumber
process.argv.splice(i, 1)
}
} else if (arg === '--version' || arg === '-v') {
message.log(NAME, VERSION)
process.exit()
} else if (arg.indexOf('--htpasswd=') > -1) {
opts.htpasswd = arg.substring(11)
process.argv.splice(i, 1)
} else if (arg === '--cors') {
opts.cors = true
process.argv.splice(i, 1)
} else if (arg === '--no-cors') {
opts.cors = false
process.argv.splice(i, 1)
} else if (arg.indexOf('--https=') > -1) {
const https = arg.substring(8)
if (https === 'true') opts.https = true
else opts.https = https
process.argv.splice(i, 1)
} else if (arg.indexOf('--https-module=') > -1) {
opts.httpsModule = arg.substring(15)
process.argv.splice(i, 1)
} else if (arg.indexOf('--proxy=') > -1) {
// split only on the first ":", as the URL will contain ":" as well
const match = arg.substring(8).match(/([^:]+):(.+)$/)
if (!match) message.error('Option --proxy is wrong', null, false)
else {
opts.proxy.push([match[1], match[2]])
process.argv.splice(i, 1)
}
} else if (arg.indexOf('--middleware=') > -1) {
opts.middleware.push(arg.substring(13))
process.argv.splice(i, 1)
} else if (arg === '--help' || arg === '-h') {
const cli_options = `Usage: five-server [OPTIONS]
Options:
-h, --help Print help information
-v, --version Print version information
More:
https://github.com/yandeu/five-server/blob/main/src/bin.ts
https://github.com/yandeu/five-server/blob/main/src/types.ts
`
console.log(cli_options)
process.exit()
} else if (arg === '--test') {
// Hidden param for tests (with jest) to exit automatically
isTesting = true
process.argv.splice(i, 1)
}
} else if (arg.indexOf('--middleware=') > -1) {
opts.middleware.push(arg.substring(13))
process.argv.splice(i, 1)
} else if (arg === '--help' || arg === '-h') {
const cli_options = `Usage: five-server [OPTIONS]
Options:
-h, --help Print help information
-v, --version Print version information
More:
https://github.com/yandeu/five-server/blob/main/src/bin.ts
https://github.com/yandeu/five-server/blob/main/src/types.ts
`
console.log(cli_options)
process.exit()
} else if (arg === '--test') {
// Hidden param for tests (with jest) to exit automatically
isTesting = true
process.argv.splice(i, 1)
}
}

// Patch paths
opts.root = process.argv[2] ? removeLeadingSlash(process.argv[2]) : opts.root
// Patch paths
opts.root = process.argv[2] ? removeLeadingSlash(process.argv[2]) : opts.root

if (opts.watch) {
if (opts.length === 1 && opts.watch[0] === 'true') opts.watch = true
else if (opts.length === 1 && opts.watch[0] === 'false') opts.watch = false
}
if (opts.ignore) {
if (typeof opts.ignore === 'string') opts.ignore = [opts.ignore]
}
if (opts.watch) {
if (opts.length === 1 && opts.watch[0] === 'true') opts.watch = true
else if (opts.length === 1 && opts.watch[0] === 'false') opts.watch = false
}
if (opts.ignore) {
if (typeof opts.ignore === 'string') opts.ignore = [opts.ignore]
}

if (!isTesting) liveServer.start(opts)
if (!isTesting) liveServer.start(opts)

// when testing with jest
if (isTesting) {
liveServer.start(opts).then(() => {
setTimeout(() => {
liveServer.shutdown().then(() => {
setTimeout(() => {
process.exit(0)
}, 500)
})
}, 500)
})
// when testing with jest
if (isTesting) {
liveServer.start(opts).then(() => {
setTimeout(() => {
liveServer.shutdown().then(() => {
setTimeout(() => {
process.exit(0)
}, 500)
})
}, 500)
})
}
}

main()
2 changes: 1 addition & 1 deletion src/dirname.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname } from 'path'
import { fileURLToPath } from 'url'

export const __dirname = dirname(fileURLToPath(import.meta.url))
// export const __dirname = dirname(fileURLToPath(import.meta.url))
Loading

0 comments on commit 038dddd

Please sign in to comment.