rootless-config 1.5.0 → 1.6.0

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": "rootless-config",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "Store project config files outside the project root, auto-deploy them where tools expect them.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,95 @@
1
+ /*-------- rootless run — execute a file from .root/ as if it were in project root --------*/
2
+
3
+ import { spawn } from 'node:child_process'
4
+ import path from 'node:path'
5
+ import { fileExists } from '../../utils/fsUtils.js'
6
+ import { createLogger } from '../../utils/logger.js'
7
+
8
+ // Search for a filename across all .root subdirectories
9
+ async function findInRoot(containerPath, name) {
10
+ // Strip leading ./ or .\ (user might type .\.server.ps1)
11
+ const base = name.replace(/^\.[\\/]/g, '')
12
+ for (const sub of ['assets', 'configs', 'env']) {
13
+ const candidate = path.join(containerPath, sub, base)
14
+ if (await fileExists(candidate)) return candidate
15
+ }
16
+ return null
17
+ }
18
+
19
+ function buildSpawnArgs(filePath, extraArgs) {
20
+ const ext = path.extname(filePath).toLowerCase()
21
+ if (ext === '.ps1') {
22
+ return {
23
+ cmd: 'powershell',
24
+ args: ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', filePath, ...extraArgs],
25
+ }
26
+ }
27
+ if (ext === '.cmd' || ext === '.bat') {
28
+ return { cmd: 'cmd', args: ['/c', filePath, ...extraArgs] }
29
+ }
30
+ if (ext === '.sh') {
31
+ return { cmd: 'bash', args: [filePath, ...extraArgs] }
32
+ }
33
+ return { cmd: filePath, args: extraArgs }
34
+ }
35
+
36
+ export default {
37
+ name: 'run',
38
+ description: 'Run a file from .root/ as if it were in project root — no file copying',
39
+
40
+ async handler(args) {
41
+ const logger = createLogger({ verbose: args.verbose ?? false })
42
+ const scriptArgs = args.scriptArgs ?? []
43
+
44
+ if (scriptArgs.length === 0) {
45
+ logger.fail('No file specified.')
46
+ logger.info('Usage: rootless run <file> [args...]')
47
+ logger.info('Example: rootless run .server.ps1')
48
+ logger.info('Example: rootless run .server.run.cmd')
49
+ process.exitCode = 1
50
+ return
51
+ }
52
+
53
+ const projectRoot = args.cwd ? path.resolve(args.cwd) : process.cwd()
54
+ const containerPath = path.join(projectRoot, '.root')
55
+ const [name, ...rest] = scriptArgs
56
+
57
+ // Check project root first, then .root subdirs
58
+ const inRoot = path.join(projectRoot, name.replace(/^\.[\\/]/g, ''))
59
+ let filePath = (await fileExists(inRoot)) ? inRoot : await findInRoot(containerPath, name)
60
+
61
+ if (!filePath) {
62
+ logger.fail(`Not found: "${name}"`)
63
+ logger.info('Searched: project root, .root/assets/, .root/configs/, .root/env/')
64
+ process.exitCode = 1
65
+ return
66
+ }
67
+
68
+ const rel = filePath.startsWith(containerPath)
69
+ ? `.root/${path.relative(containerPath, filePath).replace(/\\/g, '/')}`
70
+ : path.relative(projectRoot, filePath)
71
+
72
+ logger.info(`Running: ${name} [${rel}]`)
73
+ logger.info(`cwd: ${projectRoot}`)
74
+ logger.info('')
75
+
76
+ const { cmd, args: cmdArgs } = buildSpawnArgs(filePath, rest)
77
+
78
+ const child = spawn(cmd, cmdArgs, {
79
+ cwd: projectRoot, // always runs from project root
80
+ stdio: 'inherit',
81
+ shell: false,
82
+ })
83
+
84
+ child.on('error', (err) => {
85
+ logger.fail(`Failed to start: ${err.message}`)
86
+ process.exitCode = 1
87
+ })
88
+
89
+ await new Promise(resolve => child.on('exit', (code) => {
90
+ process.exitCode = code ?? 0
91
+ resolve()
92
+ }))
93
+ },
94
+ }
95
+
@@ -0,0 +1,118 @@
1
+ /*-------- rootless shell — interactive shell where .root/ files act as if in project root --------*/
2
+
3
+ import { spawn } from 'node:child_process'
4
+ import { writeFile, unlink, readdir } from 'node:fs/promises'
5
+ import path from 'node:path'
6
+ import os from 'node:os'
7
+ import { fileExists } from '../../utils/fsUtils.js'
8
+ import { createLogger } from '../../utils/logger.js'
9
+
10
+ /**
11
+ * Build a temporary PowerShell profile that:
12
+ * 1. Adds .root/assets/ to PATH (so .cmd/.bat/.exe files are runnable by name)
13
+ * 2. Creates a function wrapper for each .ps1 file so it's callable by name
14
+ * 3. Overrides prompt to show [rootless] prefix
15
+ */
16
+ async function buildPsProfile(projectRoot, containerPath) {
17
+ const assetsDir = path.join(containerPath, 'assets')
18
+ const configsDir = path.join(containerPath, 'configs')
19
+ const envDir = path.join(containerPath, 'env')
20
+
21
+ const lines = []
22
+
23
+ // Add .root/assets/ and .root/configs/ to PATH for executable resolution
24
+ lines.push(`$env:PATH = "${assetsDir};${configsDir};$env:PATH"`)
25
+ lines.push('')
26
+
27
+ // Create function wrappers for every .ps1 file in .root/
28
+ for (const dir of [assetsDir, configsDir]) {
29
+ if (!(await fileExists(dir))) continue
30
+ const entries = await readdir(dir, { withFileTypes: true })
31
+ for (const e of entries) {
32
+ if (!e.isFile() || !e.name.endsWith('.ps1')) continue
33
+ const fullPath = path.join(dir, e.name).replace(/\\/g, '\\\\')
34
+ // Function name: strip leading dot for valid PS identifier, but also set alias with dot
35
+ const safeName = e.name.replace(/^\./, '_').replace(/\.ps1$/, '')
36
+ const dotName = e.name.replace(/\.ps1$/, '') // e.g. ".server"
37
+ lines.push(`function global:${safeName} { & "${fullPath}" @args }`)
38
+ // Also register the dotted version via alias if it doesn't start with dot
39
+ if (!dotName.startsWith('.')) {
40
+ lines.push(`Set-Alias -Name "${dotName}.ps1" -Value ${safeName} -Scope Global`)
41
+ }
42
+ }
43
+ }
44
+
45
+ lines.push('')
46
+ lines.push('# Prompt')
47
+ lines.push('function global:prompt {')
48
+ lines.push(' Write-Host "[rootless] " -NoNewline -ForegroundColor Cyan')
49
+ lines.push(' Write-Host (Get-Location) -NoNewline -ForegroundColor White')
50
+ lines.push(' return "> "')
51
+ lines.push('}')
52
+ lines.push('')
53
+
54
+ // Welcome banner
55
+ lines.push('Write-Host ""')
56
+ lines.push('Write-Host " Rootless Shell" -ForegroundColor Cyan')
57
+ lines.push('Write-Host " Files from .root/ work as commands. cwd = project root." -ForegroundColor Gray')
58
+ lines.push('Write-Host " Type exit to leave." -ForegroundColor Gray')
59
+ lines.push('Write-Host ""')
60
+
61
+ // List available .ps1 wrappers
62
+ lines.push('Write-Host " Available:" -ForegroundColor DarkCyan')
63
+ for (const dir of [assetsDir, configsDir]) {
64
+ if (!(await fileExists(dir))) continue
65
+ const entries = await readdir(dir, { withFileTypes: true })
66
+ for (const e of entries) {
67
+ if (!e.isFile()) continue
68
+ const rel = `.root/${path.basename(dir)}/${e.name}`
69
+ lines.push(`Write-Host " ${e.name.padEnd(30)} [${rel}]" -ForegroundColor Gray`)
70
+ }
71
+ }
72
+ lines.push('Write-Host ""')
73
+
74
+ return lines.join('\n')
75
+ }
76
+
77
+ export default {
78
+ name: 'shell',
79
+ description: 'Open an interactive shell where .root/ files are accessible as commands',
80
+
81
+ async handler(args) {
82
+ const logger = createLogger({ verbose: args.verbose ?? false })
83
+ const projectRoot = args.cwd ? path.resolve(args.cwd) : process.cwd()
84
+ const containerPath = path.join(projectRoot, '.root')
85
+
86
+ if (!(await fileExists(containerPath))) {
87
+ logger.fail('No .root/ container found. Run: rootless setup')
88
+ process.exitCode = 1
89
+ return
90
+ }
91
+
92
+ // Write temp profile
93
+ const profilePath = path.join(os.tmpdir(), `rootless-profile-${Date.now()}.ps1`)
94
+ const profileContent = await buildPsProfile(projectRoot, containerPath)
95
+ await writeFile(profilePath, profileContent, 'utf8')
96
+
97
+ logger.info('Starting rootless shell…')
98
+
99
+ const child = spawn(
100
+ 'powershell',
101
+ ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-NoExit', '-Command', `. "${profilePath}"`],
102
+ {
103
+ cwd: projectRoot,
104
+ stdio: 'inherit',
105
+ shell: false,
106
+ }
107
+ )
108
+
109
+ child.on('error', async (err) => {
110
+ logger.fail(`Failed to start shell: ${err.message}`)
111
+ await unlink(profilePath).catch(() => {})
112
+ process.exitCode = 1
113
+ })
114
+
115
+ await new Promise(resolve => child.on('exit', resolve))
116
+ await unlink(profilePath).catch(() => {})
117
+ },
118
+ }
package/src/cli/index.js CHANGED
@@ -29,15 +29,24 @@ async function run(argv) {
29
29
  sub.option('--port <number>', 'Port to listen on (default: 3000)')
30
30
  }
31
31
 
32
+ if (cmd.name === 'run') {
33
+ sub.argument('[scriptArgs...]', 'File to run and optional arguments')
34
+ sub.passThroughOptions(true)
35
+ }
36
+
32
37
  sub.option('--verbose', 'Enable verbose output')
33
38
  sub.option('--silent', 'Suppress all output')
34
39
 
35
- sub.action(async (options) => {
40
+ sub.action(async (scriptArgsOrOptions, options) => {
41
+ // For 'run' command, first arg is the positional array
42
+ const opts = cmd.name === 'run'
43
+ ? { ...options, scriptArgs: scriptArgsOrOptions }
44
+ : scriptArgsOrOptions
36
45
  try {
37
- await cmd.handler(options)
46
+ await cmd.handler(opts)
38
47
  } catch (err) {
39
48
  logger.fail(err.message)
40
- if (options.verbose) process.stderr.write(err.stack + '\n')
49
+ if (opts.verbose) process.stderr.write(err.stack + '\n')
41
50
  process.exitCode = 1
42
51
  }
43
52
  })