pinokiod 0.1.6 → 0.1.7

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.7",
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,27 @@ 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
+ sudo.exec(message, { name: "Pinokio" }, (err, stdout, stderr) => {
1233
+ if (err) {
1234
+ reject(err)
1235
+ } else if (stderr) {
1236
+ reject(stderr)
1237
+ } else {
1238
+ resolve(stdout)
1239
+ }
1240
+ });
1241
+ })
1242
+ return response
1243
+ }
1244
+ async syncConfig() {
1245
+ // 1. THEME
1246
+ this.theme = this.kernel.store.get("theme") || "light"
1253
1247
  if (this.theme === "dark") {
1254
1248
  this.colors = {
1255
1249
  color: "rgb(31, 29, 39)",
@@ -1263,6 +1257,86 @@ class Server {
1263
1257
  }
1264
1258
  //this.logo = (this.theme === 'dark' ? "<img class='icon' src='/pinokio-white.png'>" : "<img class='icon' src='/pinokio-black.png'>")
1265
1259
  this.logo = '<i class="fa-solid fa-house"></i>'
1260
+
1261
+ // 2. HOME
1262
+ // 2.1. Check if the config includes NEW_HOME => if so,
1263
+ // - move the HOME folder to NEW_HOME
1264
+ // - set HOME=NEW_HOME
1265
+ // - remove NEW_HOME
1266
+ let new_home = this.kernel.store.get("new_home")
1267
+ console.log("new_home", new_home)
1268
+ if (new_home) {
1269
+ // move the home folder to new_home
1270
+
1271
+ let existing_home = this.kernel.store.get("home")
1272
+
1273
+ console.log({ existing_home, new_home })
1274
+
1275
+ if (existing_home) {
1276
+ // if the home is already set, move
1277
+ console.log("RUNNING MV")
1278
+ const mv_cmd = `${(this.kernel.platform === "win32" ? "move" : "mv")} ${existing_home} ${new_home}`
1279
+ await this.sudo_exec(mv_cmd, existing_home)
1280
+ console.log("FINISHED MV")
1281
+
1282
+ //// Next, empty the bin folder => need to reinitialize because of symlinks, etc. with the package managers
1283
+ console.log("RUNNING RM BIN")
1284
+ const path_to_delete = path.resolve(new_home, "bin")
1285
+ let del_cmd
1286
+ if (this.kernel.platform === "win32") {
1287
+ del_cmd = `rd /s /q ${path_to_delete}`
1288
+ } else {
1289
+ del_cmd = `rm -rf ${path_to_delete}`
1290
+ }
1291
+ console.log("del_cmd", del_cmd)
1292
+
1293
+ await this.sudo_exec(del_cmd, new_home)
1294
+ console.log("FINISHED RM BIN")
1295
+
1296
+ // set home=new_home
1297
+ this.kernel.store.set("home", new_home)
1298
+ // delete new_home
1299
+ console.log("STORE", this.kernel.store)
1300
+ this.kernel.store.delete("new_home")
1301
+ } else {
1302
+ // if the home is not already set, don't need to do anything
1303
+ }
1304
+ }
1305
+ }
1306
+ async setConfig(config) {
1307
+ console.log("setConfig", config)
1308
+ let home = this.kernel.store.get("home")
1309
+ let theme = this.kernel.store.get("theme")
1310
+ console.log("before setting config", { home, theme })
1311
+
1312
+ // 1. Handle THEME
1313
+ if (config.theme) {
1314
+ this.kernel.store.set("theme", config.theme)
1315
+ //this.theme = config.theme
1316
+ }
1317
+ // 2. Handle HOME
1318
+ if (config.home) {
1319
+ // set "new_home"
1320
+ const basename = path.basename(config.home)
1321
+
1322
+ // check for invalid path
1323
+ let isValidPath = (basename !== '' && basename !== config.home)
1324
+ if (isValidPath) {
1325
+ this.kernel.store.set("new_home", config.home)
1326
+ } else {
1327
+ throw new Error("Invalid path: " + config.home)
1328
+ }
1329
+
1330
+ // check if the destination already exists => throw error
1331
+ let exists = await fse.pathExists(config.home)
1332
+ if (exists) {
1333
+ throw new Error(`The path ${config.home} already exists. Please remove the folder and retry`)
1334
+ }
1335
+ }
1336
+
1337
+ home = this.kernel.store.get("home")
1338
+ theme = this.kernel.store.get("theme")
1339
+ console.log("after setting config", { home, theme })
1266
1340
  }
1267
1341
  async start(debug) {
1268
1342
 
@@ -1277,6 +1351,11 @@ class Server {
1277
1351
  await this.httpTerminator.terminate();
1278
1352
  console.log("terminate end")
1279
1353
  }
1354
+
1355
+ // configure from kernel.store
1356
+ this.syncConfig()
1357
+
1358
+ // initialize kernel
1280
1359
  await this.kernel.init()
1281
1360
 
1282
1361
 
@@ -1315,7 +1394,7 @@ class Server {
1315
1394
 
1316
1395
 
1317
1396
 
1318
- await this.configure()
1397
+ //await this.configure()
1319
1398
 
1320
1399
 
1321
1400
  this.started = false
@@ -1334,7 +1413,7 @@ class Server {
1334
1413
  this.app.use(cookieParser());
1335
1414
  this.app.use(session({secret: "secret" }))
1336
1415
 
1337
- let home = this.kernel.store.get("home")
1416
+ let home = this.kernel.homedir
1338
1417
  this.app.get("/", async (req, res) => {
1339
1418
  console.log("### req.headers", req.headers)
1340
1419
 
@@ -1867,104 +1946,13 @@ class Server {
1867
1946
  })
1868
1947
  this.app.post("/config", async (req, res) => {
1869
1948
  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
- }
1949
+ try {
1950
+ await this.setConfig(req.body)
1951
+ res.json({ success: true })
1952
+ } catch (e) {
1953
+ res.json({ error: e.stack })
1967
1954
  }
1955
+
1968
1956
  // update homedir
1969
1957
  })
1970
1958
  // install