plankui 0.1.0 → 0.1.1
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/index.mjs +36 -5
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
// npx plankui@latest add button select dialog
|
|
7
7
|
// npx plankui@latest list
|
|
8
8
|
// npx plankui@latest login YOUR_LICENSE_KEY
|
|
9
|
-
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
10
10
|
import { homedir } from "node:os";
|
|
11
|
-
import { dirname, join, relative } from "node:path";
|
|
11
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
12
12
|
import { spawnSync } from "node:child_process";
|
|
13
13
|
import { createInterface } from "node:readline/promises";
|
|
14
14
|
|
|
15
|
-
const VERSION = "0.1.
|
|
15
|
+
const VERSION = "0.1.1";
|
|
16
16
|
const DEFAULT_REGISTRY = "https://plankui.com/r";
|
|
17
17
|
|
|
18
18
|
/* ── Arguments ── */
|
|
@@ -147,10 +147,41 @@ async function fetchJson(name) {
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
/** Write files under the project base, keeping existing ones unless asked. Returns what happened per file. */
|
|
150
|
+
/** True when `p` is `root` itself or inside it. */
|
|
151
|
+
const inside = (root, p) => p === root || p.startsWith(root + sep);
|
|
152
|
+
|
|
153
|
+
/** The real path of `p`, or of its nearest existing parent, so links are followed before we compare. */
|
|
154
|
+
function realish(p) {
|
|
155
|
+
let dir = p;
|
|
156
|
+
while (!existsSync(dir) && dirname(dir) !== dir) dir = dirname(dir);
|
|
157
|
+
return resolve(realpathSync(dir), relative(dir, p));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Where a registry file may be written: inside the project, never above it.
|
|
162
|
+
* A registry is remote input (and --registry can point anywhere), so a path like "../../.zshrc",
|
|
163
|
+
* an absolute path, or a symlinked folder that leads outside the project stops the whole install.
|
|
164
|
+
*/
|
|
165
|
+
function safeTarget(base, path) {
|
|
166
|
+
const bad = (why) => fail(`The registry sent an unsafe file path (${why}): ${JSON.stringify(path)}. Nothing was written.`);
|
|
167
|
+
if (typeof path !== "string" || !path.trim()) bad("empty");
|
|
168
|
+
if (path.includes("\0")) bad("null byte");
|
|
169
|
+
if (isAbsolute(path) || /^[a-zA-Z]:/.test(path) || path.startsWith("\\\\")) bad("absolute");
|
|
170
|
+
if (path.split(/[\\/]+/).includes("..")) bad("goes up a folder");
|
|
171
|
+
const root = resolve(base);
|
|
172
|
+
const target = resolve(root, path);
|
|
173
|
+
if (!inside(root, target) || target === root) bad("outside the project");
|
|
174
|
+
if (!inside(realish(root), realish(target))) bad("a linked folder leads outside the project");
|
|
175
|
+
if (existsSync(target) && lstatSync(target).isSymbolicLink()) bad("the file is a symlink");
|
|
176
|
+
return target;
|
|
177
|
+
}
|
|
178
|
+
|
|
150
179
|
async function writeFiles(base, files) {
|
|
180
|
+
// Check every path first, so a bad one stops the install before any file is touched.
|
|
181
|
+
const targets = files.map((f) => safeTarget(base, f.path));
|
|
151
182
|
const done = [];
|
|
152
|
-
for (const f of files) {
|
|
153
|
-
const target =
|
|
183
|
+
for (const [i, f] of files.entries()) {
|
|
184
|
+
const target = targets[i];
|
|
154
185
|
const shown = relative(cwd, target);
|
|
155
186
|
if (existsSync(target) && !overwrite) {
|
|
156
187
|
const same = readFileSync(target, "utf8") === f.content;
|