@makano/rew 1.1.12 → 1.1.17

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/bin/rew CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  const path = require('path');
3
3
  const fs = require('fs');
4
- const rew_mod = path.resolve(process.cwd(), 'node_modules/@makano/rew');
4
+ const rew_mod = path.resolve(process.cwd(), 'snode_moduless/@makano/rew');
5
5
  if(fs.existsSync(rew_mod)){
6
6
  require(path.join(rew_mod, 'lib/rew/cli/cli.js'));
7
7
  } else {
@@ -47,8 +47,6 @@ yargs(hideBin(process.argv))
47
47
  data.forEach(file => {
48
48
  watchIt(file);
49
49
  });
50
- } else {
51
- process.exit();
52
50
  }
53
51
  }).send({ filePath, watch: argv.watch });
54
52
  if(argv.watch) watchIt(filePath);
@@ -1,6 +1,4 @@
1
- const path = require('path');
2
1
  const { run } = require('../main');
3
- const { watch } = require('fs');
4
2
 
5
3
 
6
4
  function exec(filePath){
@@ -8,13 +6,12 @@ function exec(filePath){
8
6
  .context.module.imports;
9
7
  }
10
8
 
11
-
12
- process.on('message', ({ filePath, watch }) => {
9
+ const onmsg = ({ filePath, watch }) => {
13
10
  const imports = exec(filePath);
14
11
  if(watch){
15
12
  process.send(imports);
16
- process.exit();
17
- } else {
18
- process.exit();
19
13
  }
20
- });
14
+ process.off('message', onmsg);
15
+ }
16
+
17
+ process.on('message', onmsg);
@@ -107,7 +107,7 @@ module.exports = {
107
107
  const c = jsYaml.load(fs.readFileSync(confPath, { encoding: 'utf-8' }));
108
108
  if(c.entry){
109
109
  const r = path.resolve(root, c.entry);
110
- const mod_path = path.resolve(root, 'node_modules/@makano/rew');
110
+ const mod_path = path.resolve(root, 'snode_moduless/@makano/rew');
111
111
  const mod_path_lib = path.join(mod_path, 'lib/rew/cli');
112
112
  if(fs.existsSync(mod_path) && __dirname !== mod_path_lib){
113
113
  const mod_path_utilsjs = path.join(mod_path_lib, '../main.js');
@@ -7,6 +7,7 @@ const { match } = require("../functions/match");
7
7
  const { map } = require("../functions/map");
8
8
  const { typex, typeis, typedef, typei } = require("../functions/types");
9
9
  const { isEmpty, clone, deepClone, merge, uniqueId, compose, curry } = require("../functions/core");
10
+ const { print } = require("../functions/stdout");
10
11
 
11
12
  module.exports = {
12
13
  cenum,
@@ -29,7 +30,5 @@ module.exports = {
29
30
  compose,
30
31
  curry,
31
32
 
32
- print: function (...arguments) {
33
- return console.log(...arguments);
34
- },
33
+ print
35
34
  };
@@ -0,0 +1,8 @@
1
+ const execOptions = {
2
+ sharedContext: true,
3
+ resolveExtensions: [{ext: '.js', options: { type: 'js' }}, '.coffee'],
4
+ nativeRequire: false,
5
+ cwdAlias: '$'
6
+ }
7
+
8
+ module.exports.execOptions = execOptions;
@@ -1,4 +1,5 @@
1
1
 
2
+
2
3
  function isEmpty(value) {
3
4
  if (Array.isArray(value) || typeof value === 'string') {
4
5
  return value.length === 0;
@@ -0,0 +1,54 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { execOptions } = require('../const/opt');
4
+
5
+
6
+ module.exports = (currentFile) => {
7
+
8
+ function gp(filepath){
9
+ return path.resolve(filepath.startsWith(execOptions.cwdAlias) ? process.cwd() : path.dirname(currentFile), filepath.replaceAll(execOptions.cwdAlias+'/', ''));
10
+ }
11
+
12
+ function read(filepath, options = { encoding: 'utf-8' }){
13
+ return fs.readFileSync(gp(filepath), options);
14
+ }
15
+
16
+ function write(filepath, content, options){
17
+ return fs.writeFileSync(gp(filepath), content, options);
18
+ }
19
+
20
+ function exists(filepath, options){
21
+ return fs.existsSync(filepath);
22
+ }
23
+
24
+ function fstat(filepath, options){
25
+ return fs.statSync(gp(filepath), options);
26
+ }
27
+
28
+ function rm(filepath, options){
29
+ return fs.unlinkSync(filepath);
30
+ }
31
+
32
+ function chmod(filepath, mode, options){
33
+ return fs.chmodSync(gp(filepath), mode);
34
+ }
35
+
36
+ function mkdir(filepath, options){
37
+ return fs.mkdirSync(gp(filepath), options);
38
+ }
39
+
40
+ function ls(filepath, options){
41
+ return fs.readdirSync(gp(filepath), options);
42
+ }
43
+
44
+ return {
45
+ ls,
46
+ mkdir,
47
+ chmod,
48
+ rm,
49
+ fstat,
50
+ exists,
51
+ write,
52
+ read
53
+ };
54
+ }
@@ -5,6 +5,9 @@ const { findPackage, getPackage } = require("../pkgs/pkgs");
5
5
  const { existsSync, readFileSync } = require("fs");
6
6
  const conf = require("../pkgs/conf");
7
7
  const jsYaml = require("js-yaml");
8
+ const { execOptions } = require("../const/opt");
9
+
10
+ const cachedFiles = [];
8
11
 
9
12
  const lookUpInOtherApps = (fullPath) => {
10
13
  const con = conf({});
@@ -30,26 +33,45 @@ module.exports.imp = function (runPath, context) {
30
33
 
31
34
  // console.log(typeof runPath);
32
35
 
33
- if(!ispkg && !existsSync(filepath)){
36
+ const lookUp = () => {
34
37
  const otherPath = lookUpInOtherApps(filename);
35
38
  if(!otherPath) throw new Error('Module "'+filename+'" not found');
36
39
  else filepath = otherPath;
37
40
  }
38
41
 
42
+ const foundCache = cachedFiles.find(f => f.filepath == filepath);
43
+
44
+ if(!ispkg && foundCache){
45
+ exports = foundCache.exports;
46
+ }
47
+
48
+ if(!ispkg && !existsSync(filepath)){
49
+ if(Array.isArray(execOptions.resolveExtensions) && execOptions.resolveExtensions.length){
50
+ const resolve = execOptions.resolveExtensions.find(ext => typeof ext == "string" ? existsSync(filepath+ext) : existsSync(filepath+(ext.ext || '')));
51
+ if(resolve) {
52
+ filepath += typeof resolve == "string" ? resolve : resolve.ext;
53
+ if(typeof resolve == "object" && resolve.options){
54
+ if(resolve.options.type) type = resolve.options.type;
55
+ for(let i in resolve.options) options[i] = resolve.options[i];
56
+ }
57
+ } else lookUp();
58
+ } else lookUp();
59
+ }
60
+
61
+ const exec = (coptions = {}) => runPath(
62
+ filepath,
63
+ { ...options, useContext: execOptions.sharedContext == false ? false : true, ...coptions },
64
+ execOptions.sharedContext == false ? {} : context,
65
+ ).context.module.exports;
66
+
39
67
  if (ispkg) {
40
68
  exports = getPackage(filename)(context);
69
+ } else if(foundCache) {
70
+
41
71
  } else if (type == "coffee") {
42
- exports = runPath(
43
- filepath,
44
- { ...options, useContext: true },
45
- context,
46
- ).context.module.exports;
72
+ exports = exec({ });
47
73
  } else if (type == "js") {
48
- exports = runPath(
49
- filepath,
50
- { ...options, useContext: true, compile: false },
51
- context,
52
- ).context.module.exports;
74
+ exports = exec({ compile: false });
53
75
  } else if (type == "yaml" || type == "json" || type == "text") {
54
76
  const f = getFile(filepath);
55
77
  if (type == "yaml") {
@@ -74,6 +96,7 @@ module.exports.imp = function (runPath, context) {
74
96
  }
75
97
 
76
98
  if(!ispkg) context.module.imports.push(filepath);
99
+ if(!ispkg) cachedFiles.push({ filepath, exports });
77
100
 
78
101
  return exports;
79
102
  };
@@ -0,0 +1,41 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+
5
+ module.exports.customRequire = function customRequire(modulePath, filePath) {
6
+ const resolvedPath = resolveModulePath(modulePath, filePath);
7
+ return require(resolvedPath);
8
+ }
9
+
10
+ function resolveModulePath(modulePath, filePath) {
11
+ if (modulePath.startsWith('./') || modulePath.startsWith('../') || path.isAbsolute(modulePath)) {
12
+ return path.resolve(modulePath);
13
+ }
14
+
15
+ const paths = module.constructor._nodeModulePaths(path.dirname(filePath));
16
+ for (const basePath of paths) {
17
+ const fullPath = path.join(basePath, modulePath);
18
+ if (fs.existsSync(fullPath + '.js')) {
19
+ return fullPath + '.js';
20
+ }
21
+ if (fs.existsSync(fullPath + '.json')) {
22
+ return fullPath + '.json';
23
+ }
24
+ if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
25
+ const packageJsonPath = path.join(fullPath, 'package.json');
26
+ if (fs.existsSync(packageJsonPath)) {
27
+ const main = require(packageJsonPath).main || 'index.js';
28
+ const mainPath = path.join(fullPath, main);
29
+ if (fs.existsSync(mainPath)) {
30
+ return mainPath;
31
+ }
32
+ }
33
+ const indexPath = path.join(fullPath, 'index.js');
34
+ if (fs.existsSync(indexPath)) {
35
+ return indexPath;
36
+ }
37
+ }
38
+ }
39
+
40
+ throw new Error(`Cannot find module '${modulePath}'`);
41
+ }
@@ -0,0 +1,5 @@
1
+
2
+
3
+ module.exports.print = function print(...arguments) {
4
+ return console.log(...arguments);
5
+ };
@@ -1,7 +1,10 @@
1
1
  const defaultContext = require("../const/default");
2
+ const { execOptions } = require("../const/opt");
2
3
  const emitter = require("../functions/emitter");
3
4
  const { exportsFunction } = require("../functions/export");
4
5
  const { imp } = require("../functions/import");
6
+ const { customRequire } = require("../functions/require");
7
+ const fsLib = require('../functions/fs');
5
8
 
6
9
  module.exports.prepareContext = function (
7
10
  custom_context,
@@ -16,6 +19,7 @@ module.exports.prepareContext = function (
16
19
  filepath,
17
20
  imports: []
18
21
  },
22
+ ...fsLib(filepath)
19
23
  };
20
24
  if (options.useContext) {
21
25
  context = {
@@ -28,11 +32,17 @@ module.exports.prepareContext = function (
28
32
  ...defaultContext,
29
33
  require: (package) => {
30
34
  try {
31
- return require(package);
35
+ return execOptions.nativeRequire || package.startsWith('node:') ? require(package.startsWith('node:') ? package.split('node:')[1] : package) : customRequire(package, filepath);
32
36
  } catch (e) {
33
- throw new Error("Module not found");
37
+ throw new Error("Module "+package+" not found");
34
38
  }
35
39
  },
40
+ opt: {
41
+ set: (key, value) => execOptions[key] = value,
42
+ get: (key) => execOptions[key],
43
+ push: (key, value) => execOptions[key]?.push(value),
44
+ pop: (key) => execOptions[key]?.pop()
45
+ },
36
46
  ...custom_context,
37
47
  };
38
48
  context.imp = imp(runPath, context);
@@ -44,6 +54,8 @@ module.exports.prepareContext = function (
44
54
  argv: process.argv,
45
55
  target: emitter(),
46
56
  env: process.env,
57
+ cwd: () => process.cwd(),
58
+ arch: process.arch
47
59
  };
48
60
  // console.log(custom_context);
49
61
  return context;
@@ -0,0 +1,9 @@
1
+
2
+
3
+ module.exports = (context) => ({
4
+ has: (key) => key in process.env,
5
+ get: (key) => process.env[key],
6
+ set: (key, value) => process.env[key] = value,
7
+ rm: (key) => delete process.env[key],
8
+ is: (key, value) => process.env[key] == value
9
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.1.12",
3
+ "version": "1.1.17",
4
4
  "description": "A simple coffescript runtime",
5
5
  "main": "lib/rew/main.js",
6
6
  "directories": {