@contember/create 2.2.0-alpha.2 → 2.2.0-alpha.20
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 +36 -23
- package/dist/development/src/commands/WorkspaceCreateCommand.cjs.map +1 -1
- package/dist/development/src/commands/WorkspaceCreateCommand.js +37 -24
- package/dist/development/src/commands/WorkspaceCreateCommand.js.map +1 -1
- package/dist/development/src/lib/FileSystem.cjs +10 -4
- package/dist/development/src/lib/FileSystem.cjs.map +1 -1
- package/dist/development/src/lib/FileSystem.js +8 -2
- package/dist/development/src/lib/FileSystem.js.map +1 -1
- package/dist/development/src/lib/TemplateInstaller.cjs +71 -60
- package/dist/development/src/lib/TemplateInstaller.cjs.map +1 -1
- package/dist/development/src/lib/TemplateInstaller.js +71 -60
- package/dist/development/src/lib/TemplateInstaller.js.map +1 -1
- package/dist/development/src/lib/fsErrors.cjs +7 -0
- package/dist/development/src/lib/fsErrors.cjs.map +1 -0
- package/dist/development/src/lib/fsErrors.js +7 -0
- package/dist/development/src/lib/fsErrors.js.map +1 -0
- package/dist/development/src/lib/projectPath.cjs +66 -0
- package/dist/development/src/lib/projectPath.cjs.map +1 -0
- package/dist/development/src/lib/projectPath.js +66 -0
- package/dist/development/src/lib/projectPath.js.map +1 -0
- package/dist/development/src/lib/remoteTemplate.cjs +108 -0
- package/dist/development/src/lib/remoteTemplate.cjs.map +1 -0
- package/dist/development/src/lib/remoteTemplate.js +108 -0
- package/dist/development/src/lib/remoteTemplate.js.map +1 -0
- package/dist/development/src/lib/tar.cjs +243 -0
- package/dist/development/src/lib/tar.cjs.map +1 -0
- package/dist/development/src/lib/tar.js +243 -0
- package/dist/development/src/lib/tar.js.map +1 -0
- 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 +36 -23
- package/dist/production/src/commands/WorkspaceCreateCommand.cjs.map +1 -1
- package/dist/production/src/commands/WorkspaceCreateCommand.js +37 -24
- package/dist/production/src/commands/WorkspaceCreateCommand.js.map +1 -1
- package/dist/production/src/lib/FileSystem.cjs +10 -4
- package/dist/production/src/lib/FileSystem.cjs.map +1 -1
- package/dist/production/src/lib/FileSystem.js +8 -2
- package/dist/production/src/lib/FileSystem.js.map +1 -1
- package/dist/production/src/lib/TemplateInstaller.cjs +71 -60
- package/dist/production/src/lib/TemplateInstaller.cjs.map +1 -1
- package/dist/production/src/lib/TemplateInstaller.js +71 -60
- package/dist/production/src/lib/TemplateInstaller.js.map +1 -1
- package/dist/production/src/lib/fsErrors.cjs +7 -0
- package/dist/production/src/lib/fsErrors.cjs.map +1 -0
- package/dist/production/src/lib/fsErrors.js +7 -0
- package/dist/production/src/lib/fsErrors.js.map +1 -0
- package/dist/production/src/lib/projectPath.cjs +66 -0
- package/dist/production/src/lib/projectPath.cjs.map +1 -0
- package/dist/production/src/lib/projectPath.js +66 -0
- package/dist/production/src/lib/projectPath.js.map +1 -0
- package/dist/production/src/lib/remoteTemplate.cjs +108 -0
- package/dist/production/src/lib/remoteTemplate.cjs.map +1 -0
- package/dist/production/src/lib/remoteTemplate.js +108 -0
- package/dist/production/src/lib/remoteTemplate.js.map +1 -0
- package/dist/production/src/lib/tar.cjs +243 -0
- package/dist/production/src/lib/tar.cjs.map +1 -0
- package/dist/production/src/lib/tar.js +243 -0
- package/dist/production/src/lib/tar.js.map +1 -0
- 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 +2 -0
- 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/lib/fsErrors.d.ts +3 -0
- package/dist/types/lib/fsErrors.d.ts.map +1 -0
- package/dist/types/lib/projectPath.d.ts +12 -0
- package/dist/types/lib/projectPath.d.ts.map +1 -0
- package/dist/types/lib/remoteTemplate.d.ts +15 -0
- package/dist/types/lib/remoteTemplate.d.ts.map +1 -0
- package/dist/types/lib/tar.d.ts +28 -0
- package/dist/types/lib/tar.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -7
- 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/editor/common/editor-inline-toolbar.tsx +3 -0
- package/resources/templates/default/admin/lib/editor/sortable-block.tsx +5 -4
- 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 +453 -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/lib/ui/label.tsx +25 -2
- package/resources/templates/default/admin/lib/ui/menu.tsx +1 -1
- package/resources/templates/default/admin/lib/ui/progress.tsx +75 -11
- package/resources/templates/default/admin/lib/ui/separator.tsx +27 -3
- package/resources/templates/default/admin/lib/ui/sidebar.tsx +1 -1
- package/resources/templates/default/admin/lib/ui/toggle.tsx +41 -2
- package/resources/templates/default/admin/lib/utils/classNames.ts +107 -0
- package/resources/templates/default/admin/lib/utils/cn.ts +3 -3
- package/resources/templates/default/admin/lib/utils/index.ts +1 -0
- package/resources/templates/default/admin/lib/utils/uic.tsx +2 -3
- package/resources/templates/default/admin/package.json +1 -7
- 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 +51 -24
- package/src/lib/FileSystem.ts +7 -2
- package/src/lib/TemplateInstaller.ts +87 -69
- package/src/lib/fsErrors.ts +2 -0
- package/src/lib/projectPath.ts +79 -0
- package/src/lib/remoteTemplate.ts +130 -0
- package/src/lib/tar.ts +283 -0
- package/src/run.ts +8 -7
- package/tests/cases/unit/create-process.test.ts +66 -0
- package/tests/cases/unit/fileSystem.test.ts +14 -0
- package/tests/cases/unit/fixtures/template.tar.gz +0 -0
- package/tests/cases/unit/remoteTemplate.test.ts +277 -0
- package/tests/cases/unit/template-installer-cleanup.test.ts +150 -0
- package/tests/cases/unit/template-installer-containment.test.ts +133 -0
- package/tests/cases/unit/template-installer-symlinks.test.ts +37 -0
- package/tests/cases/unit/workspace-create.test.ts +81 -0
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,14 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { rm } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { dirname } from 'node:path'
|
|
5
|
+
import { FileSystem } from '../../../src/lib/FileSystem.js'
|
|
6
|
+
|
|
7
|
+
test('creates the temporary directory inside the OS temp directory', async () => {
|
|
8
|
+
const dir = await new FileSystem().createTempDir()
|
|
9
|
+
try {
|
|
10
|
+
expect(dirname(dir)).toBe(tmpdir())
|
|
11
|
+
} finally {
|
|
12
|
+
await rm(dir, { recursive: true, force: true })
|
|
13
|
+
}
|
|
14
|
+
})
|
|
Binary file
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { readFileSync } from 'node:fs'
|
|
3
|
+
import { lstat, mkdir, mkdtemp, readdir, readFile, readlink, rm, symlink } from 'node:fs/promises'
|
|
4
|
+
import { tmpdir } from 'node:os'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { downloadTemplate, type Fetch, parseTemplateSource, templateArchiveUrl } from '../../../src/lib/remoteTemplate.js'
|
|
7
|
+
import { extractTar, parseTar, type TarEntry } from '../../../src/lib/tar.js'
|
|
8
|
+
|
|
9
|
+
const fixture = readFileSync(new URL('./fixtures/template.tar.gz', import.meta.url))
|
|
10
|
+
|
|
11
|
+
const tempDirs: string[] = []
|
|
12
|
+
const createTempDir = async (): Promise<string> => {
|
|
13
|
+
const dir = await mkdtemp(join(tmpdir(), 'contember-remote-template-test-'))
|
|
14
|
+
tempDirs.push(dir)
|
|
15
|
+
return dir
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
afterEach(async () => {
|
|
19
|
+
await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })))
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const listTree = async (root: string, dir = root): Promise<string[]> => {
|
|
23
|
+
const result: string[] = []
|
|
24
|
+
for (const name of (await readdir(dir)).sort()) {
|
|
25
|
+
const path = join(dir, name)
|
|
26
|
+
const stat = await lstat(path)
|
|
27
|
+
const relative = path.slice(root.length + 1)
|
|
28
|
+
if (stat.isSymbolicLink()) {
|
|
29
|
+
result.push(`${relative} -> ${await readlink(path)}`)
|
|
30
|
+
} else if (stat.isDirectory()) {
|
|
31
|
+
result.push(`${relative}/`, ...(await listTree(root, path)))
|
|
32
|
+
} else {
|
|
33
|
+
result.push(relative)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Builds a ustar archive; enough to express the hostile entries a real tar tool would refuse to create. */
|
|
40
|
+
const tar = (entries: readonly { path: string; type?: string; data?: string; link?: string }[]): Uint8Array => {
|
|
41
|
+
const blocks: Uint8Array[] = []
|
|
42
|
+
for (const { path, type = '0', data = '', link = '' } of entries) {
|
|
43
|
+
const header = new Uint8Array(512)
|
|
44
|
+
const write = (offset: number, value: string) => header.set(new TextEncoder().encode(value), offset)
|
|
45
|
+
const body = new TextEncoder().encode(data)
|
|
46
|
+
write(0, path)
|
|
47
|
+
write(100, '0000644\0')
|
|
48
|
+
write(124, body.length.toString(8).padStart(11, '0') + '\0')
|
|
49
|
+
write(156, type)
|
|
50
|
+
write(157, link)
|
|
51
|
+
write(257, 'ustar\u000000')
|
|
52
|
+
header.fill(0x20, 148, 156)
|
|
53
|
+
const checksum = header.reduce((sum, byte) => sum + byte, 0)
|
|
54
|
+
write(148, checksum.toString(8).padStart(6, '0') + '\0 ')
|
|
55
|
+
blocks.push(header, body, new Uint8Array((512 - (body.length % 512)) % 512))
|
|
56
|
+
}
|
|
57
|
+
blocks.push(new Uint8Array(1024))
|
|
58
|
+
return Buffer.concat(blocks)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const respond = (body: Uint8Array, status = 200): Fetch => async () => new Response(body, { status })
|
|
62
|
+
|
|
63
|
+
const redirectTo = (location: string, then: Fetch): Fetch => {
|
|
64
|
+
let redirected = false
|
|
65
|
+
return async (url, init) => {
|
|
66
|
+
if (redirected) {
|
|
67
|
+
return then(url, init)
|
|
68
|
+
}
|
|
69
|
+
redirected = true
|
|
70
|
+
return new Response(null, { status: 302, headers: { location } })
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
describe('parseTemplateSource', () => {
|
|
75
|
+
test('reads the degit source forms', () => {
|
|
76
|
+
expect(parseTemplateSource('owner/repo')).toStrictEqual({ host: 'github', user: 'owner', name: 'repo', subdir: [], ref: 'HEAD' })
|
|
77
|
+
expect(parseTemplateSource('owner/repo/templates/basic#v1.2')).toStrictEqual({
|
|
78
|
+
host: 'github',
|
|
79
|
+
user: 'owner',
|
|
80
|
+
name: 'repo',
|
|
81
|
+
subdir: ['templates', 'basic'],
|
|
82
|
+
ref: 'v1.2',
|
|
83
|
+
})
|
|
84
|
+
expect(parseTemplateSource('https://github.com/owner/repo.git').name).toBe('repo')
|
|
85
|
+
expect(parseTemplateSource('git@github.com:owner/repo#feature/x').ref).toBe('feature/x')
|
|
86
|
+
expect(parseTemplateSource('gitlab:owner/repo').host).toBe('gitlab')
|
|
87
|
+
expect(parseTemplateSource('bitbucket.org/owner/repo').host).toBe('bitbucket')
|
|
88
|
+
expect(parseTemplateSource('git.sr.ht/~owner/repo').host).toBe('git.sr.ht')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('rejects unknown hosts, unparsable sources and dot segments', () => {
|
|
92
|
+
expect(() => parseTemplateSource('example.com/owner/repo')).toThrow('supported from GitHub')
|
|
93
|
+
expect(() => parseTemplateSource('repo')).toThrow('Could not parse')
|
|
94
|
+
expect(() => parseTemplateSource('owner/repo/../x')).toThrow('Could not parse')
|
|
95
|
+
expect(() => parseTemplateSource('owner/repo#../main')).toThrow('Could not parse')
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('templateArchiveUrl', () => {
|
|
100
|
+
test('builds each host archive URL', () => {
|
|
101
|
+
const source = (host: 'github' | 'gitlab' | 'bitbucket' | 'git.sr.ht') => ({ host, user: 'o', name: 'r', subdir: [], ref: 'feature/x' })
|
|
102
|
+
expect(templateArchiveUrl(source('github'))).toBe('https://github.com/o/r/archive/feature/x.tar.gz')
|
|
103
|
+
expect(templateArchiveUrl(source('gitlab'))).toBe('https://gitlab.com/o/r/-/archive/feature/x/r-feature-x.tar.gz')
|
|
104
|
+
expect(templateArchiveUrl(source('bitbucket'))).toBe('https://bitbucket.org/o/r/get/feature/x.tar.gz')
|
|
105
|
+
expect(templateArchiveUrl(source('git.sr.ht'))).toBe('https://git.sr.ht/o/r/archive/feature/x.tar.gz')
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
describe('downloadTemplate', () => {
|
|
110
|
+
test('extracts a subdirectory of a git archive with its symlink, executable bit and long path', async () => {
|
|
111
|
+
const target = await createTempDir()
|
|
112
|
+
const requests: string[] = []
|
|
113
|
+
await downloadTemplate('owner/repo/templates/basic#main', target, async (url, init) => {
|
|
114
|
+
requests.push(`${url} ${init.redirect}`)
|
|
115
|
+
return requests.length === 1
|
|
116
|
+
? new Response(null, { status: 302, headers: { location: 'https://codeload.example.com/archive.tar.gz' } })
|
|
117
|
+
: respond(fixture)(url, init)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
expect(requests).toStrictEqual([
|
|
121
|
+
'https://github.com/owner/repo/archive/main.tar.gz manual',
|
|
122
|
+
'https://codeload.example.com/archive.tar.gz manual',
|
|
123
|
+
])
|
|
124
|
+
const deep = 'deep/'.repeat(12)
|
|
125
|
+
expect(await listTree(target)).toStrictEqual([
|
|
126
|
+
'bin/',
|
|
127
|
+
'bin/run.sh',
|
|
128
|
+
'contember.template.yaml',
|
|
129
|
+
...Array.from({ length: 12 }, (_, index) => `${'deep/'.repeat(index + 1)}`),
|
|
130
|
+
`${deep}file-with-a-long-name.txt`,
|
|
131
|
+
'link.yaml -> contember.template.yaml',
|
|
132
|
+
])
|
|
133
|
+
expect((await lstat(join(target, 'bin/run.sh'))).mode & 0o100).toBe(0o100)
|
|
134
|
+
expect(await readFile(join(target, deep, 'file-with-a-long-name.txt'), 'utf8')).toBe('long\n')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test('extracts the whole repository without its top-level directory', async () => {
|
|
138
|
+
const target = await createTempDir()
|
|
139
|
+
await downloadTemplate('owner/repo', target, respond(fixture))
|
|
140
|
+
expect((await readdir(target)).sort()).toStrictEqual(['README.md', 'templates'])
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('reports an HTTP error with the URL', async () => {
|
|
144
|
+
const target = await createTempDir()
|
|
145
|
+
await expect(downloadTemplate('owner/repo#nope', target, respond(new Uint8Array(), 404))).rejects.toThrow(
|
|
146
|
+
'Could not download https://github.com/owner/repo/archive/nope.tar.gz: HTTP 404',
|
|
147
|
+
)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('refuses a redirect hop to plain HTTP', async () => {
|
|
151
|
+
const target = await createTempDir()
|
|
152
|
+
const fetchArchive = redirectTo('http://example.com/a.tar.gz', respond(fixture))
|
|
153
|
+
await expect(downloadTemplate('owner/repo', target, fetchArchive)).rejects.toThrow('non-HTTPS')
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
test('stops after too many redirects', async () => {
|
|
157
|
+
const target = await createTempDir()
|
|
158
|
+
const loop: Fetch = async () => new Response(null, { status: 302, headers: { location: 'https://example.com/again' } })
|
|
159
|
+
await expect(downloadTemplate('owner/repo', target, loop)).rejects.toThrow('too many or invalid redirects')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('refuses an archive that is not gzip', async () => {
|
|
163
|
+
const target = await createTempDir()
|
|
164
|
+
await expect(downloadTemplate('owner/repo', target, respond(new TextEncoder().encode('<html>')))).rejects.toThrow('not a readable gzip')
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
describe('tar extraction', () => {
|
|
169
|
+
const extract = async (archive: Uint8Array) => {
|
|
170
|
+
const target = await createTempDir()
|
|
171
|
+
await extractTar(parseTar(archive), join(target, 'out'), { strip: 1 })
|
|
172
|
+
return target
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
test.each([
|
|
176
|
+
['a parent-directory path', [{ path: 'top/../../evil.txt', data: 'x' }]],
|
|
177
|
+
['an absolute path', [{ path: '/tmp/evil.txt', data: 'x' }]],
|
|
178
|
+
['a symlink leaving the template', [{ path: 'top/link', type: '2', link: '../../outside' }]],
|
|
179
|
+
['an absolute symlink', [{ path: 'top/link', type: '2', link: '/etc/passwd' }]],
|
|
180
|
+
['a Windows drive symlink', [{ path: 'top/link', type: '2', link: 'C:/Windows' }]],
|
|
181
|
+
['a backslash symlink', [{ path: 'top/link', type: '2', link: '..\\..\\outside' }]],
|
|
182
|
+
['a file below a symlink', [{ path: 'top/dir', type: '2', link: '.' }, { path: 'top/dir/file.txt', data: 'x' }]],
|
|
183
|
+
['a symlink chain leaving the template', [
|
|
184
|
+
{ path: 'top/d/l', type: '2', link: '..' },
|
|
185
|
+
{ path: 'top/esc', type: '2', link: 'd/l/..' },
|
|
186
|
+
{ path: 'top/esc/planted', type: '2', link: 'x' },
|
|
187
|
+
]],
|
|
188
|
+
])('refuses %s before writing anything', async (_, entries) => {
|
|
189
|
+
const target = await createTempDir()
|
|
190
|
+
// nested, so that a `..` escaping the output directory would still land inside `target`
|
|
191
|
+
await expect(extractTar(parseTar(tar(entries)), join(target, 'a/b/out'), { strip: 1 })).rejects.toThrow('Refusing')
|
|
192
|
+
expect(await readdir(target)).toStrictEqual([])
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
// A link the name checks cannot see stands in for an alias on a case-insensitive filesystem (`D/L` is `d/l`).
|
|
196
|
+
test.each([
|
|
197
|
+
['a file written through it', [{ path: 'top/alias/x.txt', data: 'x' }]],
|
|
198
|
+
['a symlink created through it', [{ path: 'top/alias/x', type: '2', link: 'y' }]],
|
|
199
|
+
['a symlink resolving through it', [{ path: 'top/esc', type: '2', link: 'alias/..' }]],
|
|
200
|
+
])('checks the filesystem for links the names do not show: %s', async (_, entries) => {
|
|
201
|
+
const target = await createTempDir()
|
|
202
|
+
const out = join(target, 'a/b/out')
|
|
203
|
+
await mkdir(out, { recursive: true })
|
|
204
|
+
await symlink('..', join(out, 'alias'))
|
|
205
|
+
await expect(extractTar(parseTar(tar(entries)), out, { strip: 1 })).rejects.toThrow('Refusing')
|
|
206
|
+
expect(await readdir(join(target, 'a/b'))).toStrictEqual(['out'])
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
test('refuses a too deeply nested path', async () => {
|
|
210
|
+
const entries: TarEntry[] = [{ type: 'file', path: `top/${'a/'.repeat(300)}x`, mode: 0o644, data: new Uint8Array() }]
|
|
211
|
+
await expect(extractTar(entries, join(await createTempDir(), 'out'), { strip: 1 })).rejects.toThrow('too deeply nested path')
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
test('refuses a symlink resolving through a file', async () => {
|
|
215
|
+
const archive = tar([{ path: 'top/f.txt', data: 'x' }, { path: 'top/link', type: '2', link: 'f.txt/x/../g' }])
|
|
216
|
+
await expect(extract(archive)).rejects.toThrow('symlink resolving through a file')
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
test('refuses an overlong path', async () => {
|
|
220
|
+
const entries: TarEntry[] = [{ type: 'file', path: `top/${'a/'.repeat(5000)}x`, mode: 0o644, data: new Uint8Array() }]
|
|
221
|
+
await expect(extractTar(entries, join(await createTempDir(), 'out'), { strip: 1 })).rejects.toThrow('overlong path')
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test('allows a symlink to a symlink inside the template', async () => {
|
|
225
|
+
const target = await extract(tar([
|
|
226
|
+
{ path: 'top/c.txt', data: 'c' },
|
|
227
|
+
{ path: 'top/b', type: '2', link: 'c.txt' },
|
|
228
|
+
{ path: 'top/a', type: '2', link: 'b' },
|
|
229
|
+
]))
|
|
230
|
+
expect(await readFile(join(target, 'out/a'), 'utf8')).toBe('c')
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
test('ignores unusual names outside the requested subdirectory', async () => {
|
|
234
|
+
const target = await createTempDir()
|
|
235
|
+
const entries = parseTar(tar([{ path: 'top/other/back\\slash.txt', data: 'x' }, { path: 'top/tpl/a.txt', data: 'a' }]))
|
|
236
|
+
await extractTar(entries, join(target, 'out'), { strip: 1, subdir: ['tpl'] })
|
|
237
|
+
expect(await readdir(join(target, 'out'))).toStrictEqual(['a.txt'])
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
test('reads GNU long names and long link targets', async () => {
|
|
241
|
+
const longName = `top/${'n'.repeat(120)}.txt`
|
|
242
|
+
const longLink = `${'n'.repeat(120)}.txt`
|
|
243
|
+
const target = await extract(tar([
|
|
244
|
+
{ path: '././@LongLink', type: 'L', data: `${longName}\0` },
|
|
245
|
+
{ path: 'top/truncated', data: 'long' },
|
|
246
|
+
{ path: '././@LongLink', type: 'K', data: `${longLink}\0` },
|
|
247
|
+
{ path: 'top/link', type: '2', link: 'truncated' },
|
|
248
|
+
]))
|
|
249
|
+
expect(await readFile(join(target, 'out', `${'n'.repeat(120)}.txt`), 'utf8')).toBe('long')
|
|
250
|
+
expect(await readlink(join(target, 'out/link'))).toBe(longLink)
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('rejects hard links and other entry types', () => {
|
|
254
|
+
expect(() => parseTar(tar([{ path: 'top/a', type: '1', link: 'top/b' }]))).toThrow('Unsupported tar entry')
|
|
255
|
+
expect(() => parseTar(tar([{ path: 'top/fifo', type: '6' }]))).toThrow('Unsupported tar entry')
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
test('rejects a PAX size that is not a plain number', () => {
|
|
259
|
+
const record = '14 size=-1600\n'
|
|
260
|
+
expect(() => parseTar(tar([{ path: 'pax', type: 'x', data: record }, { path: 'top/a', data: '' }]))).toThrow('unreadable PAX size')
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
test('rejects a corrupted header and a truncated archive', () => {
|
|
264
|
+
const archive = tar([{ path: 'top/a.txt', data: 'hello' }])
|
|
265
|
+
const corrupted = new Uint8Array(archive)
|
|
266
|
+
corrupted[0] = 'X'.charCodeAt(0)
|
|
267
|
+
expect(() => parseTar(corrupted)).toThrow('checksum')
|
|
268
|
+
expect(() => parseTar(archive.subarray(0, 512))).toThrow('exceeds the archive')
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test('keeps paths from PAX headers', async () => {
|
|
272
|
+
const pax = `path=top/${'p'.repeat(150)}.txt\n`
|
|
273
|
+
const record = `${pax.length + 4} ${pax}`
|
|
274
|
+
const target = await extract(tar([{ path: 'pax', type: 'x', data: record }, { path: 'ignored', data: 'pax' }]))
|
|
275
|
+
expect(await readFile(join(target, 'out', `${'p'.repeat(150)}.txt`), 'utf8')).toBe('pax')
|
|
276
|
+
})
|
|
277
|
+
})
|
|
@@ -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
|
+
})
|