create-platformatic 0.26.1 → 0.28.1

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": "0.26.1",
3
+ "version": "0.28.1",
4
4
  "description": "Create platformatic-db interactive tool",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,9 +21,9 @@
21
21
  "desm": "^1.3.0",
22
22
  "es-main": "^1.2.0",
23
23
  "execa": "^7.1.1",
24
- "fastify": "^4.17.0",
24
+ "fastify": "^4.18.0",
25
25
  "help-me": "^4.2.0",
26
- "inquirer": "^9.2.6",
26
+ "inquirer": "^9.2.7",
27
27
  "log-update": "^5.0.1",
28
28
  "minimist": "^1.2.8",
29
29
  "ora": "^6.3.1",
@@ -33,20 +33,20 @@
33
33
  "semver": "^7.5.1",
34
34
  "undici": "^5.22.1",
35
35
  "which": "^3.0.1",
36
- "@platformatic/config": "0.26.1"
36
+ "@platformatic/config": "0.28.1"
37
37
  },
38
38
  "devDependencies": {
39
39
  "ajv": "^8.12.0",
40
- "c8": "^7.13.0",
40
+ "c8": "^8.0.0",
41
41
  "cross-env": "^7.0.3",
42
- "dotenv": "^16.0.3",
43
- "esmock": "^2.2.3",
42
+ "dotenv": "^16.1.4",
43
+ "esmock": "^2.3.1",
44
44
  "snazzy": "^9.0.0",
45
- "standard": "^17.0.0",
46
- "tap": "^16.3.4",
47
- "yaml": "^2.2.2",
48
- "@platformatic/db": "0.26.1",
49
- "@platformatic/service": "0.26.1"
45
+ "standard": "^17.1.0",
46
+ "tap": "^16.3.6",
47
+ "yaml": "^2.3.1",
48
+ "@platformatic/db": "0.28.1",
49
+ "@platformatic/service": "0.28.1"
50
50
  },
51
51
  "scripts": {
52
52
  "test": "standard | snazzy && cross-env NODE_OPTIONS=\"--loader=esmock --no-warnings\" c8 --100 tap --no-coverage test/*test.mjs test/*/*test.mjs",
@@ -19,6 +19,16 @@ export const getUseTypescript = typescript => {
19
19
  }
20
20
  }
21
21
 
22
+ export const getOverwriteReadme = () => {
23
+ return {
24
+ type: 'list',
25
+ name: 'shouldReplace',
26
+ message: 'Do you want to overwrite the existing README.md?',
27
+ default: true,
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) {
@@ -13,20 +13,14 @@ import ora from 'ora'
13
13
  import createComposer from './create-composer.mjs'
14
14
  import askDir from '../ask-dir.mjs'
15
15
  import { askDynamicWorkspaceCreateGHAction, askStaticWorkspaceGHAction } from '../ghaction.mjs'
16
- import { getRunPackageManagerInstall, getPort } from '../cli-options.mjs'
16
+ import { getRunPackageManagerInstall, getPort, getOverwriteReadme } from '../cli-options.mjs'
17
17
 
18
18
  export const createReadme = async (logger, dir = '.') => {
19
19
  const readmeFileName = join(dir, 'README.md')
20
20
  let isReadmeExists = await isFileAccessible(readmeFileName)
21
21
  if (isReadmeExists) {
22
22
  logger.debug(`${readmeFileName} found, asking to overwrite it.`)
23
- const { shouldReplace } = await inquirer.prompt([{
24
- type: 'list',
25
- name: 'shouldReplace',
26
- message: 'Do you want to overwrite the existing README.md?',
27
- default: true,
28
- choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
29
- }])
23
+ const { shouldReplace } = await inquirer.prompt([getOverwriteReadme()])
30
24
  isReadmeExists = !shouldReplace
31
25
  }
32
26
 
@@ -85,7 +79,7 @@ const createPlatformaticComposer = async (_args, opts) => {
85
79
  // Create the package.json, notes that we don't have the option for TS (yet) so we don't generate
86
80
  // the package.json with the TS build
87
81
  if (!opts.skipPackageJson) {
88
- await createPackageJson('composer', version, fastifyVersion, logger, projectDir, false)
82
+ await createPackageJson(version, fastifyVersion, logger, projectDir, false)
89
83
  }
90
84
  if (!opts.skipGitignore) {
91
85
  await createGitignore(logger, projectDir)
@@ -7,10 +7,10 @@ const packageJsonTemplate = (addTSBuild = false) => (`\
7
7
  {
8
8
  "scripts": {
9
9
  ${addTSBuild
10
- ? `"start": "npm run clean && platformatic {type} start",
10
+ ? `"start": "npm run clean && platformatic start",
11
11
  "clean": "rm -fr ./dist",
12
12
  "build": "npx tsc"`
13
- : '"start": "platformatic {type} start"'}
13
+ : '"start": "platformatic start"'}
14
14
  },
15
15
  "devDependencies": {
16
16
  "fastify": "^{fastifyVersion}"
@@ -23,12 +23,14 @@ const packageJsonTemplate = (addTSBuild = false) => (`\
23
23
  }
24
24
  }`)
25
25
 
26
- export const createPackageJson = async (type, platVersion, fastifyVersion, logger, dir, addTSBuild = false) => {
26
+ export const createPackageJson = async (platVersion, fastifyVersion, logger, dir, addTSBuild = false, scripts = {}) => {
27
27
  const packageJsonFileName = join(dir, 'package.json')
28
28
  const isPackageJsonExists = await isFileAccessible(packageJsonFileName)
29
29
  if (!isPackageJsonExists) {
30
- const packageJson = pupa(packageJsonTemplate(addTSBuild), { platVersion, fastifyVersion, type })
31
- await writeFile(packageJsonFileName, packageJson)
30
+ const packageJson = pupa(packageJsonTemplate(addTSBuild), { platVersion, fastifyVersion })
31
+ const parsed = JSON.parse(packageJson)
32
+ Object.assign(parsed.scripts, scripts)
33
+ await writeFile(packageJsonFileName, JSON.stringify(parsed, null, 2))
32
34
  logger.debug(`${packageJsonFileName} successfully created.`)
33
35
  } else {
34
36
  logger.debug(`${packageJsonFileName} found, skipping creation of package.json file.`)
package/src/db/README.md CHANGED
@@ -18,7 +18,7 @@ npm install
18
18
  2. Apply migrations:
19
19
 
20
20
  ```bash
21
- npx platformatic db migrations apply
21
+ npm run migrate
22
22
  ```
23
23
 
24
24
 
@@ -14,20 +14,14 @@ import ora from 'ora'
14
14
  import createDB from './create-db.mjs'
15
15
  import askDir from '../ask-dir.mjs'
16
16
  import { askDynamicWorkspaceCreateGHAction, askStaticWorkspaceGHAction } from '../ghaction.mjs'
17
- import { getRunPackageManagerInstall, getUseTypescript, getPort } from '../cli-options.mjs'
17
+ import { getRunPackageManagerInstall, getUseTypescript, getPort, getOverwriteReadme } from '../cli-options.mjs'
18
18
 
19
19
  export const createReadme = async (logger, dir = '.') => {
20
20
  const readmeFileName = join(dir, 'README.md')
21
21
  let isReadmeExists = await isFileAccessible(readmeFileName)
22
22
  if (isReadmeExists) {
23
23
  logger.debug(`${readmeFileName} found, asking to overwrite it.`)
24
- const { shouldReplace } = await inquirer.prompt([{
25
- type: 'list',
26
- name: 'shouldReplace',
27
- message: 'Do you want to overwrite the existing README.md?',
28
- default: true,
29
- choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
30
- }])
24
+ const { shouldReplace } = await inquirer.prompt([getOverwriteReadme()])
31
25
  isReadmeExists = !shouldReplace
32
26
  }
33
27
 
@@ -114,8 +108,12 @@ const createPlatformaticDB = async (_args, opts) => {
114
108
 
115
109
  const fastifyVersion = await getDependencyVersion('fastify')
116
110
 
111
+ const scripts = {
112
+ migrate: 'platformatic db migrations apply'
113
+ }
114
+
117
115
  // Create the package.json, .gitignore, readme
118
- await createPackageJson('db', version, fastifyVersion, logger, projectDir, useTypescript)
116
+ await createPackageJson(version, fastifyVersion, logger, projectDir, useTypescript, scripts)
119
117
  await createGitignore(logger, projectDir)
120
118
  await createReadme(logger, projectDir)
121
119
 
@@ -61,6 +61,9 @@ function generateConfig (migrations, plugin, types, typescript, version) {
61
61
  connectionString: '{DATABASE_URL}',
62
62
  graphql: true,
63
63
  openapi: true
64
+ },
65
+ watch: {
66
+ ignore: ['*.sqlite', '*.sqlite-journal']
64
67
  }
65
68
  }
66
69
 
@@ -1,4 +1,3 @@
1
-
2
1
  export const getPkgManager = () => {
3
2
  const userAgent = process.env.npm_config_user_agent
4
3
  if (!userAgent) {
@@ -2,7 +2,7 @@ import { getVersion, getDependencyVersion, isFileAccessible } from '../utils.mjs
2
2
  import { createPackageJson } from '../create-package-json.mjs'
3
3
  import { createGitignore } from '../create-gitignore.mjs'
4
4
  import { getPkgManager } from '../get-pkg-manager.mjs'
5
- import { join } from 'path'
5
+ import { join, relative } from 'path'
6
6
  import inquirer from 'inquirer'
7
7
  import { readFile, writeFile, mkdir } from 'fs/promises'
8
8
  import pino from 'pino'
@@ -12,7 +12,7 @@ import ora from 'ora'
12
12
  import createRuntime from './create-runtime.mjs'
13
13
  import askDir from '../ask-dir.mjs'
14
14
  import { askDynamicWorkspaceCreateGHAction, askStaticWorkspaceGHAction } from '../ghaction.mjs'
15
- import { getRunPackageManagerInstall } from '../cli-options.mjs'
15
+ import { getOverwriteReadme, getRunPackageManagerInstall } from '../cli-options.mjs'
16
16
  import generateName from 'boring-name-generator'
17
17
  import { chooseKind } from '../index.mjs'
18
18
 
@@ -21,13 +21,7 @@ export const createReadme = async (logger, dir = '.') => {
21
21
  let isReadmeExists = await isFileAccessible(readmeFileName)
22
22
  if (isReadmeExists) {
23
23
  logger.debug(`${readmeFileName} found, asking to overwrite it.`)
24
- const { shouldReplace } = await inquirer.prompt([{
25
- type: 'list',
26
- name: 'shouldReplace',
27
- message: 'Do you want to overwrite the existing README.md?',
28
- default: true,
29
- choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
30
- }])
24
+ const { shouldReplace } = await inquirer.prompt([getOverwriteReadme()])
31
25
  isReadmeExists = !shouldReplace
32
26
  }
33
27
 
@@ -52,21 +46,24 @@ export async function createPlatformaticRuntime (_args) {
52
46
  const pkgManager = getPkgManager()
53
47
 
54
48
  const projectDir = await askDir(logger, '.')
55
- const servicesDir = await askDir(logger, 'services', 'Where would you like to load your services from?')
49
+
50
+ // Create the project directory
51
+ await mkdir(projectDir, { recursive: true })
52
+
53
+ const baseServicesDir = join(relative(process.cwd(), projectDir), 'services')
54
+ const servicesDir = await askDir(logger, baseServicesDir, 'Where would you like to load your services from?')
56
55
 
57
56
  const { runPackageManagerInstall } = await inquirer.prompt([
58
57
  getRunPackageManagerInstall(pkgManager)
59
58
  ])
60
59
 
61
- // Create the project directory
62
- await mkdir(projectDir, { recursive: true })
63
60
  await mkdir(servicesDir, { recursive: true })
64
61
 
65
62
  const fastifyVersion = await getDependencyVersion('fastify')
66
63
 
67
64
  // Create the package.json, notes that we don't have the option for TS (yet) so we don't generate
68
65
  // the package.json with the TS build
69
- await createPackageJson('runtime', version, fastifyVersion, logger, projectDir, false)
66
+ await createPackageJson(version, fastifyVersion, logger, projectDir, false)
70
67
  await createGitignore(logger, projectDir)
71
68
  await createReadme(logger, projectDir)
72
69
 
@@ -130,13 +127,24 @@ export async function createRuntimeService ({ servicesDir, names, logger }) {
130
127
  type: 'input',
131
128
  name: 'name',
132
129
  message: 'What is the name of the service?',
133
- default: generateName().dashed
130
+ default: generateName().dashed,
131
+ validate: (value) => {
132
+ if (value.length === 0) {
133
+ return 'Please enter a name'
134
+ }
135
+
136
+ if (value.includes(' ')) {
137
+ return 'Please enter a name without spaces'
138
+ }
139
+
140
+ if (names.includes(value)) {
141
+ return 'This name is already used, please choose another one.'
142
+ }
143
+
144
+ return true
145
+ }
134
146
  })
135
147
 
136
- if (names.includes(name)) {
137
- logger.warn('This name is already used, please choose another one.')
138
- return false
139
- }
140
148
  names.push(name)
141
149
 
142
150
  const serviceDir = join(servicesDir, name)
@@ -13,20 +13,14 @@ import ora from 'ora'
13
13
  import createService from './create-service.mjs'
14
14
  import askDir from '../ask-dir.mjs'
15
15
  import { askDynamicWorkspaceCreateGHAction, askStaticWorkspaceGHAction } from '../ghaction.mjs'
16
- import { getRunPackageManagerInstall, getUseTypescript, getPort } from '../cli-options.mjs'
16
+ import { getRunPackageManagerInstall, getUseTypescript, getPort, getOverwriteReadme } from '../cli-options.mjs'
17
17
 
18
18
  export const createReadme = async (logger, dir = '.') => {
19
19
  const readmeFileName = join(dir, 'README.md')
20
20
  let isReadmeExists = await isFileAccessible(readmeFileName)
21
21
  if (isReadmeExists) {
22
22
  logger.debug(`${readmeFileName} found, asking to overwrite it.`)
23
- const { shouldReplace } = await inquirer.prompt([{
24
- type: 'list',
25
- name: 'shouldReplace',
26
- message: 'Do you want to overwrite the existing README.md?',
27
- default: true,
28
- choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
29
- }])
23
+ const { shouldReplace } = await inquirer.prompt([getOverwriteReadme()])
30
24
  isReadmeExists = !shouldReplace
31
25
  }
32
26
 
@@ -86,7 +80,7 @@ const createPlatformaticService = async (_args, opts = {}) => {
86
80
  if (!opts.skipPackageJson) {
87
81
  // Create the package.json, notes that we don't have the option for TS (yet) so we don't generate
88
82
  // the package.json with the TS build
89
- await createPackageJson('service', version, fastifyVersion, logger, projectDir, false)
83
+ await createPackageJson(version, fastifyVersion, logger, projectDir, false)
90
84
  }
91
85
  if (!opts.skipGitignore) {
92
86
  await createGitignore(logger, projectDir)
@@ -8,7 +8,7 @@ const TS_OUT_DIR = 'dist'
8
8
  function generateConfig (version, typescript) {
9
9
  const plugins = {
10
10
  paths: [
11
- './plugins',
11
+ { path: './plugins', encapsulate: false },
12
12
  './routes'
13
13
  ]
14
14
  }
@@ -45,15 +45,16 @@ PLT_SERVER_LOGGER_LEVEL=info
45
45
  }
46
46
 
47
47
  const JS_PLUGIN_WITH_TYPES_SUPPORT = `\
48
+ /// <reference types="@platformatic/service" />
48
49
  'use strict'
49
50
  /** @param {import('fastify').FastifyInstance} fastify */
50
51
  module.exports = async function (fastify, opts) {
51
52
  fastify.decorate('example', 'foobar')
52
53
  }
53
- module.exports[Symbol.for('skip-override')] = true
54
54
  `
55
55
 
56
56
  const TS_PLUGIN_WITH_TYPES_SUPPORT = `\
57
+ /// <reference types="@platformatic/service" />
57
58
  import { FastifyInstance, FastifyPluginOptions } from 'fastify'
58
59
 
59
60
  export default async function (fastify: FastifyInstance, opts: FastifyPluginOptions) {
@@ -62,6 +63,7 @@ export default async function (fastify: FastifyInstance, opts: FastifyPluginOpti
62
63
  `
63
64
 
64
65
  const JS_ROUTES_WITH_TYPES_SUPPORT = `\
66
+ /// <reference types="@platformatic/service" />
65
67
  'use strict'
66
68
  /** @param {import('fastify').FastifyInstance} fastify */
67
69
  module.exports = async function (fastify, opts) {
@@ -72,6 +74,7 @@ module.exports = async function (fastify, opts) {
72
74
  `
73
75
 
74
76
  const TS_ROUTES_WITH_TYPES_SUPPORT = `\
77
+ /// <reference types="@platformatic/service" />
75
78
  import { FastifyInstance, FastifyPluginOptions } from 'fastify'
76
79
 
77
80
  declare module 'fastify' {
@@ -1,5 +1,5 @@
1
1
  import { test } from 'tap'
2
- import { getRunPackageManagerInstall, getUseTypescript, getPort } from '../src/cli-options.mjs'
2
+ import { getRunPackageManagerInstall, getUseTypescript, getPort, getOverwriteReadme } from '../src/cli-options.mjs'
3
3
 
4
4
  test('getRunPackageManagerInstall', async ({ same }) => {
5
5
  same(
@@ -28,6 +28,19 @@ test('getUseTypescript', async ({ same }) => {
28
28
  )
29
29
  })
30
30
 
31
+ test('getOverwriteReadme', async ({ same }) => {
32
+ same(
33
+ getOverwriteReadme(),
34
+ {
35
+ type: 'list',
36
+ name: 'shouldReplace',
37
+ message: 'Do you want to overwrite the existing README.md?',
38
+ default: true,
39
+ choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
40
+ }
41
+ )
42
+ })
43
+
31
44
  test('getPort', async ({ same }) => {
32
45
  same(
33
46
  getPort(undefined),
@@ -23,12 +23,12 @@ afterEach(() => {
23
23
  test('creates package.json file for db project', async ({ equal }) => {
24
24
  const version = '1.2.3'
25
25
  const fastifyVersion = '4.5.6'
26
- await createPackageJson('db', version, fastifyVersion, fakeLogger, tmpDir, false)
26
+ await createPackageJson(version, fastifyVersion, fakeLogger, tmpDir, false)
27
27
  equal(log, `${join(tmpDir, 'package.json')} successfully created.`)
28
28
  const accessible = await isFileAccessible(join(tmpDir, 'package.json'))
29
29
  equal(accessible, true)
30
30
  const packageJson = JSON.parse(readFileSync(join(tmpDir, 'package.json')))
31
- equal(packageJson.scripts.start, 'platformatic db start')
31
+ equal(packageJson.scripts.start, 'platformatic start')
32
32
  equal(packageJson.scripts.build, undefined)
33
33
  equal(packageJson.dependencies.platformatic, `^${version}`)
34
34
  equal(packageJson.devDependencies.fastify, `^${fastifyVersion}`)
@@ -37,12 +37,12 @@ test('creates package.json file for db project', async ({ equal }) => {
37
37
  test('creates package.json file for service project', async ({ equal }) => {
38
38
  const version = '1.2.3'
39
39
  const fastifyVersion = '4.5.6'
40
- await createPackageJson('service', version, fastifyVersion, fakeLogger, tmpDir, false)
40
+ await createPackageJson(version, fastifyVersion, fakeLogger, tmpDir, false)
41
41
  equal(log, `${join(tmpDir, 'package.json')} successfully created.`)
42
42
  const accessible = await isFileAccessible(join(tmpDir, 'package.json'))
43
43
  equal(accessible, true)
44
44
  const packageJson = JSON.parse(readFileSync(join(tmpDir, 'package.json')))
45
- equal(packageJson.scripts.start, 'platformatic service start')
45
+ equal(packageJson.scripts.start, 'platformatic start')
46
46
  equal(packageJson.dependencies.platformatic, `^${version}`)
47
47
  equal(packageJson.devDependencies.fastify, `^${fastifyVersion}`)
48
48
  })
@@ -52,19 +52,19 @@ test('do not create package.json file because already present', async ({ equal }
52
52
  const fastifyVersion = '4.5.6'
53
53
  const packagejson = join(tmpDir, 'package.json')
54
54
  writeFileSync(packagejson, 'TEST')
55
- await createPackageJson('db', version, fastifyVersion, fakeLogger, tmpDir, false)
55
+ await createPackageJson(version, fastifyVersion, fakeLogger, tmpDir, false)
56
56
  equal(log, `${join(tmpDir, 'package.json')} found, skipping creation of package.json file.`)
57
57
  })
58
58
 
59
59
  test('creates package.json file with TS build', async ({ equal }) => {
60
60
  const version = '1.2.3'
61
61
  const fastifyVersion = '4.5.6'
62
- await createPackageJson('db', version, fastifyVersion, fakeLogger, tmpDir, true)
62
+ await createPackageJson(version, fastifyVersion, fakeLogger, tmpDir, true)
63
63
  equal(log, `${join(tmpDir, 'package.json')} successfully created.`)
64
64
  const accessible = await isFileAccessible(join(tmpDir, 'package.json'))
65
65
  equal(accessible, true)
66
66
  const packageJson = JSON.parse(readFileSync(join(tmpDir, 'package.json')))
67
- equal(packageJson.scripts.start, 'npm run clean && platformatic db start')
67
+ equal(packageJson.scripts.start, 'npm run clean && platformatic start')
68
68
  equal(packageJson.scripts.clean, 'rm -fr ./dist')
69
69
  equal(packageJson.scripts.build, 'npx tsc')
70
70
  equal(packageJson.dependencies.platformatic, `^${version}`)
@@ -59,7 +59,7 @@ test('creates service with typescript', async ({ equal, same, ok }) => {
59
59
  equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
60
60
  equal(process.env.PORT, '6666')
61
61
 
62
- same(plugins.paths, ['./plugins', './routes'])
62
+ same(plugins.paths, [{ path: './plugins', encapsulate: false }, './routes'])
63
63
  equal(plugins.typescript, true)
64
64
 
65
65
  ok(await isFileAccessible(join(tmpDir, 'tsconfig.json')))
@@ -95,7 +95,7 @@ test('creates service with javascript', async ({ equal, same, ok }) => {
95
95
  equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
96
96
  equal(process.env.PORT, '6666')
97
97
 
98
- same(plugins, { paths: ['./plugins', './routes'] })
98
+ same(plugins, { paths: [{ path: './plugins', encapsulate: false }, './routes'] })
99
99
  ok(await isFileAccessible(join(tmpDir, 'plugins', 'example.js')))
100
100
  ok(await isFileAccessible(join(tmpDir, 'routes', 'root.js')))
101
101
  })