@jahands/dagger-helpers 0.7.3 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ export { constants } from './constants.js'
2
+
3
+ export { ParamsToEnv, envStorage } from './env.js'
4
+ export type { EnvContext } from './env.js'
5
+
6
+ export { shell } from './shell.js'
7
+
8
+ export { getModulePath } from './path.js'
9
+
10
+ export { convertToCamel } from './convert/snake-to-camel.js'
11
+ export { convertToSnake } from './convert/camel-to-snake.js'
@@ -0,0 +1,34 @@
1
+ import path from 'node:path'
2
+ import { afterEach, describe, expect, it } from 'vitest'
3
+
4
+ import { getModulePath, getRepoRoot } from './path.js'
5
+
6
+ const testDir = `${__dirname}/test`
7
+ const fixtures = `${testDir}/fixtures`
8
+ const repoDir = `${fixtures}/repo`
9
+ const moduleDir = `${repoDir}/path/to/module`
10
+ const moduleSrcDir = `${moduleDir}/.dagger/src`
11
+
12
+ afterEach(() => {
13
+ process.chdir(path.resolve(__dirname, '..'))
14
+ })
15
+
16
+ /** convert from absolute path to relative of dagger-helpers package */
17
+ function trimPath(p: string): string {
18
+ return p.replace(testDir, '')
19
+ }
20
+
21
+ describe('getRepoRoot()', () => {
22
+ it('returns the directory of the first pnpm-lock.yaml it finds', () => {
23
+ process.chdir(moduleSrcDir)
24
+ expect(trimPath(getRepoRoot())).toBe(`/fixtures/repo`)
25
+ })
26
+ })
27
+
28
+ describe('getModulePath()', () => {
29
+ it('should return path to the dagger module relative to repo root', () => {
30
+ process.chdir(moduleSrcDir)
31
+ const p = getModulePath()
32
+ expect(p).toBe('path/to/module')
33
+ })
34
+ })
package/src/path.ts ADDED
@@ -0,0 +1,39 @@
1
+ import path from 'node:path'
2
+ import { up } from 'empathic/find'
3
+
4
+ export function getRepoRoot(cwd?: string): string {
5
+ cwd = cwd ?? process.cwd()
6
+
7
+ const lockfile = up('pnpm-lock.yaml', { cwd })
8
+ if (!lockfile) {
9
+ throw new Error('could not determine repo root path: unable to find pnpm-lock.yaml')
10
+ }
11
+
12
+ return path.dirname(lockfile)
13
+ }
14
+
15
+ /**
16
+ * Get the path to the dagger module.
17
+ *
18
+ * Requires `pnpm-lock.yaml` to exist in the repo root.
19
+ *
20
+ * @param cwd - The current working directory. **Default:** `process.cwd()`
21
+ *
22
+ * @returns The path to the dagger module relative to the repo root
23
+ */
24
+ export function getModulePath(cwd?: string): string {
25
+ cwd = cwd ?? process.cwd()
26
+
27
+ const lockfile = up('pnpm-lock.yaml', { cwd })
28
+ if (!lockfile) {
29
+ throw new Error('could not determine repo root path: unable to find pnpm-lock.yaml')
30
+ }
31
+ const repoRoot = path.dirname(lockfile)
32
+
33
+ const daggerJson = up('dagger.json', { cwd, last: repoRoot })
34
+ if (!daggerJson) {
35
+ throw new Error('could not determine dagger.json path: unable to find dagger.json')
36
+ }
37
+
38
+ return path.relative(repoRoot, path.dirname(daggerJson))
39
+ }
package/src/shell.ts ADDED
@@ -0,0 +1,58 @@
1
+ import { match } from 'ts-pattern'
2
+
3
+ /**
4
+ * Shell options for a given command
5
+ */
6
+ export type ShellOptions = {
7
+ /**
8
+ * Prefix to add to all commands.
9
+ *
10
+ * @default
11
+ * ```sh
12
+ * # bash, zsh
13
+ * set -euo pipefail;
14
+ * # sh
15
+ * set -eu;
16
+ * ```
17
+ */
18
+ prefix?: string
19
+ }
20
+
21
+ /**
22
+ * Create a new shell helper with the given shell type
23
+ *
24
+ * @param shellName - The name of the shell to use
25
+ *
26
+ * @example
27
+ *
28
+ * ```ts
29
+ * const sh = shell('bash')
30
+ *
31
+ * const con = dag.container()
32
+ * .withExec(sh('echo hello world!'))
33
+ * ```
34
+ */
35
+ export function shell(shellName: 'sh' | 'bash' | 'zsh') {
36
+ return (input: string | string[], options?: ShellOptions): string[] => {
37
+ const inputAr = Array.isArray(input) ? input : [input]
38
+ const trimmedInput = inputAr.map((i) =>
39
+ i
40
+ .trim()
41
+ .split('\n')
42
+ .map((l) => l.trim())
43
+ .join('\n')
44
+ )
45
+
46
+ let prefix = match(shellName)
47
+ .with('bash', 'zsh', () => options?.prefix || 'set -euo pipefail')
48
+ .with('sh', () => options?.prefix || 'set -eu')
49
+ .exhaustive()
50
+ .trim()
51
+
52
+ if (!prefix.endsWith(';')) {
53
+ prefix += ';'
54
+ }
55
+
56
+ return [shellName, '-c', `${prefix} ${trimmedInput}`]
57
+ }
58
+ }
@@ -0,0 +1 @@
1
+ // fake dagger module
File without changes