redput 1.2.1 → 1.4.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,18 @@
1
+ 2023.08.16, v1.4.0
2
+
3
+ feature:
4
+ - 6812e73 redput: prepare rule before write
5
+ - 49939c1 write: nested: convert to commonjs
6
+ - 52145ff write: nested: optional
7
+
8
+ 2023.08.16, v1.3.0
9
+
10
+ feature:
11
+ - 90f4471 redput: nested: update overall test
12
+ - a78d8d4 redput: move add-rule
13
+ - 095ccdf redput: nested: update index.js
14
+ - 08146ac redput: plugin-insert-get-rule: simplify
15
+
1
16
  2023.08.14, v1.2.1
2
17
 
3
18
  fix:
@@ -33,10 +33,6 @@ export const traverse = ({push}) => {
33
33
 
34
34
  const process = ({push, template}) => (path) => {
35
35
  const {__a} = getTemplateValues(path, template);
36
-
37
- if (!__a)
38
- return;
39
-
40
36
  const value = parseValue(__a);
41
37
 
42
38
  push({
package/lib/redput.js CHANGED
@@ -1,6 +1,4 @@
1
1
  import tryToCatch from 'try-to-catch';
2
- import {exists} from 'fs';
3
- import {access} from 'fs/promises';
4
2
  import {getReport} from './get-report/get-report.js';
5
3
  import {readGist} from './read-gist/read-gist.js';
6
4
  import {writePlugin} from './write/index.js';
@@ -34,12 +32,14 @@ export const redput = async (link, {token}) => {
34
32
  const rule = lines.join('\n');
35
33
  const report = getReport(rule);
36
34
 
37
- await writePlugin(name, {
35
+ const [errorWrite] = await tryToCatch(writePlugin, name, {
38
36
  rule,
39
37
  fixture,
40
38
  report,
41
39
  });
42
40
 
41
+ if (errorWrite)
42
+ return [errorWrite];
43
+
43
44
  return SUCCESS;
44
45
  };
45
-
@@ -1,20 +1,12 @@
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';
1
+ import {access} from 'fs/promises';
13
2
  import {
3
+ updateNestedIndex,
4
+ updateOverallNestedFixtures,
5
+ updateOverallNestedTest,
14
6
  writeNestedFixtures,
15
7
  writeNestedRule,
16
8
  writeNestedTests,
17
- } from './nested.js';
9
+ } from './nested/nested.js';
18
10
  import {
19
11
  writeFixtures,
20
12
  writeRule,
@@ -25,13 +17,12 @@ import tryToCatch from 'try-to-catch';
25
17
  export const writePlugin = async (name, {rule, fixture, report}) => {
26
18
  const [isNested] = await tryToCatch(access, './package.json');
27
19
 
28
- if (isNested) {
20
+ if (isNested)
29
21
  return await writeNested(name, {
30
22
  rule,
31
23
  fixture,
32
24
  report,
33
25
  });
34
- }
35
26
 
36
27
  await writeSimple(name, {
37
28
  rule,
@@ -44,6 +35,16 @@ export const writeNested = async (name, {rule, fixture, report}) => {
44
35
  await writeNestedRule(name, rule);
45
36
  await writeNestedFixtures(name, fixture);
46
37
  await writeNestedTests(name, report);
38
+
39
+ await tryToCatch(writeNestedOptional, name, {
40
+ fixture,
41
+ });
42
+ };
43
+
44
+ export const writeNestedOptional = async (name, {fixture}) => {
45
+ await updateNestedIndex(name);
46
+ await updateOverallNestedFixtures(name, fixture);
47
+ await updateOverallNestedTest(name);
47
48
  };
48
49
 
49
50
  export const writeSimple = async (name, {rule, fixture, report}) => {
@@ -0,0 +1,40 @@
1
+ import {
2
+ types,
3
+ operator,
4
+ template,
5
+ } from 'putout';
6
+
7
+ const {StringLiteral, SpreadElement} = types;
8
+ const {traverse} = operator;
9
+
10
+ export const report = () => `Insert 'getRule()'`;
11
+
12
+ const createRule = template('getRule(%%name%%)');
13
+
14
+ export const match = ({options}) => ({
15
+ 'module.exports.rules = __object': ({__object}) => {
16
+ let exists = false;
17
+ const {name} = options;
18
+
19
+ traverse(__object, {
20
+ [`getRule('${name}')`]: () => {
21
+ exists = true;
22
+ },
23
+ });
24
+
25
+ return !exists;
26
+ },
27
+ });
28
+
29
+ export const replace = ({options}) => ({
30
+ 'module.exports.rules = __object': ({__object}, path) => {
31
+ const {name} = options;
32
+ const node = SpreadElement(createRule({
33
+ name: StringLiteral(name),
34
+ }));
35
+
36
+ __object.properties.push(node);
37
+
38
+ return path;
39
+ },
40
+ });
@@ -0,0 +1,18 @@
1
+ import putout from 'putout';
2
+ import * as insertTestPlugin from './plugin-insert-test/index.js';
3
+
4
+ export const insertTest = (rule, plugin, source) => {
5
+ const {code} = putout(source, {
6
+ rules: {
7
+ 'insert-test': ['on', {
8
+ rule,
9
+ plugin,
10
+ }],
11
+ },
12
+ plugins: [
13
+ ['insert-test', insertTestPlugin],
14
+ ],
15
+ });
16
+
17
+ return code;
18
+ };
@@ -0,0 +1,31 @@
1
+ import {template} from 'putout';
2
+
3
+ export const report = () => 'Insert test';
4
+
5
+ export const fix = ({path, nodeTest}) => {
6
+ path.node.body.push(nodeTest);
7
+ };
8
+
9
+ export const traverse = ({push, pathStore, options}) => {
10
+ const {plugin = 'github', rule = 'insert-test'} = options;
11
+ const strTest = `
12
+ test('plugin-${plugin}: transform: ${rule}', (t) => {
13
+ t.transform('${rule}');
14
+ t.end();
15
+ });
16
+ `;
17
+
18
+ const nodeTest = template.ast(strTest);
19
+
20
+ return {
21
+ [strTest]: pathStore,
22
+ Program: {
23
+ exit(path) {
24
+ !pathStore().length && push({
25
+ path,
26
+ nodeTest,
27
+ });
28
+ },
29
+ },
30
+ };
31
+ };
@@ -9,6 +9,8 @@ import {
9
9
  join,
10
10
  } from 'path';
11
11
  import rendy from 'rendy';
12
+ import {addRule} from './add-rule/add-rule.js';
13
+ import {insertTest} from './insert-test/insert-test.js';
12
14
 
13
15
  export const writeNestedRule = async (name, data) => {
14
16
  await mkdir(`./${name}`, {
@@ -28,7 +30,7 @@ export const writeNestedFixtures = async (name, data) => {
28
30
  };
29
31
 
30
32
  export const writeNestedTests = async (name, report) => {
31
- const templatePath = new URL('../../templates/plugin.js', import.meta.url).pathname;
33
+ const templatePath = new URL('../../../templates/plugin.js', import.meta.url).pathname;
32
34
  const template = await readFile(templatePath, 'utf8');
33
35
  const nestedPluginName = basename(dirname(join(process.cwd(), '..')));
34
36
  const nested = nestedPluginName.replace('plugin-', '');
@@ -40,3 +42,24 @@ export const writeNestedTests = async (name, report) => {
40
42
  importPath: '.',
41
43
  }));
42
44
  };
45
+
46
+ export const updateNestedIndex = async (name) => {
47
+ const source = await readFile('./index.js', 'utf8');
48
+ const code = addRule(name, source);
49
+ await writeFile(`./index.js`, code);
50
+ };
51
+
52
+ export const updateOverallNestedTest = async (name) => {
53
+ const nestedPluginName = basename(dirname(join(process.cwd())));
54
+ const plugin = nestedPluginName.replace('plugin-', '');
55
+
56
+ const source = await readFile(`../test/${plugin}.js`, 'utf8');
57
+ const code = insertTest(name, plugin, source);
58
+
59
+ await writeFile(`../test/${plugin}.js`, code);
60
+ };
61
+
62
+ export const updateOverallNestedFixtures = async (name, data) => {
63
+ await writeFile(`../test/fixture/${name}.js`, data);
64
+ await writeFile(`../test/fixture/${name}-fix.js`, data);
65
+ };
@@ -0,0 +1,13 @@
1
+ import putout from 'putout';
2
+
3
+ export const prepareRule = (source) => {
4
+ const {code} = putout(source, {
5
+ plugins: [
6
+ 'declare',
7
+ 'convert-esm-to-commonjs',
8
+ 'putout',
9
+ ],
10
+ });
11
+
12
+ return code;
13
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redput",
3
- "version": "1.2.1",
3
+ "version": "1.4.0",
4
4
  "description": "CLI tool to convert source from 🐊Putout Editor to files",
5
5
  "main": "lib/redput.js",
6
6
  "bin": {
@@ -1,47 +0,0 @@
1
- import {
2
- operator,
3
- types,
4
- template,
5
- } from 'putout';
6
-
7
- const {getTemplateValues} = operator;
8
- const {StringLiteral, SpreadElement} = types;
9
- const RULES = 'module.exports.rules = __object';
10
- const GET_RULE = template('getRule(%%name%%)');
11
-
12
- export const report = () => `Insert 'getRule()'`;
13
-
14
- export const fix = ({__object}, {options}) => {
15
- const {name} = options;
16
- const getRuleNode = SpreadElement(GET_RULE({
17
- name: StringLiteral(name),
18
- }));
19
-
20
- __object.properties.push(getRuleNode);
21
- };
22
-
23
- export const traverse = ({push, options}) => ({
24
- [RULES](path) {
25
- const {__object} = getTemplateValues(path, RULES);
26
- const {name} = options;
27
-
28
- if (!check(name, __object))
29
- return;
30
-
31
- push({
32
- path,
33
- __object,
34
- });
35
- },
36
- });
37
-
38
- function check(name, {properties}) {
39
- for (const {argument} of properties) {
40
- const [first] = argument.arguments;
41
-
42
- if (first.value === name)
43
- return false;
44
- }
45
-
46
- return true;
47
- }