create-platformatic 1.1.0 → 1.2.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-platformatic",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Create platformatic-db interactive tool",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,9 +32,10 @@
32
32
  "pino-pretty": "^10.2.0",
33
33
  "pupa": "^3.1.0",
34
34
  "semver": "^7.5.4",
35
- "undici": "^5.25.3",
35
+ "strip-ansi": "^7.1.0",
36
+ "undici": "^5.25.4",
36
37
  "which": "^3.0.1",
37
- "@platformatic/config": "1.1.0"
38
+ "@platformatic/config": "1.2.0"
38
39
  },
39
40
  "devDependencies": {
40
41
  "ajv": "^8.12.0",
@@ -47,11 +48,12 @@
47
48
  "tap": "^16.3.9",
48
49
  "typescript": "~5.2.2",
49
50
  "yaml": "^2.3.2",
50
- "@platformatic/service": "1.1.0",
51
- "@platformatic/db": "1.1.0"
51
+ "@platformatic/db": "1.2.0",
52
+ "@platformatic/service": "1.2.0"
52
53
  },
53
54
  "scripts": {
54
- "test": "standard | snazzy && cross-env NODE_OPTIONS=\"--loader=esmock --no-warnings\" c8 tap --no-coverage test/*test.mjs test/*/*test.mjs",
55
+ "test:cli": "tap --no-coverage test/cli/*test.mjs -t90",
56
+ "test": "standard | snazzy && cross-env NODE_OPTIONS=\"--loader=esmock --no-warnings\" c8 tap --no-coverage test/*test.mjs test/*[!cli]/*test.mjs && npm run test:cli",
55
57
  "lint": "standard | snazzy"
56
58
  }
57
59
  }
@@ -11,7 +11,7 @@ import { execa } from 'execa'
11
11
  import ora from 'ora'
12
12
  import createComposer from './create-composer.mjs'
13
13
  import askDir from '../ask-dir.mjs'
14
- import { getRunPackageManagerInstall, getPort } from '../cli-options.mjs'
14
+ import { getRunPackageManagerInstall, getPort, getUseTypescript } from '../cli-options.mjs'
15
15
  import { createReadme } from '../create-readme.mjs'
16
16
  import { stat } from 'node:fs/promises'
17
17
  import { join } from 'path'
@@ -61,6 +61,7 @@ const createPlatformaticComposer = async (_args, opts) => {
61
61
  const portQuestion = getPort(args.port)
62
62
  portQuestion.when = !isRuntimeContext
63
63
  toAsk.push(portQuestion)
64
+ toAsk.push(getUseTypescript(args.typescript))
64
65
 
65
66
  if (isRuntimeContext) {
66
67
  const servicesNames = opts.runtimeContext.servicesNames.filter(
@@ -95,7 +96,8 @@ const createPlatformaticComposer = async (_args, opts) => {
95
96
  servicesToCompose,
96
97
  port,
97
98
  staticWorkspaceGitHubAction,
98
- dynamicWorkspaceGitHubAction
99
+ dynamicWorkspaceGitHubAction,
100
+ useTypescript
99
101
  } = await inquirer.prompt(toAsk)
100
102
 
101
103
  // Create the project directory
@@ -108,7 +110,8 @@ const createPlatformaticComposer = async (_args, opts) => {
108
110
  servicesToCompose,
109
111
  staticWorkspaceGitHubAction,
110
112
  dynamicWorkspaceGitHubAction,
111
- runtimeContext: opts.runtimeContext
113
+ runtimeContext: opts.runtimeContext,
114
+ typescript: useTypescript
112
115
  }
113
116
 
114
117
  await createComposer(
@@ -123,7 +126,7 @@ const createPlatformaticComposer = async (_args, opts) => {
123
126
  // Create the package.json, notes that we don't have the option for TS (yet) so we don't generate
124
127
  // the package.json with the TS build
125
128
  if (!opts.skipPackageJson) {
126
- await createPackageJson(version, fastifyVersion, logger, projectDir, false)
129
+ await createPackageJson(version, fastifyVersion, logger, projectDir, useTypescript)
127
130
  }
128
131
  if (!opts.skipGitignore) {
129
132
  await createGitignore(logger, projectDir)
@@ -4,8 +4,11 @@ import { join } from 'path'
4
4
  import * as desm from 'desm'
5
5
  import { generatePlugins, generateRouteWithTypesSupport } from '../create-plugins.mjs'
6
6
  import { createDynamicWorkspaceGHAction, createStaticWorkspaceGHAction } from '../ghaction.mjs'
7
+ import { getTsConfig } from '../get-tsconfig.mjs'
7
8
 
8
- function generateConfig (isRuntimeContext, version, servicesToCompose, envPrefix) {
9
+ const TS_OUT_DIR = 'dist'
10
+
11
+ function generateConfig (isRuntimeContext, version, servicesToCompose, typescript, envPrefix) {
9
12
  const config = {
10
13
  $schema: `https://platformatic.dev/schemas/v${version}/composer`,
11
14
  composer: {
@@ -35,9 +38,7 @@ function generateConfig (isRuntimeContext, version, servicesToCompose, envPrefix
35
38
  level: '{PLT_SERVER_LOGGER_LEVEL}'
36
39
  }
37
40
  }
38
- }
39
-
40
- if (isRuntimeContext) {
41
+ } else {
41
42
  config.composer.services = servicesToCompose.map((serviceName) => {
42
43
  return {
43
44
  id: serviceName,
@@ -49,10 +50,14 @@ function generateConfig (isRuntimeContext, version, servicesToCompose, envPrefix
49
50
  })
50
51
  }
51
52
 
53
+ if (typescript === true && config.plugins) {
54
+ config.plugins.typescript = `{PLT_${envPrefix}TYPESCRIPT}`
55
+ }
56
+
52
57
  return config
53
58
  }
54
59
 
55
- function generateEnv (isRuntimeContext, hostname, port, envPrefix) {
60
+ function generateEnv (isRuntimeContext, hostname, port, typescript, envPrefix) {
56
61
  let env = ''
57
62
 
58
63
  if (!isRuntimeContext) {
@@ -64,6 +69,13 @@ PLT_SERVER_LOGGER_LEVEL=info`
64
69
  env += `
65
70
  PLT_${envPrefix}EXAMPLE_ORIGIN=
66
71
  `
72
+ if (typescript === true) {
73
+ env += `\
74
+ # Set to false to disable automatic typescript compilation.
75
+ # Changing this setting is needed for production
76
+ PLT_${envPrefix}TYPESCRIPT=true
77
+ `
78
+ }
67
79
  return env
68
80
  }
69
81
 
@@ -71,11 +83,18 @@ async function createComposer (
71
83
  params,
72
84
  logger,
73
85
  currentDir = process.cwd(),
74
- version,
75
- staticWorkspaceGitHubAction,
76
- dynamicWorkspaceGitHubAction
86
+ version
77
87
  ) {
78
- const { isRuntimeContext, hostname, port, servicesToCompose = [], runtimeContext } = params
88
+ const {
89
+ isRuntimeContext,
90
+ hostname,
91
+ port,
92
+ servicesToCompose = [],
93
+ runtimeContext,
94
+ typescript,
95
+ staticWorkspaceGitHubAction,
96
+ dynamicWorkspaceGitHubAction
97
+ } = params
79
98
 
80
99
  const composerEnv = {
81
100
  PLT_SERVER_LOGGER_LEVEL: 'info',
@@ -91,11 +110,11 @@ async function createComposer (
91
110
  if (accessibleConfigFilename === undefined) {
92
111
  const envPrefix = runtimeContext !== undefined ? `${runtimeContext.envPrefix}_` : ''
93
112
 
94
- const config = generateConfig(isRuntimeContext, version, servicesToCompose, envPrefix)
113
+ const config = generateConfig(isRuntimeContext, version, servicesToCompose, typescript, envPrefix)
95
114
  await writeFile(join(currentDir, 'platformatic.composer.json'), JSON.stringify(config, null, 2))
96
115
  logger.info('Configuration file platformatic.composer.json successfully created.')
97
116
 
98
- const env = generateEnv(isRuntimeContext, hostname, port, envPrefix)
117
+ const env = generateEnv(isRuntimeContext, hostname, port, typescript, envPrefix)
99
118
  const envFileExists = await isFileAccessible('.env', currentDir)
100
119
  await appendFile(join(currentDir, '.env'), env)
101
120
  await writeFile(join(currentDir, '.env.sample'), env)
@@ -108,14 +127,24 @@ async function createComposer (
108
127
  } else {
109
128
  logger.info(`Configuration file ${accessibleConfigFilename} found, skipping creation of configuration file.`)
110
129
  }
111
- await generatePlugins(logger, currentDir, false, 'composer')
112
- await generateRouteWithTypesSupport(logger, currentDir, false)
130
+ await generatePlugins(logger, currentDir, typescript, 'composer')
131
+ await generateRouteWithTypesSupport(logger, currentDir, true)
132
+
133
+ if (typescript === true) {
134
+ const tsConfigFileName = join(currentDir, 'tsconfig.json')
135
+ const tsConfig = getTsConfig(TS_OUT_DIR)
136
+ await writeFile(tsConfigFileName, JSON.stringify(tsConfig, null, 2))
137
+ logger.info(`Typescript configuration file ${tsConfigFileName} successfully created.`)
138
+
139
+ // TODO: global.d.ts is needed to compile the project. Still need to populate it
140
+ await writeFile(join(currentDir, 'global.d.ts'), '')
141
+ }
113
142
 
114
143
  if (staticWorkspaceGitHubAction) {
115
- await createStaticWorkspaceGHAction(logger, composerEnv, './platformatic.service.json', currentDir, false)
144
+ await createStaticWorkspaceGHAction(logger, composerEnv, './platformatic.service.json', currentDir, typescript)
116
145
  }
117
146
  if (dynamicWorkspaceGitHubAction) {
118
- await createDynamicWorkspaceGHAction(logger, composerEnv, './platformatic.service.json', currentDir, false)
147
+ await createDynamicWorkspaceGHAction(logger, composerEnv, './platformatic.service.json', currentDir, typescript)
119
148
  }
120
149
 
121
150
  return composerEnv
@@ -170,7 +170,7 @@ const createPlatformaticDB = async (_args, opts) => {
170
170
  choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
171
171
  })
172
172
 
173
- // Promtp for questions
173
+ // Prompt for questions
174
174
  const wizardOptions = await inquirer.prompt(toAsk)
175
175
 
176
176
  await mkdir(projectDir, { recursive: true })
@@ -298,7 +298,7 @@ function generateConfig (isRuntimeContext, migrations, plugin, types, typescript
298
298
  }
299
299
  }
300
300
 
301
- if (typescript === true) {
301
+ if (typescript === true && config.plugins) {
302
302
  config.plugins.typescript = `{PLT_${envPrefix}TYPESCRIPT}`
303
303
  }
304
304
 
@@ -418,14 +418,9 @@ export async function createDB (params, logger, currentDir, version) {
418
418
 
419
419
  if (typescript === true) {
420
420
  const tsConfigFileName = join(currentDir, 'tsconfig.json')
421
- const isTsConfigExists = await isFileAccessible(tsConfigFileName)
422
- if (!isTsConfigExists) {
423
- const tsConfig = getTsConfig(TS_OUT_DIR)
424
- await writeFile(tsConfigFileName, JSON.stringify(tsConfig, null, 2))
425
- logger.info(`Typescript configuration file ${tsConfigFileName} successfully created.`)
426
- } else {
427
- logger.info(`Typescript configuration file ${tsConfigFileName} found, skipping creation of typescript configuration file.`)
428
- }
421
+ const tsConfig = getTsConfig(TS_OUT_DIR)
422
+ await writeFile(tsConfigFileName, JSON.stringify(tsConfig, null, 2))
423
+ logger.info(`Typescript configuration file ${tsConfigFileName} successfully created.`)
429
424
  }
430
425
 
431
426
  if (plugin) {