@oaknational/oak-components 3.21.0 → 3.22.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/dist/npm/bin/migrate/cli.js +59 -0
- package/dist/npm/bin/migrate/cli.test.js +21 -0
- package/dist/npm/bin/migrate/helper.js +29 -0
- package/dist/npm/bin/migrate/helper.test.js +20 -0
- package/dist/npm/bin/migrate/index.js +40 -0
- package/dist/npm/bin/migrate/index.test.js +42 -0
- package/dist/npm/bin/migrate/mods/oaksecondarylink/index.js +64 -0
- package/package.json +6 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env ./node_modules/.bin/tsx
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var tslib = require('tslib');
|
|
5
|
+
var process = require('node:process');
|
|
6
|
+
var index = require('./index.js');
|
|
7
|
+
var yargs = require('yargs');
|
|
8
|
+
var helpers = require('yargs/helpers');
|
|
9
|
+
var helper = require('./helper.js');
|
|
10
|
+
var promises = require('node:fs/promises');
|
|
11
|
+
require('node:path');
|
|
12
|
+
require('glob');
|
|
13
|
+
require('jscodeshift/src/Runner');
|
|
14
|
+
require('node:fs');
|
|
15
|
+
|
|
16
|
+
function parse() {
|
|
17
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
18
|
+
var mods, argv, transformPath, result;
|
|
19
|
+
var _a;
|
|
20
|
+
return tslib.__generator(this, function (_b) {
|
|
21
|
+
switch (_b.label) {
|
|
22
|
+
case 0: return [4 /*yield*/, promises.readdir(__dirname + "/mods")];
|
|
23
|
+
case 1:
|
|
24
|
+
mods = _b.sent();
|
|
25
|
+
return [4 /*yield*/, yargs(helpers.hideBin(process.argv))
|
|
26
|
+
.usage("$0 <mod> <path>", "Migrate to the new spacing tokens.", function (yargs) {
|
|
27
|
+
return yargs
|
|
28
|
+
.positional("mod", {
|
|
29
|
+
type: "string",
|
|
30
|
+
choices: mods,
|
|
31
|
+
describe: "mod to apply",
|
|
32
|
+
required: true,
|
|
33
|
+
})
|
|
34
|
+
.positional("path", {
|
|
35
|
+
type: "string",
|
|
36
|
+
describe: "place to search for files",
|
|
37
|
+
default: process.cwd(),
|
|
38
|
+
required: true,
|
|
39
|
+
});
|
|
40
|
+
})
|
|
41
|
+
.option("dry", {
|
|
42
|
+
type: "boolean",
|
|
43
|
+
describe: "dry run mode",
|
|
44
|
+
})
|
|
45
|
+
.help().argv];
|
|
46
|
+
case 2:
|
|
47
|
+
argv = (_b.sent());
|
|
48
|
+
transformPath = helper.resolveWithExtension(__dirname + "/mods/".concat(argv.mod));
|
|
49
|
+
result = index.run(transformPath, {
|
|
50
|
+
path: argv.path,
|
|
51
|
+
dry: (_a = argv.dry) !== null && _a !== void 0 ? _a : false,
|
|
52
|
+
});
|
|
53
|
+
console.log(result);
|
|
54
|
+
return [2 /*return*/];
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
parse();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_child_process = require('node:child_process');
|
|
4
|
+
|
|
5
|
+
describe("cli", function () {
|
|
6
|
+
it("callable (help)", function () {
|
|
7
|
+
var result = node_child_process.spawnSync(__dirname + "/../../node_modules/.bin/tsx", [
|
|
8
|
+
__dirname + "/cli.ts",
|
|
9
|
+
"--help",
|
|
10
|
+
]);
|
|
11
|
+
expect(result.stdout.toString()).toContain("cli.ts <mod> <path>");
|
|
12
|
+
});
|
|
13
|
+
it("mod errors with invalid args", function () {
|
|
14
|
+
var result = node_child_process.spawnSync(__dirname + "/../../node_modules/.bin/tsx", [
|
|
15
|
+
__dirname + "/cli.ts",
|
|
16
|
+
"foo",
|
|
17
|
+
"./foo.ts",
|
|
18
|
+
]);
|
|
19
|
+
expect(result.stderr.toString()).toContain("Argument: mod, Given: \"foo\", Choices: \"oaksecondarylink\"");
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var node_path = require('node:path');
|
|
5
|
+
var node_fs = require('node:fs');
|
|
6
|
+
|
|
7
|
+
// When we compile the code we end up with a .js file rather than a .ts file
|
|
8
|
+
function resolveWithExtension(file) {
|
|
9
|
+
var e_1, _a;
|
|
10
|
+
try {
|
|
11
|
+
for (var _b = tslib.__values(["", ".js", ".ts", ".json"]), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
12
|
+
var ext = _c.value;
|
|
13
|
+
var candidate = node_path.resolve(file + ext);
|
|
14
|
+
if (node_fs.existsSync(candidate)) {
|
|
15
|
+
return candidate;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
20
|
+
finally {
|
|
21
|
+
try {
|
|
22
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
23
|
+
}
|
|
24
|
+
finally { if (e_1) throw e_1.error; }
|
|
25
|
+
}
|
|
26
|
+
throw new Error("Cannot resolve ".concat(file));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.resolveWithExtension = resolveWithExtension;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var helper = require('./helper.js');
|
|
4
|
+
require('tslib');
|
|
5
|
+
require('node:path');
|
|
6
|
+
require('node:fs');
|
|
7
|
+
|
|
8
|
+
describe("resolveWithExtension", function () {
|
|
9
|
+
it("resolve .ts", function () {
|
|
10
|
+
var output = helper.resolveWithExtension(__dirname + "/helper.ts");
|
|
11
|
+
expect(output).toBe(__dirname + "/helper.ts");
|
|
12
|
+
});
|
|
13
|
+
it("no ext", function () {
|
|
14
|
+
var output = helper.resolveWithExtension(__dirname + "/helper");
|
|
15
|
+
expect(output).toBe(__dirname + "/helper.ts");
|
|
16
|
+
});
|
|
17
|
+
it("error when missing", function () {
|
|
18
|
+
expect(function () { return helper.resolveWithExtension(__dirname + "/missing"); }).toThrow();
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env ./node_modules/.bin/tsx
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var tslib = require('tslib');
|
|
5
|
+
var process = require('node:process');
|
|
6
|
+
var node_path = require('node:path');
|
|
7
|
+
var glob = require('glob');
|
|
8
|
+
var Runner = require('jscodeshift/src/Runner');
|
|
9
|
+
|
|
10
|
+
function run(transformPath_1, _a) {
|
|
11
|
+
return tslib.__awaiter(this, arguments, void 0, function (transformPath, _b) {
|
|
12
|
+
var options, searchPath, paths, pathsFiltered;
|
|
13
|
+
var path = _b.path, dry = _b.dry, _c = _b.silent, silent = _c === void 0 ? false : _c;
|
|
14
|
+
return tslib.__generator(this, function (_d) {
|
|
15
|
+
switch (_d.label) {
|
|
16
|
+
case 0:
|
|
17
|
+
options = {
|
|
18
|
+
dry: dry,
|
|
19
|
+
print: false,
|
|
20
|
+
verbose: 0,
|
|
21
|
+
silent: silent,
|
|
22
|
+
parser: "tsx",
|
|
23
|
+
};
|
|
24
|
+
searchPath = path.startsWith("/") ? path : node_path.join(process.cwd(), path);
|
|
25
|
+
return [4 /*yield*/, glob.glob(searchPath.match(/\.(ts|tsx)$/)
|
|
26
|
+
? searchPath
|
|
27
|
+
: "".concat(searchPath, "/**/*.{ts,tsx}"))];
|
|
28
|
+
case 1:
|
|
29
|
+
paths = _d.sent();
|
|
30
|
+
pathsFiltered = paths.filter(function (path) { return !path.match(/fixture.ts$/); });
|
|
31
|
+
return [4 /*yield*/, Runner.run(transformPath, pathsFiltered, options)];
|
|
32
|
+
case 2:
|
|
33
|
+
_d.sent();
|
|
34
|
+
return [2 /*return*/];
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.run = run;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var tmp = require('tmp');
|
|
5
|
+
var promises = require('fs/promises');
|
|
6
|
+
var index = require('./index.js');
|
|
7
|
+
var helper = require('./helper.js');
|
|
8
|
+
require('node:process');
|
|
9
|
+
require('node:path');
|
|
10
|
+
require('glob');
|
|
11
|
+
require('jscodeshift/src/Runner');
|
|
12
|
+
require('node:fs');
|
|
13
|
+
|
|
14
|
+
tmp.setGracefulCleanup();
|
|
15
|
+
var oaksecondarylinkContentInput = "\nimport { OakSecondaryLink } from \"@oaknational/oak-components\";\n\nfunction Main () {\n return <OakSecondaryLink iconName=\"audio\" />;\n}\n".trim();
|
|
16
|
+
var oaksecondarylinkContentOutput = "\nimport { OakLink } from \"@oaknational/oak-components\";\n\nfunction Main () {\n return <OakLink variant=\"secondary\" iconName=\"audio\" />;\n}\n".trim();
|
|
17
|
+
describe("migrate", function () {
|
|
18
|
+
describe("mods/oaksecondarylink", function () {
|
|
19
|
+
it("should transform oaksecondarylink correctly", function () { return tslib.__awaiter(void 0, void 0, void 0, function () {
|
|
20
|
+
var name, filepath, transformPath, content;
|
|
21
|
+
return tslib.__generator(this, function (_a) {
|
|
22
|
+
switch (_a.label) {
|
|
23
|
+
case 0:
|
|
24
|
+
name = tmp.dirSync().name;
|
|
25
|
+
filepath = name + "/oaksecondarylink.ts";
|
|
26
|
+
return [4 /*yield*/, promises.writeFile(filepath, oaksecondarylinkContentInput, "utf8")];
|
|
27
|
+
case 1:
|
|
28
|
+
_a.sent();
|
|
29
|
+
transformPath = helper.resolveWithExtension(__dirname + "/mods/oaksecondarylink/index");
|
|
30
|
+
return [4 /*yield*/, index.run(transformPath, { path: name, dry: false, silent: true })];
|
|
31
|
+
case 2:
|
|
32
|
+
_a.sent();
|
|
33
|
+
return [4 /*yield*/, promises.readFile(filepath, "utf8")];
|
|
34
|
+
case 3:
|
|
35
|
+
content = _a.sent();
|
|
36
|
+
expect(content).toEqual(oaksecondarylinkContentOutput);
|
|
37
|
+
return [2 /*return*/];
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}); });
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
|
|
5
|
+
function index (fileInfo, api) {
|
|
6
|
+
var j = api.jscodeshift;
|
|
7
|
+
var root = j(fileInfo.source);
|
|
8
|
+
// Replace <OakSecondaryLink> with <OakLink variant="secondary">
|
|
9
|
+
root
|
|
10
|
+
.find(j.JSXElement, {
|
|
11
|
+
openingElement: {
|
|
12
|
+
name: {
|
|
13
|
+
type: "JSXIdentifier",
|
|
14
|
+
name: "OakSecondaryLink",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
.replaceWith(function (_a) {
|
|
19
|
+
var _b, _c;
|
|
20
|
+
var value = _a.value;
|
|
21
|
+
var existingAttributes = (_c = (_b = value.openingElement.attributes) === null || _b === void 0 ? void 0 : _b.filter(function (attribute) {
|
|
22
|
+
return !(attribute.type === "JSXAttribute" &&
|
|
23
|
+
attribute.name.name === "variant");
|
|
24
|
+
})) !== null && _c !== void 0 ? _c : [];
|
|
25
|
+
var openingElement = j.jsxOpeningElement(j.jsxIdentifier("OakLink"), tslib.__spreadArray([
|
|
26
|
+
j.jsxAttribute(j.jsxIdentifier("variant"), j.stringLiteral("secondary"))
|
|
27
|
+
], tslib.__read(existingAttributes), false), value.openingElement.selfClosing);
|
|
28
|
+
var closingElement = value.openingElement.selfClosing
|
|
29
|
+
? null
|
|
30
|
+
: j.jsxClosingElement(j.jsxIdentifier("OakLink"));
|
|
31
|
+
return j.jsxElement(openingElement, closingElement, value.children);
|
|
32
|
+
});
|
|
33
|
+
// Update import:
|
|
34
|
+
// OakSecondaryLink -> OakLink
|
|
35
|
+
root
|
|
36
|
+
.find(j.ImportDeclaration, {
|
|
37
|
+
source: {
|
|
38
|
+
value: "@oaknational/oak-components",
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
.forEach(function (path) {
|
|
42
|
+
var _a;
|
|
43
|
+
var specifiers = (_a = path.value.specifiers) !== null && _a !== void 0 ? _a : [];
|
|
44
|
+
var hasOakLink = specifiers.some(function (specifier) {
|
|
45
|
+
return specifier.type === "ImportSpecifier" &&
|
|
46
|
+
specifier.imported.name === "OakLink";
|
|
47
|
+
});
|
|
48
|
+
path.value.specifiers = specifiers.flatMap(function (specifier) {
|
|
49
|
+
if (specifier.type === "ImportSpecifier" &&
|
|
50
|
+
specifier.imported.name === "OakSecondaryLink") {
|
|
51
|
+
// If OakLink is already imported, just remove OakSecondaryLink.
|
|
52
|
+
if (hasOakLink) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
// Otherwise replace it in-place, preserving import ordering.
|
|
56
|
+
return [j.importSpecifier(j.identifier("OakLink"))];
|
|
57
|
+
}
|
|
58
|
+
return [specifier];
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
return root.toSource();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = index;
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oaknational/oak-components",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.22.0",
|
|
4
4
|
"licence": "MIT",
|
|
5
5
|
"packageManager": "pnpm@11.2.0",
|
|
6
6
|
"description": "Shared components for Oak applications",
|
|
7
|
+
"bin": {
|
|
8
|
+
"oc-migrate": "./dist/bin/migrate/index.js"
|
|
9
|
+
},
|
|
7
10
|
"exports": {
|
|
8
11
|
".": {
|
|
9
12
|
"import": {
|
|
@@ -101,6 +104,7 @@
|
|
|
101
104
|
"@types/react-transition-group": "^4.4.12",
|
|
102
105
|
"@types/rollup-plugin-peer-deps-external": "^2.2.6",
|
|
103
106
|
"@types/styled-components": "^5.1.36",
|
|
107
|
+
"@types/tmp": "^0.2.6",
|
|
104
108
|
"@types/yargs": "^17.0.35",
|
|
105
109
|
"@typescript-eslint/eslint-plugin": "^6.18.1",
|
|
106
110
|
"@typescript-eslint/parser": "^6.18.1",
|
|
@@ -134,6 +138,7 @@
|
|
|
134
138
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
135
139
|
"rollup-plugin-typescript-paths": "^1.5.0",
|
|
136
140
|
"semantic-release": "^25.0.3",
|
|
141
|
+
"tmp": "^0.2.7",
|
|
137
142
|
"storybook": "10.6.0",
|
|
138
143
|
"ts-jest": "^29.4.9",
|
|
139
144
|
"tsx": "^4.21.0",
|