@toptal/davinci-engine 10.5.5 → 10.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,10 +7,8 @@ import globby from 'globby'
7
7
  import bytes from 'bytes'
8
8
 
9
9
  import { paths } from '../configs/paths.js'
10
-
11
10
  const successExitCode = 0
12
11
  const errorExitCode = 1
13
-
14
12
  const calcPercentageChange = (limit, size) => {
15
13
  if (size === 0) {
16
14
  return limit * 100
@@ -21,16 +19,13 @@ const calcPercentageChange = (limit, size) => {
21
19
 
22
20
  return ((size - limit) / limit) * 100
23
21
  }
24
-
25
22
  const formatChange = limit => Math.abs(Math.floor(limit))
26
-
27
23
  const checkBundleSize = async (files, limitInBytes) => {
28
24
  const result = await sizeLimit([filePlugin], files)
29
25
 
30
26
  if (!result) {
31
27
  panic('Unable to check bundle size')
32
28
  }
33
-
34
29
  const size = result[0].size
35
30
 
36
31
  if (size > limitInBytes) {
@@ -39,7 +34,6 @@ const checkBundleSize = async (files, limitInBytes) => {
39
34
  print.success(generateMessage(size, limitInBytes))
40
35
  }
41
36
  }
42
-
43
37
  const checkSizeOfEachChunk = async (files, maxChunksLimitInBytes) => {
44
38
  const chunksExceedingLimit = []
45
39
 
@@ -53,7 +47,6 @@ const checkSizeOfEachChunk = async (files, maxChunksLimitInBytes) => {
53
47
  })
54
48
  }
55
49
  }
56
-
57
50
  if (chunksExceedingLimit.length > 0) {
58
51
  chunksExceedingLimit.forEach(chunk => {
59
52
  print.red(
@@ -64,14 +57,12 @@ const checkSizeOfEachChunk = async (files, maxChunksLimitInBytes) => {
64
57
  })
65
58
  process.exit(errorExitCode)
66
59
  }
67
-
68
60
  print.success(
69
61
  `All chunks are ok. Each of them is smaller than ${bytes(
70
62
  maxChunksLimitInBytes
71
63
  )} limit.`
72
64
  )
73
65
  }
74
-
75
66
  const generateMessage = (size, limit, isLarger) => {
76
67
  const change = formatChange(calcPercentageChange(limit, size))
77
68
 
@@ -81,15 +72,12 @@ const generateMessage = (size, limit, isLarger) => {
81
72
  limit
82
73
  )} limit.\nTo perform a more in-depth analysis of your bundle run "davinci-engine analyze"`
83
74
  }
84
-
85
75
  const panic = message => {
86
76
  print.red(message)
87
77
  process.exit(errorExitCode)
88
78
  }
89
-
90
79
  const sizeCommand = async ({ limit, maxChunkSize }) => {
91
80
  print.header('Checking bundle size')
92
-
93
81
  const limitInBytes = typeof limit === 'number' ? limit : bytes(limit)
94
82
  const maxChunksLimitInBytes =
95
83
  typeof maxChunkSize === 'number' ? limit : bytes(maxChunkSize)
@@ -99,44 +87,38 @@ const sizeCommand = async ({ limit, maxChunkSize }) => {
99
87
  `Build directory ${paths.appBuild} appears to be empty. Please build your app before running this command.`
100
88
  )
101
89
  }
102
-
103
90
  const files = await globby(path.join(paths.appBuild, '**/*.js'))
104
91
 
105
92
  if (!limit && !maxChunkSize) {
106
93
  print.grey(`No limit provided. Skipping size check.\n`)
107
94
  process.exit(successExitCode)
108
95
  }
109
-
110
96
  try {
111
97
  if (limit) {
112
98
  await checkBundleSize(files, limitInBytes)
113
99
  }
114
-
115
100
  if (maxChunkSize) {
116
101
  await checkSizeOfEachChunk(files, maxChunksLimitInBytes)
117
102
  }
118
-
119
103
  process.exit(successExitCode)
120
104
  } catch (e) {
121
105
  panic(`${e.stack || e}\n`)
122
106
  }
123
107
  }
124
108
 
125
- const sizeCommandCreator = {
126
- action: sizeCommand,
127
- command: 'size',
128
- description:
129
- 'Checks the bundle size against provided limit — fails if limit is exceeded.',
130
- options: [
131
- {
132
- label: 'limit to check the size against: 2.5Mb, 1337Kb, etc',
133
- name: '--limit <limit>',
134
- },
135
- {
136
- label: 'max allowed chunk size: 100Kb, 1Mb, etc',
137
- name: '--maxChunkSize <maxChunkSize>',
138
- },
139
- ],
109
+ export const createSizeCommand = program => {
110
+ return program
111
+ .createCommand('size')
112
+ .description(
113
+ 'Checks the bundle size against provided limit — fails if limit is exceeded.'
114
+ )
115
+ .action(sizeCommand)
116
+ .option(
117
+ '--limit <limit>',
118
+ 'limit to check the size against: 2.5Mb, 1337Kb, etc'
119
+ )
120
+ .option(
121
+ '--maxChunkSize <maxChunkSize>',
122
+ 'max allowed chunk size: 100Kb, 1Mb, etc'
123
+ )
140
124
  }
141
-
142
- export default sizeCommandCreator
@@ -1,7 +1,24 @@
1
- import sizeCommandCreator from './size.js'
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
+ import { jest } from '@jest/globals'
3
+ // eslint-disable-next-line import/no-extraneous-dependencies
4
+ import { Command } from 'commander'
2
5
 
3
- describe('sizeCommandCreator', () => {
4
- it('has the correct command structure', () => {
5
- expect(sizeCommandCreator).toMatchSnapshot()
6
+ const { createSizeCommand } = await import('./size.js')
7
+
8
+ describe('createSizeCommand', () => {
9
+ let program
10
+
11
+ beforeEach(() => {
12
+ program = new Command()
13
+ })
14
+
15
+ afterEach(() => {
16
+ jest.clearAllMocks()
17
+ })
18
+
19
+ it('creates a command with the correct parameters', () => {
20
+ const command = createSizeCommand(program)
21
+
22
+ expect(JSON.stringify(command, null, 2)).toMatchSnapshot()
6
23
  })
7
24
  })
package/src/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import { createStartCommand } from './commands/start.js'
2
- import buildCommandCreator from './commands/build.js'
3
- import compilePackageCommandCreator from './commands/compile-package.js'
4
- import publishPackageCommandCreator from './commands/publish-package.js'
5
- import releasePackageCommandCreator from './commands/release-package.js'
6
- import analyzeCommandCreator from './commands/analyze.js'
7
- import sizeCommandCreator from './commands/size.js'
8
- import sentryUploadSourceMaps from './commands/sentry-upload-sourcemaps.js'
2
+ import { createBuildCommand } from './commands/build.js'
3
+ import { createCompilePackageCommand } from './commands/compile-package.js'
4
+ import { createPublishPackageCommand } from './commands/publish-package.js'
5
+ import { createReleasePackageCommand } from './commands/release-package.js'
6
+ import { createAnalyzeCommand } from './commands/analyze.js'
7
+ import { createSizeCommand } from './commands/size.js'
8
+ import { createSentryUploadSourceMapsCommand } from './commands/sentry-upload-sourcemaps.js'
9
9
  import createWebpackConfig from './configs/webpack-config/index.js'
10
10
  import { getWorkspaceRoot, getEnvVariables } from './utils/index.js'
11
11
  import jscOptions from './configs/swc/.swcrc.js'
@@ -20,13 +20,13 @@ export { default as createWebpackConfig } from './configs/webpack-config/index.j
20
20
 
21
21
  export const commands = [
22
22
  createStartCommand,
23
- buildCommandCreator,
24
- compilePackageCommandCreator,
25
- publishPackageCommandCreator,
26
- releasePackageCommandCreator,
27
- analyzeCommandCreator,
28
- sizeCommandCreator,
29
- sentryUploadSourceMaps,
23
+ createBuildCommand,
24
+ createCompilePackageCommand,
25
+ createPublishPackageCommand,
26
+ createReleasePackageCommand,
27
+ createAnalyzeCommand,
28
+ createSizeCommand,
29
+ createSentryUploadSourceMapsCommand,
30
30
  ]
31
31
 
32
32
  export default {