redput 1.0.3 → 1.2.0

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/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2023.08.14, v1.2.0
2
+
3
+ feature:
4
+ - aaebe09 redput: add support of simple plugins (when 'package.json' in current directory)
5
+
6
+ 2023.08.14, v1.1.0
7
+
8
+ feature:
9
+ - 3d1eed2 get-report: commonjs
10
+ - 543a0a8 redput: get-report: add support of StringLiteral
11
+
1
12
  2023.08.14, v1.0.3
2
13
 
3
14
  feature:
@@ -1,30 +1,55 @@
1
- import {operator} from 'putout';
1
+ import {
2
+ operator,
3
+ types,
4
+ } from 'putout';
2
5
 
6
+ const {isStringLiteral} = types;
3
7
  const {getTemplateValues} = operator;
8
+
4
9
  const REPORT = 'export const report = () => __a';
10
+ const REPORT_COMMONJS = 'module.exports.report = () => __a';
5
11
  const ASSIGN = 'export const report = __a';
6
12
 
13
+ const templates = [
14
+ REPORT,
15
+ REPORT_COMMONJS,
16
+ ASSIGN,
17
+ ];
18
+
7
19
  export const report = ({value}) => value;
8
20
  export const fix = () => {};
9
- export const traverse = ({push}) => ({
10
- [REPORT](path) {
11
- const {__a} = getTemplateValues(path, REPORT);
12
-
13
- push({
14
- path,
15
- value: __a.quasis[0].value.cooked,
16
- });
17
-
18
- path.stop();
19
- },
20
- [ASSIGN](path) {
21
- const {__a} = getTemplateValues(path, ASSIGN);
22
-
23
- push({
24
- path,
25
- value: __a.quasis[0].value.cooked,
21
+ export const traverse = ({push}) => {
22
+ const visitors = {};
23
+
24
+ for (const template of templates) {
25
+ visitors[template] = process({
26
+ push,
27
+ template,
26
28
  });
27
-
28
- path.stop();
29
- },
30
- });
29
+ }
30
+
31
+ return visitors;
32
+ };
33
+
34
+ const process = ({push, template}) => (path) => {
35
+ const {__a} = getTemplateValues(path, template);
36
+
37
+ if (!__a)
38
+ return;
39
+
40
+ const value = parseValue(__a);
41
+
42
+ push({
43
+ path,
44
+ value,
45
+ });
46
+
47
+ path.stop();
48
+ };
49
+
50
+ function parseValue(a) {
51
+ if (isStringLiteral(a))
52
+ return a.value;
53
+
54
+ return a.quasis[0].value.cooked;
55
+ }
package/lib/redput.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import tryToCatch from 'try-to-catch';
2
+ import {exists} from 'fs';
3
+ import {access} from 'fs/promises';
2
4
  import {getReport} from './get-report/get-report.js';
3
5
  import {readGist} from './read-gist/read-gist.js';
4
- import {
5
- writeRule,
6
- writeFixtures,
7
- writeTestForNested,
8
- } from './write.js';
6
+ import {writePlugin} from './write/index.js';
7
+
8
+ const SUCCESS = [null, 'Done'];
9
9
 
10
10
  const URL = 'https://putout.cloudcmd.io/#/gist/';
11
11
 
@@ -26,7 +26,7 @@ export const redput = async (link, {token}) => {
26
26
  const [comment, ...lines] = raw.split('\n');
27
27
  const name = comment.replace(/\/\/\s+?/, '');
28
28
 
29
- if (name.includes(':'))
29
+ if (name.includes(':') || name.includes('{'))
30
30
  return [
31
31
  Error(`Bad name: ${name}`),
32
32
  ];
@@ -34,9 +34,12 @@ export const redput = async (link, {token}) => {
34
34
  const rule = lines.join('\n');
35
35
  const report = getReport(rule);
36
36
 
37
- await writeRule(name, rule);
38
- await writeFixtures(name, fixture);
39
- await writeTestForNested(name, report);
37
+ writePlugin(name, {
38
+ rule,
39
+ fixture,
40
+ report,
41
+ });
40
42
 
41
- return [null, 'Done'];
43
+ return SUCCESS;
42
44
  };
45
+
@@ -0,0 +1,53 @@
1
+ import rendy from 'rendy';
2
+ import {
3
+ readFile,
4
+ writeFile,
5
+ mkdir,
6
+ access,
7
+ } from 'fs/promises';
8
+ import {
9
+ dirname,
10
+ basename,
11
+ join,
12
+ } from 'path';
13
+ import {
14
+ writeNestedFixtures,
15
+ writeNestedRule,
16
+ writeNestedTests,
17
+ } from './nested.js';
18
+ import {
19
+ writeFixtures,
20
+ writeRule,
21
+ writeTests,
22
+ } from './simple.js';
23
+ import tryToCatch from 'try-to-catch';
24
+
25
+ export const writePlugin = async (name, {rule, fixture, report}) => {
26
+ const [isNested] = await tryToCatch(access, './package.json');
27
+
28
+ if (isNested) {
29
+ return await writeNested(name, {
30
+ rule,
31
+ fixture,
32
+ report,
33
+ });
34
+ }
35
+
36
+ await writeSimple(name, {
37
+ rule,
38
+ fixture,
39
+ report,
40
+ });
41
+ };
42
+
43
+ export const writeNested = async (name, {rule, fixture, report}) => {
44
+ await writeNestedRule(name, rule);
45
+ await writeNestedFixtures(name, fixture);
46
+ await writeNestedTests(name, report);
47
+ };
48
+
49
+ export const writeSimple = async (name, {rule, fixture, report}) => {
50
+ await writeRule(name, rule);
51
+ await writeFixtures(name, fixture);
52
+ await writeTests(name, report);
53
+ };
@@ -1,16 +1,16 @@
1
- import rendy from 'rendy';
2
1
  import {
2
+ mkdir,
3
3
  readFile,
4
4
  writeFile,
5
- mkdir,
6
5
  } from 'fs/promises';
7
6
  import {
8
- dirname,
9
7
  basename,
8
+ dirname,
10
9
  join,
11
10
  } from 'path';
11
+ import rendy from 'rendy';
12
12
 
13
- export const writeRule = async (name, data) => {
13
+ export const writeNestedRule = async (name, data) => {
14
14
  await mkdir(`./${name}`, {
15
15
  recursive: true,
16
16
  });
@@ -18,7 +18,7 @@ export const writeRule = async (name, data) => {
18
18
  await writeFile(`./${name}/index.js`, data);
19
19
  };
20
20
 
21
- export const writeFixtures = async (name, data) => {
21
+ export const writeNestedFixtures = async (name, data) => {
22
22
  await mkdir(`./${name}/fixture`, {
23
23
  recursive: true,
24
24
  });
@@ -27,8 +27,8 @@ export const writeFixtures = async (name, data) => {
27
27
  await writeFile(`./${name}/fixture/${name}-fix.js`, data);
28
28
  };
29
29
 
30
- export const writeTestForNested = async (name, report) => {
31
- const templatePath = new URL('../templates/nested.js', import.meta.url).pathname;
30
+ export const writeNestedTests = async (name, report) => {
31
+ const templatePath = new URL('../../templates/plugin.js', import.meta.url).pathname;
32
32
  const template = await readFile(templatePath, 'utf8');
33
33
  const nestedPluginName = basename(dirname(join(process.cwd(), '..')));
34
34
  const nested = nestedPluginName.replace('plugin-', '');
@@ -37,5 +37,6 @@ export const writeTestForNested = async (name, report) => {
37
37
  name,
38
38
  nested,
39
39
  report,
40
+ importPath: '.',
40
41
  }));
41
42
  };
@@ -0,0 +1,42 @@
1
+ import {
2
+ mkdir,
3
+ readFile,
4
+ writeFile,
5
+ } from 'fs/promises';
6
+ import {
7
+ basename,
8
+ dirname,
9
+ join,
10
+ } from 'path';
11
+ import rendy from 'rendy';
12
+
13
+ export const writeRule = async (name, data) => {
14
+ await mkdir(`./lib`, {
15
+ recursive: true,
16
+ });
17
+
18
+ await writeFile(`./lib/${name}.js`, data);
19
+ };
20
+
21
+ export const writeFixtures = async (name, data) => {
22
+ await mkdir(`./test/fixture`, {
23
+ recursive: true,
24
+ });
25
+
26
+ await writeFile(`./test/fixture/${name}.js`, data);
27
+ await writeFile(`./test/fixture/${name}-fix.js`, data);
28
+ };
29
+
30
+ export const writeTests = async (name, report) => {
31
+ const templatePath = new URL('../../templates/plugin.js', import.meta.url).pathname;
32
+ const template = await readFile(templatePath, 'utf8');
33
+ const nestedPluginName = basename(dirname(join(process.cwd(), '..')));
34
+ const nested = nestedPluginName.replace('plugin-', '');
35
+
36
+ await writeFile(`./test/${name}.js`, rendy(template, {
37
+ name,
38
+ nested,
39
+ report,
40
+ importPath: '..',
41
+ }));
42
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redput",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "description": "CLI tool to convert source from 🐊Putout Editor to files",
5
5
  "main": "lib/redput.js",
6
6
  "bin": {
@@ -1,5 +1,5 @@
1
1
  const {createTest} = require('@putout/test');
2
- const plugin = require('.');
2
+ const plugin = require('{{ importPath }}');
3
3
 
4
4
  const test = createTest(__dirname, {
5
5
  printer: 'putout',