@contember/cli 1.4.0-alpha.4 → 1.4.0-alpha.5

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 (90) hide show
  1. package/dist/src/commands/deploy/DeployCommand.js +2 -2
  2. package/dist/src/commands/deploy/DeployCommand.js.map +1 -1
  3. package/dist/src/commands/migrations/MigrationDiffCommand.js +2 -2
  4. package/dist/src/commands/migrations/MigrationDiffCommand.js.map +1 -1
  5. package/dist/src/commands/migrations/MigrationExecuteCommand.js +2 -2
  6. package/dist/src/commands/migrations/MigrationExecuteCommand.js.map +1 -1
  7. package/dist/src/commands/migrations/MigrationStatusCommand.d.ts.map +1 -1
  8. package/dist/src/commands/migrations/MigrationStatusCommand.js +5 -4
  9. package/dist/src/commands/migrations/MigrationStatusCommand.js.map +1 -1
  10. package/dist/src/commands/migrations/SystemApiClientResolver.d.ts +1 -1
  11. package/dist/src/commands/migrations/SystemApiClientResolver.d.ts.map +1 -1
  12. package/dist/src/commands/migrations/SystemApiClientResolver.js +2 -2
  13. package/dist/src/commands/migrations/SystemApiClientResolver.js.map +1 -1
  14. package/dist/src/tsconfig.tsbuildinfo +1 -1
  15. package/dist/src/utils/migrations/MigrationCreateHelper.d.ts +1 -5
  16. package/dist/src/utils/migrations/MigrationCreateHelper.d.ts.map +1 -1
  17. package/dist/src/utils/migrations/MigrationCreateHelper.js.map +1 -1
  18. package/dist/src/utils/migrations/MigrationExecuteHelper.d.ts +6 -7
  19. package/dist/src/utils/migrations/MigrationExecuteHelper.d.ts.map +1 -1
  20. package/dist/src/utils/migrations/MigrationExecuteHelper.js +12 -52
  21. package/dist/src/utils/migrations/MigrationExecuteHelper.js.map +1 -1
  22. package/dist/src/utils/migrations/MigrationsContainer.d.ts +4 -4
  23. package/dist/src/utils/migrations/MigrationsContainer.d.ts.map +1 -1
  24. package/dist/src/utils/migrations/MigrationsContainer.js +23 -15
  25. package/dist/src/utils/migrations/MigrationsContainer.js.map +1 -1
  26. package/dist/src/utils/migrations/migrations.d.ts +2 -54
  27. package/dist/src/utils/migrations/migrations.d.ts.map +1 -1
  28. package/dist/src/utils/migrations/migrations.js +7 -88
  29. package/dist/src/utils/migrations/migrations.js.map +1 -1
  30. package/package.json +10 -10
  31. package/src/commands/deploy/DeployCommand.ts +1 -1
  32. package/src/commands/migrations/MigrationDiffCommand.ts +1 -1
  33. package/src/commands/migrations/MigrationExecuteCommand.ts +1 -1
  34. package/src/commands/migrations/MigrationStatusCommand.ts +3 -2
  35. package/src/commands/migrations/SystemApiClientResolver.ts +1 -1
  36. package/src/tsconfig.json +1 -1
  37. package/src/utils/migrations/MigrationCreateHelper.ts +8 -5
  38. package/src/utils/migrations/MigrationExecuteHelper.ts +23 -61
  39. package/src/utils/migrations/MigrationsContainer.ts +20 -12
  40. package/src/utils/migrations/migrations.ts +3 -148
  41. package/dist/src/utils/migrations/JsLoader.d.ts +0 -9
  42. package/dist/src/utils/migrations/JsLoader.d.ts.map +0 -1
  43. package/dist/src/utils/migrations/JsLoader.js +0 -33
  44. package/dist/src/utils/migrations/JsLoader.js.map +0 -1
  45. package/dist/src/utils/migrations/JsonLoader.d.ts +0 -9
  46. package/dist/src/utils/migrations/JsonLoader.d.ts.map +0 -1
  47. package/dist/src/utils/migrations/JsonLoader.js +0 -17
  48. package/dist/src/utils/migrations/JsonLoader.js.map +0 -1
  49. package/dist/src/utils/migrations/MigrationCreator.d.ts +0 -21
  50. package/dist/src/utils/migrations/MigrationCreator.d.ts.map +0 -1
  51. package/dist/src/utils/migrations/MigrationCreator.js +0 -47
  52. package/dist/src/utils/migrations/MigrationCreator.js.map +0 -1
  53. package/dist/src/utils/migrations/MigrationFile.d.ts +0 -36
  54. package/dist/src/utils/migrations/MigrationFile.d.ts.map +0 -1
  55. package/dist/src/utils/migrations/MigrationFile.js +0 -6
  56. package/dist/src/utils/migrations/MigrationFile.js.map +0 -1
  57. package/dist/src/utils/migrations/MigrationFileLoader.d.ts +0 -5
  58. package/dist/src/utils/migrations/MigrationFileLoader.d.ts.map +0 -1
  59. package/dist/src/utils/migrations/MigrationFileLoader.js +0 -3
  60. package/dist/src/utils/migrations/MigrationFileLoader.js.map +0 -1
  61. package/dist/src/utils/migrations/MigrationFilesManager.d.ts +0 -18
  62. package/dist/src/utils/migrations/MigrationFilesManager.d.ts.map +0 -1
  63. package/dist/src/utils/migrations/MigrationFilesManager.js +0 -114
  64. package/dist/src/utils/migrations/MigrationFilesManager.js.map +0 -1
  65. package/dist/src/utils/migrations/MigrationParser.d.ts +0 -6
  66. package/dist/src/utils/migrations/MigrationParser.d.ts.map +0 -1
  67. package/dist/src/utils/migrations/MigrationParser.js +0 -33
  68. package/dist/src/utils/migrations/MigrationParser.js.map +0 -1
  69. package/dist/src/utils/migrations/MigrationsResolver.d.ts +0 -14
  70. package/dist/src/utils/migrations/MigrationsResolver.d.ts.map +0 -1
  71. package/dist/src/utils/migrations/MigrationsResolver.js +0 -37
  72. package/dist/src/utils/migrations/MigrationsResolver.js.map +0 -1
  73. package/dist/src/utils/migrations/SchemaVersionBuilder.d.ts +0 -13
  74. package/dist/src/utils/migrations/SchemaVersionBuilder.d.ts.map +0 -1
  75. package/dist/src/utils/migrations/SchemaVersionBuilder.js +0 -26
  76. package/dist/src/utils/migrations/SchemaVersionBuilder.js.map +0 -1
  77. package/dist/src/utils/system/index.d.ts +0 -30
  78. package/dist/src/utils/system/index.d.ts.map +0 -1
  79. package/dist/src/utils/system/index.js +0 -118
  80. package/dist/src/utils/system/index.js.map +0 -1
  81. package/src/utils/migrations/JsLoader.ts +0 -37
  82. package/src/utils/migrations/JsonLoader.ts +0 -18
  83. package/src/utils/migrations/MigrationCreator.ts +0 -60
  84. package/src/utils/migrations/MigrationFile.ts +0 -52
  85. package/src/utils/migrations/MigrationFileLoader.ts +0 -5
  86. package/src/utils/migrations/MigrationFilesManager.ts +0 -102
  87. package/src/utils/migrations/MigrationParser.ts +0 -32
  88. package/src/utils/migrations/MigrationsResolver.ts +0 -44
  89. package/src/utils/migrations/SchemaVersionBuilder.ts +0 -33
  90. package/src/utils/system/index.ts +0 -150
@@ -1,102 +0,0 @@
1
- import { MigrationVersionHelper } from '@contember/engine-common'
2
- import * as fs from 'node:fs/promises'
3
- import { MigrationFileLoader } from './MigrationFileLoader'
4
- import { MigrationContent, MigrationFile } from './MigrationFile'
5
- import { join } from 'node:path'
6
- import { Dirent } from 'node:fs'
7
-
8
- export class MigrationFilesManager {
9
- private migrations: null | Promise<MigrationFile[]> = null
10
-
11
- constructor(
12
- public readonly directory: string,
13
- private readonly loaders: Record<string, MigrationFileLoader>,
14
- ) {}
15
-
16
- public async createFile(content: string, name: string, extension = 'json'): Promise<string> {
17
- this.migrations = null
18
- const path = this.formatPath(name, extension)
19
- await fs.writeFile(path, content, { encoding: 'utf8' })
20
- return await fs.realpath(path)
21
- }
22
-
23
- public async removeFile(name: string) {
24
- this.migrations = null
25
- const path = this.formatPath(name)
26
- await fs.unlink(path)
27
- }
28
-
29
- public async moveFile(oldName: string, newName: string) {
30
- this.migrations = null
31
- await fs.rename(this.formatPath(oldName), this.formatPath(newName))
32
- }
33
-
34
- public async createDirIfNotExist(): Promise<void> {
35
- try {
36
- await fs.mkdir(this.directory)
37
- } catch (e) {
38
- if (!(e instanceof Error) || !('code' in e) || (e as any).code !== 'EEXIST') {
39
- throw e
40
- }
41
- }
42
- }
43
-
44
- public async readFiles(): Promise<MigrationFile[]> {
45
- return this.migrations ??= this.forceReadFiles()
46
- }
47
-
48
- public async forceReadFiles(): Promise<MigrationFile[]> {
49
- const files = (await this.listFiles()).map(it => ({
50
- filename: it,
51
- version: MigrationVersionHelper.extractVersion(it),
52
- }))
53
-
54
- return files.map(({ version, filename }): MigrationFile => {
55
- const ext = filename.substring(filename.lastIndexOf('.') + 1)
56
- const path = join(this.directory, filename)
57
- let content: Promise<MigrationContent> | null = null
58
- const file: MigrationFile = {
59
- path: path,
60
- version: version,
61
- name: MigrationVersionHelper.extractName(path.substring(path.lastIndexOf('/') + 1)),
62
- getContent: () => {
63
- content ??= this.loaders[ext].load(file)
64
- return content
65
- },
66
- }
67
- return file
68
- })
69
- }
70
-
71
- private async tryReadDir(): Promise<Dirent[]> {
72
- try {
73
- return await fs.readdir(this.directory, { withFileTypes: true })
74
- } catch (e) {
75
- if (e instanceof Error && 'code' in e && (e as any).code === 'ENOENT') {
76
- return []
77
- }
78
- throw e
79
- }
80
- }
81
-
82
- private async listFiles(): Promise<string[]> {
83
- const files = await this.tryReadDir()
84
-
85
- const extensions = Object.keys(this.loaders).join('|')
86
- const regex = new RegExp('^\\d{4}-\\d{2}-\\d{2}-\\d{6}-[\\w-]+\\.(' + extensions + ')$')
87
- const filteredFiles = await Promise.all(
88
- files
89
- .filter(it => it.isFile())
90
- .map(it => it.name)
91
- .filter(it => it.match(regex)),
92
- )
93
-
94
- return filteredFiles.sort()
95
- }
96
-
97
- private formatPath(version: string, extension = 'json'): string {
98
- const filename = `${version}.${extension}`
99
- const path = `${this.directory}/${filename}`
100
- return path
101
- }
102
- }
@@ -1,32 +0,0 @@
1
- import { JSONValue } from '@contember/cli-common'
2
- import { Migration, VERSION_INITIAL } from '@contember/schema-migrations'
3
- import { ContentMigrationQuery, MigrationFile, ResolvedMigrationContent } from './MigrationFile'
4
-
5
- export class MigrationParser {
6
- public parse(migrationFile: MigrationFile, content: JSONValue): ResolvedMigrationContent {
7
- if (typeof content !== 'object' || content === null) {
8
- throw new Error('Invalid migration')
9
- }
10
-
11
- if ('modifications' in content) {
12
- const validated = content as Migration // todo validate
13
-
14
- return {
15
- type: 'schema',
16
- version: migrationFile.version,
17
- name: migrationFile.name,
18
- formatVersion: validated.formatVersion || VERSION_INITIAL,
19
- modifications: validated.modifications,
20
- ...(validated.skippedErrors ? { skippedErrors: validated.skippedErrors } : {}),
21
- }
22
- } else if ('queries' in content || 'query' in content) {
23
- return {
24
- type: 'content',
25
- version: migrationFile.version,
26
- name: migrationFile.name,
27
- queries: ('queries' in content ? content.queries : [content]) as ContentMigrationQuery[],
28
- }
29
- }
30
- throw new Error('Invalid migration content')
31
- }
32
- }
@@ -1,44 +0,0 @@
1
- import { MigrationFilesManager } from './MigrationFilesManager'
2
- import { Migration } from '@contember/schema-migrations'
3
- import { isSchemaMigration, MigrationFile } from './MigrationFile'
4
- import { MigrationVersionHelper } from '@contember/engine-common'
5
-
6
- export class MigrationsResolver {
7
-
8
- constructor(private readonly migrationFilesManager: MigrationFilesManager) {}
9
-
10
- public get directory(): string {
11
- return this.migrationFilesManager.directory
12
- }
13
-
14
- public async getMigrationFiles(): Promise<MigrationFile[]> {
15
- return await this.migrationFilesManager.readFiles()
16
- }
17
-
18
- public async getSchemaMigrations(): Promise<Migration[]> {
19
- const migrationFiles = await this.getMigrationFiles()
20
-
21
- const migrations = await Promise.all(migrationFiles.map(this.convertMigrationFileToSchemaMigration))
22
-
23
- return migrations.filter((it): it is Migration => it !== null)
24
- }
25
-
26
- public async findLatestSchemaMigration(): Promise<Migration | null> {
27
- const migrations = await this.getSchemaMigrations()
28
- return migrations.length > 0 ? migrations[migrations.length - 1] : null
29
- }
30
-
31
- public async findSchemaMigrationByVersion(version: string): Promise<Migration | null> {
32
- const migrations = await this.getSchemaMigrations()
33
- return migrations
34
- .find(it =>
35
- MigrationVersionHelper.extractVersion(it.version) === MigrationVersionHelper.extractVersion(version),
36
- )
37
- ?? null
38
- }
39
-
40
- private async convertMigrationFileToSchemaMigration(file: MigrationFile): Promise<Migration | null> {
41
- const result = await file.getContent()
42
- return isSchemaMigration(result) ? result : null
43
- }
44
- }
@@ -1,33 +0,0 @@
1
- import { Schema } from '@contember/schema'
2
- import { emptySchema } from '@contember/schema-utils'
3
- import { SchemaMigrator } from '@contember/schema-migrations'
4
- import { MigrationsResolver } from './MigrationsResolver'
5
-
6
- export class SchemaVersionBuilder {
7
- constructor(
8
- private readonly migrationsResolver: MigrationsResolver,
9
- private readonly schemaMigrator: SchemaMigrator,
10
- ) {}
11
-
12
- async buildSchema(targetVersion?: string): Promise<Schema> {
13
- return this.buildSchemaAdvanced(emptySchema, version => !targetVersion || version <= targetVersion)
14
- }
15
-
16
- async buildSchemaUntil(targetVersion: string): Promise<Schema> {
17
- return this.buildSchemaAdvanced(emptySchema, version => version < targetVersion)
18
- }
19
-
20
- async continue(schema: Schema, previousVersion: string | null, targetVersion: string): Promise<Schema> {
21
- return this.buildSchemaAdvanced(schema, version => version <= targetVersion && version > (previousVersion || ''))
22
- }
23
-
24
- public async buildSchemaAdvanced(initialSchema: Schema, condition: (version: string) => boolean): Promise<Schema> {
25
- return (await this.migrationsResolver.getSchemaMigrations())
26
- .filter(({ version }) => condition(version))
27
- .reduce<Schema>(
28
- (schema, { modifications, formatVersion }) =>
29
- this.schemaMigrator.applyModifications(schema, modifications, formatVersion),
30
- initialSchema,
31
- )
32
- }
33
- }
@@ -1,150 +0,0 @@
1
- import { GraphQLClient } from 'graphql-request'
2
- import { ExecutedMigration, ExecutedMigrationInfo } from '../migrations/migrations'
3
-
4
- export type MigrateError = {
5
- readonly code: MigrateErrorCode
6
- readonly migration: string
7
- readonly message: string
8
- }
9
-
10
- export enum MigrateErrorCode {
11
- MustFollowLatest = 'MUST_FOLLOW_LATEST',
12
- AlreadyExecuted = 'ALREADY_EXECUTED',
13
- InvalidFormat = 'INVALID_FORMAT',
14
- InvalidSchema = 'INVALID_SCHEMA',
15
- MigrationFailed = 'MIGRATION_FAILED',
16
- }
17
-
18
- export type MigrateResponse = {
19
- readonly ok: boolean
20
- readonly errors: MigrateError[]
21
- }
22
-
23
- export const createSystemUrl = (baseUrl: string, projectName: string) => {
24
- if (baseUrl.endsWith('/')) {
25
- baseUrl = baseUrl.substring(0, baseUrl.length - 1)
26
- }
27
- return baseUrl + '/system/' + projectName
28
- }
29
-
30
- export class SystemClient {
31
- constructor(private readonly apiClient: GraphQLClient) {}
32
-
33
- public static create(baseUrl: string, projectName: string, apiToken: string): SystemClient {
34
- const graphqlClient = new GraphQLClient(createSystemUrl(baseUrl, projectName), {
35
- headers: {
36
- Authorization: `Bearer ${apiToken}`,
37
- },
38
- })
39
- return new SystemClient(graphqlClient)
40
- }
41
-
42
- public async migrate(migrations: any[], force = false): Promise<void> {
43
- const query = `
44
- mutation($migrations: [Migration!]!) {
45
- migrate: ${force ? 'forceMigrate' : 'migrate'}(migrations: $migrations) {
46
- ok
47
- errors {
48
- code
49
- migration
50
- message: developerMessage
51
- }
52
- }
53
- }
54
- `
55
- const result = (
56
- await this.apiClient.request<{
57
- migrate: { ok: boolean; errors: { code: MigrateErrorCode; migration: string; message: string }[] }
58
- }>(query, {
59
- migrations,
60
- })
61
- ).migrate
62
- if (!result.ok) {
63
- throw result.errors
64
- }
65
- }
66
-
67
- public async migrationDelete(version: string): Promise<void> {
68
- const query = `
69
- mutation($version: String!) {
70
- migrationDelete(migration: $version) {
71
- ok
72
- error {
73
- developerMessage
74
- }
75
- }
76
- }
77
- `
78
- const result = (
79
- await this.apiClient.request<{
80
- migrationDelete: { ok: boolean; error?: { developerMessage: string } }
81
- }>(query, {
82
- version,
83
- })
84
- ).migrationDelete
85
- if (!result.ok) {
86
- throw result.error?.developerMessage
87
- }
88
- }
89
-
90
- public async migrationModify(version: string, modification: Partial<any>): Promise<void> {
91
- const query = `
92
- mutation($version: String!, $modification: MigrationModification!) {
93
- migrationModify(migration: $version, modification: $modification) {
94
- ok
95
- error {
96
- developerMessage
97
- }
98
- }
99
- }
100
- `
101
- const result = (
102
- await this.apiClient.request<{
103
- migrationModify: { ok: boolean; error?: { developerMessage: string } }
104
- }>(query, {
105
- version,
106
- modification,
107
- })
108
- ).migrationModify
109
- if (!result.ok) {
110
- throw result.error?.developerMessage
111
- }
112
- }
113
-
114
- public async listExecutedMigrations(): Promise<ExecutedMigrationInfo[]> {
115
- const query = `query {
116
- executedMigrations {
117
- name
118
- version
119
- formatVersion
120
- checksum
121
- executedAt
122
- }
123
- }`
124
- return (
125
- await this.apiClient.request<{
126
- executedMigrations: ExecutedMigrationInfo[]
127
- }>(query, {})
128
- ).executedMigrations.map(it => ({ ...it, executedAt: new Date(it.executedAt) }))
129
- }
130
-
131
- public async getExecutedMigration(version: string): Promise<ExecutedMigration> {
132
- const query = `query($version: String!) {
133
- executedMigrations(version: $version) {
134
- name
135
- version
136
- formatVersion
137
- checksum
138
- executedAt
139
- modifications
140
- }
141
- }`
142
- return (
143
- (
144
- await this.apiClient.request<{
145
- executedMigrations: ExecutedMigration[]
146
- }>(query, { version })
147
- ).executedMigrations.map(it => ({ ...it, executedAt: new Date(it.executedAt) }))[0] || null
148
- )
149
- }
150
- }