@makano/rew 1.2.94 → 1.2.96

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.
@@ -338,18 +338,45 @@ yargs(hideBin(process.argv))
338
338
  }
339
339
  },
340
340
  )
341
-
342
341
  .command(
343
342
  'cache <command>',
344
343
  'Manage cache',
344
+ (yargs) => {
345
+ yargs
346
+ .positional('command', {
347
+ describe: 'Command to clear/list/show',
348
+ type: 'string',
349
+ })
350
+ .example('rew cache list', 'Lists all caches')
351
+ .example('rew cache clear', 'Clears all caches')
352
+ .example('rew cache clear all', 'Clears all caches')
353
+ .example('rew cache clear [id]', 'Clears all caches for id [id]')
354
+ .example('rew cache show [id]', 'Shows commits downloaded for [id]')
355
+ .example('rew cache show [id]', 'Shows commits downloaded for [id]')
356
+ .example('rew cache show [id]/clone', 'Shows the contents for [id]')
357
+ .example('rew cache show [id]#tag', 'Shows the tag for [id]')
358
+ .example('rew cache show [id]#commit', 'Shows the current commit for [id]')
359
+ .example('rew cache show [id]#name', 'Shows the name for [id]')
360
+ .example('rew cache show [id]/clone/app.yaml', 'Shows the app config for [id]')
361
+ .example('rew cache show [id]/clone/path/to/file', 'Gives you the path to the file inside [id]')
362
+ .example('rew cache show [id]/clone/path/to/file', 'Gives you the path to the file inside [id]')
363
+ .example('rew cache install [id]', 'Installs cache')
364
+ },
365
+ async (argv) => {
366
+ require('./utils').cache(argv.command, ...argv._.slice(1));
367
+ },
368
+ )
369
+ .command(
370
+ 'misc <command>',
371
+ 'Misc functions',
345
372
  (yargs) => {
346
373
  yargs.positional('command', {
347
- describe: 'Command to clear/list',
374
+ describe: 'Misc command name',
348
375
  type: 'string',
349
376
  });
350
377
  },
351
- async (argv) => {
352
- require('./utils').cache(argv.command)
378
+ (argv) => {
379
+ require('./miscUtils')[argv.command](...argv._.slice(1));
353
380
  },
354
381
  )
355
382
  .command(
@@ -35,11 +35,36 @@ function getAllPipeInput(){
35
35
  });
36
36
  }
37
37
 
38
+ /**
39
+ *
40
+ * @param {string} string
41
+ * @param {Record<string,(replacedString: string, originalString: string) => any>} order
42
+ * @returns { { string: string, [key: string]: any } }
43
+ */
44
+ function hashTags(string, order){
45
+ const hashes = Object.keys(order);
46
+ const h = {};
47
+ let s = string;
48
+ for(let i of hashes){
49
+ if(string.includes(`#${i}`)){
50
+ const str = s.replace(`#${i}`, '');
51
+ h[i] = order[i](str, string);
52
+ if(h[i]?.$set) {s = h[i].$set; string = s }
53
+ else s = str;
54
+ }
55
+ }
56
+ return {
57
+ string: s,
58
+ ...h
59
+ };
60
+ }
61
+
38
62
  module.exports = {
39
63
  binpath,
40
64
  logspath,
41
65
  cachepath,
42
66
  localBinPath,
43
67
  npm_package_name,
44
- getAllPipeInput
68
+ getAllPipeInput,
69
+ hashTags
45
70
  }
@@ -0,0 +1,41 @@
1
+ const { findAppInfo } = require("../misc/findAppInfo");
2
+ const { log } = require("./log");
3
+ const colors = require('colors');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const { CONFIG_PATH } = require("../const/config_path");
7
+
8
+ module.exports = {
9
+ types(projectPath){
10
+ if(!projectPath) projectPath = process.cwd();
11
+ else projectPath = path.resolve(process.cwd(), projectPath);
12
+ const projectinfo = findAppInfo(projectPath+'/app.yaml');
13
+ if(!projectinfo){
14
+ log('Path not a rew app'.red.bold, ':end')
15
+ return;
16
+ }
17
+ let typesToLoad = ['rew'];
18
+ if(projectinfo.config?.types){
19
+ typesToLoad = projectinfo.config?.types;
20
+ }
21
+ fs.mkdirSync(path.join(projectPath, 'node_modules/@types/rew'), { recursive: true });
22
+
23
+
24
+ typesToLoad.forEach(name => {
25
+ let filename = name+'.d.ts';
26
+ if(name == 'rew'){
27
+ fs.copyFileSync(path.join(__dirname, '../../../runtime.d.ts'), path.join(projectPath, 'node_modules/@types/rew/index.d.ts'));
28
+ return;
29
+ }
30
+ let p = path.resolve(CONFIG_PATH, name, 'app', 'types.d.ts');
31
+
32
+ if(name.indexOf('/') > -1) {
33
+ const fn = name.split('/').slice(1).join('/')+'.d.ts';
34
+ p = path.resolve(CONFIG_PATH, name.split('/')[0], 'app', fn);
35
+ filename = name.split('/')[0]+'-'+path.basename(fn);
36
+ }
37
+ if(fs.existsSync(p)) fs.copyFileSync(p, path.join(projectPath, 'node_modules/@types/rew/'+filename));
38
+ });
39
+ },
40
+
41
+ }
@@ -12,9 +12,7 @@ const { to_qrew } = require('../qrew/compile');
12
12
  const { findAppInfo } = require('../misc/findAppInfo');
13
13
  const { req } = require('../misc/req');
14
14
  const { CONFIG_PATH } = require('../const/config_path');
15
- const { watch } = require('chokidar');
16
- const { execRewFile, runFileWithArgv } = require('./run');
17
- const { seededID } = require('../misc/seededid');
15
+ const { runFileWithArgv } = require('./run');
18
16
  const loading = require('loading-cli');
19
17
  const sleep = require('../functions/sleep');
20
18
  const { gen_key } = require('../misc/bin');
@@ -24,7 +22,8 @@ const {
24
22
  binpath,
25
23
  logspath,
26
24
  cachepath,
27
- localBinPath
25
+ localBinPath,
26
+ hashTags
28
27
  } = require('./helpers');
29
28
  const { input } = require('../functions/stdout');
30
29
 
@@ -94,9 +93,13 @@ module.exports = {
94
93
  const project = {};
95
94
  const create = () => {
96
95
  fs.mkdirSync(projectPath, { recursive: true });
96
+ const confObj = { manifest: { package: project.package, private: false }, exec: { entry: 'main'+(project.civet ? REW_FILE_TYPE.EXTENSION : '.coffee') }, assets: { icon: 'assets/icon.png', folder: './assets' }, install: { requirements: [] } };
97
97
  const confPath = path.join(projectPath, 'app.yaml');
98
98
  const entryFile = path.join(projectPath, 'main'+(project.civet ? REW_FILE_TYPE.EXTENSION : '.coffee'));
99
- fs.writeFileSync(confPath, jsYaml.dump({ manifest: { package: project.package, private: false }, exec: { entry: 'main'+(project.civet ? REW_FILE_TYPE.EXTENSION : '.coffee') }, assets: { icon: 'assets/icon.png', folder: './assets' }, install: { requirements: [] } }));
99
+ if(project.intellisense) {
100
+ confObj.types = ['rew'];
101
+ }
102
+ fs.writeFileSync(confPath, jsYaml.dump(confObj));
100
103
  fs.writeFileSync(entryFile, `print("Hello World!")`);
101
104
  fs.mkdirSync(path.join(projectPath, 'assets'), { recursive: true });
102
105
  if (project.git) {
@@ -104,8 +107,8 @@ module.exports = {
104
107
  execSync('cd ' + projectPath + ' && git init . && git branch -m main', { stdio: 'ignore' });
105
108
  }
106
109
  if(project.intellisense){
107
- fs.mkdirSync(path.join(projectPath, 'node_modules/@types/rew'), { recursive: true });
108
- fs.copyFileSync(path.join(__dirname, '../../../runtime.d.ts'), path.join(projectPath, 'node_modules/@types/rew/index.d.ts'));
110
+ require('./miscUtils')
111
+ .types(projectPath);
109
112
  }
110
113
  execSync('cd ' + projectPath + ' && npm init -y', { stdio: 'ignore' });
111
114
  if(project.civet){
@@ -382,11 +385,50 @@ module.exports = {
382
385
  processFile(filePath, importsArray);
383
386
  log('󰈔 Compiled'.yellow, (importsArray.length + 1 + '').blue, `file${importsArray.length ? 's' : ''}.`.yellow, ':end');
384
387
  },
385
- cache(command){
388
+ cache(command, file){
389
+
390
+ const findGitCommitPath = (p) => {
391
+ const heads = path.join(p, '.git/refs/heads');
392
+ if(!fs.existsSync(heads)) return '';
393
+ const refs = fs.readdirSync(heads);
394
+ if(!refs.length) return '';
395
+ return fs.readFileSync(path.join(heads, refs[0]), { encoding: 'utf-8' }).trim();
396
+ }
397
+
386
398
  if(command == 'list'){
387
399
  console.log(fs.readdirSync(cachepath).join('\n').trim());
388
- } else {
389
- fs.readdirSync(cachepath).forEach(file => fs.rmSync(path.join(cachepath, file), { recursive: true }));
400
+ } else if(command == 'clear'){
401
+ if(file) fs.rmSync(path.join(cachepath, file), { recursive: true });
402
+ else if(file == 'all') fs.readdirSync(cachepath).forEach(file => fs.rmSync(path.join(cachepath, file), { recursive: true }));
403
+ } else if(command == 'install'){
404
+ if(!file) return process.exit(1);
405
+ this.installApp(path.join(cachepath, file, 'clone'), {
406
+ update: true
407
+ });
408
+ } else if(command == 'show'){
409
+ if(!file) return console.log(fs.readdirSync(cachepath).join('\n').trim());
410
+
411
+ const hashed = hashTags(file, {
412
+ tag: (str) => ({ $set: str + '#name#commit' }),
413
+ commit: (file) => findGitCommitPath(path.join(cachepath, file.replace(/#name/, ''), 'clone')),
414
+ name: (file) => jsYaml.load(fs.readFileSync(path.join(cachepath, file, 'clone/app.yaml'), { encoding: 'utf-8' }).trim()).manifest.package
415
+ });
416
+ file = hashed.string;
417
+
418
+ if(hashed.tag){
419
+ return console.log(hashed.name +':'+ hashed.commit);
420
+ }
421
+ if(hashed.commit) return console.log(hashed.commit);
422
+ if(hashed.name) return console.log(hashed.name);
423
+
424
+ const f = path.join(cachepath, file);
425
+ if(!fs.existsSync(f)) return;
426
+ if(fs.statSync(f).isDirectory()) console.log(
427
+ fs.readdirSync(f).join('\n')
428
+ );
429
+ else if(file.endsWith('app.yaml')){
430
+ console.log(fs.readFileSync(f, { encoding: 'utf-8' }).trim());
431
+ } else console.log(f);
390
432
  }
391
433
  },
392
434
  async cloneGit(gitpath, opts) {
@@ -9,6 +9,12 @@ const { execOptions } = require('../const/opt');
9
9
  const { REW_FILE_TYPE } = require('../const/ext');
10
10
  const { straceLog } = require('../misc/strace');
11
11
 
12
+ const _returns = (options, content) => {
13
+ if(options.useDefaultForPackages){
14
+ return content?.default ? content : { default: content };
15
+ } else return content;
16
+ }
17
+
12
18
  const cachedFiles = [];
13
19
  module.exports.cleanCache = () => {
14
20
  while(cachedFiles.length) cachedFiles.pop();
@@ -37,7 +43,10 @@ const lookUpInOtherApps = (fullPath) => {
37
43
  module.exports.imp = function (runPath, context) {
38
44
  return function (filename, options = {}) {
39
45
  if (!options) options = {};
40
- let type = options.type || filename.endsWith('.coffee') ? 'coffee' : REW_FILE_TYPE.TYPE;
46
+ let type = options.type ? options.type : filename.endsWith('.coffee') ? 'coffee' : (
47
+ filename.endsWith(REW_FILE_TYPE.EXTENSION) ? REW_FILE_TYPE.TYPE :
48
+ path.extname(filename).slice(1)
49
+ );
41
50
  let exports,
42
51
  ispkg = findPackage(filename);
43
52
 
@@ -58,10 +67,10 @@ module.exports.imp = function (runPath, context) {
58
67
  else filepath = otherPath;
59
68
  };
60
69
 
61
- const foundCache = cachedFiles.find((f) => f.filepath == filepath);
70
+ const foundCache = cachedFiles.find((f) => f.filepath == type+':'+filepath);
62
71
 
63
72
  if (!ispkg && foundCache) {
64
- exports = foundCache.exports;
73
+ exports = options.useDefaultForPackages === false ? foundCache.exports.default || foundCache.exports : _returns(options, foundCache.exports);
65
74
  }
66
75
 
67
76
  if (!ispkg && !existsSync(filepath) && !foundCache) {
@@ -117,17 +126,17 @@ module.exports.imp = function (runPath, context) {
117
126
  const f = getFile(filepath);
118
127
  if (type == 'yaml') {
119
128
  straceLog('===> FROM_YAML()');
120
- exports = importYaml(f.path, f);
129
+ exports = _returns(options, importYaml(f.path, f));
121
130
  } else if (type == 'json') {
122
131
  straceLog('===>');
123
132
  try {
124
- exports = JSON.parse(f.content);
133
+ exports = _returns(options, JSON.parse(f.content));
125
134
  } catch (e) {
126
- exports = {};
135
+ exports = _returns(options, {});
127
136
  }
128
137
  } else {
129
138
  straceLog('===> FROM_TEXT');
130
- exports = f.content;
139
+ exports = _returns(options, f.content);
131
140
  }
132
141
  }
133
142
 
@@ -143,13 +152,13 @@ module.exports.imp = function (runPath, context) {
143
152
  // descriptive code
144
153
  // you put them in comment blocks
145
154
  // and name it something
146
- // then you can simple see
155
+ // then you can simply see
147
156
  // which part of a code contains a certain
148
157
  // task. cool right?
149
158
 
150
159
  //** If is not package, post exec section
151
160
  /**/ if (!ispkg) context.module.imports.push(filepath);
152
- /**/ if (!ispkg) cachedFiles.push({ filepath, exports });
161
+ /**/ if (!ispkg) cachedFiles.push({ filepath: type+':'+filepath, exports });
153
162
  //**
154
163
 
155
164
  //** Mock imports section
@@ -115,6 +115,13 @@ const ValueIfy = (val) => {
115
115
  }
116
116
  }
117
117
 
118
+ function insertTokenAt(array, index, value) {
119
+ if (index < 0 || index > array.length) {
120
+ throw new RangeError('Index out of bounds');
121
+ }
122
+ array.splice(index, 0, value);
123
+ }
124
+
118
125
  const gnextToken = (i, n, tokens) => {
119
126
  return tokens[i + n] ? (tokens[i + n].type == 'WHITESPACE' ? gnextToken(i, n + 1, tokens) : { token: tokens[i + n], n, ti: i + n }) : null;
120
127
  };
@@ -409,11 +416,25 @@ function compileRewStuff(content, options) {
409
416
  } else straceLog('==> UNKNOWN');
410
417
  }
411
418
 
419
+ if (token.type === 'IDENTIFIER' && token.value === 'as' && !options.keepImports) {
420
+ const isFrom = gnextToken(i, 3, tokens);
421
+ const isInImport = tokens[i-2];
422
+ if(isFrom?.token.value == 'from' && isInImport?.value !== '*'){
423
+ insertTokenAt(tokens, i, { type: 'WHITESPACE', value: ' ' });
424
+ insertTokenAt(tokens, i, { type: 'OTHER', value: '*' });
425
+ insertTokenAt(tokens, i, { type: 'WHITESPACE', value: ' ' });
426
+ insertTokenAt(tokens, i, { type: 'IDENTIFIER', value: 'import' });
427
+ i -= 1;
428
+ continue;
429
+ }
430
+ }
431
+
412
432
  if (token.type === 'IDENTIFIER' && token.value === 'import' && !options.keepImports) {
413
433
  // console.log(nextToken.type);
414
434
  straceLog('IMPORT()');
415
435
  straceLog('==> WARN: SLOWS DOWN TOKENIZATION');
416
436
  let ind = i + n + 2;
437
+ let isAs = false;
417
438
 
418
439
  let defaultName;
419
440
  if (nextToken.type === 'STRING') {
@@ -441,14 +462,15 @@ function compileRewStuff(content, options) {
441
462
  }
442
463
  } else if (nextToken.value === '*') {
443
464
  const asToken = fnextToken(ind, tokens, 'IDENTIFIER', 'as');
444
- const nameToken = fnextToken(asToken.ri, tokens, 'STRING');
445
465
  if (asToken) {
466
+ const nameToken = fnextToken(asToken.ti, tokens, 'STRING');
446
467
  const nextToken = fnextToken(asToken.ti + 1, tokens, 'IDENTIFIER');
447
468
  defaultName = nextToken.value;
448
469
  straceLog('==>', defaultName, 'from', nameToken.value);
449
470
  if(useImp(nameToken, options)) updateAliases(aliases);
450
471
  result += `${defaultName} ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
451
472
  i = ind + 6;
473
+ isAs = true;
452
474
  }
453
475
  } else if (nextToken) {
454
476
  const nameToken = fnextToken(ind, tokens, 'STRING');
@@ -485,14 +507,14 @@ function compileRewStuff(content, options) {
485
507
  if(assertionToken.token.type == 'OTHER' && assertionToken.token.value == '{'){
486
508
  hooks.push({
487
509
  index: assertionToken.token.ti,
488
- value: ' useDefaultForPackages: true, '
510
+ value: ' useDefaultForPackages: '+(isAs?'false':'true')+', '
489
511
  })
490
512
  } else {
491
- result += 'useDefaultForPackages: true, '
513
+ result += 'useDefaultForPackages: '+(isAs?'false':'true')+', '
492
514
  }
493
515
  i += 3;
494
516
  } else {
495
- result += ", { useDefaultForPackages: true }"
517
+ result += ", { useDefaultForPackages: "+(isAs?'false':'true')+" }"
496
518
  }
497
519
 
498
520
  continue;
@@ -36,7 +36,8 @@ module.exports.runPath = function runPath(filepath, options = {}, custom_context
36
36
 
37
37
  if(context.app){
38
38
  const p = path.join(CONFIG_PATH, context.app.config.manifest.package, 'app');
39
- if(existsSync(p) && context.app.path !== p){
39
+ const p2 = path.join(CONFIG_PATH, context.app.config.manifest.package, 'app/.allow');
40
+ if(existsSync(p) && context.app.path !== p && !existsSync(p2)){
40
41
  console.log("App with the same package name has been installed. Conflicts happened. \nTo fix this, change your app's package name or remove the app making the conflict.");
41
42
  return {
42
43
  context: { module: { exports: null } },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.2.94",
3
+ "version": "1.2.96",
4
4
  "description": "A simple coffescript runtime and app manager",
5
5
  "main": "main.js",
6
6
  "directories": {