|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { describe, it } from "node:test"; |
| 3 | +import dedent from "dedent"; |
| 4 | +import jsonLang from "@ast-grep/lang-json"; |
| 5 | +import { registerDynamicLanguage, parse } from "@ast-grep/napi"; |
| 6 | +import type { Edit } from "@ast-grep/napi"; |
| 7 | +import { getScriptsNode, getNodeJsUsage, replaceNodeJsArgs } from "./package-json.ts"; |
| 8 | + |
| 9 | +registerDynamicLanguage({ json: jsonLang }); |
| 10 | + |
| 11 | +describe("package-json utilities", () => { |
| 12 | + describe("getScriptsNode", () => { |
| 13 | + it("should get the scripts node", () => { |
| 14 | + const input = dedent` |
| 15 | + { |
| 16 | + "scripts": { |
| 17 | + "test": "echo \\"Error: no test specified\\" && exit 1" |
| 18 | + } |
| 19 | + } |
| 20 | + `; |
| 21 | + |
| 22 | + const result = getScriptsNode(parse('json', input)); |
| 23 | + |
| 24 | + assert(result); |
| 25 | + assert.strictEqual(result.children().length, 3); // curly braces + pair + curly braces |
| 26 | + }); |
| 27 | + |
| 28 | + it("should return empty array if any scripts is present", () => { |
| 29 | + const input = dedent` |
| 30 | + { |
| 31 | + "name": "example-package", |
| 32 | + "version": "1.0.0" |
| 33 | + } |
| 34 | + `; |
| 35 | + |
| 36 | + const result = getNodeJsUsage(parse('json', input)); |
| 37 | + |
| 38 | + assert.strictEqual(result.length, 0); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should throw an error if multiple scripts nodes are found", () => { |
| 42 | + const input = dedent` |
| 43 | + { |
| 44 | + "scripts": { |
| 45 | + "test": "echo \\"Error: no test specified\\" && exit 1" |
| 46 | + }, |
| 47 | + "scripts": { |
| 48 | + "start": "node index.js" |
| 49 | + } |
| 50 | + } |
| 51 | + `; |
| 52 | + |
| 53 | + assert.throws(() => getScriptsNode(parse('json', input)), { |
| 54 | + message: /Multiple "scripts" fields found/ |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("getNodeJsUsage", () => { |
| 60 | + it("should get Node.js usage in scripts", () => { |
| 61 | + const input = dedent` |
| 62 | + { |
| 63 | + "scripts": { |
| 64 | + "start": "node script.js", |
| 65 | + "test": "echo \\"Error: no test specified\\" && exit 1" |
| 66 | + } |
| 67 | + } |
| 68 | + `; |
| 69 | + |
| 70 | + const result = getNodeJsUsage(parse('json', input)); |
| 71 | + |
| 72 | + assert.strictEqual(result.length, 1); |
| 73 | + assert.strictEqual(result[0].text(), "node script.js"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should not catch `node_modules`", () => { |
| 77 | + const input = dedent` |
| 78 | + { |
| 79 | + "scripts": { |
| 80 | + "start": "node_modules/.bin/some-tool", |
| 81 | + "test": "node another-script.js" |
| 82 | + } |
| 83 | + } |
| 84 | + `; |
| 85 | + |
| 86 | + const result = getNodeJsUsage(parse('json', input)); |
| 87 | + |
| 88 | + assert.strictEqual(result.length, 1); |
| 89 | + assert.strictEqual(result[0].text(), "node another-script.js"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should return empty array if no Node.js usage is found", () => { |
| 93 | + const input = dedent` |
| 94 | + { |
| 95 | + "scripts": { |
| 96 | + "start": "npm run build", |
| 97 | + "test": "echo \\"Error: no test specified\\" && exit 1" |
| 98 | + } |
| 99 | + } |
| 100 | + `; |
| 101 | + |
| 102 | + const result = getNodeJsUsage(parse('json', input)); |
| 103 | + |
| 104 | + assert.strictEqual(result.length, 0); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("replaceNodeJsArgs", () => { |
| 109 | + it("should replace Node.js arguments in scripts", () => { |
| 110 | + const input = dedent` |
| 111 | + { |
| 112 | + "scripts": { |
| 113 | + "start": "node --experimental-foo script.js", |
| 114 | + } |
| 115 | + } |
| 116 | + `; |
| 117 | + |
| 118 | + const edits: Edit[] = []; |
| 119 | + replaceNodeJsArgs(parse('json', input), { '--experimental-foo': '--experimental-bar' }, edits); |
| 120 | + |
| 121 | + assert.strictEqual(edits.length, 1); |
| 122 | + assert.strictEqual(edits[0].insertedText, 'node --experimental-bar script.js'); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should not replace an arg that contains same", () => { |
| 126 | + const input = dedent` |
| 127 | + { |
| 128 | + "scripts": { |
| 129 | + "start": "node --experimental-foo-prop script.js", |
| 130 | + } |
| 131 | + } |
| 132 | + `; |
| 133 | + |
| 134 | + const edits: Edit[] = []; |
| 135 | + replaceNodeJsArgs(parse('json', input), { '--experimental-foo': '--experimental-bar' }, edits); |
| 136 | + |
| 137 | + assert.strictEqual(edits.length, 0); |
| 138 | + }); |
| 139 | + |
| 140 | + |
| 141 | + it("should handle multiple replacements", () => { |
| 142 | + const input = dedent` |
| 143 | + { |
| 144 | + "scripts": { |
| 145 | + "start": "node --experimental-foo script.js", |
| 146 | + "test": "node --experimental-foo script.js" |
| 147 | + } |
| 148 | + } |
| 149 | + `; |
| 150 | + |
| 151 | + const edits: Edit[] = []; |
| 152 | + replaceNodeJsArgs(parse('json', input), { '--experimental-foo': '--experimental-bar' }, edits); |
| 153 | + |
| 154 | + assert.strictEqual(edits.length, 2); |
| 155 | + assert.strictEqual(edits[0].insertedText, 'node --experimental-bar script.js'); |
| 156 | + assert.strictEqual(edits[1].insertedText, 'node --experimental-bar script.js'); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments