platformatic 2.16.0-alpha.1 → 2.17.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.
Files changed (2) hide show
  1. package/lib/resolve.js +118 -24
  2. package/package.json +17 -18
package/lib/resolve.js CHANGED
@@ -1,47 +1,141 @@
1
- import { Store } from '@platformatic/config'
1
+ import { join, relative, resolve as resolvePath } from 'node:path'
2
+ import { access, writeFile, readFile, mkdir, readdir } from 'node:fs/promises'
3
+ import { Store, getParser, getStringifier } from '@platformatic/config'
2
4
  import { platformaticRuntime } from '@platformatic/runtime'
3
5
  import parseArgs from 'minimist'
4
6
  import pino from 'pino'
5
7
  import pretty from 'pino-pretty'
6
- import { resolveServices } from 'wattpm'
8
+ import { execa } from 'execa'
9
+
10
+ const RESOLVED_SERVICES_DIRNAME = 'external'
7
11
 
8
12
  export async function resolve (argv) {
9
13
  const args = parseArgs(argv, {
10
14
  alias: {
11
15
  config: 'c',
12
16
  username: 'u',
13
- password: 'p'
17
+ password: 'p',
14
18
  },
15
- string: ['config', 'username', 'password']
19
+ boolean: ['test'],
20
+ string: ['config', 'username', 'password'],
21
+ default: { test: false },
16
22
  })
17
23
 
18
- const logger = pino(
19
- pretty({
20
- translateTime: 'SYS:HH:MM:ss',
21
- ignore: 'hostname,pid'
22
- })
23
- )
24
+ const logger = pino(pretty({
25
+ translateTime: 'SYS:HH:MM:ss',
26
+ ignore: 'hostname,pid',
27
+ }))
24
28
  try {
25
- const store = new Store({
26
- cwd: process.cwd(),
27
- logger
29
+ await resolveServices(args.config, logger, {
30
+ test: args.test,
31
+ username: args.username,
32
+ password: args.password,
28
33
  })
29
- store.add(platformaticRuntime)
34
+ } catch (err) {
35
+ console.log(err)
36
+ process.exit(1)
37
+ }
38
+ }
30
39
 
31
- const { configManager } = await store.loadConfig({
32
- config: args.config,
33
- overrides: {
34
- onMissingEnv () {
35
- return ''
40
+ async function resolveServices (configPath, logger, options = {}) {
41
+ const store = new Store({
42
+ cwd: process.cwd(),
43
+ logger,
44
+ })
45
+ store.add(platformaticRuntime)
46
+
47
+ const { configManager } = await store.loadConfig({
48
+ config: configPath,
49
+ overrides: {
50
+ onMissingEnv (key) {
51
+ return '{' + key + '}'
52
+ },
53
+ },
54
+ })
55
+
56
+ configPath = configManager.fullPath
57
+
58
+ const parseConfig = getParser(configPath)
59
+ const configFile = await readFile(configPath, 'utf8')
60
+ const config = await parseConfig(configFile)
61
+
62
+ if (!config.services || config.services.length === 0) {
63
+ logger.info('No external services to resolve')
64
+ return
65
+ }
66
+
67
+ const projectDir = configManager.dirname
68
+ const externalDir = join(projectDir, RESOLVED_SERVICES_DIRNAME)
69
+
70
+ const services = config.services || []
71
+ for (const service of services) {
72
+ if (service.url) {
73
+ let path = service.path
74
+ if (path && path.startsWith('{') && path.endsWith('}')) {
75
+ path = await configManager.replaceEnv(path)
76
+
77
+ // Failed to resolve the path
78
+ if (path.startsWith('{') && path.endsWith('}')) {
79
+ path = null
36
80
  }
37
81
  }
38
- })
39
82
 
40
- await resolveServices(logger, configManager.dirname, configManager.fullPath, args.username, args.password, false)
83
+ if (!path) {
84
+ await mkdir(externalDir, { recursive: true })
85
+ path = join(externalDir, service.id)
86
+ service.path = relative(projectDir, path)
87
+ } else {
88
+ path = resolvePath(projectDir, path)
89
+ }
90
+
91
+ const isNotEmpty = await isDirectoryNotEmpty(path)
92
+ if (isNotEmpty) {
93
+ logger.info(`Skipping ${service.id} as it is not empty`)
94
+ continue
95
+ }
96
+
97
+ const relativePath = relative(projectDir, path)
98
+
99
+ logger.info(`Cloning ${service.url} into ${relativePath}`)
100
+ if (!options.test) {
101
+ let url = service.url
102
+ if (options.username && options.password) {
103
+ const urlObj = new URL(service.url)
104
+ if (!urlObj.username && !urlObj.password) {
105
+ urlObj.username = options.username
106
+ urlObj.password = options.password
107
+ }
108
+ url = urlObj.href
109
+ }
110
+ await execa('git', ['clone', url, path])
111
+ }
41
112
 
42
- logger.info('✅ All external services have been resolved')
113
+ // TODO: replace it with a proper runtime build step
114
+ logger.info(`Resolving dependencies for service "${service.id}"`)
115
+ if (!options.test) {
116
+ await execa('npm', ['i'], { cwd: path })
117
+ }
118
+
119
+ if (!service.path) {
120
+ service.path = relativePath
121
+ }
122
+ }
123
+ }
124
+
125
+ const stringifyConfig = getStringifier(configPath)
126
+ const newConfig = stringifyConfig(config)
127
+
128
+ await writeFile(configManager.fullPath, newConfig, 'utf8')
129
+
130
+ logger.info('✅ All external services have been resolved')
131
+ }
132
+
133
+ async function isDirectoryNotEmpty (directoryPath) {
134
+ try {
135
+ await access(directoryPath)
136
+ const files = await readdir(directoryPath)
137
+ return files.length > 0
43
138
  } catch (err) {
44
- console.log(err)
45
- process.exit(1)
139
+ return false
46
140
  }
47
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "platformatic",
3
- "version": "2.16.0-alpha.1",
3
+ "version": "2.17.0",
4
4
  "description": "Platformatic CLI",
5
5
  "main": "cli.js",
6
6
  "type": "module",
@@ -28,7 +28,7 @@
28
28
  ],
29
29
  "devDependencies": {
30
30
  "add": "^2.0.6",
31
- "borp": "^0.18.0",
31
+ "borp": "^0.19.0",
32
32
  "c8": "^10.0.0",
33
33
  "eslint": "9",
34
34
  "fastify": "^5.0.0",
@@ -37,14 +37,14 @@
37
37
  "mkdirp": "^2.1.6",
38
38
  "neostandard": "^0.11.1",
39
39
  "typescript": "^5.5.3",
40
- "@platformatic/composer": "2.16.0-alpha.1",
41
- "@platformatic/astro": "2.16.0-alpha.1",
42
- "@platformatic/db": "2.16.0-alpha.1",
43
- "@platformatic/next": "2.16.0-alpha.1",
44
- "@platformatic/node": "2.16.0-alpha.1",
45
- "@platformatic/remix": "2.16.0-alpha.1",
46
- "@platformatic/vite": "2.16.0-alpha.1",
47
- "@platformatic/service": "2.16.0-alpha.1"
40
+ "@platformatic/astro": "2.17.0",
41
+ "@platformatic/composer": "2.17.0",
42
+ "@platformatic/next": "2.17.0",
43
+ "@platformatic/node": "2.17.0",
44
+ "@platformatic/db": "2.17.0",
45
+ "@platformatic/remix": "2.17.0",
46
+ "@platformatic/vite": "2.17.0",
47
+ "@platformatic/service": "2.17.0"
48
48
  },
49
49
  "dependencies": {
50
50
  "@fastify/error": "^4.0.0",
@@ -60,14 +60,13 @@
60
60
  "pino-pretty": "^13.0.0",
61
61
  "split2": "^4.2.0",
62
62
  "undici": "^6.9.0",
63
- "@platformatic/basic": "2.16.0-alpha.1",
64
- "@platformatic/control": "2.16.0-alpha.1",
65
- "@platformatic/config": "2.16.0-alpha.1",
66
- "@platformatic/runtime": "2.16.0-alpha.1",
67
- "@platformatic/client-cli": "2.16.0-alpha.1",
68
- "@platformatic/utils": "2.16.0-alpha.1",
69
- "create-platformatic": "2.16.0-alpha.1",
70
- "wattpm": "2.16.0-alpha.1"
63
+ "@platformatic/basic": "2.17.0",
64
+ "@platformatic/client-cli": "2.17.0",
65
+ "@platformatic/config": "2.17.0",
66
+ "@platformatic/control": "2.17.0",
67
+ "@platformatic/runtime": "2.17.0",
68
+ "@platformatic/utils": "2.17.0",
69
+ "create-platformatic": "2.17.0"
71
70
  },
72
71
  "scripts": {
73
72
  "test": "pnpm run lint && borp --no-timeout --concurrency 1",