create-platformatic 1.2.0 → 1.3.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.
@@ -37,5 +37,5 @@ export {
37
37
  createDynamicWorkspaceGHAction
38
38
  } from './src/ghaction.mjs'
39
39
 
40
- export { createGitignore, createPackageJson, getDependencyVersion, getVersion } from './src/index.mjs'
40
+ export { createGitignore, createGitRepository, createPackageJson, getDependencyVersion, getVersion } from './src/index.mjs'
41
41
  export { default as createService } from './src/service/create-service.mjs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-platformatic",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Create platformatic-db interactive tool",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,7 +35,7 @@
35
35
  "strip-ansi": "^7.1.0",
36
36
  "undici": "^5.25.4",
37
37
  "which": "^3.0.1",
38
- "@platformatic/config": "1.2.0"
38
+ "@platformatic/config": "1.3.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "ajv": "^8.12.0",
@@ -48,11 +48,11 @@
48
48
  "tap": "^16.3.9",
49
49
  "typescript": "~5.2.2",
50
50
  "yaml": "^2.3.2",
51
- "@platformatic/db": "1.2.0",
52
- "@platformatic/service": "1.2.0"
51
+ "@platformatic/db": "1.3.0",
52
+ "@platformatic/service": "1.3.0"
53
53
  },
54
54
  "scripts": {
55
- "test:cli": "tap --no-coverage test/cli/*test.mjs -t90",
55
+ "test:cli": "tap --no-coverage test/cli/*test.mjs -t120",
56
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",
57
57
  "lint": "standard | snazzy"
58
58
  }
@@ -19,6 +19,16 @@ export const getUseTypescript = typescript => {
19
19
  }
20
20
  }
21
21
 
22
+ export const getInitGitRepository = () => {
23
+ return {
24
+ type: 'list',
25
+ name: 'initGitRepository',
26
+ message: 'Do you want to init the git repository?',
27
+ default: false,
28
+ choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
29
+ }
30
+ }
31
+
22
32
  let port = 3042
23
33
  export const getPort = (nextPort) => {
24
34
  if (nextPort === undefined) {
@@ -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, getUseTypescript } from '../cli-options.mjs'
14
+ import { getRunPackageManagerInstall, getPort, getUseTypescript, getInitGitRepository } 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'
@@ -91,13 +91,18 @@ const createPlatformaticComposer = async (_args, opts) => {
91
91
  choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
92
92
  })
93
93
  }
94
+
95
+ if (!opts.skipGitRepository) {
96
+ toAsk.push(getInitGitRepository())
97
+ }
94
98
  const {
95
99
  runPackageManagerInstall,
96
100
  servicesToCompose,
97
101
  port,
98
102
  staticWorkspaceGitHubAction,
99
103
  dynamicWorkspaceGitHubAction,
100
- useTypescript
104
+ useTypescript,
105
+ initGitRepository
101
106
  } = await inquirer.prompt(toAsk)
102
107
 
103
108
  // Create the project directory
@@ -111,7 +116,8 @@ const createPlatformaticComposer = async (_args, opts) => {
111
116
  staticWorkspaceGitHubAction,
112
117
  dynamicWorkspaceGitHubAction,
113
118
  runtimeContext: opts.runtimeContext,
114
- typescript: useTypescript
119
+ typescript: useTypescript,
120
+ initGitRepository
115
121
  }
116
122
 
117
123
  await createComposer(
@@ -5,6 +5,7 @@ import * as desm from 'desm'
5
5
  import { generatePlugins, generateRouteWithTypesSupport } from '../create-plugins.mjs'
6
6
  import { createDynamicWorkspaceGHAction, createStaticWorkspaceGHAction } from '../ghaction.mjs'
7
7
  import { getTsConfig } from '../get-tsconfig.mjs'
8
+ import { createGitRepository } from '../create-git-repository.mjs'
8
9
 
9
10
  const TS_OUT_DIR = 'dist'
10
11
 
@@ -93,7 +94,8 @@ async function createComposer (
93
94
  runtimeContext,
94
95
  typescript,
95
96
  staticWorkspaceGitHubAction,
96
- dynamicWorkspaceGitHubAction
97
+ dynamicWorkspaceGitHubAction,
98
+ initGitRepository
97
99
  } = params
98
100
 
99
101
  const composerEnv = {
@@ -147,6 +149,10 @@ async function createComposer (
147
149
  await createDynamicWorkspaceGHAction(logger, composerEnv, './platformatic.service.json', currentDir, typescript)
148
150
  }
149
151
 
152
+ if (initGitRepository) {
153
+ await createGitRepository(logger, currentDir)
154
+ }
155
+
150
156
  return composerEnv
151
157
  }
152
158
 
@@ -0,0 +1,105 @@
1
+ import { execa } from 'execa'
2
+
3
+ export const GIT_FIRST_COMMIT_MESSAGE = 'Platformatic project started! 🚀'
4
+ export const GIT_MAIN_BRANCH = 'main'
5
+
6
+ /**
7
+ * Creates a Git repository and performs the initial commit if it doesn't already exist.
8
+ *
9
+ * This function checks if Git is installed, initializes a Git repository in the specified
10
+ * directory if it's not already a Git repository, and performs the initial commit.
11
+ *
12
+ * @param {import('pino.').BaseLogger} logger - The logger interface for logging messages.
13
+ * @param {string} [dir='.'] - The target directory where the Git repository should be created.
14
+ */
15
+ export async function createGitRepository (logger, dir = '.') {
16
+ if (!await isGitInstalled()) {
17
+ logger.error('Git is not installed')
18
+ return
19
+ }
20
+
21
+ if (!await gitInit(logger, dir)) {
22
+ return
23
+ }
24
+
25
+ if (!await gitCommit(logger, dir)) {
26
+ return
27
+ }
28
+
29
+ logger.info('Git repository initialized.')
30
+ }
31
+
32
+ /**
33
+ * Checks if Git is installed on the system.
34
+ *
35
+ * @async
36
+ * @returns {Promise<boolean>} A Promise that resolves to true if Git is installed, false otherwise.
37
+ */
38
+ async function isGitInstalled () {
39
+ try {
40
+ await execa('git', ['--version'])
41
+ return true
42
+ } catch (err) {
43
+ return false
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Checks if a Git repository exists in the specified directory.
49
+ *
50
+ * @async
51
+ * @param {string} dir - The directory to check for a Git repository.
52
+ * @returns {Promise<boolean>} A Promise that resolves to true if a Git repository exists in the directory, false otherwise.
53
+ */
54
+ async function doesGitRepositoryExist (dir) {
55
+ try {
56
+ await execa('git', ['rev-parse', '--is-inside-work-tree'], { cwd: dir })
57
+ return true
58
+ } catch (e) {
59
+ return false
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Initializes a Git repository in the specified directory if it doesn't already exist.
65
+ *
66
+ * @async
67
+ * @param {import('pino.').BaseLogger} - The logger object for logging messages.
68
+ * @param {string} dir - The directory where the Git repository should be initialized.
69
+ * @returns {Promise<boolean>} A Promise that resolves to true if the Git repository is successfully initialized, false otherwise.
70
+ */
71
+ async function gitInit (logger, dir) {
72
+ try {
73
+ if (await doesGitRepositoryExist(dir)) {
74
+ logger.info('Git repository already exists.')
75
+ return false
76
+ }
77
+
78
+ await execa('git', ['init', '-b', GIT_MAIN_BRANCH], { cwd: dir })
79
+ logger.debug('Git repository initialized.')
80
+ return true
81
+ } catch (err) {
82
+ logger.error('Git repository init failed.', err)
83
+ return false
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Commits changes in a Git repository located in the specified directory.
89
+ *
90
+ * @async
91
+ * @param {import('pino.').BaseLogger} - The logger object for logging messages.
92
+ * @param {string} dir - The directory of the Git repository where changes should be committed.
93
+ * @returns {Promise<boolean>} A Promise that resolves to true if the Git commit is successful, false otherwise.
94
+ */
95
+ async function gitCommit (logger, dir) {
96
+ try {
97
+ await execa('git', ['add', '-A'], { cwd: dir })
98
+ await execa('git', ['commit', '-m', GIT_FIRST_COMMIT_MESSAGE], { cwd: dir })
99
+ logger.debug('Git commit done.')
100
+ return true
101
+ } catch (err) {
102
+ logger.error('Git commit failed.', err)
103
+ return false
104
+ }
105
+ }
@@ -12,7 +12,7 @@ import { execa } from 'execa'
12
12
  import ora from 'ora'
13
13
  import { getConnectionString, createDB } from './create-db.mjs'
14
14
  import askDir from '../ask-dir.mjs'
15
- import { getRunPackageManagerInstall, getUseTypescript, getPort } from '../cli-options.mjs'
15
+ import { getRunPackageManagerInstall, getUseTypescript, getPort, getInitGitRepository } from '../cli-options.mjs'
16
16
  import { createReadme } from '../create-readme.mjs'
17
17
  import { join } from 'node:path'
18
18
 
@@ -169,7 +169,9 @@ const createPlatformaticDB = async (_args, opts) => {
169
169
  when: !opts.skipGitHubActions,
170
170
  choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
171
171
  })
172
-
172
+ if (!opts.skipGitRepository) {
173
+ toAsk.push(getInitGitRepository())
174
+ }
173
175
  // Prompt for questions
174
176
  const wizardOptions = await inquirer.prompt(toAsk)
175
177
 
@@ -191,7 +193,8 @@ const createPlatformaticDB = async (_args, opts) => {
191
193
  typescript: useTypescript,
192
194
  staticWorkspaceGitHubAction: wizardOptions.staticWorkspaceGitHubAction,
193
195
  dynamicWorkspaceGitHubAction: wizardOptions.dynamicWorkspaceGitHubAction,
194
- runtimeContext: opts.runtimeContext
196
+ runtimeContext: opts.runtimeContext,
197
+ initGitRepository: wizardOptions.initGitRepository
195
198
  }
196
199
 
197
200
  await createDB(params, logger, projectDir, version)
@@ -4,6 +4,7 @@ import { addPrefixToEnv, findDBConfigFile, isFileAccessible } from '../utils.mjs
4
4
  import { getTsConfig } from '../get-tsconfig.mjs'
5
5
  import { generatePlugins } from '../create-plugins.mjs'
6
6
  import { createDynamicWorkspaceGHAction, createStaticWorkspaceGHAction } from '../ghaction.mjs'
7
+ import { createGitRepository } from '../create-git-repository.mjs'
7
8
 
8
9
  const connectionStrings = {
9
10
  postgres: 'postgres://postgres:postgres@127.0.0.1:5432/postgres',
@@ -349,7 +350,8 @@ export async function createDB (params, logger, currentDir, version) {
349
350
  connectionString,
350
351
  staticWorkspaceGitHubAction,
351
352
  dynamicWorkspaceGitHubAction,
352
- runtimeContext
353
+ runtimeContext,
354
+ initGitRepository
353
355
  } = params
354
356
 
355
357
  const dbEnv = {
@@ -457,9 +459,14 @@ export async function createDB (params, logger, currentDir, version) {
457
459
  await createDynamicWorkspaceGHAction(logger, dbEnv, './platformatic.db.json', currentDir, typescript)
458
460
  }
459
461
 
462
+ if (initGitRepository) {
463
+ await createGitRepository(logger, currentDir)
464
+ }
465
+
460
466
  if (isRuntimeContext) {
461
467
  return addPrefixToEnv(isRuntimeContext)
462
468
  }
469
+
463
470
  return dbEnv
464
471
  }
465
472
 
package/src/index.mjs CHANGED
@@ -11,6 +11,7 @@ import commist from 'commist'
11
11
  import { getUsername, getVersion, minimumSupportedNodeVersions, isCurrentVersionSupported, findRuntimeConfigFile, getDependencyVersion } from './utils.mjs'
12
12
  import { createPackageJson } from './create-package-json.mjs'
13
13
  import { createGitignore } from './create-gitignore.mjs'
14
+ import { createGitRepository } from './create-git-repository.mjs'
14
15
 
15
16
  export async function chooseKind (argv, opts = {}) {
16
17
  const skip = opts.skip
@@ -107,6 +108,7 @@ export default createPlatformatic
107
108
  export {
108
109
  createPackageJson,
109
110
  createGitignore,
111
+ createGitRepository,
110
112
  getVersion,
111
113
  getDependencyVersion
112
114
  }
@@ -11,7 +11,7 @@ import { execa } from 'execa'
11
11
  import ora from 'ora'
12
12
  import createRuntime from './create-runtime.mjs'
13
13
  import askDir from '../ask-dir.mjs'
14
- import { getPort, getRunPackageManagerInstall } from '../cli-options.mjs'
14
+ import { getInitGitRepository, getPort, getRunPackageManagerInstall } from '../cli-options.mjs'
15
15
  import generateName from 'boring-name-generator'
16
16
  import { chooseKind } from '../index.mjs'
17
17
  import { createReadme } from '../create-readme.mjs'
@@ -67,10 +67,12 @@ export async function createPlatformaticRuntime (_args) {
67
67
  choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
68
68
  })
69
69
 
70
+ toAsk.push(getInitGitRepository())
70
71
  const {
71
72
  runPackageManagerInstall,
72
73
  staticWorkspaceGitHubAction,
73
- dynamicWorkspaceGitHubAction
74
+ dynamicWorkspaceGitHubAction,
75
+ initGitRepository
74
76
  } = await inquirer.prompt(toAsk)
75
77
 
76
78
  await mkdir(servicesDir, { recursive: true })
@@ -130,7 +132,8 @@ export async function createPlatformaticRuntime (_args) {
130
132
  entrypointPort,
131
133
  staticWorkspaceGitHubAction,
132
134
  dynamicWorkspaceGitHubAction,
133
- serviceNames: names
135
+ serviceNames: names,
136
+ initGitRepository
134
137
  }
135
138
 
136
139
  await createRuntime(params, logger, projectDir, version)
@@ -180,6 +183,7 @@ export async function createRuntimeService ({ servicesDir, names, logger }) {
180
183
  skipGitHubActions: true,
181
184
  skipPackageJson: true,
182
185
  skipGitignore: true,
186
+ skipGitRepository: true,
183
187
  port: '0',
184
188
  isRuntimeContext: true,
185
189
  runtimeContext: {
@@ -3,6 +3,7 @@ import { findRuntimeConfigFile } from '../utils.mjs'
3
3
  import { join, relative, isAbsolute } from 'path'
4
4
  import * as desm from 'desm'
5
5
  import { createDynamicWorkspaceGHAction, createStaticWorkspaceGHAction } from '../ghaction.mjs'
6
+ import { createGitRepository } from '../create-git-repository.mjs'
6
7
 
7
8
  function generateConfig (version, path, entrypoint) {
8
9
  const config = {
@@ -25,9 +26,10 @@ async function createRuntime (params, logger, currentDir = process.cwd(), versio
25
26
  entrypoint,
26
27
  entrypointPort,
27
28
  staticWorkspaceGitHubAction,
28
- dynamicWorkspaceGitHubAction
29
+ dynamicWorkspaceGitHubAction,
30
+ initGitRepository
29
31
  } = params
30
-
32
+ console.log('@@@@@@@@@@@@@@@@', params)
31
33
  if (!version) {
32
34
  const pkg = await readFile(desm.join(import.meta.url, '..', '..', 'package.json'))
33
35
  version = JSON.parse(pkg).version
@@ -59,6 +61,9 @@ async function createRuntime (params, logger, currentDir = process.cwd(), versio
59
61
  await createDynamicWorkspaceGHAction(logger, runtimeEnv, './platformatic.runtime.json', currentDir, false)
60
62
  }
61
63
 
64
+ if (initGitRepository) {
65
+ await createGitRepository(logger, currentDir)
66
+ }
62
67
  return {}
63
68
  }
64
69
  /**
@@ -12,7 +12,7 @@ import { execa } from 'execa'
12
12
  import ora from 'ora'
13
13
  import createService from './create-service.mjs'
14
14
  import askDir from '../ask-dir.mjs'
15
- import { getRunPackageManagerInstall, getUseTypescript, getPort } from '../cli-options.mjs'
15
+ import { getRunPackageManagerInstall, getUseTypescript, getPort, getInitGitRepository } from '../cli-options.mjs'
16
16
  import { createReadme } from '../create-readme.mjs'
17
17
 
18
18
  const createPlatformaticService = async (_args, opts = {}) => {
@@ -71,12 +71,16 @@ const createPlatformaticService = async (_args, opts = {}) => {
71
71
  choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
72
72
  })
73
73
  }
74
+ if (!opts.skipGitRepository) {
75
+ toAsk.push(getInitGitRepository())
76
+ }
74
77
  const {
75
78
  runPackageManagerInstall,
76
79
  useTypescript,
77
80
  port,
78
81
  staticWorkspaceGitHubAction,
79
- dynamicWorkspaceGitHubAction
82
+ dynamicWorkspaceGitHubAction,
83
+ initGitRepository
80
84
  } = await inquirer.prompt(toAsk)
81
85
 
82
86
  // Create the project directory
@@ -89,7 +93,8 @@ const createPlatformaticService = async (_args, opts = {}) => {
89
93
  typescript: useTypescript,
90
94
  staticWorkspaceGitHubAction,
91
95
  dynamicWorkspaceGitHubAction,
92
- runtimeContext: opts.runtimeContext
96
+ runtimeContext: opts.runtimeContext,
97
+ initGitRepository
93
98
  }
94
99
 
95
100
  await createService(params, logger, projectDir, version)
@@ -5,6 +5,7 @@ import { addPrefixToEnv, findServiceConfigFile, isFileAccessible } from '../util
5
5
  import { getTsConfig } from '../get-tsconfig.mjs'
6
6
  import { generatePlugins } from '../create-plugins.mjs'
7
7
  import { createDynamicWorkspaceGHAction, createStaticWorkspaceGHAction } from '../ghaction.mjs'
8
+ import { createGitRepository } from '../create-git-repository.mjs'
8
9
 
9
10
  const TS_OUT_DIR = 'dist'
10
11
 
@@ -72,7 +73,8 @@ async function createService (params, logger, currentDir = process.cwd(), versio
72
73
  typescript = false,
73
74
  staticWorkspaceGitHubAction,
74
75
  dynamicWorkspaceGitHubAction,
75
- runtimeContext
76
+ runtimeContext,
77
+ initGitRepository
76
78
  } = params
77
79
 
78
80
  const serviceEnv = {
@@ -139,7 +141,9 @@ async function createService (params, logger, currentDir = process.cwd(), versio
139
141
  if (isRuntimeContext) {
140
142
  return addPrefixToEnv(serviceEnv, runtimeContext.envPrefix)
141
143
  }
142
-
144
+ if (initGitRepository) {
145
+ await createGitRepository(logger, currentDir)
146
+ }
143
147
  return serviceEnv
144
148
  }
145
149