@contember/create 2.2.0-alpha.3 → 2.2.0-alpha.4
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/CLAUDE.md +22 -0
- package/dist/development/run.cjs +7 -7
- package/dist/development/run.cjs.map +1 -1
- package/dist/development/run.js +8 -8
- package/dist/development/run.js.map +1 -1
- package/dist/development/src/commands/WorkspaceCreateCommand.cjs +27 -13
- package/dist/development/src/commands/WorkspaceCreateCommand.cjs.map +1 -1
- package/dist/development/src/commands/WorkspaceCreateCommand.js +28 -14
- package/dist/development/src/commands/WorkspaceCreateCommand.js.map +1 -1
- package/dist/development/src/lib/TemplateInstaller.cjs +72 -60
- package/dist/development/src/lib/TemplateInstaller.cjs.map +1 -1
- package/dist/development/src/lib/TemplateInstaller.js +72 -60
- package/dist/development/src/lib/TemplateInstaller.js.map +1 -1
- package/dist/production/run.cjs +7 -7
- package/dist/production/run.cjs.map +1 -1
- package/dist/production/run.js +8 -8
- package/dist/production/run.js.map +1 -1
- package/dist/production/src/commands/WorkspaceCreateCommand.cjs +27 -13
- package/dist/production/src/commands/WorkspaceCreateCommand.cjs.map +1 -1
- package/dist/production/src/commands/WorkspaceCreateCommand.js +28 -14
- package/dist/production/src/commands/WorkspaceCreateCommand.js.map +1 -1
- package/dist/production/src/lib/TemplateInstaller.cjs +72 -60
- package/dist/production/src/lib/TemplateInstaller.cjs.map +1 -1
- package/dist/production/src/lib/TemplateInstaller.js +72 -60
- package/dist/production/src/lib/TemplateInstaller.js.map +1 -1
- package/dist/types/commands/WorkspaceCreateCommand.d.ts +15 -3
- package/dist/types/commands/WorkspaceCreateCommand.d.ts.map +1 -1
- package/dist/types/lib/FileSystem.d.ts.map +1 -1
- package/dist/types/lib/TemplateInstaller.d.ts +9 -1
- package/dist/types/lib/TemplateInstaller.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/resources/templates/default/README.md +3 -1
- package/resources/templates/default/admin/app/components/navigation.tsx +95 -8
- package/resources/templates/default/admin/app/pages/tenant.tsx +328 -77
- package/resources/templates/default/admin/entrypoint/components/login.tsx +1 -1
- package/resources/templates/default/admin/entrypoint/pages/magicLink.tsx +1 -1
- package/resources/templates/default/admin/entrypoint/pages/magicLinkSent.tsx +1 -1
- package/resources/templates/default/admin/entrypoint/pages/passwordReset.tsx +1 -1
- package/resources/templates/default/admin/entrypoint/pages/resetRequest.tsx +1 -1
- package/resources/templates/default/admin/entrypoint/pages/resetRequestSuccess.tsx +1 -1
- package/resources/templates/default/admin/lib/tenant/config/auth-policy-list.tsx +86 -0
- package/resources/templates/default/admin/lib/tenant/config/common.tsx +71 -0
- package/resources/templates/default/admin/lib/tenant/config/identity-provider-list.tsx +113 -0
- package/resources/templates/default/admin/lib/tenant/config/index.ts +5 -0
- package/resources/templates/default/admin/lib/tenant/config/mail-template-list.tsx +108 -0
- package/resources/templates/default/admin/lib/tenant/config/tenant-config-view.tsx +182 -0
- package/resources/templates/default/admin/lib/tenant/dict.ts +443 -4
- package/resources/templates/default/admin/lib/tenant/errors.ts +10 -0
- package/resources/templates/default/admin/lib/tenant/forms/add-project-member.tsx +57 -0
- package/resources/templates/default/admin/lib/tenant/forms/change-my-profile.tsx +40 -0
- package/resources/templates/default/admin/lib/tenant/forms/change-profile.tsx +55 -0
- package/resources/templates/default/admin/lib/tenant/forms/common.tsx +9 -2
- package/resources/templates/default/admin/lib/tenant/forms/create-global-api-key.tsx +69 -0
- package/resources/templates/default/admin/lib/tenant/forms/global-roles-control.tsx +113 -0
- package/resources/templates/default/admin/lib/tenant/forms/index.ts +11 -0
- package/resources/templates/default/admin/lib/tenant/forms/invite.tsx +42 -4
- package/resources/templates/default/admin/lib/tenant/forms/login.tsx +4 -10
- package/resources/templates/default/admin/lib/tenant/forms/memberships-control.tsx +3 -1
- package/resources/templates/default/admin/lib/tenant/forms/mfa-code-fields.tsx +81 -0
- package/resources/templates/default/admin/lib/tenant/forms/passwordless-signin.tsx +4 -10
- package/resources/templates/default/admin/lib/tenant/forms/set-person-password.tsx +58 -0
- package/resources/templates/default/admin/lib/tenant/forms/set-project-secret.tsx +59 -0
- package/resources/templates/default/admin/lib/tenant/forms/sign-up.tsx +73 -0
- package/resources/templates/default/admin/lib/tenant/hooks/index.ts +2 -0
- package/resources/templates/default/admin/lib/tenant/idp/identity-provider-connections.tsx +124 -0
- package/resources/templates/default/admin/lib/tenant/idp/index.ts +1 -0
- package/resources/templates/default/admin/lib/tenant/index.ts +7 -0
- package/resources/templates/default/admin/lib/tenant/listing/api-key-list.tsx +160 -22
- package/resources/templates/default/admin/lib/tenant/listing/auth-log-list.tsx +179 -0
- package/resources/templates/default/admin/lib/tenant/listing/global-api-key-list.tsx +149 -0
- package/resources/templates/default/admin/lib/tenant/listing/index.ts +7 -0
- package/resources/templates/default/admin/lib/tenant/listing/member-delete-dialog.tsx +1 -1
- package/resources/templates/default/admin/lib/tenant/listing/member-list.tsx +20 -11
- package/resources/templates/default/admin/lib/tenant/listing/mfa.tsx +29 -0
- package/resources/templates/default/admin/lib/tenant/listing/person-actions.tsx +179 -0
- package/resources/templates/default/admin/lib/tenant/listing/person-detail.tsx +141 -0
- package/resources/templates/default/admin/lib/tenant/listing/person-list.tsx +13 -2
- package/resources/templates/default/admin/lib/tenant/listing/persons-list.tsx +128 -0
- package/resources/templates/default/admin/lib/tenant/listing/project-secret-list.tsx +82 -0
- package/resources/templates/default/admin/lib/tenant/otp/backup-codes.tsx +81 -0
- package/resources/templates/default/admin/lib/tenant/otp/email-otp-setup.tsx +133 -0
- package/resources/templates/default/admin/lib/tenant/otp/index.ts +2 -0
- package/resources/templates/default/admin/lib/tenant/security/index.ts +2 -0
- package/resources/templates/default/admin/lib/tenant/security/passwordless-toggle.tsx +63 -0
- package/resources/templates/default/admin/lib/tenant/security/session-list.tsx +143 -0
- package/resources/templates/default/admin/package.json +1 -0
- package/resources/templates/default/docker/object-storage/README.md +13 -0
- package/resources/templates/default/docker/object-storage/identities.json +13 -0
- package/resources/templates/default/docker-compose.yaml +29 -7
- package/src/commands/WorkspaceCreateCommand.ts +42 -14
- package/src/lib/TemplateInstaller.ts +87 -69
- package/src/run.ts +8 -7
- package/tests/cases/unit/create-process.test.ts +66 -0
- package/tests/cases/unit/template-installer-cleanup.test.ts +150 -0
- package/tests/cases/unit/workspace-create.test.ts +81 -0
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import { join } from 'node:path'
|
|
2
|
-
import { FileSystem } from './FileSystem.js'
|
|
3
|
-
import jsyaml from 'js-yaml'
|
|
4
2
|
import degit from 'degit'
|
|
3
|
+
import jsyaml from 'js-yaml'
|
|
4
|
+
import { FileSystem } from './FileSystem.js'
|
|
5
|
+
|
|
6
|
+
interface TemplateInstallerOutput {
|
|
7
|
+
warn(message: string): void
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type CloneTemplate = (source: string, target: string) => Promise<void>
|
|
11
|
+
|
|
12
|
+
const cloneTemplate: CloneTemplate = async (source, target) => {
|
|
13
|
+
const emitter = degit(source, { cache: false, force: true, verbose: true })
|
|
14
|
+
await emitter.clone(target)
|
|
15
|
+
}
|
|
5
16
|
|
|
6
17
|
export class TemplateInstaller {
|
|
7
18
|
private localTemplates: Record<string, string>
|
|
@@ -9,6 +20,8 @@ export class TemplateInstaller {
|
|
|
9
20
|
constructor(
|
|
10
21
|
private readonly resourceDir: string,
|
|
11
22
|
private readonly fs: FileSystem,
|
|
23
|
+
private readonly output: TemplateInstallerOutput,
|
|
24
|
+
private readonly clone: CloneTemplate = cloneTemplate,
|
|
12
25
|
) {
|
|
13
26
|
this.localTemplates = {
|
|
14
27
|
['default']: join(this.resourceDir, 'templates/default'),
|
|
@@ -20,85 +33,90 @@ export class TemplateInstaller {
|
|
|
20
33
|
targetDir: string,
|
|
21
34
|
variables: Record<string, string> = {},
|
|
22
35
|
) => {
|
|
23
|
-
let
|
|
36
|
+
let temporaryTemplateDir: string | undefined
|
|
37
|
+
let remoteTemplate: string | undefined
|
|
24
38
|
|
|
25
39
|
if (this.localTemplates[template]) {
|
|
26
40
|
template = this.localTemplates[template]
|
|
27
41
|
} else {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
await emitter.clone(tmpDir)
|
|
42
|
+
remoteTemplate = template
|
|
43
|
+
temporaryTemplateDir = await this.fs.createTempDir()
|
|
44
|
+
template = temporaryTemplateDir
|
|
45
|
+
}
|
|
33
46
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
47
|
+
try {
|
|
48
|
+
if (remoteTemplate !== undefined) {
|
|
49
|
+
try {
|
|
50
|
+
await this.clone(remoteTemplate, template)
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throw new Error(`Failed to clone template from repository: ${error}`)
|
|
40
53
|
}
|
|
41
|
-
|
|
42
|
-
template = tmpDir
|
|
43
|
-
} catch (error) {
|
|
44
|
-
throw new Error(`Failed to clone template from repository: ${error}`)
|
|
45
54
|
}
|
|
46
|
-
}
|
|
47
55
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
})
|
|
56
|
+
const templateConfigFile = join(template, 'contember.template.yaml')
|
|
57
|
+
if (!(await this.fs.pathExists(templateConfigFile))) {
|
|
58
|
+
throw new Error(`${template} is not a Contember template`)
|
|
59
|
+
}
|
|
60
|
+
const config = (await this.readYaml(templateConfigFile)) as {
|
|
61
|
+
type?: string
|
|
62
|
+
remove?: string[]
|
|
63
|
+
patchPackageJson?: boolean
|
|
64
|
+
rename?: Record<string, string>
|
|
65
|
+
copy?: Record<string, string>
|
|
66
|
+
replaceVariables?: string[]
|
|
67
|
+
}
|
|
68
|
+
const nodeModulesDir = join(template, 'node_modules')
|
|
69
|
+
const skippedFiles = new Set([...(config.remove || []).map(it => join(template, it)), templateConfigFile])
|
|
70
|
+
if (await this.fs.pathExists(targetDir)) {
|
|
71
|
+
throw `${targetDir} already exists`
|
|
72
|
+
}
|
|
73
|
+
await this.fs.copy(template, targetDir, {
|
|
74
|
+
filter: src => !src.startsWith(nodeModulesDir) && !skippedFiles.has(src),
|
|
75
|
+
})
|
|
69
76
|
|
|
70
|
-
|
|
77
|
+
if (config.patchPackageJson) {
|
|
78
|
+
await this.replaceFileContent(join(targetDir, 'package.json'), content => {
|
|
79
|
+
const {
|
|
80
|
+
name,
|
|
81
|
+
version,
|
|
82
|
+
'scripts-template': scripts,
|
|
83
|
+
scripts: _nullScripts,
|
|
84
|
+
license,
|
|
85
|
+
...json
|
|
86
|
+
} = JSON.parse(content)
|
|
87
|
+
return JSON.stringify({ scripts, ...json }, null, ' ')
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
for (const [source, target] of Object.entries(config.rename || {})) {
|
|
91
|
+
await this.fs.rename(join(targetDir, source), join(targetDir, target))
|
|
92
|
+
}
|
|
93
|
+
for (const [source, target] of Object.entries(config.copy || {})) {
|
|
94
|
+
await this.fs.copy(join(targetDir, source), join(targetDir, target))
|
|
95
|
+
}
|
|
71
96
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
for (const [source, target] of Object.entries(config.copy || {})) {
|
|
89
|
-
await this.fs.copy(join(targetDir, source), join(targetDir, target))
|
|
97
|
+
for (const file of config.replaceVariables || []) {
|
|
98
|
+
const path = join(targetDir, file)
|
|
99
|
+
if (!(await this.fs.pathExists(path))) {
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
await this.replaceFileContent(path, content =>
|
|
103
|
+
Object.entries(variables).reduce(
|
|
104
|
+
(content, [key, value]) => content.replace(new RegExp(`{${key}}`, 'g'), value),
|
|
105
|
+
content,
|
|
106
|
+
))
|
|
107
|
+
}
|
|
108
|
+
} finally {
|
|
109
|
+
if (temporaryTemplateDir !== undefined) {
|
|
110
|
+
await this.cleanupTemporaryTemplate(temporaryTemplateDir)
|
|
111
|
+
}
|
|
90
112
|
}
|
|
113
|
+
}
|
|
91
114
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
await this.replaceFileContent(path, content =>
|
|
98
|
-
Object.entries(variables).reduce(
|
|
99
|
-
(content, [key, value]) => content.replace(new RegExp(`{${key}}`, 'g'), value),
|
|
100
|
-
content,
|
|
101
|
-
))
|
|
115
|
+
private cleanupTemporaryTemplate = async (path: string): Promise<void> => {
|
|
116
|
+
try {
|
|
117
|
+
await this.fs.remove(path, { recursive: true, force: true })
|
|
118
|
+
} catch {
|
|
119
|
+
this.output.warn('Failed to clean up the temporary template directory.')
|
|
102
120
|
}
|
|
103
121
|
}
|
|
104
122
|
|
package/src/run.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { Application, CommandManager } from '@contember/cli-common'
|
|
2
|
+
import { Application, CommandManager, exitProcess, Output, readGlobalOptionsFromArgs, renderCliError } from '@contember/cli-common'
|
|
3
3
|
import { WorkspaceCreateCommand } from './commands/index.js'
|
|
4
|
+
import { FileSystem } from './lib/FileSystem.js'
|
|
4
5
|
import { TemplateInstaller } from './lib/TemplateInstaller.js'
|
|
5
6
|
import { resourcesDir } from './paths.js'
|
|
6
|
-
|
|
7
|
+
|
|
8
|
+
const output = new Output()
|
|
9
|
+
output.applyGlobalOptions(readGlobalOptionsFromArgs(process.argv.slice(2)))
|
|
7
10
|
;(async () => {
|
|
8
11
|
const commandManager = new CommandManager({
|
|
9
12
|
['workspace']: () =>
|
|
@@ -11,6 +14,7 @@ import { FileSystem } from './lib/FileSystem.js'
|
|
|
11
14
|
new TemplateInstaller(
|
|
12
15
|
resourcesDir,
|
|
13
16
|
new FileSystem(),
|
|
17
|
+
output,
|
|
14
18
|
),
|
|
15
19
|
),
|
|
16
20
|
})
|
|
@@ -19,9 +23,6 @@ import { FileSystem } from './lib/FileSystem.js'
|
|
|
19
23
|
if (nodeVersion && Number(nodeVersion[1]) < 18) {
|
|
20
24
|
throw `Node >= 18 is required`
|
|
21
25
|
}
|
|
22
|
-
const app = new Application(commandManager, `Contember installer
|
|
26
|
+
const app = new Application(commandManager, `Contember installer`, output)
|
|
23
27
|
await app.runCommand('workspace', process.argv.slice(2))
|
|
24
|
-
})().catch(
|
|
25
|
-
console.log(e)
|
|
26
|
-
process.exit(1)
|
|
27
|
-
})
|
|
28
|
+
})().catch(error => exitProcess(renderCliError(error, output)))
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import { mkdtemp, rm } from 'node:fs/promises'
|
|
4
|
+
import { tmpdir } from 'node:os'
|
|
5
|
+
import { join, resolve } from 'node:path'
|
|
6
|
+
|
|
7
|
+
const repoRoot = resolve(import.meta.dir, '../../../../..')
|
|
8
|
+
const sourceEntrypoint = join(repoRoot, 'packages/create/src/run.ts')
|
|
9
|
+
const builtEntrypoint = join(repoRoot, 'packages/create/dist/production/run.js')
|
|
10
|
+
const testBuiltCli = process.env.CONTEMBER_TEST_BUILT_CLI === '1'
|
|
11
|
+
const createInvocation = testBuiltCli ? ['node', builtEntrypoint] : ['bun', '--conditions=typescript', sourceEntrypoint]
|
|
12
|
+
const temporaryDirectories: string[] = []
|
|
13
|
+
|
|
14
|
+
afterEach(async () => {
|
|
15
|
+
await Promise.all(temporaryDirectories.splice(0).map(path => rm(path, { recursive: true, force: true })))
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const runCreate = async (cwd: string, args: string[]) => {
|
|
19
|
+
if (testBuiltCli && !existsSync(builtEntrypoint)) {
|
|
20
|
+
throw new Error(`Built create entrypoint is missing: ${builtEntrypoint}`)
|
|
21
|
+
}
|
|
22
|
+
const process = Bun.spawn([...createInvocation, ...args], {
|
|
23
|
+
cwd,
|
|
24
|
+
env: { ...Bun.env, NO_COLOR: '1' },
|
|
25
|
+
stdin: 'ignore',
|
|
26
|
+
stdout: 'pipe',
|
|
27
|
+
stderr: 'pipe',
|
|
28
|
+
})
|
|
29
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
30
|
+
new Response(process.stdout).text(),
|
|
31
|
+
new Response(process.stderr).text(),
|
|
32
|
+
process.exited,
|
|
33
|
+
])
|
|
34
|
+
return { stdout, stderr, exitCode }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe('@contember/create process output', () => {
|
|
38
|
+
test('writes a successful JSON result to stdout only', async () => {
|
|
39
|
+
const cwd = await mkdtemp(join(tmpdir(), 'contember-create-process-'))
|
|
40
|
+
temporaryDirectories.push(cwd)
|
|
41
|
+
|
|
42
|
+
const result = await runCreate(cwd, ['demo', '--json'])
|
|
43
|
+
|
|
44
|
+
expect(result.exitCode).toBe(0)
|
|
45
|
+
expect(result.stderr).toBe('')
|
|
46
|
+
expect(JSON.parse(result.stdout)).toMatchObject({
|
|
47
|
+
projectName: 'demo',
|
|
48
|
+
projectDirectory: join(cwd, 'demo'),
|
|
49
|
+
template: 'default',
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('writes a structured JSON error to stderr only', async () => {
|
|
54
|
+
const cwd = await mkdtemp(join(tmpdir(), 'contember-create-process-'))
|
|
55
|
+
temporaryDirectories.push(cwd)
|
|
56
|
+
|
|
57
|
+
const result = await runCreate(cwd, ['--json'])
|
|
58
|
+
|
|
59
|
+
expect(result.exitCode).toBe(1)
|
|
60
|
+
expect(result.stdout).toBe('')
|
|
61
|
+
expect(JSON.parse(result.stderr)).toMatchObject({
|
|
62
|
+
ok: false,
|
|
63
|
+
error: { retryable: false },
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
})
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { Output, OutputMode, OutputStream } from '@contember/cli-common'
|
|
6
|
+
import { FileSystem } from '../../../src/lib/FileSystem.js'
|
|
7
|
+
import { TemplateInstaller } from '../../../src/lib/TemplateInstaller.js'
|
|
8
|
+
|
|
9
|
+
const temporaryDirectories: string[] = []
|
|
10
|
+
|
|
11
|
+
afterEach(async () => {
|
|
12
|
+
await Promise.all(temporaryDirectories.splice(0).map(path => rm(path, { recursive: true, force: true })))
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
class CapturingStream implements OutputStream {
|
|
16
|
+
public chunks: string[] = []
|
|
17
|
+
public readonly isTty = false
|
|
18
|
+
|
|
19
|
+
public write(text: string): void {
|
|
20
|
+
this.chunks.push(text)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public get text(): string {
|
|
24
|
+
return this.chunks.join('')
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
class CleanupFileSystem extends FileSystem {
|
|
29
|
+
public temporaryDirectory: string | undefined
|
|
30
|
+
public cleanupAttempts = 0
|
|
31
|
+
public cleanupCompleted = false
|
|
32
|
+
|
|
33
|
+
public constructor(private readonly failCleanup: boolean) {
|
|
34
|
+
super()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public override createTempDir = async (): Promise<string> => {
|
|
38
|
+
this.temporaryDirectory = await mkdtemp(join(tmpdir(), 'contember-template-installer-'))
|
|
39
|
+
temporaryDirectories.push(this.temporaryDirectory)
|
|
40
|
+
return this.temporaryDirectory
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public override remove: FileSystem['remove'] = async (path, options) => {
|
|
44
|
+
this.cleanupAttempts++
|
|
45
|
+
if (this.failCleanup) {
|
|
46
|
+
throw new Error('cleanup secret')
|
|
47
|
+
}
|
|
48
|
+
await rm(path, options)
|
|
49
|
+
await Promise.resolve()
|
|
50
|
+
this.cleanupCompleted = true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const createOutput = (mode: OutputMode = 'human') => {
|
|
55
|
+
const stdout = new CapturingStream()
|
|
56
|
+
const stderr = new CapturingStream()
|
|
57
|
+
const output = new Output({ stdout, stderr })
|
|
58
|
+
output.setMode(mode)
|
|
59
|
+
return { output, stdout, stderr }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const createTarget = async (): Promise<string> => {
|
|
63
|
+
const root = await mkdtemp(join(tmpdir(), 'contember-template-target-'))
|
|
64
|
+
temporaryDirectories.push(root)
|
|
65
|
+
return join(root, 'project')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const writeValidTemplate = async (directory: string): Promise<void> => {
|
|
69
|
+
await writeFile(join(directory, 'contember.template.yaml'), '{}')
|
|
70
|
+
await writeFile(join(directory, 'README.md'), 'template')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe('TemplateInstaller remote cleanup', () => {
|
|
74
|
+
test('awaits cleanup after a successful remote installation', async () => {
|
|
75
|
+
const fileSystem = new CleanupFileSystem(false)
|
|
76
|
+
const { output, stderr } = createOutput()
|
|
77
|
+
const installer = new TemplateInstaller('/unused', fileSystem, output, async (_source, directory) => {
|
|
78
|
+
await writeValidTemplate(directory)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
await installer.installTemplate('owner/repository', await createTarget())
|
|
82
|
+
|
|
83
|
+
expect(fileSystem.cleanupAttempts).toBe(1)
|
|
84
|
+
expect(fileSystem.cleanupCompleted).toBe(true)
|
|
85
|
+
expect(await fileSystem.pathExists(fileSystem.temporaryDirectory ?? '')).toBe(false)
|
|
86
|
+
expect(stderr.text).toBe('')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('preserves the primary failure when cleanup also fails', async () => {
|
|
90
|
+
const fileSystem = new CleanupFileSystem(true)
|
|
91
|
+
const { output, stderr } = createOutput()
|
|
92
|
+
const installer = new TemplateInstaller('/unused', fileSystem, output, async () => {
|
|
93
|
+
throw new Error('primary clone failure')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
await expect(installer.installTemplate('owner/repository', await createTarget())).rejects.toThrow('primary clone failure')
|
|
97
|
+
|
|
98
|
+
expect(fileSystem.cleanupAttempts).toBe(1)
|
|
99
|
+
expect(stderr.text).toContain('Failed to clean up the temporary template directory.')
|
|
100
|
+
expect(stderr.text).not.toContain('cleanup secret')
|
|
101
|
+
expect(stderr.text).not.toContain('owner/repository')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('preserves a template validation failure when cleanup also fails', async () => {
|
|
105
|
+
const fileSystem = new CleanupFileSystem(true)
|
|
106
|
+
const { output, stderr } = createOutput()
|
|
107
|
+
const installer = new TemplateInstaller('/unused', fileSystem, output, async () => {})
|
|
108
|
+
|
|
109
|
+
await expect(installer.installTemplate('owner/repository', await createTarget())).rejects.toThrow('is not a Contember template')
|
|
110
|
+
|
|
111
|
+
expect(fileSystem.cleanupAttempts).toBe(1)
|
|
112
|
+
expect(stderr.text).toContain('Failed to clean up the temporary template directory.')
|
|
113
|
+
expect(stderr.text).not.toContain('cleanup secret')
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
for (const mode of ['json', 'quiet'] satisfies OutputMode[]) {
|
|
117
|
+
test(`suppresses cleanup warnings in ${mode} mode`, async () => {
|
|
118
|
+
const fileSystem = new CleanupFileSystem(true)
|
|
119
|
+
const { output, stdout, stderr } = createOutput(mode)
|
|
120
|
+
const installer = new TemplateInstaller('/unused', fileSystem, output, async (_source, directory) => {
|
|
121
|
+
await writeValidTemplate(directory)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
await installer.installTemplate('owner/repository', await createTarget())
|
|
125
|
+
|
|
126
|
+
expect(fileSystem.cleanupAttempts).toBe(1)
|
|
127
|
+
expect(stdout.text).toBe('')
|
|
128
|
+
expect(stderr.text).toBe('')
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
test('does not clone or clean up local templates', async () => {
|
|
133
|
+
const resourceDirectory = await mkdtemp(join(tmpdir(), 'contember-template-resources-'))
|
|
134
|
+
temporaryDirectories.push(resourceDirectory)
|
|
135
|
+
const templateDirectory = join(resourceDirectory, 'templates/default')
|
|
136
|
+
await mkdir(templateDirectory, { recursive: true })
|
|
137
|
+
await writeValidTemplate(templateDirectory)
|
|
138
|
+
const fileSystem = new CleanupFileSystem(false)
|
|
139
|
+
const { output } = createOutput()
|
|
140
|
+
let cloneCalled = false
|
|
141
|
+
const installer = new TemplateInstaller(resourceDirectory, fileSystem, output, async () => {
|
|
142
|
+
cloneCalled = true
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
await installer.installTemplate('default', await createTarget())
|
|
146
|
+
|
|
147
|
+
expect(cloneCalled).toBe(false)
|
|
148
|
+
expect(fileSystem.cleanupAttempts).toBe(0)
|
|
149
|
+
})
|
|
150
|
+
})
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { Output, OutputStream } from '@contember/cli-common'
|
|
3
|
+
import { TemplateInstaller } from '../../../src/lib/TemplateInstaller.js'
|
|
4
|
+
import { WorkspaceCreateCommand, WorkspaceCreateRuntime } from '../../../src/commands/WorkspaceCreateCommand.js'
|
|
5
|
+
|
|
6
|
+
class CapturingStream implements OutputStream {
|
|
7
|
+
public chunks: string[] = []
|
|
8
|
+
public readonly isTty = false
|
|
9
|
+
|
|
10
|
+
public write(text: string): void {
|
|
11
|
+
this.chunks.push(text)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public get text(): string {
|
|
15
|
+
return this.chunks.join('')
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const createOutput = () => {
|
|
20
|
+
const stdout = new CapturingStream()
|
|
21
|
+
const stderr = new CapturingStream()
|
|
22
|
+
return { output: new Output({ stdout, stderr }), stdout, stderr }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const createCommand = () => {
|
|
26
|
+
const installs: { template: string; targetDir: string; variables: Record<string, string> }[] = []
|
|
27
|
+
const installer: Pick<TemplateInstaller, 'installTemplate'> = {
|
|
28
|
+
installTemplate: async (template, targetDir, variables = {}) => {
|
|
29
|
+
installs.push({ template, targetDir, variables })
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
const runtime: WorkspaceCreateRuntime = {
|
|
33
|
+
cwd: () => '/work',
|
|
34
|
+
getPackageVersion: async () => '2.2.0-test',
|
|
35
|
+
detectPackageManager: () => 'bun',
|
|
36
|
+
}
|
|
37
|
+
return { command: new WorkspaceCreateCommand(installer, runtime), installs }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('WorkspaceCreateCommand output', () => {
|
|
41
|
+
test('--json returns stable created-project data', async () => {
|
|
42
|
+
const { command, installs } = createCommand()
|
|
43
|
+
const { output, stdout, stderr } = createOutput()
|
|
44
|
+
|
|
45
|
+
await command.run(['demo', '--template', 'custom', '--json'], output)
|
|
46
|
+
|
|
47
|
+
expect(JSON.parse(stdout.text)).toEqual({
|
|
48
|
+
projectName: 'demo',
|
|
49
|
+
projectDirectory: '/work/demo',
|
|
50
|
+
template: 'custom',
|
|
51
|
+
packageManager: 'bun',
|
|
52
|
+
})
|
|
53
|
+
expect(stderr.text).toBe('')
|
|
54
|
+
expect(installs).toEqual([{
|
|
55
|
+
template: 'custom',
|
|
56
|
+
targetDir: '/work/demo',
|
|
57
|
+
variables: { version: '2.2.0-test', projectName: 'demo', packageManager: 'bun' },
|
|
58
|
+
}])
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('--quiet returns only the created directory', async () => {
|
|
62
|
+
const { command } = createCommand()
|
|
63
|
+
const { output, stdout, stderr } = createOutput()
|
|
64
|
+
|
|
65
|
+
await command.run(['demo', '--quiet'], output)
|
|
66
|
+
|
|
67
|
+
expect(stdout.text).toBe('/work/demo\n')
|
|
68
|
+
expect(stderr.text).toBe('')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('human instructions sanitize project-controlled terminal sequences', async () => {
|
|
72
|
+
const { command } = createCommand()
|
|
73
|
+
const { output, stdout } = createOutput()
|
|
74
|
+
|
|
75
|
+
await command.run(['demo\u001b[31m', '--no-color'], output)
|
|
76
|
+
|
|
77
|
+
expect(stdout.text).toContain('Contember project successfully created in /work/demo[31m')
|
|
78
|
+
expect(stdout.text).not.toContain('\u001b')
|
|
79
|
+
expect(stdout.text).toContain('bun install')
|
|
80
|
+
})
|
|
81
|
+
})
|