pinokiod 0.1.6 → 0.1.8

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/kernel/index.js CHANGED
@@ -239,5 +239,14 @@ class Kernel {
239
239
  console.log("### ERROR", e)
240
240
  }
241
241
  }
242
+ async exec(params, ondata) {
243
+ // params.path = this.path()
244
+ // if (this.client) {
245
+ // params.cols = this.client.cols
246
+ // params.rows = this.client.rows
247
+ // }
248
+ let response = await this.shell.run(params, null, ondata)
249
+ return response
250
+ }
242
251
  }
243
252
  module.exports = Kernel
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinokiod",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/script/index.js CHANGED
@@ -22,6 +22,15 @@ class Store {
22
22
  return null
23
23
  }
24
24
  }
25
+ delete(key) {
26
+ try {
27
+ let str = fs.readFileSync(path.resolve(__dirname, "pinokio.json"), "utf8")
28
+ let o = JSON.parse(str)
29
+ delete o[key]
30
+ fs.writeFileSync(path.resolve(__dirname, "pinokio.json"), JSON.stringify(o, null, 2))
31
+ } catch (e) {
32
+ }
33
+ }
25
34
  }
26
35
  const server = new Server({
27
36
  port: 42000,
package/server/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const express = require('express');
2
+ const sudo = require("sudo-prompt-programfiles-x86");
2
3
  const compressing = require('compressing');
3
4
  const { rimraf } = require('rimraf')
4
5
  const { createHttpTerminator } = require('http-terminator')
@@ -327,10 +328,9 @@ class Server {
327
328
  query: req.query
328
329
  })
329
330
  } else if (pathComponents.length === 0 && req.query.mode === "settings") {
330
- let home = this.kernel.homedir
331
331
  let configArray = [{
332
332
  key: "home",
333
- val: home, //this.kernel.store.get("home"),
333
+ val: this.kernel.homedir,
334
334
  placeholder: "Enter the absolute path to use as your Pinokio home folder (D:\\pinokio, /Users/alice/pinokiofs, etc.)"
335
335
  }, {
336
336
  key: "theme",
@@ -1223,33 +1223,32 @@ class Server {
1223
1223
  return installed
1224
1224
  }
1225
1225
  }
1226
- async configure(newConfig) {
1227
-
1228
- // if (newConfig && newConfig.home) {
1229
- // this.kernel.store.set("home", newConfig.home)
1230
- // }
1231
- if (newConfig && newConfig.theme) {
1232
- this.kernel.store.set("theme", newConfig.theme)
1233
- this.theme = newConfig.theme
1234
- } else {
1235
- this.theme = this.kernel.store.get("theme") || "light"
1236
- }
1237
-
1238
- // // write theme
1239
- // const p = path.resolve(this.kernel.homedir, "config.json")
1240
- // let config = (await this.kernel.loader.load(p)).resolved
1241
- // if (!config) {
1242
- // await fs.promises.writeFile(p, JSON.stringify({
1243
- // theme: "light"
1244
- // }))
1245
- // config = (await this.kernel.loader.load(p)).resolved
1246
- // }
1247
- //
1248
- // if (newConfig) {
1249
- // await fs.promises.writeFile(p, JSON.stringify(newConfig))
1250
- // config = (await this.kernel.loader.load(p)).resolved
1251
- // }
1252
- // this.theme = config.theme
1226
+ async sudo_exec(message, homedir) {
1227
+ console.log("sudo_exec", { message, homedir })
1228
+ // sudo-prompt uses TEMP
1229
+ let TEMP = path.resolve(homedir, "cache", "TEMP")
1230
+ await fs.promises.mkdir(TEMP, { recursive: true }).catch((e) => { })
1231
+ let response = await new Promise((resolve, reject) => {
1232
+ let env = Object.assign({}, process.env)
1233
+ env.TEMP = TEMP
1234
+ sudo.exec(message, {
1235
+ name: "Pinokio",
1236
+ env,
1237
+ }, (err, stdout, stderr) => {
1238
+ if (err) {
1239
+ reject(err)
1240
+ } else if (stderr) {
1241
+ reject(stderr)
1242
+ } else {
1243
+ resolve(stdout)
1244
+ }
1245
+ });
1246
+ })
1247
+ return response
1248
+ }
1249
+ async syncConfig() {
1250
+ // 1. THEME
1251
+ this.theme = this.kernel.store.get("theme") || "light"
1253
1252
  if (this.theme === "dark") {
1254
1253
  this.colors = {
1255
1254
  color: "rgb(31, 29, 39)",
@@ -1263,6 +1262,86 @@ class Server {
1263
1262
  }
1264
1263
  //this.logo = (this.theme === 'dark' ? "<img class='icon' src='/pinokio-white.png'>" : "<img class='icon' src='/pinokio-black.png'>")
1265
1264
  this.logo = '<i class="fa-solid fa-house"></i>'
1265
+
1266
+ // 2. HOME
1267
+ // 2.1. Check if the config includes NEW_HOME => if so,
1268
+ // - move the HOME folder to NEW_HOME
1269
+ // - set HOME=NEW_HOME
1270
+ // - remove NEW_HOME
1271
+ let new_home = this.kernel.store.get("new_home")
1272
+ console.log("new_home", new_home)
1273
+ if (new_home) {
1274
+ // move the home folder to new_home
1275
+
1276
+ let existing_home = this.kernel.store.get("home")
1277
+
1278
+ console.log({ existing_home, new_home })
1279
+
1280
+ if (existing_home) {
1281
+ // if the home is already set, move
1282
+ console.log("RUNNING MV")
1283
+ const mv_cmd = `${(this.kernel.platform === "win32" ? "move" : "mv")} ${existing_home} ${new_home}`
1284
+ await this.sudo_exec(mv_cmd, existing_home)
1285
+ console.log("FINISHED MV")
1286
+
1287
+ //// Next, empty the bin folder => need to reinitialize because of symlinks, etc. with the package managers
1288
+ console.log("RUNNING RM BIN")
1289
+ const path_to_delete = path.resolve(new_home, "bin")
1290
+ let del_cmd
1291
+ if (this.kernel.platform === "win32") {
1292
+ del_cmd = `rd /s /q ${path_to_delete}`
1293
+ } else {
1294
+ del_cmd = `rm -rf ${path_to_delete}`
1295
+ }
1296
+ console.log("del_cmd", del_cmd)
1297
+
1298
+ await this.sudo_exec(del_cmd, new_home)
1299
+ console.log("FINISHED RM BIN")
1300
+
1301
+ // set home=new_home
1302
+ this.kernel.store.set("home", new_home)
1303
+ // delete new_home
1304
+ console.log("STORE", this.kernel.store)
1305
+ this.kernel.store.delete("new_home")
1306
+ } else {
1307
+ // if the home is not already set, don't need to do anything
1308
+ }
1309
+ }
1310
+ }
1311
+ async setConfig(config) {
1312
+ console.log("setConfig", config)
1313
+ let home = this.kernel.store.get("home")
1314
+ let theme = this.kernel.store.get("theme")
1315
+ console.log("before setting config", { home, theme })
1316
+
1317
+ // 1. Handle THEME
1318
+ if (config.theme) {
1319
+ this.kernel.store.set("theme", config.theme)
1320
+ //this.theme = config.theme
1321
+ }
1322
+ // 2. Handle HOME
1323
+ if (config.home) {
1324
+ // set "new_home"
1325
+ const basename = path.basename(config.home)
1326
+
1327
+ // check for invalid path
1328
+ let isValidPath = (basename !== '' && basename !== config.home)
1329
+ if (isValidPath) {
1330
+ this.kernel.store.set("new_home", config.home)
1331
+ } else {
1332
+ throw new Error("Invalid path: " + config.home)
1333
+ }
1334
+
1335
+ // check if the destination already exists => throw error
1336
+ let exists = await fse.pathExists(config.home)
1337
+ if (exists) {
1338
+ throw new Error(`The path ${config.home} already exists. Please remove the folder and retry`)
1339
+ }
1340
+ }
1341
+
1342
+ home = this.kernel.store.get("home")
1343
+ theme = this.kernel.store.get("theme")
1344
+ console.log("after setting config", { home, theme })
1266
1345
  }
1267
1346
  async start(debug) {
1268
1347
 
@@ -1277,6 +1356,11 @@ class Server {
1277
1356
  await this.httpTerminator.terminate();
1278
1357
  console.log("terminate end")
1279
1358
  }
1359
+
1360
+ // configure from kernel.store
1361
+ this.syncConfig()
1362
+
1363
+ // initialize kernel
1280
1364
  await this.kernel.init()
1281
1365
 
1282
1366
 
@@ -1315,7 +1399,7 @@ class Server {
1315
1399
 
1316
1400
 
1317
1401
 
1318
- await this.configure()
1402
+ //await this.configure()
1319
1403
 
1320
1404
 
1321
1405
  this.started = false
@@ -1334,7 +1418,7 @@ class Server {
1334
1418
  this.app.use(cookieParser());
1335
1419
  this.app.use(session({secret: "secret" }))
1336
1420
 
1337
- let home = this.kernel.store.get("home")
1421
+ let home = this.kernel.homedir
1338
1422
  this.app.get("/", async (req, res) => {
1339
1423
  console.log("### req.headers", req.headers)
1340
1424
 
@@ -1867,104 +1951,13 @@ class Server {
1867
1951
  })
1868
1952
  this.app.post("/config", async (req, res) => {
1869
1953
  console.log("/config", { body: req.body })
1870
- await this.configure(req.body)
1871
-
1872
- // get the existing homedir
1873
- let existingHome = this.kernel.homedir
1874
- console.log("Existinghome", existingHome)
1875
-
1876
- // update the pinokio.json file
1877
- if (req.body.home && req.body.home.length > 0) {
1878
- if (req.body.home === existingHome) {
1879
- res.json({ success: true })
1880
- } else {
1881
- const basename = path.basename(req.body.home)
1882
- let isValidPath = (basename !== '' && basename !== req.body.home);
1883
- console.log({isValidPath, basename})
1884
- if (isValidPath) {
1885
- // move the existing home directory to the new home directory
1886
-
1887
-
1888
- try {
1889
- // First move the home
1890
- //await fse.move(existingHome, req.body.home)
1891
- console.log({ home: req.body.home, existingHome })
1892
-
1893
- // check if the dest exists
1894
- // if it exists, display an error
1895
- // console.log("REMOVE DEST IF EXISTS")
1896
- // await fse.remove(req.body.home)
1897
- let exists = await fse.pathExists(req.body.home)
1898
- console.log({ exists, home: req.body.home })
1899
- if (exists) {
1900
- res.json({ error: `The path ${req.body.home} already exists. Please remove the folder and retry` })
1901
- return
1902
- }
1903
-
1904
- console.log("RUNNING MV")
1905
- await this.kernel.bin.exec({
1906
- message: `${(this.kernel.platform === "win32" ? "move" : "mv")} ${existingHome} ${req.body.home}`,
1907
- sudo: true
1908
- }, (stream) => {
1909
- console.log(stream)
1910
- })
1911
- console.log("FINISHED MV")
1912
-
1913
- //// Next, empty the bin folder => need to reinitialize because of symlinks, etc. with the package managers
1914
- let defaultBin = path.resolve(req.body.home, "bin")
1915
- await rimraf(defaultBin)
1916
-
1917
- // set the home variable
1918
- this.kernel.store.set("home", req.body.home)
1919
-
1920
- res.json({ success: true })
1921
- } catch (e) {
1922
- console.log("ERROR", e)
1923
- res.json({ error: e.stack })
1924
- }
1925
-
1926
- } else {
1927
- res.json({ error: "invalid filepath" })
1928
- }
1929
- }
1930
- } else {
1931
- // if the home directory is empty, remove the home attribute, and move the existing home to the homedir
1932
- let defaultHome = path.resolve(os.homedir(), "pinokio")
1933
- try {
1934
- // First, move the home
1935
- //await fse.move(existingHome, defaultHome)
1936
- console.log({ defaultHome, existingHome })
1937
-
1938
- // console.log("REMOVE DEST IF EXISTS")
1939
- // await fse.remove(defaultHome)
1940
- let exists = await fse.pathExists(defaultHome)
1941
- console.log("Exists", exists)
1942
- if (exists) {
1943
- res.json({ error: `The path ${defaultHome} already exists. Please remove the folder and retry` })
1944
- return
1945
- }
1946
-
1947
- console.log("RUNNING MV")
1948
- await this.kernel.bin.exec({
1949
- message: `${(this.kernel.platform === "win32" ? "move" : "mv")} ${existingHome} ${defaultHome}`,
1950
- sudo: true
1951
- }, (stream) => {
1952
- console.log(stream)
1953
- })
1954
- console.log("FINISHED MV")
1955
-
1956
- //// Next, empty the bin folder => need to reinitialize because of symlinks, etc. with the package managers
1957
- let defaultBin = path.resolve(defaultHome, "bin")
1958
- await rimraf(defaultBin)
1959
-
1960
- //this.kernel.store.set("home", null)
1961
- this.kernel.store.set("home", defaultHome)
1962
-
1963
- res.json({ success: true })
1964
- } catch (e) {
1965
- res.json({ error: e.stack })
1966
- }
1954
+ try {
1955
+ await this.setConfig(req.body)
1956
+ res.json({ success: true })
1957
+ } catch (e) {
1958
+ res.json({ error: e.stack })
1967
1959
  }
1960
+
1968
1961
  // update homedir
1969
1962
  })
1970
1963
  // install