@contember/create 2.2.0-alpha.17 → 2.2.0-alpha.19

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 (95) hide show
  1. package/dist/development/src/commands/WorkspaceCreateCommand.cjs +13 -14
  2. package/dist/development/src/commands/WorkspaceCreateCommand.cjs.map +1 -1
  3. package/dist/development/src/commands/WorkspaceCreateCommand.js +14 -15
  4. package/dist/development/src/commands/WorkspaceCreateCommand.js.map +1 -1
  5. package/dist/development/src/lib/FileSystem.cjs +10 -4
  6. package/dist/development/src/lib/FileSystem.cjs.map +1 -1
  7. package/dist/development/src/lib/FileSystem.js +8 -2
  8. package/dist/development/src/lib/FileSystem.js.map +1 -1
  9. package/dist/development/src/lib/TemplateInstaller.cjs +13 -14
  10. package/dist/development/src/lib/TemplateInstaller.cjs.map +1 -1
  11. package/dist/development/src/lib/TemplateInstaller.js +7 -8
  12. package/dist/development/src/lib/TemplateInstaller.js.map +1 -1
  13. package/dist/development/src/lib/fsErrors.cjs +7 -0
  14. package/dist/development/src/lib/fsErrors.cjs.map +1 -0
  15. package/dist/development/src/lib/fsErrors.js +7 -0
  16. package/dist/development/src/lib/fsErrors.js.map +1 -0
  17. package/dist/development/src/lib/projectPath.cjs +66 -0
  18. package/dist/development/src/lib/projectPath.cjs.map +1 -0
  19. package/dist/development/src/lib/projectPath.js +66 -0
  20. package/dist/development/src/lib/projectPath.js.map +1 -0
  21. package/dist/development/src/lib/remoteTemplate.cjs +108 -0
  22. package/dist/development/src/lib/remoteTemplate.cjs.map +1 -0
  23. package/dist/development/src/lib/remoteTemplate.js +108 -0
  24. package/dist/development/src/lib/remoteTemplate.js.map +1 -0
  25. package/dist/development/src/lib/tar.cjs +243 -0
  26. package/dist/development/src/lib/tar.cjs.map +1 -0
  27. package/dist/development/src/lib/tar.js +243 -0
  28. package/dist/development/src/lib/tar.js.map +1 -0
  29. package/dist/production/src/commands/WorkspaceCreateCommand.cjs +13 -14
  30. package/dist/production/src/commands/WorkspaceCreateCommand.cjs.map +1 -1
  31. package/dist/production/src/commands/WorkspaceCreateCommand.js +14 -15
  32. package/dist/production/src/commands/WorkspaceCreateCommand.js.map +1 -1
  33. package/dist/production/src/lib/FileSystem.cjs +10 -4
  34. package/dist/production/src/lib/FileSystem.cjs.map +1 -1
  35. package/dist/production/src/lib/FileSystem.js +8 -2
  36. package/dist/production/src/lib/FileSystem.js.map +1 -1
  37. package/dist/production/src/lib/TemplateInstaller.cjs +13 -14
  38. package/dist/production/src/lib/TemplateInstaller.cjs.map +1 -1
  39. package/dist/production/src/lib/TemplateInstaller.js +7 -8
  40. package/dist/production/src/lib/TemplateInstaller.js.map +1 -1
  41. package/dist/production/src/lib/fsErrors.cjs +7 -0
  42. package/dist/production/src/lib/fsErrors.cjs.map +1 -0
  43. package/dist/production/src/lib/fsErrors.js +7 -0
  44. package/dist/production/src/lib/fsErrors.js.map +1 -0
  45. package/dist/production/src/lib/projectPath.cjs +66 -0
  46. package/dist/production/src/lib/projectPath.cjs.map +1 -0
  47. package/dist/production/src/lib/projectPath.js +66 -0
  48. package/dist/production/src/lib/projectPath.js.map +1 -0
  49. package/dist/production/src/lib/remoteTemplate.cjs +108 -0
  50. package/dist/production/src/lib/remoteTemplate.cjs.map +1 -0
  51. package/dist/production/src/lib/remoteTemplate.js +108 -0
  52. package/dist/production/src/lib/remoteTemplate.js.map +1 -0
  53. package/dist/production/src/lib/tar.cjs +243 -0
  54. package/dist/production/src/lib/tar.cjs.map +1 -0
  55. package/dist/production/src/lib/tar.js +243 -0
  56. package/dist/production/src/lib/tar.js.map +1 -0
  57. package/dist/types/commands/WorkspaceCreateCommand.d.ts.map +1 -1
  58. package/dist/types/lib/FileSystem.d.ts +2 -0
  59. package/dist/types/lib/FileSystem.d.ts.map +1 -1
  60. package/dist/types/lib/TemplateInstaller.d.ts.map +1 -1
  61. package/dist/types/lib/fsErrors.d.ts +3 -0
  62. package/dist/types/lib/fsErrors.d.ts.map +1 -0
  63. package/dist/types/lib/projectPath.d.ts +12 -0
  64. package/dist/types/lib/projectPath.d.ts.map +1 -0
  65. package/dist/types/lib/remoteTemplate.d.ts +15 -0
  66. package/dist/types/lib/remoteTemplate.d.ts.map +1 -0
  67. package/dist/types/lib/tar.d.ts +28 -0
  68. package/dist/types/lib/tar.d.ts.map +1 -0
  69. package/dist/types/tsconfig.tsbuildinfo +1 -1
  70. package/package.json +5 -7
  71. package/resources/templates/default/admin/lib/editor/sortable-block.tsx +5 -4
  72. package/resources/templates/default/admin/lib/tenant/dict.ts +3 -0
  73. package/resources/templates/default/admin/lib/ui/label.tsx +25 -2
  74. package/resources/templates/default/admin/lib/ui/menu.tsx +1 -1
  75. package/resources/templates/default/admin/lib/ui/progress.tsx +75 -11
  76. package/resources/templates/default/admin/lib/ui/separator.tsx +27 -3
  77. package/resources/templates/default/admin/lib/ui/sidebar.tsx +1 -1
  78. package/resources/templates/default/admin/lib/ui/toggle.tsx +41 -2
  79. package/resources/templates/default/admin/lib/utils/classNames.ts +107 -0
  80. package/resources/templates/default/admin/lib/utils/cn.ts +3 -3
  81. package/resources/templates/default/admin/lib/utils/index.ts +1 -0
  82. package/resources/templates/default/admin/lib/utils/uic.tsx +2 -3
  83. package/resources/templates/default/admin/package.json +0 -7
  84. package/src/commands/WorkspaceCreateCommand.ts +14 -15
  85. package/src/lib/FileSystem.ts +7 -2
  86. package/src/lib/TemplateInstaller.ts +8 -8
  87. package/src/lib/fsErrors.ts +2 -0
  88. package/src/lib/projectPath.ts +79 -0
  89. package/src/lib/remoteTemplate.ts +130 -0
  90. package/src/lib/tar.ts +283 -0
  91. package/tests/cases/unit/fileSystem.test.ts +14 -0
  92. package/tests/cases/unit/fixtures/template.tar.gz +0 -0
  93. package/tests/cases/unit/remoteTemplate.test.ts +277 -0
  94. package/tests/cases/unit/template-installer-containment.test.ts +133 -0
  95. package/tests/cases/unit/template-installer-symlinks.test.ts +37 -0
@@ -1,23 +1,87 @@
1
- import * as ProgressPrimitive from '@radix-ui/react-progress'
1
+ import { Slot } from '@radix-ui/react-slot'
2
2
  import * as React from 'react'
3
+ import { ComponentPropsWithoutRef, createContext, forwardRef, useContext } from 'react'
3
4
  import { uic } from '../utils/index.js'
4
5
 
5
- const ProgressRoot = uic(ProgressPrimitive.Root, {
6
+ const DEFAULT_MAX = 100
7
+
8
+ type ProgressState = 'indeterminate' | 'complete' | 'loading'
9
+
10
+ const ProgressContext = createContext<{ value: number | null; max: number }>({ value: null, max: DEFAULT_MAX })
11
+
12
+ const getProgressState = (value: number | null, max: number): ProgressState =>
13
+ value === null ? 'indeterminate' : value === max ? 'complete' : 'loading'
14
+
15
+ const isValidMax = (max: number | undefined): max is number => typeof max === 'number' && !Number.isNaN(max) && max > 0
16
+
17
+ const isValidValue = (value: number | null, max: number): value is number =>
18
+ typeof value === 'number' && !Number.isNaN(value) && value <= max && value >= 0
19
+
20
+ const defaultGetValueLabel = (value: number, max: number) => `${Math.round((value / max) * 100)}%`
21
+
22
+ export type ProgressRootProps = ComponentPropsWithoutRef<'div'> & {
23
+ /** `null` or `undefined` means indeterminate. */
24
+ value?: number | null
25
+ max?: number
26
+ getValueLabel?: (value: number, max: number) => string
27
+ asChild?: boolean
28
+ }
29
+
30
+ const ProgressRootBase = forwardRef<HTMLDivElement, ProgressRootProps>(
31
+ ({ asChild, value: valueProp = null, max: maxProp, getValueLabel = defaultGetValueLabel, ...props }, ref) => {
32
+ const Comp = asChild ? Slot : 'div'
33
+ if ((maxProp || maxProp === 0) && !isValidMax(maxProp)) {
34
+ console.error(
35
+ `Invalid prop \`max\` of value \`${maxProp}\` supplied to \`Progress\`. Only numbers greater than 0 are valid. Defaulting to \`${DEFAULT_MAX}\`.`,
36
+ )
37
+ }
38
+ const max = isValidMax(maxProp) ? maxProp : DEFAULT_MAX
39
+ if (valueProp !== null && !isValidValue(valueProp, max)) {
40
+ console.error(
41
+ `Invalid prop \`value\` of value \`${valueProp}\` supplied to \`Progress\`. It must be between 0 and \`max\` (${max}), or null. Defaulting to \`null\`.`,
42
+ )
43
+ }
44
+ const value = isValidValue(valueProp, max) ? valueProp : null
45
+ return (
46
+ <ProgressContext.Provider value={{ value, max }}>
47
+ <Comp
48
+ aria-valuemax={max}
49
+ aria-valuemin={0}
50
+ aria-valuenow={value ?? undefined}
51
+ aria-valuetext={value !== null ? getValueLabel(value, max) : undefined}
52
+ role="progressbar"
53
+ data-state={getProgressState(value, max)}
54
+ data-value={value ?? undefined}
55
+ data-max={max}
56
+ {...props}
57
+ ref={ref}
58
+ />
59
+ </ProgressContext.Provider>
60
+ )
61
+ },
62
+ )
63
+ ProgressRootBase.displayName = 'Progress'
64
+
65
+ const ProgressIndicatorBase = forwardRef<HTMLDivElement, ComponentPropsWithoutRef<'div'> & { asChild?: boolean }>(({ asChild, ...props }, ref) => {
66
+ const Comp = asChild ? Slot : 'div'
67
+ const { value, max } = useContext(ProgressContext)
68
+ return <Comp data-state={getProgressState(value, max)} data-value={value ?? undefined} data-max={max} {...props} ref={ref} />
69
+ })
70
+ ProgressIndicatorBase.displayName = 'ProgressIndicator'
71
+
72
+ const ProgressRoot = uic(ProgressRootBase, {
6
73
  baseClass: 'relative h-1.5 w-full overflow-hidden rounded-full bg-primary/20',
7
- displayName: ProgressPrimitive.Root.displayName,
74
+ displayName: 'Progress',
8
75
  })
9
76
 
10
- const ProgressIndicator = uic(ProgressPrimitive.Indicator, {
77
+ const ProgressIndicator = uic(ProgressIndicatorBase, {
11
78
  baseClass: 'h-full w-full flex-1 transition-all bg-gradient-to-b from-blue-500 to-blue-400',
12
- displayName: ProgressPrimitive.Indicator.displayName,
79
+ displayName: 'ProgressIndicator',
13
80
  })
14
81
 
15
- export const Progress = React.forwardRef<
16
- React.ElementRef<typeof ProgressPrimitive.Root>,
17
- React.ComponentPropsWithoutRef<typeof ProgressPrimitive.Root>
18
- >(({ className, value, ...props }, ref) => (
19
- <ProgressRoot ref={ref} {...props}>
82
+ export const Progress = React.forwardRef<HTMLDivElement, ProgressRootProps>(({ className, value, ...props }, ref) => (
83
+ <ProgressRoot ref={ref} value={value} {...props}>
20
84
  <ProgressIndicator style={{ transform: `translateX(-${100 - (value || 0)}%)` }} />
21
85
  </ProgressRoot>
22
86
  ))
23
- Progress.displayName = ProgressPrimitive.Root.displayName
87
+ Progress.displayName = 'Progress'
@@ -1,7 +1,30 @@
1
- import * as SeparatorPrimitive from '@radix-ui/react-separator'
1
+ import { Slot } from '@radix-ui/react-slot'
2
+ import { ComponentPropsWithoutRef, forwardRef } from 'react'
2
3
  import { uic } from '../utils/index.js'
3
4
 
4
- export const Separator = uic(SeparatorPrimitive.Root, {
5
+ type SeparatorBaseProps = ComponentPropsWithoutRef<'div'> & {
6
+ orientation?: 'horizontal' | 'vertical'
7
+ /** A purely visual separator, hidden from assistive technology. */
8
+ decorative?: boolean
9
+ asChild?: boolean
10
+ }
11
+
12
+ const SeparatorBase = forwardRef<HTMLDivElement, SeparatorBaseProps>(({ asChild, decorative, orientation: orientationProp, ...props }, ref) => {
13
+ const Comp = asChild ? Slot : 'div'
14
+ const orientation = orientationProp === 'vertical' ? 'vertical' : 'horizontal'
15
+ return (
16
+ <Comp
17
+ data-orientation={orientation}
18
+ role={decorative ? 'none' : 'separator'}
19
+ aria-orientation={!decorative && orientation === 'vertical' ? orientation : undefined}
20
+ {...props}
21
+ ref={ref}
22
+ />
23
+ )
24
+ })
25
+ SeparatorBase.displayName = 'Separator'
26
+
27
+ export const Separator = uic(SeparatorBase, {
5
28
  baseClass: 'shrink-0 bg-border',
6
29
  variants: {
7
30
  orientation: {
@@ -9,5 +32,6 @@ export const Separator = uic(SeparatorPrimitive.Root, {
9
32
  vertical: 'h-full w-[1px]',
10
33
  },
11
34
  },
12
- displayName: SeparatorPrimitive.Root.displayName,
35
+ passVariantProps: ['orientation'],
36
+ displayName: 'Separator',
13
37
  })
@@ -1,7 +1,7 @@
1
1
  import { createRequiredContext } from '@contember/react-utils'
2
2
  import { dataAttribute } from '@contember/utilities'
3
3
  import { Slot } from '@radix-ui/react-slot'
4
- import { cva, VariantProps } from 'class-variance-authority'
4
+ import { cva, VariantProps } from '../utils/classNames.js'
5
5
  import { PanelLeft } from 'lucide-react'
6
6
  import { ComponentProps, CSSProperties, ElementRef, forwardRef, PropsWithChildren, ReactNode, useCallback, useEffect, useMemo, useState } from 'react'
7
7
  import { cn, uic } from '../utils/index.js'
@@ -1,4 +1,6 @@
1
- import * as TogglePrimitive from '@radix-ui/react-toggle'
1
+ import { composeEventHandlers } from '@contember/utilities'
2
+ import { Slot } from '@radix-ui/react-slot'
3
+ import { ComponentPropsWithoutRef, forwardRef, useState } from 'react'
2
4
  import { ConfigVariants, uic, uiconfig } from '../utils/index.js'
3
5
 
4
6
  export const toggleConfig = uiconfig({
@@ -23,4 +25,41 @@ export const toggleConfig = uiconfig({
23
25
 
24
26
  export type ToggleProps = ConfigVariants<Exclude<typeof toggleConfig.variants, undefined>>
25
27
 
26
- export const Toggle = uic(TogglePrimitive.Root, toggleConfig)
28
+ type ToggleBaseProps = ComponentPropsWithoutRef<'button'> & {
29
+ /** Controlled pressed state. */
30
+ pressed?: boolean
31
+ defaultPressed?: boolean
32
+ onPressedChange?: (pressed: boolean) => void
33
+ asChild?: boolean
34
+ }
35
+
36
+ const ToggleBase = forwardRef<HTMLButtonElement, ToggleBaseProps>(
37
+ ({ asChild, pressed: pressedProp, defaultPressed, onPressedChange, ...props }, ref) => {
38
+ const Comp = asChild ? Slot : 'button'
39
+ const [uncontrolledPressed, setUncontrolledPressed] = useState(defaultPressed ?? false)
40
+ const isControlled = pressedProp !== undefined
41
+ const pressed = isControlled ? pressedProp : uncontrolledPressed
42
+ return (
43
+ <Comp
44
+ type="button"
45
+ aria-pressed={pressed}
46
+ data-state={pressed ? 'on' : 'off'}
47
+ data-disabled={props.disabled ? '' : undefined}
48
+ {...props}
49
+ ref={ref}
50
+ onClick={composeEventHandlers(props.onClick, () => {
51
+ if (props.disabled) {
52
+ return
53
+ }
54
+ if (!isControlled) {
55
+ setUncontrolledPressed(!pressed)
56
+ }
57
+ onPressedChange?.(!pressed)
58
+ })}
59
+ />
60
+ )
61
+ },
62
+ )
63
+ ToggleBase.displayName = 'Toggle'
64
+
65
+ export const Toggle = uic(ToggleBase, toggleConfig)
@@ -0,0 +1,107 @@
1
+ export type ClassDictionary = Record<string, unknown>
2
+ export type ClassArray = ClassValue[]
3
+ export type ClassValue = ClassArray | ClassDictionary | string | number | bigint | null | boolean | undefined
4
+
5
+ const toClassString = (value: ClassValue): string => {
6
+ if (typeof value === 'string' || typeof value === 'number') {
7
+ return String(value)
8
+ }
9
+ if (typeof value !== 'object' || value === null) {
10
+ return ''
11
+ }
12
+ let result = ''
13
+ if (Array.isArray(value)) {
14
+ for (const item of value) {
15
+ const itemString = item ? toClassString(item) : ''
16
+ if (itemString) {
17
+ result += (result ? ' ' : '') + itemString
18
+ }
19
+ }
20
+ return result
21
+ }
22
+ for (const key in value) {
23
+ if (value[key]) {
24
+ result += (result ? ' ' : '') + key
25
+ }
26
+ }
27
+ return result
28
+ }
29
+
30
+ /**
31
+ * Joins class names; same semantics as `clsx`: strings and numbers are kept, arrays are flattened,
32
+ * objects contribute their keys with truthy values, everything else is dropped.
33
+ */
34
+ export const cx = (...inputs: ClassValue[]): string => toClassString(inputs)
35
+
36
+ type ConfigSchema = Record<string, Record<string, ClassValue>>
37
+
38
+ type StringToBoolean<T> = T extends 'true' | 'false' ? boolean : T
39
+
40
+ type ClassProp =
41
+ | { class: ClassValue; className?: never }
42
+ | { class?: never; className: ClassValue }
43
+ | { class?: never; className?: never }
44
+
45
+ type CvaVariants<T extends ConfigSchema> = {
46
+ [Variant in keyof T]?: StringToBoolean<keyof T[Variant]> | null | undefined
47
+ }
48
+
49
+ type CvaVariantsMulti<T extends ConfigSchema> = {
50
+ [Variant in keyof T]?: StringToBoolean<keyof T[Variant]> | StringToBoolean<keyof T[Variant]>[] | undefined
51
+ }
52
+
53
+ type CvaConfig<T> = T extends ConfigSchema ? {
54
+ variants?: T
55
+ defaultVariants?: CvaVariants<T>
56
+ compoundVariants?: ((CvaVariants<T> | CvaVariantsMulti<T>) & ClassProp)[]
57
+ }
58
+ : never
59
+
60
+ type CvaProps<T> = T extends ConfigSchema ? CvaVariants<T> & ClassProp : ClassProp
61
+
62
+ export type VariantProps<Component extends (props?: never) => string> = Omit<Exclude<Parameters<Component>[0], undefined>, 'class' | 'className'>
63
+
64
+ const variantKey = (value: unknown): unknown => typeof value === 'boolean' ? `${value}` : value === 0 ? '0' : value
65
+
66
+ /**
67
+ * Builds a class name from a base, variant classes, compound variant classes and `class` / `className`;
68
+ * same semantics as `cva` from `class-variance-authority` 0.7.
69
+ */
70
+ export const cva = <T>(base?: ClassValue, config?: CvaConfig<T>) => (props?: CvaProps<T>): string => {
71
+ const propsRecord: Record<string, unknown> = { ...props }
72
+ const variants: ConfigSchema | undefined = config?.variants
73
+ if (variants == null) {
74
+ return cx(base, props?.class, props?.className)
75
+ }
76
+ const defaultVariants: Record<string, unknown> = { ...config?.defaultVariants }
77
+
78
+ const variantClassNames = Object.keys(variants).map(variant => {
79
+ const variantProp = propsRecord[variant]
80
+ if (variantProp === null) {
81
+ return null
82
+ }
83
+ const key = variantKey(variantProp) || variantKey(defaultVariants[variant])
84
+ // Property access converts the key with String(), like cva's `variants[variant][key]`.
85
+ return variants[variant][String(key)]
86
+ })
87
+
88
+ const resolvedVariants: Record<string, unknown> = { ...defaultVariants }
89
+ for (const [key, value] of Object.entries(propsRecord)) {
90
+ if (value !== undefined) {
91
+ resolvedVariants[key] = value
92
+ }
93
+ }
94
+
95
+ const compoundClassNames: ClassValue[] = []
96
+ for (const compoundVariant of config?.compoundVariants ?? []) {
97
+ const { class: cvClass, className: cvClassName, ...conditions } = compoundVariant
98
+ const matches = Object.entries(conditions).every(([key, value]) =>
99
+ Array.isArray(value) ? value.includes(resolvedVariants[key]) : resolvedVariants[key] === value
100
+ )
101
+ if (matches) {
102
+ compoundClassNames.push(cvClass, cvClassName)
103
+ }
104
+ }
105
+
106
+ return cx(base, variantClassNames, compoundClassNames, props?.class, props?.className)
107
+ }
@@ -1,8 +1,8 @@
1
- import { ClassValue, clsx } from 'clsx'
1
+ import { ClassValue, cx } from './classNames.js'
2
2
  import { twMerge } from 'tailwind-merge'
3
3
 
4
4
  /**
5
- * `cn` is a utility function that combines Tailwind CSS class names using `clsx`
5
+ * `cn` is a utility function that combines Tailwind CSS class names using `cx` (`clsx` semantics)
6
6
  * and merges conflicting classes with `twMerge`.
7
7
  *
8
8
  * Useful for composing conditional and dynamic className values in React components
@@ -19,4 +19,4 @@ import { twMerge } from 'tailwind-merge'
19
19
  * }
20
20
  * ```
21
21
  */
22
- export const cn = (...inputs: ClassValue[]) => twMerge(clsx(...inputs))
22
+ export const cn = (...inputs: ClassValue[]) => twMerge(cx(...inputs))
@@ -1,2 +1,3 @@
1
+ export * from './classNames.js'
1
2
  export * from './cn.js'
2
3
  export * from './uic.js'
@@ -1,7 +1,6 @@
1
1
  import { dataAttribute } from '@contember/utilities'
2
2
  import { Slot } from '@radix-ui/react-slot'
3
- import { cva } from 'class-variance-authority'
4
- import { ClassValue, clsx } from 'clsx'
3
+ import { ClassValue, cva, cx } from './classNames.js'
5
4
  import { ComponentType, createElement, forwardRef, ReactNode, useMemo } from 'react'
6
5
  import { twMerge } from 'tailwind-merge'
7
6
  import { useObjectMemo } from '@contember/react-utils'
@@ -148,7 +147,7 @@ export const uic = <El extends React.ElementType, Variants extends ConfigSchema
148
147
  }
149
148
  }
150
149
  const style = useMemo(() => config?.style ? { ...config.style, ...(rest.style || {}) } : rest.style, [rest.style])
151
- const finalClassName = useMemo(() => twMerge(clsx(cls(variantsMemoized), classNameProp)), [variantsMemoized, classNameProp])
150
+ const finalClassName = useMemo(() => twMerge(cx(cls(variantsMemoized), classNameProp)), [variantsMemoized, classNameProp])
152
151
 
153
152
  let FinalComponent: React.ElementType = Component
154
153
  if (props.asChild && typeof Component === 'string') {
@@ -40,22 +40,15 @@
40
40
  "@radix-ui/react-avatar": "^1.1.3",
41
41
  "@radix-ui/react-dialog": "^1.1.2",
42
42
  "@radix-ui/react-dropdown-menu": "^2.1.2",
43
- "@radix-ui/react-label": "^2.1.0",
44
43
  "@radix-ui/react-popover": "^1.1.2",
45
- "@radix-ui/react-portal": "^1.1.2",
46
- "@radix-ui/react-progress": "^1.1.0",
47
44
  "@radix-ui/react-scroll-area": "^1.2.0",
48
45
  "@radix-ui/react-select": "^2.1.2",
49
- "@radix-ui/react-separator": "^1.1.2",
50
46
  "@radix-ui/react-slot": "^1.1.0",
51
47
  "@radix-ui/react-switch": "^1.1.1",
52
48
  "@radix-ui/react-toast": "^1.2.2",
53
- "@radix-ui/react-toggle": "^1.1.0",
54
49
  "@radix-ui/react-toggle-group": "^1.1.0",
55
50
  "@radix-ui/react-tooltip": "^1.1.3",
56
51
  "@sentry/react": "^8.47.0",
57
- "class-variance-authority": "^0.7.0",
58
- "clsx": "^2.1.1",
59
52
  "lucide-react": "^0.451.0",
60
53
  "qrcode-generator": "^1.4.4",
61
54
  "react": "^18.3.1",
@@ -1,6 +1,5 @@
1
- import { Command, CommandConfiguration, escapeTerminalText, Input, Output } from '@contember/cli-common'
1
+ import { Command, CommandConfiguration, escapeTerminalText, Input, Output, style } from '@contember/cli-common'
2
2
  import { join } from 'node:path'
3
- import chalk from 'chalk'
4
3
  import { TemplateInstaller } from '../lib/TemplateInstaller.js'
5
4
  import { getPackageVersion } from '../lib/version.js'
6
5
  import { detectPackageManager } from '../lib/packageManagerDetector.js'
@@ -76,29 +75,29 @@ Contember project successfully created in ${safeProjectDirectory}
76
75
  Next steps:
77
76
 
78
77
  1. Navigate to the project directory:
79
- $ ${chalk.green(`cd ${safeProjectName}`)}
78
+ $ ${style('green', `cd ${safeProjectName}`)}
80
79
 
81
80
  2. Install dependencies:
82
- $ ${chalk.green(`${safePackageManager} install`)}
81
+ $ ${style('green', `${safePackageManager} install`)}
83
82
 
84
83
  3. Start the Contember stack:
85
- $ ${chalk.green(`${safePackageManager} start`)}
84
+ $ ${style('green', `${safePackageManager} start`)}
86
85
 
87
86
  Available services:
88
87
 
89
- ${chalk.bold('UI & Admin')}
90
- - Contember Admin ${chalk.blue('http://localhost:1480')}
88
+ ${style('bold', 'UI & Admin')}
89
+ - Contember Admin ${style('blue', 'http://localhost:1480')}
91
90
 
92
- ${chalk.bold('API & Backend')}
93
- - Contember Engine API ${chalk.blue('http://localhost:1481')}
91
+ ${style('bold', 'API & Backend')}
92
+ - Contember Engine API ${style('blue', 'http://localhost:1481')}
94
93
  Auth token: 0000000000000000000000000000000000000000
95
94
 
96
- ${chalk.bold('Development Tools')}
97
- - PostgreSQL database ${chalk.blue('http://localhost:1482')} ${chalk.grey('credentials: contember / contember')}
98
- - S3 object storage ${chalk.blue('http://localhost:1483')} ${chalk.grey('credentials: contember / contember')}
99
- - Mailpit SMTP testing ${chalk.blue('http://localhost:1484')}
100
- - Adminer DB manager ${chalk.blue('http://localhost:1485')}
95
+ ${style('bold', 'Development Tools')}
96
+ - PostgreSQL database ${style('blue', 'http://localhost:1482')} ${style('grey', 'credentials: contember / contember')}
97
+ - S3 object storage ${style('blue', 'http://localhost:1483')} ${style('grey', 'credentials: contember / contember')}
98
+ - Mailpit SMTP testing ${style('blue', 'http://localhost:1484')}
99
+ - Adminer DB manager ${style('blue', 'http://localhost:1485')}
101
100
 
102
- ${chalk.bold('Need help?')} Ask the community: https://github.com/orgs/contember/discussions
101
+ ${style('bold', 'Need help?')} Ask the community: https://github.com/orgs/contember/discussions
103
102
  `
104
103
  }
@@ -1,14 +1,18 @@
1
1
  import * as fs from 'node:fs/promises'
2
2
  import { access } from 'node:fs/promises'
3
3
  import { tmpdir } from 'node:os'
4
+ import { join } from 'node:path'
4
5
 
5
6
  export class FileSystem {
6
7
  readFile = fs.readFile
7
8
  writeFile = fs.writeFile
8
9
  rename = fs.rename
10
+ realPath = fs.realpath
11
+ lstat = fs.lstat
9
12
  readDir = fs.readdir
10
13
  copy = (source: string, destination: string, options?: { filter?: (source: string, destination: string) => boolean }) =>
11
- fs.cp(source, destination, { recursive: true, ...options })
14
+ // verbatimSymlinks: by default fs.cp rewrites relative links into absolute paths into the source tree
15
+ fs.cp(source, destination, { recursive: true, verbatimSymlinks: true, ...options })
12
16
  unlink = fs.unlink
13
17
  remove = fs.rm
14
18
  pathExists = async (path: string) => {
@@ -20,6 +24,7 @@ export class FileSystem {
20
24
  }
21
25
  }
22
26
  createTempDir = async () => {
23
- return await fs.mkdtemp(tmpdir())
27
+ // mkdtemp appends random characters to the prefix, so without a separator it would create `/tmpXXXXXX`
28
+ return await fs.mkdtemp(join(tmpdir(), 'contember-template-'))
24
29
  }
25
30
  }
@@ -1,7 +1,8 @@
1
1
  import { join } from 'node:path'
2
- import degit from 'degit'
3
2
  import jsyaml from 'js-yaml'
4
3
  import { FileSystem } from './FileSystem.js'
4
+ import { resolveNewProjectPath, resolveProjectPath } from './projectPath.js'
5
+ import { downloadTemplate } from './remoteTemplate.js'
5
6
 
6
7
  interface TemplateInstallerOutput {
7
8
  warn(message: string): void
@@ -9,10 +10,7 @@ interface TemplateInstallerOutput {
9
10
 
10
11
  type CloneTemplate = (source: string, target: string) => Promise<void>
11
12
 
12
- const cloneTemplate: CloneTemplate = async (source, target) => {
13
- const emitter = degit(source, { cache: false, force: true, verbose: true })
14
- await emitter.clone(target)
15
- }
13
+ const cloneTemplate: CloneTemplate = (source, target) => downloadTemplate(source, target)
16
14
 
17
15
  export class TemplateInstaller {
18
16
  private localTemplates: Record<string, string>
@@ -87,15 +85,17 @@ export class TemplateInstaller {
87
85
  return JSON.stringify({ scripts, ...json }, null, ' ')
88
86
  })
89
87
  }
88
+ // The template is untrusted input: every directive path must stay inside the new project.
89
+ const projectPath = (path: string) => resolveProjectPath(this.fs, targetDir, path)
90
90
  for (const [source, target] of Object.entries(config.rename || {})) {
91
- await this.fs.rename(join(targetDir, source), join(targetDir, target))
91
+ await this.fs.rename(await projectPath(source), await projectPath(target))
92
92
  }
93
93
  for (const [source, target] of Object.entries(config.copy || {})) {
94
- await this.fs.copy(join(targetDir, source), join(targetDir, target))
94
+ await this.fs.copy(await projectPath(source), await resolveNewProjectPath(this.fs, targetDir, target))
95
95
  }
96
96
 
97
97
  for (const file of config.replaceVariables || []) {
98
- const path = join(targetDir, file)
98
+ const path = await projectPath(file)
99
99
  if (!(await this.fs.pathExists(path))) {
100
100
  continue
101
101
  }
@@ -0,0 +1,2 @@
1
+ export const hasCode = (error: unknown, code: string): boolean => error instanceof Error && 'code' in error && error.code === code
2
+ export const isNotFound = (error: unknown): boolean => hasCode(error, 'ENOENT')
@@ -0,0 +1,79 @@
1
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
2
+ import { FileSystem } from './FileSystem.js'
3
+ import { isNotFound } from './fsErrors.js'
4
+
5
+ /**
6
+ * Resolves a path from a template directive against the project root.
7
+ * Refuses absolute paths, the root itself and anything that leaves the root, lexically or through a symlink.
8
+ */
9
+ export const resolveProjectPath = async (fs: FileSystem, root: string, path: string): Promise<string> => {
10
+ const outside = new Error(`Template path "${path}" points outside the project`)
11
+ if (isAbsolute(path)) {
12
+ throw outside
13
+ }
14
+ const resolved = resolve(root, path)
15
+ if (!isStrictlyInside(root, resolved)) {
16
+ throw outside
17
+ }
18
+ const realRoot = await fs.realPath(root)
19
+ const realPath = await resolveExistingPrefix(fs, resolved)
20
+ if (realPath === undefined) {
21
+ throw new Error(`Template path "${path}" passes through a dangling symlink`)
22
+ }
23
+ if (!isStrictlyInside(realRoot, realPath)) {
24
+ throw outside
25
+ }
26
+ return resolved
27
+ }
28
+
29
+ /**
30
+ * Like resolveProjectPath, and the path must not exist yet. fs.cp into an existing directory writes entries the check
31
+ * above never saw, and Bun's fs.cp follows symlinks it finds there.
32
+ */
33
+ export const resolveNewProjectPath = async (fs: FileSystem, root: string, path: string): Promise<string> => {
34
+ const resolved = await resolveProjectPath(fs, root, path)
35
+ if (await isPresent(fs, resolved)) {
36
+ throw new Error(`Template path "${path}" already exists`)
37
+ }
38
+ return resolved
39
+ }
40
+
41
+ const isStrictlyInside = (root: string, path: string): boolean => {
42
+ const rel = relative(root, path)
43
+ return rel !== '' && rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel)
44
+ }
45
+
46
+ /**
47
+ * realpath() of the longest existing prefix, with the missing components appended (they cannot be symlinks).
48
+ * Returns undefined for a dangling symlink: writing to it would create its target, wherever that is.
49
+ */
50
+ const resolveExistingPrefix = async (fs: FileSystem, path: string): Promise<string | undefined> => {
51
+ try {
52
+ return await fs.realPath(path)
53
+ } catch (error) {
54
+ if (!isNotFound(error)) {
55
+ throw error
56
+ }
57
+ }
58
+ if (await isPresent(fs, path)) {
59
+ return undefined
60
+ }
61
+ const parent = dirname(path)
62
+ if (parent === path) {
63
+ return path
64
+ }
65
+ const realParent = await resolveExistingPrefix(fs, parent)
66
+ return realParent === undefined ? undefined : join(realParent, basename(path))
67
+ }
68
+
69
+ const isPresent = async (fs: FileSystem, path: string): Promise<boolean> => {
70
+ try {
71
+ await fs.lstat(path)
72
+ return true
73
+ } catch (error) {
74
+ if (isNotFound(error)) {
75
+ return false
76
+ }
77
+ throw error
78
+ }
79
+ }