File size: 4,850 Bytes
b92316a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict'
/**

 * platform-linux.js — Linux-specific runtime adapter

 * Loaded exclusively when process.platform === 'linux'

 */

const path = require('path')
const os = require('os')

const PLATFORM = 'linux'

// ── Writable path candidates for app userData ─────────────────────────────────
function getWritableCandidates(appDirName) {
  const candidates = []
  const xdgData = process.env.XDG_DATA_HOME
  if (xdgData) {
    candidates.push(path.join(xdgData, 'chahuadev', appDirName))
  }
  candidates.push(path.join(os.homedir(), '.local', 'share', 'chahuadev', appDirName))
  candidates.push(path.join(os.tmpdir(), appDirName))
  return candidates
}

// ── Command name normalization ────────────────────────────────────────────────
// Linux: no .cmd suffix needed — npm/npx/pnpm/yarn are native shell scripts.
function normalizeExecName(cmd) {
  return String(cmd || '').trim()
}

// ── Shell wrapping ────────────────────────────────────────────────────────────
// Linux commands run directly via node-pty — no cmd.exe-style wrapping needed.
function shouldUseShell(cmd) { // eslint-disable-line no-unused-vars
  return false
}

// Not called on Linux (shouldUseShell always false), but satisfies interface.
function buildShellSpawn(cmd, args) {
  return { file: cmd, args: args || [] }
}

// ── node-pty spawn extra options ──────────────────────────────────────────────
// useConpty is a Windows-only (ConPTY) option.  On Linux use standard PTY.
function getPtySpawnOptions() {
  return {}
}

// ── Node.js runtime path ──────────────────────────────────────────────────────
function resolveNodeRuntime() {
  return 'node'
}

// ── Linux build command ───────────────────────────────────────────────────────
// On Linux the AppImage runs natively — no WSL bridge required.
function _quoteBash(s) {
  return `'${String(s || '').replace(/'/g, `'"'"'`)}'`

}



function rewriteLinuxBuildCommand(cwd) {

  const script = [

    'set -euo pipefail',

    `cd ${_quoteBash(cwd)}`,

    'npm install',

    'if npm run | grep -q "build:linux"; then npm run build:linux; else npx electron-builder --linux AppImage --x64 --publish never; fi'

  ].join('; ')

  return {

    cmd: 'bash',

    args: ['-lc', script],

    rewriteInfo: {

      applied: true,

      kind: 'launcher-linux-build-inline-bash',

      targetProjectPath: cwd

    }

  }

}



// ── Protected OS path bases ───────────────────────────────────────────────────

// Base list covers standard FHS paths.

// We also scan /proc/mounts at runtime to catch non-standard system-level

// mount points (e.g. bind mounts, external drives at /mnt/*, /media/*,

// or custom rootfs at depth-1 like /data, /storage).

// Only "system-ish" mounts are protected — /home and /tmp are intentionally left open.



const _OPEN_ROOTS = new Set(['home', 'tmp', 'root'])



function _readMountRoots() {

  try {

    const fs = require('fs')

    const lines = fs.readFileSync('/proc/mounts', 'utf8').split('\n')

    const extra = new Set()

    for (const line of lines) {

      const mountPoint = line.split(' ')[1]

      if (!mountPoint || mountPoint === '/') continue

      const parts = mountPoint.split('/').filter(Boolean)

      // Protect any depth-1 non-user mount (e.g. /data, /storage)

      if (parts.length === 1 && !_OPEN_ROOTS.has(parts[0])) {

        extra.add(mountPoint)

      }

      // Protect anything under /mnt/* or /media/* (external drives, images)

      if (parts.length === 2 && (parts[0] === 'mnt' || parts[0] === 'media')) {

        extra.add(mountPoint)

      }

    }

    return [...extra]

  } catch {

    return []

  }

}



function getProtectedPathBases() {

  return [

    '/bin', '/sbin', '/lib', '/lib64',

    '/usr', '/var', '/etc', '/boot',

    '/proc', '/sys', '/dev', '/run', '/snap',

    ..._readMountRoots()

  ]

}



module.exports = {

  PLATFORM,

  getWritableCandidates,

  normalizeExecName,

  shouldUseShell,

  buildShellSpawn,

  getPtySpawnOptions,

  resolveNodeRuntime,

  rewriteLinuxBuildCommand,

  getProtectedPathBases

}