@makano/rew 1.1.15 → 1.1.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
  };
@@ -1,7 +1,8 @@
1
1
  const execOptions = {
2
2
  sharedContext: true,
3
3
  resolveExtensions: [{ext: '.js', options: { type: 'js' }}, '.coffee'],
4
- nativeRequire: false
4
+ nativeRequire: false,
5
+ cwdAlias: '$'
5
6
  }
6
7
 
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
+ }
@@ -3,12 +3,8 @@ const path = require('path');
3
3
 
4
4
 
5
5
  module.exports.customRequire = function customRequire(modulePath, filePath) {
6
- try{
7
- return require(modulePath)
8
- } catch(e){
9
- const resolvedPath = resolveModulePath(modulePath, filePath);
10
- return require(resolvedPath);
11
- }
6
+ const resolvedPath = resolveModulePath(modulePath, filePath);
7
+ return require(resolvedPath);
12
8
  }
13
9
 
14
10
  function resolveModulePath(modulePath, filePath) {
@@ -0,0 +1,5 @@
1
+
2
+
3
+ module.exports.print = function print(...arguments) {
4
+ return console.log(...arguments);
5
+ };
@@ -4,7 +4,7 @@ const emitter = require("../functions/emitter");
4
4
  const { exportsFunction } = require("../functions/export");
5
5
  const { imp } = require("../functions/import");
6
6
  const { customRequire } = require("../functions/require");
7
-
7
+ const fsLib = require('../functions/fs');
8
8
 
9
9
  module.exports.prepareContext = function (
10
10
  custom_context,
@@ -19,12 +19,7 @@ module.exports.prepareContext = function (
19
19
  filepath,
20
20
  imports: []
21
21
  },
22
- opt: {
23
- set: (key, value) => execOptions[key] = value,
24
- get: (key) => execOptions[key],
25
- push: (key, value) => execOptions[key]?.push(value),
26
- pop: (key) => execOptions[key]?.pop()
27
- }
22
+ ...fsLib(filepath)
28
23
  };
29
24
  if (options.useContext) {
30
25
  context = {
@@ -37,11 +32,17 @@ module.exports.prepareContext = function (
37
32
  ...defaultContext,
38
33
  require: (package) => {
39
34
  try {
40
- return execOptions.nativeRequire ? require(package) : customRequire(package, filepath);
35
+ return execOptions.nativeRequire || package.startsWith('node:') ? require(package.startsWith('node:') ? package.split('node:')[1] : package) : customRequire(package, filepath);
41
36
  } catch (e) {
42
37
  throw new Error("Module "+package+" not found");
43
38
  }
44
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
+ },
45
46
  ...custom_context,
46
47
  };
47
48
  context.imp = imp(runPath, context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.1.15",
3
+ "version": "1.1.17",
4
4
  "description": "A simple coffescript runtime",
5
5
  "main": "lib/rew/main.js",
6
6
  "directories": {