@xnoxs/flux-lang 4.0.2 → 4.0.4

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.
@@ -1,3 +1,5 @@
1
+ /* compiled from src/self/config.flux by Flux Lang */
2
+ 'use strict';
1
3
  // ── Flux stdlib ──
2
4
 
3
5
  function join(arr, sep) { return arr.join(sep != null ? sep : ','); }
@@ -14,10 +16,23 @@ function endsWith(s, suffix) { return String(s).endsWith(suffix); }
14
16
 
15
17
  const Fs = require("fs");
16
18
  const Path = require("path");
17
- const DEFAULT_CONFIG = { entry: "src/main.flux", outDir: "dist", sourcemap: false, mangle: false, jsx: false, jsxTarget: "browser", typecheck: true, strict: false, watch: false, ignore: [], selfHosted: false, registry: "https://registry.flux-lang.dev", pkg: { name: "", version: "1.0.0", description: "", author: "", license: "MIT", deps: { }, devDeps: { } } };
19
+ const DEFAULT_CONFIG = { entry: "src/main.flux", outDir: "dist", sourcemap: false, mangle: false, jsx: false, jsxTarget: "browser", typecheck: true, strict: false, watch: false, ignore: [], selfHosted: false, registry: "https://registry.npmjs.org", pkg: { name: "", version: "1.0.0", description: "", author: "", license: "MIT", deps: { }, devDeps: { } } };
18
20
  module.exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
19
21
  function loadConfig(cwd_) {
20
22
  const cwd = (cwd_ ?? process.cwd());
23
+ const pkgJsonPath = Path.join(cwd, "package.json");
24
+ if (Fs.existsSync(pkgJsonPath)) {
25
+ try {
26
+ const raw = Fs.readFileSync(pkgJsonPath, "utf8");
27
+ const parsed = JSON.parse(raw);
28
+ if ((parsed.flux && (typeof parsed.flux == "object"))) {
29
+ return mergeConfig(DEFAULT_CONFIG, parsed.flux);
30
+ }
31
+ }
32
+ catch (e) {
33
+ throw new Error(("Invalid package.json: " + e.message));
34
+ }
35
+ }
21
36
  const jsonPath = Path.join(cwd, "flux.json");
22
37
  if (Fs.existsSync(jsonPath)) {
23
38
  try {
@@ -29,16 +44,6 @@ function loadConfig(cwd_) {
29
44
  throw new Error(("Invalid flux.json: " + e.message));
30
45
  }
31
46
  }
32
- const jsPath = Path.join(cwd, "flux.config.js");
33
- if (Fs.existsSync(jsPath)) {
34
- try {
35
- const loaded = require(jsPath);
36
- return mergeConfig(DEFAULT_CONFIG, loaded);
37
- }
38
- catch (e2) {
39
- throw new Error(("Invalid flux.config.js: " + e2.message));
40
- }
41
- }
42
47
  return { ...DEFAULT_CONFIG };
43
48
  }
44
49
  module.exports.loadConfig = loadConfig;
@@ -58,9 +63,18 @@ function mergeConfig(base, overrides) {
58
63
  module.exports.mergeConfig = mergeConfig;
59
64
  function writeConfig(config, cwd_) {
60
65
  const cwd = (cwd_ ?? process.cwd());
61
- const jsonPath = Path.join(cwd, "flux.json");
62
- const content = (JSON.stringify(config, null, 2) + "\n");
63
- Fs.writeFileSync(jsonPath, content, "utf8");
66
+ const pkgPath = Path.join(cwd, "package.json");
67
+ let pkg = { };
68
+ if (Fs.existsSync(pkgPath)) {
69
+ try {
70
+ pkg = JSON.parse(Fs.readFileSync(pkgPath, "utf8"));
71
+ }
72
+ catch (e) {
73
+ pkg = { };
74
+ }
75
+ }
76
+ pkg.flux = config;
77
+ Fs.writeFileSync(pkgPath, (JSON.stringify(pkg, null, 2) + "\n"), "utf8");
64
78
  }
65
79
  module.exports.writeConfig = writeConfig;
66
80
  function validateConfig(config) {
@@ -76,19 +90,19 @@ function validateConfig(config) {
76
90
  module.exports.validateConfig = validateConfig;
77
91
  function readPackage(cwd_) {
78
92
  const cwd = (cwd_ ?? process.cwd());
79
- const fluxJson = Path.join(cwd, "flux.json");
80
93
  const pkgJson = Path.join(cwd, "package.json");
81
- if (Fs.existsSync(fluxJson)) {
94
+ const fluxJson = Path.join(cwd, "flux.json");
95
+ if (Fs.existsSync(pkgJson)) {
82
96
  try {
83
- return JSON.parse(Fs.readFileSync(fluxJson, "utf8"));
97
+ return JSON.parse(Fs.readFileSync(pkgJson, "utf8"));
84
98
  }
85
99
  catch (e) {
86
100
  return null;
87
101
  }
88
102
  }
89
- if (Fs.existsSync(pkgJson)) {
103
+ if (Fs.existsSync(fluxJson)) {
90
104
  try {
91
- return JSON.parse(Fs.readFileSync(pkgJson, "utf8"));
105
+ return JSON.parse(Fs.readFileSync(fluxJson, "utf8"));
92
106
  }
93
107
  catch (e2) {
94
108
  return null;
package/src/self/pkg.js CHANGED
@@ -1,7 +1,7 @@
1
+ /* compiled from src/self/pkg.flux by Flux Lang */
2
+ 'use strict';
1
3
  // ── Flux stdlib ──
2
4
 
3
- function map(arr, fn) { return arr.map(fn); }
4
-
5
5
  function join(arr, sep) { return arr.join(sep != null ? sep : ','); }
6
6
 
7
7
  function keys(obj) { return Object.keys(obj); }
@@ -17,10 +17,8 @@ const Path = require("path");
17
17
  const Https = require("https");
18
18
  const Http = require("http");
19
19
  const Os = require("os");
20
- const { readPackage, writeConfig, loadConfig } = require("./config");
20
+ const { readPackage } = require("./config");
21
21
  const REGISTRY_URL = "https://registry.npmjs.org";
22
- const FLUX_PKG_DIR = "flux_modules";
23
- const PKG_FILE = "flux.json";
24
22
  const ESC = "\u001b";
25
23
  const C = { reset: (ESC + "[0m"), bold: (ESC + "[1m"), dim: (ESC + "[2m"), red: (ESC + "[31m"), green: (ESC + "[32m"), yellow: (ESC + "[33m"), blue: (ESC + "[34m"), cyan: (ESC + "[36m"), gray: (ESC + "[90m") };
26
24
  function clr(c, s) {
@@ -79,21 +77,6 @@ async function fetchJson(url) {
79
77
  }
80
78
  return new Promise(doRequest);
81
79
  }
82
- function ensureFluxJson(cwd_) {
83
- const cwd = (cwd_ ?? process.cwd());
84
- const file = Path.join(cwd, PKG_FILE);
85
- if (!Fs.existsSync(file)) {
86
- const pkg = { name: Path.basename(cwd), version: "1.0.0", description: "", author: "", license: "MIT", scripts: { start: "flux run src/main.flux", build: "flux bundle src/main.flux -o dist/bundle.js", dev: "flux watch src/main.flux", check: "flux check src/main.flux" }, dependencies: { }, devDependencies: { } };
87
- Fs.writeFileSync(file, (JSON.stringify(pkg, null, 2) + "\n"), "utf8");
88
- ok("Created flux.json");
89
- }
90
- return JSON.parse(Fs.readFileSync(file, "utf8"));
91
- }
92
- function saveFluxJson(pkg, cwd_) {
93
- const cwd = (cwd_ ?? process.cwd());
94
- const file = Path.join(cwd, PKG_FILE);
95
- Fs.writeFileSync(file, (JSON.stringify(pkg, null, 2) + "\n"), "utf8");
96
- }
97
80
  function parsePackageSpec(spec) {
98
81
  const atIdx = spec.lastIndexOf("@");
99
82
  if ((atIdx > 0)) {
@@ -102,118 +85,61 @@ function parsePackageSpec(spec) {
102
85
  return { name: spec, version: "latest" };
103
86
  }
104
87
  async function cmdAdd(specs, opts) {
88
+ const { execSync } = require("child_process");
105
89
  const isDev = (opts?.dev ?? false);
106
90
  const cwd = process.cwd();
107
- const pkg = ensureFluxJson(cwd);
108
91
  for (const spec of specs) {
109
92
  const { name, version } = parsePackageSpec(spec);
110
- const spinner = startSpinner(((("Fetching " + clr(C.bold, name)) + clr(C.gray, ("@" + version))) + " ..."));
93
+ const spinner = startSpinner(((("Adding " + clr(C.bold, name)) + clr(C.gray, ("@" + version))) + " ..."));
111
94
  try {
112
- const info_ = await fetchJson(((REGISTRY_URL + "/") + name));
113
- const resolvedVersion = ((version == "latest") ? (info_["dist-tags"]?.latest ?? "1.0.0") : version);
114
- const versionInfo = info_.versions?.[resolvedVersion];
95
+ const saveFlag = (isDev ? "--save-dev" : "--save");
96
+ const pkgSpec = ((version == "latest") ? name : ((name + "@") + version));
115
97
  stopSpinner(spinner);
116
- if (!versionInfo) {
117
- err(((("Version " + resolvedVersion) + " not found for ") + name));
118
- continue;
119
- }
120
- const depKey = (isDev ? "devDependencies" : "dependencies");
121
- if (!pkg[depKey]) {
122
- pkg[depKey] = { };
123
- }
124
- pkg[depKey][name] = ("^" + resolvedVersion);
125
- saveFluxJson(pkg, cwd);
126
- const desc = (versionInfo.description ? clr(C.gray, (" — " + versionInfo.description)) : "");
127
- ok(((name + clr(C.green, ("@" + resolvedVersion))) + desc));
128
- info(("Added to " + (isDev ? "devDependencies" : "dependencies")));
98
+ console.log(((clr(C.cyan, " Adding ") + clr(C.bold, pkgSpec)) + clr(C.gray, " to node_modules/ and package.json...")));
99
+ execSync(((("npm install " + pkgSpec) + " ") + saveFlag), { cwd, stdio: "inherit" });
100
+ ok((("Added " + clr(C.bold, name)) + " to node_modules/ and package.json"));
129
101
  }
130
102
  catch (e) {
131
103
  stopSpinner(spinner);
132
- err(((("Failed to fetch " + name) + ": ") + e.message));
104
+ err(((("Failed to add " + name) + ": ") + e.message));
133
105
  }
134
106
  }
135
107
  console.log();
136
- console.log(((clr(C.gray, " Run ") + clr(C.yellow, "flux install")) + clr(C.gray, " to install packages")));
137
- console.log();
138
108
  }
139
109
  module.exports.cmdAdd = cmdAdd;
140
110
  function cmdRemove(names, opts) {
111
+ const { execSync } = require("child_process");
141
112
  const cwd = process.cwd();
142
- const pkg = ensureFluxJson(cwd);
143
- let removed = 0;
144
113
  for (const name of names) {
145
- let found = false;
146
- if ((pkg.dependencies && pkg.dependencies[name])) {
147
- delete pkg.dependencies[name];
148
- found = true;
149
- }
150
- if ((pkg.devDependencies && pkg.devDependencies[name])) {
151
- delete pkg.devDependencies[name];
152
- found = true;
153
- }
154
- if (found) {
114
+ try {
115
+ console.log(((clr(C.cyan, " Removing ") + clr(C.bold, name)) + " ..."));
116
+ execSync(("npm uninstall " + name), { cwd, stdio: "inherit" });
155
117
  ok(("Removed " + clr(C.bold, name)));
156
- removed = (removed + 1);
157
118
  }
158
- else {
159
- err((name + " not found in flux.json"));
119
+ catch (e) {
120
+ err(((("Failed to remove " + name) + ": ") + e.message));
160
121
  }
161
122
  }
162
- if ((removed > 0)) {
163
- saveFluxJson(pkg, cwd);
164
- }
165
123
  }
166
124
  module.exports.cmdRemove = cmdRemove;
167
125
  async function cmdInstall(opts) {
168
126
  const { execSync } = require("child_process");
169
127
  const cwd = process.cwd();
170
- const pkg = ensureFluxJson(cwd);
171
- const fluxModDir = Path.join(cwd, "flux_modules");
172
- const deps = (pkg.dependencies ?? { });
173
- const devDeps = (pkg.devDependencies ?? { });
174
- const all = { ...deps, ...devDeps };
175
- const names = Object.keys(all);
176
- if ((names.length == 0)) {
177
- info("No dependencies found in flux.json");
128
+ const pkgJsonPath = Path.join(cwd, "package.json");
129
+ if (!Fs.existsSync(pkgJsonPath)) {
130
+ err("No package.json found. Run: flux init");
178
131
  return;
179
132
  }
180
- console.log(((clr(C.cyan, "\n Installing ") + names.length) + " package(s) into flux_modules/...\n"));
181
- for (const name of names) {
182
- const spec = all[name];
183
- console.log(((clr(C.gray, " ○ ") + name) + clr(C.gray, ("@" + spec))));
184
- }
185
- console.log();
186
- if (!Fs.existsSync(fluxModDir)) {
187
- Fs.mkdirSync(fluxModDir, { recursive: true });
188
- }
189
- const fluxPkgFile = Path.join(fluxModDir, "package.json");
190
- if (!Fs.existsSync(fluxPkgFile)) {
191
- const fluxPkg = { name: (pkg.name + "-flux-modules"), version: "1.0.0", private: true };
192
- Fs.writeFileSync(fluxPkgFile, (JSON.stringify(fluxPkg, null, 2) + "\n"), "utf8");
193
- }
194
- const prodNames = Object.keys(deps);
195
- const devNames = Object.keys(devDeps);
133
+ console.log(clr(C.cyan, "\n Installing dependencies from package.json...\n"));
196
134
  try {
197
- if ((prodNames.length > 0)) {
198
- const prodPkgs = prodNames.map((n) => ((n + "@") + (deps[n] ?? "latest"))).join(" ");
199
- const installCmd = ((("npm install --prefix " + fluxModDir) + " ") + prodPkgs);
200
- info(("Running: " + installCmd));
201
- execSync(installCmd, { cwd, stdio: "inherit" });
202
- }
203
- if ((devNames.length > 0)) {
204
- const devPkgs = devNames.map((n) => ((n + "@") + (devDeps[n] ?? "latest"))).join(" ");
205
- const devCmd = ((("npm install --prefix " + fluxModDir) + " --save-dev ") + devPkgs);
206
- info(("Running: " + devCmd));
207
- execSync(devCmd, { cwd, stdio: "inherit" });
208
- }
135
+ execSync("npm install", { cwd, stdio: "inherit" });
209
136
  console.log();
210
- ok((names.length + " package(s) installed into flux_modules/node_modules/"));
211
- info("Packages resolve automatically when using: flux run, flux bundle");
137
+ ok("Dependencies installed into node_modules/");
212
138
  }
213
139
  catch (e) {
214
140
  console.log();
215
141
  err(("Install failed: " + e.message));
216
- info("Try manually: npm install --prefix ./flux_modules <package>");
142
+ info("Try manually: npm install");
217
143
  }
218
144
  console.log();
219
145
  }
@@ -222,7 +148,7 @@ function cmdList(opts) {
222
148
  const cwd = process.cwd();
223
149
  const pkg = readPackage(cwd);
224
150
  if (!pkg) {
225
- err("No flux.json found. Run: flux init");
151
+ err("No package.json found. Run: flux init");
226
152
  return;
227
153
  }
228
154
  console.log();
@@ -311,11 +237,12 @@ async function cmdInfo(name, opts) {
311
237
  }
312
238
  module.exports.cmdInfo = cmdInfo;
313
239
  async function cmdUpgrade(opts) {
240
+ const { execSync } = require("child_process");
314
241
  const isCheck = (opts?.check ?? false);
315
242
  const cwd = process.cwd();
316
243
  const pkg = readPackage(cwd);
317
244
  if (!pkg) {
318
- err("No flux.json found. Run: flux init");
245
+ err("No package.json found. Run: flux init");
319
246
  return;
320
247
  }
321
248
  const deps = (pkg.dependencies ?? { });
@@ -356,11 +283,9 @@ async function cmdUpgrade(opts) {
356
283
  console.log(((((" " + clr(C.yellow, "↑ ")) + clr(C.bold, name)) + clr(C.gray, ((" " + current) + " → "))) + clr(C.green, ("^" + latest))));
357
284
  }
358
285
  else {
359
- const isDev = (devDeps[name] != null);
360
- const depKey = (isDev ? "devDependencies" : "dependencies");
361
- pkg[depKey][name] = ("^" + latest);
362
286
  updated = (updated + 1);
363
287
  console.log(((((" " + clr(C.green, "✓ ")) + clr(C.bold, name)) + clr(C.gray, ((" " + current) + " → "))) + clr(C.green, ("^" + latest))));
288
+ execSync((((("npm install " + name) + "@") + latest) + " --save"), { cwd, stdio: "pipe" });
364
289
  }
365
290
  }
366
291
  }
@@ -384,9 +309,7 @@ async function cmdUpgrade(opts) {
384
309
  ok("All packages are already up to date");
385
310
  }
386
311
  else {
387
- saveFluxJson(pkg, cwd);
388
- ok((updated + " package(s) updated in flux.json"));
389
- console.log(((clr(C.gray, " Run ") + clr(C.yellow, "flux install")) + clr(C.gray, " to install updated versions")));
312
+ ok((updated + " package(s) updated in node_modules/ and package.json"));
390
313
  }
391
314
  }
392
315
  console.log();
@@ -396,20 +319,19 @@ function cmdPublish(opts) {
396
319
  const cwd = process.cwd();
397
320
  const pkg = readPackage(cwd);
398
321
  if (!pkg) {
399
- err("No flux.json found. Run: flux init");
322
+ err("No package.json found. Run: flux init");
400
323
  return;
401
324
  }
402
325
  console.log();
403
326
  console.log(((clr(C.cyan, " Publishing ") + clr(C.bold, ((pkg.name + "@") + pkg.version))) + " ..."));
404
327
  console.log();
405
- info("Building package...");
406
- info("Checking flux.json...");
328
+ info("Checking package.json...");
407
329
  if (!pkg.name) {
408
- err("flux.json must have a name field");
330
+ err("package.json must have a name field");
409
331
  return;
410
332
  }
411
333
  if (!pkg.version) {
412
- err("flux.json must have a version field");
334
+ err("package.json must have a version field");
413
335
  return;
414
336
  }
415
337
  console.log();