Skip to content

Commit 572097e

Browse files
authored
refactor: use desm (#1641)
1 parent f9f1f79 commit 572097e

82 files changed

Lines changed: 252 additions & 472 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package-lock.json

Lines changed: 32 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"array-unflat-js": "^0.1.3",
8787
"boxen": "^7.0.1",
8888
"chalk": "^5.2.0",
89+
"desm": "^1.3.0",
8990
"execa": "^6.1.0",
9091
"fs-extra": "^11.1.0",
9192
"is-wsl": "^2.2.0",
@@ -109,12 +110,12 @@
109110
"archiver": "^5.3.1",
110111
"eslint": "^8.31.0",
111112
"eslint-config-airbnb-base": "^15.0.0",
112-
"eslint-config-prettier": "^8.5.0",
113+
"eslint-config-prettier": "^8.6.0",
113114
"eslint-plugin-import": "^2.25.4",
114115
"eslint-plugin-prettier": "^4.2.1",
115116
"eslint-plugin-unicorn": "^45.0.2",
116117
"git-list-updated": "^1.2.1",
117-
"husky": "^8.0.2",
118+
"husky": "^8.0.3",
118119
"lint-staged": "^13.1.0",
119120
"mocha": "^10.2.0",
120121
"nyc": "^15.1.0",

src/events/http/Endpoint.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import { existsSync, readFileSync } from 'node:fs'
2-
import { dirname, resolve } from 'node:path'
3-
import { fileURLToPath } from 'node:url'
42
import { log } from '@serverless/utils/log.js'
3+
import { join } from 'desm'
54
import OfflineEndpoint from './OfflineEndpoint.js'
65

76
const { entries } = Object
87

9-
const __dirname = dirname(fileURLToPath(import.meta.url))
10-
118
// velocity template defaults
129
const defaultRequestTemplate = readFileSync(
13-
resolve(__dirname, 'templates/offline-default.req.vm'),
10+
join(import.meta.url, 'templates/offline-default.req.vm'),
1411
'utf8',
1512
)
1613
const defaultResponseTemplate = readFileSync(
17-
resolve(__dirname, 'templates/offline-default.res.vm'),
14+
join(import.meta.url, 'templates/offline-default.res.vm'),
1815
'utf8',
1916
)
2017

src/lambda/__tests__/LambdaFunction.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import assert from 'node:assert'
2-
import { dirname, resolve } from 'node:path'
32
// import { performance } from 'node:perf_hooks'
4-
import { fileURLToPath } from 'node:url'
3+
import { join } from 'desm'
54
import LambdaFunction from '../LambdaFunction.js'
65
import { DEFAULT_LAMBDA_TIMEOUT } from '../../config/index.js'
76

8-
const __dirname = dirname(fileURLToPath(import.meta.url))
9-
107
describe('LambdaFunction', () => {
118
const functionName = 'foo'
129

1310
const serverless = {
1411
config: {
1512
serverlessPath: '',
16-
servicePath: resolve(__dirname),
13+
servicePath: join(import.meta.url),
1714
},
1815
service: {
1916
provider: {

src/lambda/handler-runner/python-runner/PythonRunner.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { spawn } from 'node:child_process'
22
import { EOL, platform } from 'node:os'
3-
import { delimiter, dirname, join, relative, resolve } from 'node:path'
3+
import { delimiter, join as pathJoin, relative } from 'node:path'
44
import process, { cwd, nextTick } from 'node:process'
55
import { createInterface } from 'node:readline'
6-
import { fileURLToPath } from 'node:url'
76
import { log } from '@serverless/utils/log.js'
7+
import { join } from 'desm'
88
import { splitHandlerPathAndName } from '../../../utils/index.js'
99

1010
const { parse, stringify } = JSON
1111
const { assign, hasOwn } = Object
1212

13-
const __dirname = dirname(fileURLToPath(import.meta.url))
14-
1513
export default class PythonRunner {
1614
static #payloadIdentifier = '__offline_payload__'
1715

@@ -32,7 +30,7 @@ export default class PythonRunner {
3230
const runtimeDir = platform() === 'win32' ? 'Scripts' : 'bin'
3331

3432
process.env.PATH = [
35-
join(process.env.VIRTUAL_ENV, runtimeDir),
33+
pathJoin(process.env.VIRTUAL_ENV, runtimeDir),
3634
delimiter,
3735
process.env.PATH,
3836
].join('')
@@ -44,7 +42,7 @@ export default class PythonRunner {
4442
pythonExecutable,
4543
[
4644
'-u',
47-
resolve(__dirname, 'invoke.py'),
45+
join(import.meta.url, 'invoke.py'),
4846
relative(cwd(), handlerPath),
4947
handlerName,
5048
],

src/lambda/handler-runner/ruby-runner/RubyRunner.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { EOL, platform } from 'node:os'
2-
import { dirname, relative, resolve } from 'node:path'
2+
import { relative } from 'node:path'
33
import { cwd } from 'node:process'
4-
import { fileURLToPath } from 'node:url'
54
import { log } from '@serverless/utils/log.js'
5+
import { join } from 'desm'
66
import { execa } from 'execa'
77
import { splitHandlerPathAndName } from '../../../utils/index.js'
88

99
const { parse, stringify } = JSON
1010
const { hasOwn } = Object
1111

12-
const __dirname = dirname(fileURLToPath(import.meta.url))
13-
1412
export default class RubyRunner {
1513
static #payloadIdentifier = '__offline_payload__'
1614

@@ -86,7 +84,7 @@ export default class RubyRunner {
8684
const { stderr, stdout } = await execa(
8785
runtime,
8886
[
89-
resolve(__dirname, 'invoke.rb'),
87+
join(import.meta.url, 'invoke.rb'),
9088
relative(cwd(), this.#handlerPath),
9189
this.#handlerName,
9290
],

src/lambda/handler-runner/worker-thread-runner/WorkerThreadRunner.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import { dirname, resolve } from 'node:path'
2-
import { fileURLToPath } from 'node:url'
31
import { MessageChannel, Worker } from 'node:worker_threads'
4-
5-
const __dirname = dirname(fileURLToPath(import.meta.url))
6-
const workerThreadHelperPath = resolve(__dirname, 'workerThreadHelper.js')
2+
import { join } from 'desm'
73

84
export default class WorkerThreadRunner {
95
#workerThread = null
106

117
constructor(funOptions, env) {
128
const { codeDir, functionKey, handler, servicePath, timeout } = funOptions
139

14-
this.#workerThread = new Worker(workerThreadHelperPath, {
15-
// don't pass process.env from the main process!
16-
env,
17-
workerData: {
18-
codeDir,
19-
functionKey,
20-
handler,
21-
servicePath,
22-
timeout,
10+
this.#workerThread = new Worker(
11+
join(import.meta.url, 'workerThreadHelper.js'),
12+
{
13+
// don't pass process.env from the main process!
14+
env,
15+
workerData: {
16+
codeDir,
17+
functionKey,
18+
handler,
19+
servicePath,
20+
timeout,
21+
},
2322
},
24-
})
23+
)
2524
}
2625

2726
// () => Promise<number>

tests/_testHelpers/setupTeardown.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { dirname, resolve } from 'node:path'
21
import process, { env } from 'node:process'
3-
import { fileURLToPath } from 'node:url'
2+
import { join } from 'desm'
43
import { execaNode } from 'execa'
54

65
let serverlessProcess
76

8-
const __dirname = dirname(fileURLToPath(import.meta.url))
9-
10-
const serverlessPath = resolve(
11-
__dirname,
7+
const serverlessPath = join(
8+
import.meta.url,
129
'../../node_modules/serverless/bin/serverless',
1310
)
1411

tests/end-to-end/environmentVariables/environmentVariables.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import assert from 'node:assert'
2-
import { dirname, resolve } from 'node:path'
32
import { env } from 'node:process'
4-
import { fileURLToPath } from 'node:url'
3+
import { join } from 'desm'
54
import { BASE_URL } from '../../config.js'
65
import { setup, teardown } from '../../_testHelpers/index.js'
76

8-
const __dirname = dirname(fileURLToPath(import.meta.url))
9-
107
describe('environment variables', function desc() {
118
const ENV_VAR_QUOTED = 'I am ENV_VAR_1'
129
const ENV_VAR_UNQUOTED = 'I am ENV_VAR_2'
@@ -20,7 +17,7 @@ describe('environment variables', function desc() {
2017
env.ENV_VAR_MAPPED_FROM_ANOTHER = ENV_VAR_MAPPED
2118

2219
await setup({
23-
servicePath: resolve(__dirname, 'src'),
20+
servicePath: join(import.meta.url, 'src'),
2421
})
2522

2623
const url = new URL('/dev/hello', BASE_URL)

tests/end-to-end/optionParameters/optionParameters.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import assert from 'node:assert'
2-
import { dirname, resolve } from 'node:path'
3-
import { fileURLToPath } from 'node:url'
2+
import { join } from 'desm'
43
import { BASE_URL } from '../../config.js'
54
import { setup, teardown } from '../../_testHelpers/index.js'
65

7-
const __dirname = dirname(fileURLToPath(import.meta.url))
8-
96
describe('noPrependStageInUrl option', function desc() {
107
beforeEach(() =>
118
setup({
129
args: ['--noPrependStageInUrl'],
13-
servicePath: resolve(__dirname, 'src'),
10+
servicePath: join(import.meta.url, 'src'),
1411
}),
1512
)
1613

@@ -61,7 +58,7 @@ describe('prefix option', function desc() {
6158
beforeEach(() =>
6259
setup({
6360
args: ['--prefix', 'someprefix'],
64-
servicePath: resolve(__dirname, 'src'),
61+
servicePath: join(import.meta.url, 'src'),
6562
}),
6663
)
6764

@@ -114,7 +111,7 @@ describe('noPrependStageInUrl option with prefix option', function desc() {
114111
beforeEach(() =>
115112
setup({
116113
args: ['--noPrependStageInUrl', '--prefix', 'someprefix'],
117-
servicePath: resolve(__dirname, 'src'),
114+
servicePath: join(import.meta.url, 'src'),
118115
}),
119116
)
120117

0 commit comments

Comments
 (0)