@raisenow/tamaro-cli 1.0.8 → 1.0.9

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.
@@ -0,0 +1,102 @@
1
+ import {
2
+ fail,
3
+ getIfCoreFns,
4
+ getWidgetUuid,
5
+ logCommand,
6
+ logTable,
7
+ logTitle,
8
+ } from 'lib/helpers'
9
+ import {assertProfileValid, AwsOptions, runAwsCommandSync} from 'lib/aws'
10
+ import {AWS_S3_BUCKET, CORE_CONFIG_NAME} from 'commands/deploy'
11
+
12
+ ///////////////////////////////////////////////////////////////////////////////
13
+
14
+ export type ListDeployedOptions = AwsOptions & {
15
+ config?: string
16
+ }
17
+
18
+ ///////////////////////////////////////////////////////////////////////////////
19
+
20
+ export const listDeployed = async (
21
+ options: ListDeployedOptions,
22
+ ): Promise<void> => {
23
+ assertOptionsValid(options)
24
+
25
+ const {config} = options
26
+ const {ifCore} = getIfCoreFns()
27
+ const configName = config ?? ifCore(CORE_CONFIG_NAME, getWidgetUuid())
28
+ const title =
29
+ !config && ifCore()
30
+ ? `Listing deployments of Tamaro Core …`
31
+ : `Listing deployments of “${configName}” customer configuration …`
32
+
33
+ const deployUrl = `s3://${AWS_S3_BUCKET}/${configName}/`
34
+ const cmd = `aws s3 ls ${deployUrl}`
35
+ let out = ''
36
+
37
+ logTitle(title)
38
+ logCommand(cmd)
39
+
40
+ // If customer configuration folder does not exist on AWS S3,
41
+ // command fails with blank stderr, so we need to handle this case.
42
+ try {
43
+ const result = runAwsCommandSync(cmd)
44
+ out = result.stdout
45
+ } catch (error: any) {
46
+ out = error.stdout
47
+
48
+ if (error.stderr) {
49
+ fail(error.stderr)
50
+ }
51
+ }
52
+
53
+ const lines = out.split('\n')
54
+ const tags = parseTags(lines)
55
+ let text = ''
56
+
57
+ if (tags.length === 0) {
58
+ text = 'No deployments found.'
59
+ } else {
60
+ text = tags
61
+ .map((tag, idx) => {
62
+ // prettier-ignore
63
+ return `${idx + 1}. https://${AWS_S3_BUCKET}/${configName}/${tag}/index.html`
64
+ })
65
+ .join('\n')
66
+ }
67
+
68
+ logTable(text)
69
+ }
70
+
71
+ ///////////////////////////////////////////////////////////////////////////////
72
+
73
+ const assertOptionsValid = (options: ListDeployedOptions) => {
74
+ const {config, profile} = options
75
+
76
+ assertConfigValid(config)
77
+ assertProfileValid(profile)
78
+ }
79
+
80
+ ///////////////////////////////////////////////////////////////////////////////
81
+
82
+ const assertConfigValid = (config: string | undefined) => {
83
+ if (!config) {
84
+ return
85
+ }
86
+
87
+ const regex = /^[a-zA-Z0-9-_]+$/
88
+
89
+ if (!regex.test(config)) {
90
+ fail(`Flag "--config" has forbidden format. Allowed format: ${regex}.`)
91
+ }
92
+ }
93
+
94
+ ///////////////////////////////////////////////////////////////////////////////
95
+
96
+ const parseTags = (lines: string[]): string[] => {
97
+ const regex = /^\s*PRE\s*/
98
+
99
+ return lines
100
+ .filter((line) => regex.test(line))
101
+ .map((line) => line.replace(regex, '').replace(/\/$/, ''))
102
+ }
@@ -0,0 +1,29 @@
1
+ import {getPortPromise} from 'portfinder'
2
+ import {
3
+ getIfCoreFns,
4
+ getPaths,
5
+ logCommand,
6
+ logTitle,
7
+ runCommandSync,
8
+ } from 'lib/helpers'
9
+
10
+ ///////////////////////////////////////////////////////////////////////////////
11
+
12
+ const DEFAULT_PORT = 1234
13
+
14
+ ///////////////////////////////////////////////////////////////////////////////
15
+
16
+ export const serve = async (): Promise<void> => {
17
+ const {ifCore} = getIfCoreFns()
18
+ const paths = getPaths(ifCore)
19
+ const port = await getPortPromise({port: DEFAULT_PORT})
20
+ const cmd = `
21
+ npx -y http-server ${paths.appDist}
22
+ --cors
23
+ --port ${port}
24
+ `
25
+
26
+ logTitle(`Running web-server for pre-built bundle …`)
27
+ logCommand(cmd)
28
+ runCommandSync(cmd, {stdio: 'inherit'})
29
+ }
package/src/lib/aws.ts ADDED
@@ -0,0 +1,119 @@
1
+ import {commandSync, ExecaSyncReturnValue, SyncOptions} from 'execa'
2
+ import stripIndent from 'strip-indent'
3
+ import {fail, prepareCommand} from './helpers'
4
+
5
+ ///////////////////////////////////////////////////////////////////////////////
6
+
7
+ export type AwsOptions = {
8
+ profile: string
9
+ }
10
+
11
+ ///////////////////////////////////////////////////////////////////////////////
12
+
13
+ export const DEFAULT_AWS_PROFILE = 'payments-prod-cs-deployer'
14
+
15
+ ///////////////////////////////////////////////////////////////////////////////
16
+
17
+ export const runAwsCommandSync = (
18
+ command: string,
19
+ options?: SyncOptions,
20
+ ): ExecaSyncReturnValue => {
21
+ authenticate()
22
+
23
+ return commandSync(prepareCommand(command), options)
24
+ }
25
+
26
+ ///////////////////////////////////////////////////////////////////////////////
27
+
28
+ export const authenticate = () => {
29
+ // If "AWS_ACCESS_KEY_ID" and "AWS_SECRET_ACCESS_KEY" environment variables are set,
30
+ // then don't try to authenticate the user.
31
+ // This may be useful for using this CLI in automated systems like pipelines.
32
+ if (isSetEnv()) {
33
+ return
34
+ }
35
+
36
+ try {
37
+ checkIdentity()
38
+ } catch (error) {
39
+ login()
40
+ }
41
+ }
42
+
43
+ ///////////////////////////////////////////////////////////////////////////////
44
+
45
+ const isSetEnv = () => {
46
+ return !!(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY)
47
+ }
48
+
49
+ ///////////////////////////////////////////////////////////////////////////////
50
+
51
+ export const assertProfileValid = (profile: string) => {
52
+ // If "AWS_ACCESS_KEY_ID" and "AWS_SECRET_ACCESS_KEY" environment variables are set,
53
+ // then ignore "--profile" flag.
54
+ if (isSetEnv()) {
55
+ return
56
+ }
57
+
58
+ const profiles = getAvailableProfiles()
59
+
60
+ if (!profiles.includes(profile)) {
61
+ console.log(`Using AWS profile: “${profile}”`)
62
+
63
+ let message
64
+
65
+ if (profiles.length === 0) {
66
+ message = stripIndent(`
67
+ No AWS profiles found.
68
+ `)
69
+ } else {
70
+ message = stripIndent(`
71
+ AWS profile “${profile}” has not been found.
72
+ Available profiles are: ${profiles.map((v) => `“${v}”`).join(', ')}.
73
+ `)
74
+ }
75
+
76
+ message += stripIndent(`
77
+ Run “aws configure sso” to set up SSO-enabled profile.
78
+ Check the wiki for more information:
79
+ https://raisenow.atlassian.net/wiki/x/lIrWvg
80
+ `)
81
+
82
+ fail(message)
83
+ }
84
+
85
+ // Set it here, so we don't need to explicitly pass "--profile" flag
86
+ // to all subsequent "aws" commands.
87
+ // If "AWS_PROFILE" were set before, it will be overriden by "--profile" flag,
88
+ // which has default value, so may be omitted.
89
+ process.env.AWS_PROFILE = profile
90
+ }
91
+
92
+ ///////////////////////////////////////////////////////////////////////////////
93
+
94
+ const getAvailableProfiles = (): string[] => {
95
+ const {stdout} = commandSync('aws configure list-profiles')
96
+
97
+ return stdout.split('\n')
98
+ }
99
+
100
+ ///////////////////////////////////////////////////////////////////////////////
101
+
102
+ // Returns caller identity data (output is ignored here).
103
+ // Fails in case of expired sso session.
104
+ const checkIdentity = () => {
105
+ commandSync('aws sts get-caller-identity')
106
+ }
107
+
108
+ ///////////////////////////////////////////////////////////////////////////////
109
+
110
+ // Opens sso authentication page in the browser.
111
+ // Fails if user canceled authentication process.
112
+ const login = () => {
113
+ try {
114
+ commandSync('aws sso login', {stdio: 'inherit'})
115
+ console.log('')
116
+ } catch (error) {
117
+ fail('Login failed')
118
+ }
119
+ }
@@ -1,13 +1,15 @@
1
- import {basename, resolve} from 'path'
1
+ import {basename, relative, resolve} from 'path'
2
2
  import {existsSync, realpathSync} from 'fs'
3
3
  import chalk from 'chalk'
4
- import {notify as nodeNotify} from 'node-notifier'
4
+ import notifier from 'node-notifier'
5
5
  import {getIfUtils, IfUtils, IfUtilsFn} from 'webpack-config-utils'
6
6
  import columnify from 'columnify'
7
7
  import boxen from 'boxen'
8
8
  import {expand as dotenvExpand} from 'dotenv-expand'
9
9
  import {config as dotenvConfig} from 'dotenv'
10
+ import open from 'open'
10
11
  import stripIndent from 'strip-indent'
12
+ import {commandSync, ExecaSyncReturnValue, SyncOptions} from 'execa'
11
13
 
12
14
  ///////////////////////////////////////////////////////////////////////////////
13
15
 
@@ -21,11 +23,8 @@ export type EnvVars = {
21
23
  // Supported packages
22
24
  ///////////////////////////////////////////////////////////////////////////////
23
25
 
24
- const TAMARO_CORE_PACKAGE_NAMES = ['@raisenow/tamaro-core', 'demo-tamaro-core']
25
- const TAMARO_CONFIGURATIONS_PACKAGE_NAMES = [
26
- '@raisenow/tamaro-configurations',
27
- 'demo-tamaro-configurations',
28
- ]
26
+ const TAMARO_CORE_PACKAGE_NAMES = ['@raisenow/tamaro-core']
27
+ const TAMARO_CONFIGURATIONS_PACKAGE_NAMES = ['@raisenow/tamaro-configurations']
29
28
 
30
29
  ///////////////////////////////////////////////////////////////////////////////
31
30
 
@@ -51,9 +50,8 @@ export const getIfCoreFns = (): IfUtils => {
51
50
  }
52
51
  }
53
52
 
54
- console.error(
55
- chalk.red(
56
- stripIndent(`\
53
+ logError(
54
+ stripIndent(`\
57
55
  You must run "npx @raisenow/tamaro-cli" commands from:
58
56
  1. Root of "${chalk.bold(TAMARO_CORE_PACKAGE_NAMES[0])}" package folder.
59
57
  2. Root of particular customer configuration folder of "${chalk.bold(
@@ -61,9 +59,8 @@ export const getIfCoreFns = (): IfUtils => {
61
59
  )}" package.
62
60
  You are currently in "${chalk.bold(realpathSync(process.cwd()))}".
63
61
  `),
64
- ),
65
62
  )
66
- process.exit(0)
63
+ process.exit(1)
67
64
  }
68
65
 
69
66
  ///////////////////////////////////////////////////////////////////////////////
@@ -131,6 +128,24 @@ export const getPaths = (ifCore: IfUtilsFn) => {
131
128
  )
132
129
  }
133
130
 
131
+ ///////////////////////////////////////////////////////////////////////////////
132
+
133
+ type Paths = {
134
+ [key: string]: string | undefined
135
+ }
136
+
137
+ export const getRelativePaths = (paths: Paths): Paths => {
138
+ const relativePaths: Paths = {}
139
+
140
+ for (const [type, absPath] of Object.entries(paths)) {
141
+ if (absPath) {
142
+ relativePaths[type] = relative('./', absPath) || '.'
143
+ }
144
+ }
145
+
146
+ return relativePaths
147
+ }
148
+
134
149
  ///////////////////////////////////////////////////////////////////////////////
135
150
  // Env vars
136
151
  ///////////////////////////////////////////////////////////////////////////////
@@ -231,23 +246,77 @@ export const getEnvVars = (
231
246
  // Other
232
247
  ///////////////////////////////////////////////////////////////////////////////
233
248
 
234
- export const logCommand = (...args: any[]): void => {
235
- console.log(chalk.dim(...args))
249
+ export const delay = (ms: number) =>
250
+ new Promise((resolve) => setTimeout(resolve, ms))
251
+
252
+ ///////////////////////////////////////////////////////////////////////////////
253
+
254
+ export const logTitle = (title: string) => {
255
+ console.log(`\n${chalk.bold(title)}`)
256
+ }
257
+
258
+ ///////////////////////////////////////////////////////////////////////////////
259
+
260
+ export const logError = (message?: string) => {
261
+ if (message) {
262
+ console.log(chalk.red(message))
263
+ }
264
+ }
265
+
266
+ ///////////////////////////////////////////////////////////////////////////////
267
+
268
+ export const fail = (message?: string) => {
269
+ logError(message)
270
+ process.exit(1)
271
+ }
272
+
273
+ ///////////////////////////////////////////////////////////////////////////////
274
+
275
+ export const logCommand = (command: string): void => {
276
+ console.log(chalk.dim(stripIndent(command)))
277
+ }
278
+
279
+ ///////////////////////////////////////////////////////////////////////////////
280
+
281
+ export const prepareCommand = (command: string): string => {
282
+ return command
283
+ .replace(/\n/gm, ' ')
284
+ .replace(/[ \t]{2,}/gm, ' ')
285
+ .trim()
286
+ }
287
+
288
+ ///////////////////////////////////////////////////////////////////////////////
289
+
290
+ export const runCommandSync = (
291
+ command: string,
292
+ options?: SyncOptions,
293
+ ): ExecaSyncReturnValue => {
294
+ return commandSync(prepareCommand(command), options)
236
295
  }
237
296
 
238
297
  ///////////////////////////////////////////////////////////////////////////////
239
298
 
240
- // Log data into console as a table
241
299
  export const logDataTable = (
242
300
  vars: {[key: string]: string | undefined},
243
- title: string,
301
+ title?: string,
244
302
  ): void => {
245
- const boxTitle = chalk.bold(`${title}:\n`)
246
- const boxTable = columnify(vars, {showHeaders: false})
303
+ logTable(columnify(vars, {showHeaders: false}), title)
304
+ }
305
+
306
+ ///////////////////////////////////////////////////////////////////////////////
307
+
308
+ export const logTable = (text: string, title?: string): void => {
309
+ let out = ''
310
+ const boxTitle = title ? chalk.bold(`${title}`) : undefined
311
+
312
+ if (boxTitle) {
313
+ out += `${boxTitle}\n`
314
+ }
315
+
316
+ out += text
247
317
 
248
- // eslint-disable-next-line no-console
249
318
  console.log(
250
- boxen(`${boxTitle}\n${boxTable}`, {
319
+ boxen(out, {
251
320
  margin: 0,
252
321
  padding: {
253
322
  top: 0,
@@ -265,15 +334,23 @@ export const logDataTable = (
265
334
  export type NotifyArgs = {
266
335
  title: string
267
336
  message: string
337
+ target?: string
268
338
  }
269
339
 
270
340
  export const notify = (args: NotifyArgs): void => {
271
- const {title, message} = args
341
+ const {title, message, target} = args
272
342
 
273
- nodeNotify({
343
+ notifier.notify({
274
344
  title,
275
345
  message,
276
- contentImage: resolveOwn('src/assets/rnw-logo.png'),
346
+ contentImage: 'https://assets.raisenow.io/favicon.png',
277
347
  sound: 'Funk',
348
+ timeout: 30,
278
349
  })
350
+
351
+ if (target) {
352
+ notifier.on('click', () => {
353
+ open(target)
354
+ })
355
+ }
279
356
  }
@@ -1,4 +1,5 @@
1
1
  import {basename, dirname} from 'path'
2
+ import {existsSync} from 'fs'
2
3
  import glob from 'glob'
3
4
  import {getIfUtils, removeEmpty} from 'webpack-config-utils'
4
5
  import webpack, {
@@ -27,6 +28,7 @@ import {
27
28
  getEnvVars,
28
29
  getIfCoreFns,
29
30
  getPaths,
31
+ getRelativePaths,
30
32
  logDataTable,
31
33
  moduleFileExtensions,
32
34
  resolveApp,
@@ -50,8 +52,10 @@ const getWebpackConfig = async (env: any): Promise<Configuration> => {
50
52
  const envVars = getEnvVars(paths.appEnv, ifMin, ifCore, ifLocalCore)
51
53
  const {ifHmr} = getIfUtils({hmr: process.env.HMR_ENABLED === 'true'}, ['hmr'])
52
54
  const port = await getPortPromise({port: 1234})
55
+ const useTailwind =
56
+ ifCore() && paths.appTailwindConfig && existsSync(paths.appTailwindConfig)
53
57
 
54
- logDataTable(paths, 'Paths')
58
+ logDataTable(getRelativePaths(paths), 'Paths')
55
59
  logDataTable(envVars.raw, 'Environment variables')
56
60
 
57
61
  const getCssLoaders = (cssOptions: any = {}): RuleSetUseItem[] => {
@@ -78,7 +82,15 @@ const getWebpackConfig = async (env: any): Promise<Configuration> => {
78
82
  options: {
79
83
  postcssOptions: {
80
84
  plugins: removeEmpty([
81
- ifCore([require.resolve('tailwindcss'), paths.appTailwindConfig]),
85
+ useTailwind
86
+ ? [
87
+ // eslint-disable-next-line node/no-missing-require
88
+ require.resolve('tailwindcss', {
89
+ paths: [paths.appNodeModules],
90
+ }),
91
+ paths.appTailwindConfig,
92
+ ]
93
+ : undefined,
82
94
  require.resolve('postcss-custom-properties'),
83
95
  require.resolve('autoprefixer'),
84
96
  ]),
Binary file
@@ -1,42 +0,0 @@
1
- import {commandSync} from 'execa'
2
- import {logCommand, notify, resolveOwn} from 'lib/helpers'
3
- import resolveBin from 'resolve-bin'
4
-
5
- ///////////////////////////////////////////////////////////////////////////////
6
-
7
- export type BuildOptions = {
8
- localCore?: boolean
9
- analyze?: boolean
10
- deploy?: boolean
11
- tag?: string
12
- serve?: boolean
13
- }
14
-
15
- ///////////////////////////////////////////////////////////////////////////////
16
-
17
- const prepareBuildFlags = (options: BuildOptions): string => {
18
- const {localCore, analyze} = options
19
- let flags: string[] = []
20
-
21
- flags = localCore ? [...flags, '--env localCore'] : flags
22
- flags = analyze ? [...flags, '--env analyze'] : flags
23
-
24
- return flags.join(' ')
25
- }
26
-
27
- ///////////////////////////////////////////////////////////////////////////////
28
-
29
- export const build = async (options: BuildOptions): Promise<void> => {
30
- const flags = prepareBuildFlags(options)
31
-
32
- const wpBin = resolveBin.sync('webpack')
33
- const wpConfig = resolveOwn('dist/webpack.config.js')
34
- const cmd = `${wpBin} --config ${wpConfig} --env min ${flags}`
35
- logCommand(cmd)
36
- commandSync(cmd, {stdio: 'inherit'})
37
-
38
- notify({
39
- title: '"build" command',
40
- message: 'Process is done',
41
- })
42
- }
@@ -1,24 +0,0 @@
1
- import {commandSync} from 'execa'
2
- import {getIfCoreFns, getPaths, logCommand} from 'lib/helpers'
3
-
4
- ///////////////////////////////////////////////////////////////////////////////
5
-
6
- export type DeployOptions = {
7
- tag?: string
8
- }
9
-
10
- ///////////////////////////////////////////////////////////////////////////////
11
-
12
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
- export const deploy = async (options: DeployOptions): Promise<void> => {
14
- // todo: ensure build exists
15
- // todo: handle "tag" option
16
- // todo: implement
17
- const {ifCore} = getIfCoreFns()
18
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
19
- const paths = getPaths(ifCore)
20
-
21
- const cmd = `echo Deploying to AWS is not implemented yet`
22
- logCommand(cmd)
23
- commandSync(cmd, {stdio: 'inherit'})
24
- }
@@ -1,32 +0,0 @@
1
- import {commandSync} from 'execa'
2
- import {logCommand, resolveOwn} from 'lib/helpers'
3
- import resolveBin from 'resolve-bin'
4
-
5
- ///////////////////////////////////////////////////////////////////////////////
6
-
7
- export type DevOptions = {
8
- localCore?: boolean
9
- }
10
-
11
- ///////////////////////////////////////////////////////////////////////////////
12
-
13
- const prepareDevFlags = (options: DevOptions): string => {
14
- const {localCore} = options
15
- let flags: string[] = []
16
-
17
- flags = localCore ? [...flags, '--env localCore'] : flags
18
-
19
- return flags.join(' ')
20
- }
21
-
22
- ///////////////////////////////////////////////////////////////////////////////
23
-
24
- export const dev = async (options: DevOptions): Promise<void> => {
25
- const flags = prepareDevFlags(options)
26
-
27
- const wpBin = resolveBin.sync('webpack')
28
- const wpConfig = resolveOwn('dist/webpack.config.js')
29
- const cmd = `${wpBin} serve --config ${wpConfig} ${flags}`
30
- logCommand(cmd)
31
- commandSync(cmd, {stdio: 'inherit'})
32
- }
@@ -1,12 +0,0 @@
1
- import {commandSync} from 'execa'
2
- import {getIfCoreFns, getPaths, logCommand} from 'lib/helpers'
3
-
4
- ///////////////////////////////////////////////////////////////////////////////
5
-
6
- export const serve = async (): Promise<void> => {
7
- const {ifCore} = getIfCoreFns()
8
- const paths = getPaths(ifCore)
9
- const cmd = `npx -y http-server ${paths.appDist} --cors --port 1234`
10
- logCommand(cmd)
11
- commandSync(cmd, {stdio: 'inherit'})
12
- }