@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.
@@ -1,7 +1,24 @@
1
- import compilePackageCommandCreator from './compile-package.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('compilePackageCommandCreator', () => {
4
- it('has the correct command structure', () => {
5
- expect(compilePackageCommandCreator).toMatchSnapshot()
6
+ const { createCompilePackageCommand } = await import('./compile-package.js')
7
+
8
+ describe('createCompilePackageCommand', () => {
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 = createCompilePackageCommand(program)
21
+
22
+ expect(JSON.stringify(command, null, 2)).toMatchSnapshot()
6
23
  })
7
24
  })
@@ -10,7 +10,7 @@ import { paths } from '../configs/paths.js'
10
10
  import releaseConfig from '../configs/semantic-release/release.config.js'
11
11
  import { publishMonorepoPackages } from '../utils/publish-packages/publish-monorepo-packages.js'
12
12
 
13
- const publishPackageCommand = async ({
13
+ export const publishPackageCommand = async ({
14
14
  isAlpha,
15
15
  outputVersionFile,
16
16
  forceReleaseAsNonMonorepo,
@@ -18,9 +18,7 @@ const publishPackageCommand = async ({
18
18
  publishRootFolder,
19
19
  }) => {
20
20
  print.header('Publishing a package')
21
-
22
21
  const { lernaIsAvailable, workspacesExist } = utils.checkIfMonorepo()
23
-
24
22
  const monorepoIsMisconfigured = !lernaIsAvailable && workspacesExist
25
23
 
26
24
  if (!forceReleaseAsNonMonorepo && monorepoIsMisconfigured) {
@@ -29,7 +27,6 @@ const publishPackageCommand = async ({
29
27
  )
30
28
  process.exit(1)
31
29
  }
32
-
33
30
  const releasePackageAsMonorepo =
34
31
  !forceReleaseAsNonMonorepo && lernaIsAvailable && workspacesExist
35
32
 
@@ -49,7 +46,6 @@ const publishPackageCommand = async ({
49
46
  publishRootFolder,
50
47
  })
51
48
  }
52
-
53
49
  print.success('Done!')
54
50
  } catch (e) {
55
51
  print.red(
@@ -59,7 +55,6 @@ const publishPackageCommand = async ({
59
55
  process.exit(1)
60
56
  }
61
57
  }
62
-
63
58
  const publishPackage = async ({
64
59
  isAlpha,
65
60
  branch,
@@ -67,28 +62,26 @@ const publishPackage = async ({
67
62
  publishRootFolder = paths.appPackageBuild,
68
63
  }) => {
69
64
  validatePackageDistFolderExists()
70
-
71
65
  if (isAlpha) {
72
- await publishAlphaPackage({ branch, packageFolder: paths.appPackageBuild })
66
+ await publishAlphaPackage({
67
+ branch,
68
+ packageFolder: paths.appPackageBuild,
69
+ })
73
70
  } else {
74
71
  await publish({
75
72
  appFolder: paths.appBuild,
76
73
  packageFolder: publishRootFolder,
77
74
  })
78
75
  }
79
-
80
76
  if (outputVersionFile) {
81
77
  writeNewPackageVersionToFile(outputVersionFile)
82
78
  }
83
79
  }
84
-
85
80
  const publishAlphaPackage = async ({ branch, packageFolder }) => {
86
81
  print.yellow('Publishing alpha version of the package \n')
87
-
88
82
  const packageJson = JSON.parse(
89
83
  fs.readFileSync(path.join(packageFolder, './package.json'), 'utf8')
90
84
  )
91
-
92
85
  const preid = `alpha-${branch}`
93
86
  const newPreidVersion = await getNewPreidVersion({
94
87
  packageName: packageJson.name,
@@ -103,10 +96,8 @@ const publishAlphaPackage = async ({ branch, packageFolder }) => {
103
96
  cwd: packageFolder,
104
97
  }
105
98
  )
106
-
107
99
  cliShared.runSync('npm', ['publish', packageFolder, '--tag', preid])
108
100
  }
109
-
110
101
  const getLatestVersion = versions => {
111
102
  let latestVersion = versions[0]
112
103
 
@@ -117,26 +108,21 @@ const getLatestVersion = versions => {
117
108
  if (!semver.gt(version, latestVersion)) {
118
109
  return
119
110
  }
120
-
121
111
  latestVersion = version
122
112
  })
123
113
 
124
114
  return latestVersion
125
115
  }
126
-
127
116
  const getNewPreidVersion = async ({ packageName, preid }) => {
128
117
  try {
129
118
  const data = await packageJsonLib(packageName, {
130
119
  allVersions: true,
131
120
  })
132
-
133
121
  const versions = data.versions
134
122
  const versionsForPreid = Object.keys(versions).filter(
135
123
  version => version.indexOf(preid) >= 0
136
124
  )
137
-
138
125
  const latestVersion = getLatestVersion(versionsForPreid)
139
-
140
126
  const newVersion = semver.inc(
141
127
  latestVersion || data['dist-tags'].latest,
142
128
  'prerelease',
@@ -150,22 +136,22 @@ const getNewPreidVersion = async ({ packageName, preid }) => {
150
136
  process.exit(1)
151
137
  }
152
138
  }
153
-
154
139
  const publish = async ({ appFolder, packageFolder }) => {
155
140
  print.yellow('Publishing PRODUCTION version of the package\n')
156
-
157
- await semanticRelease(releaseConfig({ appFolder, packageFolder }))
141
+ await semanticRelease(
142
+ releaseConfig({
143
+ appFolder,
144
+ packageFolder,
145
+ })
146
+ )
158
147
  }
159
-
160
148
  const writeNewPackageVersionToFile = outputVersionFile => {
161
149
  const packageJsonPath = path.join(paths.appPackageBuild, './package.json')
162
150
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
163
-
164
151
  const newVersion = packageJson.version
165
152
 
166
153
  fs.writeFileSync(outputVersionFile, newVersion)
167
154
  }
168
-
169
155
  const validatePackageDistFolderExists = () => {
170
156
  const packageBuildFolder = paths.appPackageBuild
171
157
  const doesPackageBuildFolderExist = fs.existsSync(packageBuildFolder)
@@ -178,44 +164,39 @@ const validatePackageDistFolderExists = () => {
178
164
  }
179
165
  }
180
166
 
181
- const publishPackageCommandCreator = {
182
- action: options => {
183
- const { alpha, nonMonorepo, outputVersion, branch, publishRootFolder } =
184
- options
167
+ export const action = options => {
168
+ const { alpha, nonMonorepo, outputVersion, branch, publishRootFolder } =
169
+ options
185
170
 
186
- return publishPackageCommand({
187
- branch,
188
- isAlpha: alpha,
189
- forceReleaseAsNonMonorepo: nonMonorepo,
190
- outputVersionFile: outputVersion,
191
- publishRootFolder,
192
- })
193
- },
194
- command: 'publish-package',
195
- description: 'Publish package to npm',
196
- options: [
197
- {
198
- label: 'publish alpha version of the package',
199
- name: '--alpha',
200
- },
201
- {
202
- label: 'the file to output the new published version',
203
- name: '--outputVersion <outputVersion>',
204
- },
205
- {
206
- label:
207
- 'the branch name to be added to the release version metadata, only for alpha version',
208
- name: '--branch <branch>',
209
- },
210
- {
211
- label: 'explicitly process as non-monorepo (a single package)',
212
- name: '--nonMonorepo',
213
- },
214
- {
215
- label: 'custom publish root folder, (by default is /dist-package)',
216
- name: '--publishRootFolder <publishRootFolder>',
217
- },
218
- ],
171
+ return publishPackageCommand({
172
+ branch,
173
+ isAlpha: alpha,
174
+ forceReleaseAsNonMonorepo: nonMonorepo,
175
+ outputVersionFile: outputVersion,
176
+ publishRootFolder,
177
+ })
219
178
  }
220
179
 
221
- export default publishPackageCommandCreator
180
+ export const createPublishPackageCommand = program => {
181
+ return program
182
+ .createCommand('publish-package')
183
+ .description('Publish package to npm')
184
+ .action(action)
185
+ .option('--alpha', 'publish alpha version of the package')
186
+ .option(
187
+ '--outputVersion <outputVersion>',
188
+ 'the file to output the new published version'
189
+ )
190
+ .option(
191
+ '--branch <branch>',
192
+ 'the branch name to be added to the release version metadata, only for alpha version'
193
+ )
194
+ .option(
195
+ '--nonMonorepo',
196
+ 'explicitly process as non-monorepo (a single package)'
197
+ )
198
+ .option(
199
+ '--publishRootFolder <publishRootFolder>',
200
+ 'custom publish root folder, (by default is /dist-package)'
201
+ )
202
+ }
@@ -1,6 +1,8 @@
1
1
  import { utils } from '@toptal/davinci-monorepo'
2
2
  import { jest } from '@jest/globals'
3
3
  import { print } from '@toptal/davinci-cli-shared'
4
+ // eslint-disable-next-line import/no-extraneous-dependencies
5
+ import { Command } from 'commander'
4
6
 
5
7
  jest.unstable_mockModule(
6
8
  '../utils/publish-packages/publish-monorepo-packages.js',
@@ -17,18 +19,27 @@ const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => jest.fn())
17
19
  const { publishMonorepoPackages } = await import(
18
20
  '../utils/publish-packages/publish-monorepo-packages.js'
19
21
  )
20
- const publishPackage = (await import('./publish-package.js')).default
21
- const { action } = publishPackage
22
+ const { createPublishPackageCommand, action } = await import(
23
+ './publish-package.js'
24
+ )
22
25
 
23
26
  const checkIfMonorepo = jest.spyOn(utils, 'checkIfMonorepo')
24
27
 
25
- describe('publishPackageCommand', () => {
28
+ describe('createPublishPackageCommand', () => {
29
+ let program
30
+
31
+ beforeEach(() => {
32
+ program = new Command()
33
+ })
34
+
26
35
  afterEach(() => {
27
36
  jest.resetAllMocks()
28
37
  })
29
38
 
30
39
  it('has the correct command structure', () => {
31
- expect(publishPackage).toMatchSnapshot()
40
+ const command = createPublishPackageCommand(program)
41
+
42
+ expect(JSON.stringify(command, null, 2)).toMatchSnapshot()
32
43
  })
33
44
 
34
45
  describe('when `alpha` option is passed', () => {
@@ -1,30 +1,25 @@
1
1
  import cliShared, { print } from '@toptal/davinci-cli-shared'
2
-
3
2
  const getCompilePackageCommand = ({ entry, babelConfig, tsConfig }) => {
4
3
  const command = ['davinci-engine', 'compile-package', '--dotenv', 'auto']
5
4
 
6
5
  if (entry) {
7
6
  command.push('--entry', entry)
8
7
  }
9
-
10
8
  if (babelConfig) {
11
9
  command.push('--babelConfig', babelConfig)
12
10
  }
13
-
14
11
  if (tsConfig) {
15
12
  command.push('--tsConfig', tsConfig)
16
13
  }
17
14
 
18
15
  return command
19
16
  }
20
-
21
17
  const getPublishPackageCommand = ({ outputVersionFile, nonMonorepo }) => {
22
18
  const command = ['davinci-engine', 'publish-package']
23
19
 
24
20
  if (outputVersionFile) {
25
21
  command.push('--outputVersion', outputVersionFile)
26
22
  }
27
-
28
23
  if (nonMonorepo) {
29
24
  command.push('--nonMonorepo')
30
25
  }
@@ -32,7 +27,7 @@ const getPublishPackageCommand = ({ outputVersionFile, nonMonorepo }) => {
32
27
  return command
33
28
  }
34
29
 
35
- const releasePackageCommand = async ({
30
+ export const releasePackageCommand = async ({
36
31
  entry,
37
32
  babelConfig,
38
33
  tsConfig,
@@ -41,16 +36,21 @@ const releasePackageCommand = async ({
41
36
  }) => {
42
37
  try {
43
38
  print.header('Building package')
44
-
45
39
  cliShared.runSync(
46
40
  'yarn',
47
- getCompilePackageCommand({ babelConfig, entry, tsConfig })
41
+ getCompilePackageCommand({
42
+ babelConfig,
43
+ entry,
44
+ tsConfig,
45
+ })
48
46
  )
49
-
50
47
  print.green('Releasing package...\n')
51
48
  cliShared.runSync(
52
49
  'yarn',
53
- getPublishPackageCommand({ outputVersionFile, nonMonorepo })
50
+ getPublishPackageCommand({
51
+ outputVersionFile,
52
+ nonMonorepo,
53
+ })
54
54
  )
55
55
  } catch (e) {
56
56
  print.red(`${e.stack || e}\n`)
@@ -58,35 +58,29 @@ const releasePackageCommand = async ({
58
58
  }
59
59
  }
60
60
 
61
- const releasePackageCommandCreator = {
62
- action: releasePackageCommand,
63
- command: 'release-package',
64
- description: 'Build and release the package',
65
- options: [
66
- {
67
- label:
68
- 'path to a custom Babel config file; values: path to a Babel config file',
69
- name: '--babelConfig <babelConfig>',
70
- },
71
- {
72
- label:
73
- 'path to a custom tsconfig file; values: path to a custom tsconfig file',
74
- name: '--tsConfig <tsConfig>',
75
- },
76
- {
77
- label:
78
- 'path to a custom entry index file; values: path to a custom entry index file',
79
- name: '--entry <entry>',
80
- },
81
- {
82
- label: 'the file to output the new published version to',
83
- name: '--outputVersion <outputVersion>',
84
- },
85
- {
86
- label: 'explicitly process as non-monorepo (a single package)',
87
- name: '--nonMonorepo',
88
- },
89
- ],
61
+ export const createReleasePackageCommand = program => {
62
+ return program
63
+ .createCommand('release-package')
64
+ .description('Build and release the package')
65
+ .action(releasePackageCommand)
66
+ .option(
67
+ '--babelConfig <babelConfig>',
68
+ 'path to a custom Babel config file; values: path to a Babel config file'
69
+ )
70
+ .option(
71
+ '--tsConfig <tsConfig>',
72
+ 'path to a custom tsconfig file; values: path to a custom tsconfig file'
73
+ )
74
+ .option(
75
+ '--entry <entry>',
76
+ 'path to a custom entry index file; values: path to a custom entry index file'
77
+ )
78
+ .option(
79
+ '--outputVersion <outputVersion>',
80
+ 'the file to output the new published version to'
81
+ )
82
+ .option(
83
+ '--nonMonorepo',
84
+ 'explicitly process as non-monorepo (a single package)'
85
+ )
90
86
  }
91
-
92
- export default releasePackageCommandCreator
@@ -1,15 +1,31 @@
1
1
  import cliShared from '@toptal/davinci-cli-shared'
2
2
  import { jest } from '@jest/globals'
3
+ // eslint-disable-next-line import/no-extraneous-dependencies
4
+ import { Command } from 'commander'
3
5
 
4
- import releasePackage from './release-package.js'
6
+ const { createReleasePackageCommand, releasePackageCommand } = await import(
7
+ './release-package.js'
8
+ )
5
9
 
6
- const { action } = releasePackage
10
+ const action = releasePackageCommand
7
11
 
8
12
  const runSync = jest.spyOn(cliShared, 'runSync').mockImplementation(() => {})
9
13
 
10
- describe('releasePackageCommand', () => {
14
+ describe('createReleasePackageCommand', () => {
15
+ let program
16
+
17
+ beforeEach(() => {
18
+ program = new Command()
19
+ })
20
+
21
+ afterEach(() => {
22
+ jest.resetAllMocks()
23
+ })
24
+
11
25
  it('has the correct command structure', () => {
12
- expect(releasePackage).toMatchSnapshot()
26
+ const command = createReleasePackageCommand(program)
27
+
28
+ expect(JSON.stringify(command, null, 2)).toMatchSnapshot()
13
29
  })
14
30
 
15
31
  it('compiles and releases a package with default options', () => {
@@ -1,7 +1,6 @@
1
1
  import { print, runSync } from '@toptal/davinci-cli-shared'
2
2
 
3
3
  import { paths } from '../configs/paths.js'
4
-
5
4
  const getReleaseName = () => {
6
5
  const { stdout } = runSync(
7
6
  'yarn',
@@ -13,14 +12,11 @@ const getReleaseName = () => {
13
12
 
14
13
  return stdout
15
14
  }
16
-
17
15
  const uploadSourceMaps = ({ urlPrefix }) => {
18
16
  const releaseName = getReleaseName()
19
17
 
20
18
  print.yellow('Release name:', releaseName, '\n')
21
-
22
19
  runSync('yarn', ['sentry-cli', 'releases', 'new', releaseName])
23
-
24
20
  const args = [
25
21
  'sentry-cli',
26
22
  'releases',
@@ -33,12 +29,9 @@ const uploadSourceMaps = ({ urlPrefix }) => {
33
29
  if (urlPrefix) {
34
30
  args.push('--url-prefix', urlPrefix)
35
31
  }
36
-
37
32
  runSync('yarn', args)
38
-
39
33
  runSync('yarn', ['sentry-cli', 'releases', 'finalize', releaseName])
40
34
  }
41
-
42
35
  const failureExitIfNoEnvVariable = variableName => {
43
36
  if (!process.env[variableName]) {
44
37
  print.red(`${variableName} env variable is not defined.`)
@@ -48,34 +41,28 @@ const failureExitIfNoEnvVariable = variableName => {
48
41
  process.exit(1)
49
42
  }
50
43
  }
51
-
52
44
  const sentryUploadSourceMapsCommand = async ({ urlPrefix }) => {
53
45
  print.header('Uploading source maps to Sentry')
54
-
55
46
  failureExitIfNoEnvVariable('SENTRY_ORG')
56
47
  failureExitIfNoEnvVariable('SENTRY_PROJECT')
57
48
  failureExitIfNoEnvVariable('SENTRY_AUTH_TOKEN')
58
-
59
49
  try {
60
- await uploadSourceMaps({ urlPrefix })
61
-
50
+ await uploadSourceMaps({
51
+ urlPrefix,
52
+ })
62
53
  print.success('Done!')
63
54
  } catch (e) {
64
55
  print.red(`${e.stack || e}\n`)
65
56
  }
66
57
  }
67
58
 
68
- const sentryUploadSourceMapsCommandCreator = {
69
- action: sentryUploadSourceMapsCommand,
70
- command: 'sentry-upload-source-maps',
71
- description: 'Upload source maps to Sentry',
72
- options: [
73
- {
74
- label:
75
- "This sets URL prefix in front of all files. This defaults to the path where your production build is set e.g. '~/static/js' inside `dist` directory. This is also useful if your files are stored in a sub folder when deployed. eg: --url-prefix '~/blackfish/ui'.",
76
- name: '--url-prefix <prefix>',
77
- },
78
- ],
59
+ export const createSentryUploadSourceMapsCommand = program => {
60
+ return program
61
+ .createCommand('sentry-upload-source-maps')
62
+ .description('Upload source maps to Sentry')
63
+ .action(sentryUploadSourceMapsCommand)
64
+ .option(
65
+ '--url-prefix <prefix>',
66
+ "This sets URL prefix in front of all files. This defaults to the path where your production build is set e.g. '~/static/js' inside `dist` directory. This is also useful if your files are stored in a sub folder when deployed. eg: --url-prefix '~/blackfish/ui'."
67
+ )
79
68
  }
80
-
81
- export default sentryUploadSourceMapsCommandCreator
@@ -1,7 +1,26 @@
1
- import sentryUploadSourceMapsCommandCreator from './sentry-upload-sourcemaps.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('sentryUploadSourceMapsCommandCreator', () => {
4
- it('has the correct command structure', () => {
5
- expect(sentryUploadSourceMapsCommandCreator).toMatchSnapshot()
6
+ const { createSentryUploadSourceMapsCommand } = await import(
7
+ './sentry-upload-sourcemaps.js'
8
+ )
9
+
10
+ describe('createSentryUploadSourceMapsCommand', () => {
11
+ let program
12
+
13
+ beforeEach(() => {
14
+ program = new Command()
15
+ })
16
+
17
+ afterEach(() => {
18
+ jest.clearAllMocks()
19
+ })
20
+
21
+ it('creates a command with the correct parameters', () => {
22
+ const command = createSentryUploadSourceMapsCommand(program)
23
+
24
+ expect(JSON.stringify(command, null, 2)).toMatchSnapshot()
6
25
  })
7
26
  })