@take-out/scripts 0.1.3 → 0.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@take-out/scripts",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "main": "./src/run.ts",
6
6
  "sideEffects": false,
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@clack/prompts": "^0.8.2",
32
- "@take-out/helpers": "0.1.3",
32
+ "@take-out/helpers": "workspace:*",
33
33
  "picocolors": "^1.1.1"
34
34
  },
35
35
  "peerDependencies": {
package/src/env-update.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  import fs from 'node:fs'
4
4
 
5
5
  import { cmd } from './cmd'
6
+ import { resolveDepVersion } from './helpers/resolve-dep-version'
6
7
 
7
8
  await cmd`sync environment variables from package.json to CI and server configs`.run(
8
9
  async () => {
@@ -43,8 +44,14 @@ await cmd`sync environment variables from package.json to CI and server configs`
43
44
  }
44
45
 
45
46
  const indent = ` `
46
- const envSection = Object.keys(envVars)
47
- .map((key) => `${indent}${key}: \${{ secrets.${key} }}`)
47
+ const envSection = Object.entries(envVars)
48
+ .map(([key, value]) => {
49
+ const resolved = resolveDepVersion(value, packageJson.dependencies)
50
+ if (resolved) {
51
+ return `${indent}${key}: \${{ secrets.${key} || '${resolved}' }}`
52
+ }
53
+ return `${indent}${key}: \${{ secrets.${key} }}`
54
+ })
48
55
  .join('\n')
49
56
 
50
57
  const newDeployYml = replaceYamlSection(deployYml, envSection, { indent })
@@ -3,6 +3,7 @@
3
3
  import fs from 'node:fs'
4
4
 
5
5
  import { cmd } from './cmd'
6
+ import { resolveDepVersion } from './helpers/resolve-dep-version'
6
7
 
7
8
  await cmd`sync auto-generated env vars to local .env file`.run(async ({ path }) => {
8
9
  const ENV_PATH = path.join(process.cwd(), '.env')
@@ -26,15 +27,13 @@ await cmd`sync auto-generated env vars to local .env file`.run(async ({ path })
26
27
  const lines: string[] = [BEGIN_MARKER, `# Generated at: ${new Date().toISOString()}`]
27
28
 
28
29
  for (const [key, value] of Object.entries(envVars)) {
29
- if (typeof value === 'string' && value.startsWith('$dep:')) {
30
- // resolve version from dependency
31
- const depName = value.slice('$dep:'.length)
32
- const version = packageJson.dependencies?.[depName]?.replace(/^[\^~]/, '')
33
- if (version) {
34
- lines.push(`${key}=${version}`)
35
- } else {
36
- console.warn(`Could not resolve dependency version for ${depName}`)
37
- }
30
+ const depVersion = resolveDepVersion(value, packageJson.dependencies)
31
+ if (depVersion) {
32
+ lines.push(`${key}=${depVersion}`)
33
+ } else if (typeof value === 'string' && value.startsWith('$dep:')) {
34
+ console.warn(
35
+ `Could not resolve dependency version for ${value.slice('$dep:'.length)}`
36
+ )
38
37
  } else if (typeof value === 'string' && value !== '') {
39
38
  // non-empty string default
40
39
  lines.push(`${key}=${value}`)
@@ -61,10 +61,10 @@ export function args<const S extends string>(strings: TemplateStringsArray | S):
61
61
 
62
62
  // parse spec: "--port number --verbose boolean"
63
63
  const schema: Record<string, 'string' | 'number' | 'boolean'> = {}
64
- const matches = spec.matchAll(/--?([a-z-]+)\s+(string|number|boolean)/gi)
64
+ const matches = spec.matchAll(/--?([a-z0-9-]+)\s+(string|number|boolean)/gi)
65
65
 
66
66
  for (const [, flag, type] of matches) {
67
- const camel = flag!.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
67
+ const camel = flag!.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase())
68
68
  schema[camel] = type as 'string' | 'number' | 'boolean'
69
69
  }
70
70
 
@@ -75,7 +75,7 @@ export function args<const S extends string>(strings: TemplateStringsArray | S):
75
75
  const arg = argv[i]!
76
76
 
77
77
  if (arg.startsWith('-')) {
78
- const key = arg.replace(/^--?/, '').replace(/-([a-z])/g, (_, c) => c.toUpperCase())
78
+ const key = arg.replace(/^--?/, '').replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase())
79
79
  const type = schema[key]
80
80
 
81
81
  if (type === 'boolean') {
@@ -0,0 +1,11 @@
1
+ /**
2
+ * resolves a `$dep:package-name` value to the installed version from dependencies
3
+ */
4
+ export function resolveDepVersion(
5
+ value: string | boolean,
6
+ dependencies: Record<string, string> | undefined
7
+ ): string | undefined {
8
+ if (typeof value !== 'string' || !value.startsWith('$dep:')) return undefined
9
+ const depName = value.slice('$dep:'.length)
10
+ return dependencies?.[depName]?.replace(/^[\^~]/, '')
11
+ }