@navikt/aksel 3.1.3 → 3.2.1
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/README.md +33 -0
- package/dist/codemod/migrations.js +16 -1
- package/dist/codemod/run-codeshift.js +2 -0
- package/dist/codemod/transforms/v3.0.0/copybutton/copybutton.js +72 -0
- package/dist/codemod/utils/moveAndRenameImport.js +58 -0
- package/dist/codemod/utils/removeProps.js +16 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -22,6 +22,39 @@ To get started:
|
|
|
22
22
|
npx @navikt/aksel codemod --help
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
### v3
|
|
26
|
+
|
|
27
|
+
There is no general codemods for migrating from v2 -> v3.
|
|
28
|
+
|
|
29
|
+
#### CopyButton
|
|
30
|
+
|
|
31
|
+
`npx @navikt/aksel codemod v3-copybutton ...`
|
|
32
|
+
|
|
33
|
+
`<CopyToClipboard />` has been renamed to `<CopyButton />` and refactored.
|
|
34
|
+
|
|
35
|
+
- Namechange
|
|
36
|
+
- removed props `popoverText`, `iconPosition`, `popoverPlacement`
|
|
37
|
+
- changed variants
|
|
38
|
+
- refactored CSS and React-code. ⚠️ Overwritten CSS will not be migrated!
|
|
39
|
+
|
|
40
|
+
```diff
|
|
41
|
+
-import { CopyToClipboard } from "@navikt/ds-react-internal";
|
|
42
|
+
+import { CopyButton } from "@navikt/ds-react";
|
|
43
|
+
|
|
44
|
+
-<CopyToClipboard
|
|
45
|
+
+<CopyButton
|
|
46
|
+
- popoverText="popoverText"
|
|
47
|
+
- iconPosition="left"
|
|
48
|
+
- popoverPlacement="bottom-end"
|
|
49
|
+
copyText="Text to copy"
|
|
50
|
+
size="medium"
|
|
51
|
+
>
|
|
52
|
+
- text
|
|
53
|
+
+</CopyButton>
|
|
54
|
+
-</CopyToClipboard>
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
|
|
25
58
|
### v1 -> v2
|
|
26
59
|
|
|
27
60
|
[Documentation](https://aksel.nav.no/grunnleggende/kode/migrering#h76f47744d112)
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getMigrationString = exports.getMigrationNames = exports.getMigrationPath = exports.migrations = void 0;
|
|
6
|
+
exports.getMigrationString = exports.getMigrationNames = exports.getWarning = exports.getMigrationPath = exports.migrations = void 0;
|
|
7
7
|
const chalk_1 = __importDefault(require("chalk"));
|
|
8
8
|
exports.migrations = {
|
|
9
9
|
"1.0.0": [
|
|
@@ -50,6 +50,14 @@ exports.migrations = {
|
|
|
50
50
|
path: "v2.0.0/update-less-tokens/update-less-tokens",
|
|
51
51
|
},
|
|
52
52
|
],
|
|
53
|
+
"v3.0.0": [
|
|
54
|
+
{
|
|
55
|
+
description: "Replaces deprecated <CopyToClipboard /> with <CopyButton />",
|
|
56
|
+
value: "v3-copybutton",
|
|
57
|
+
path: "v3.0.0/copybutton/copybutton",
|
|
58
|
+
warning: "Remember to clean css-import from '@navikt/ds-css-internal' if no longer needed\nIf non-text was used as children, or different locales were handled, you need to manually fix this",
|
|
59
|
+
},
|
|
60
|
+
],
|
|
53
61
|
};
|
|
54
62
|
function getMigrationPath(str) {
|
|
55
63
|
var _a;
|
|
@@ -58,6 +66,13 @@ function getMigrationPath(str) {
|
|
|
58
66
|
.find((x) => x.value === str)) === null || _a === void 0 ? void 0 : _a.path;
|
|
59
67
|
}
|
|
60
68
|
exports.getMigrationPath = getMigrationPath;
|
|
69
|
+
function getWarning(str) {
|
|
70
|
+
var _a;
|
|
71
|
+
return (_a = Object.values(exports.migrations)
|
|
72
|
+
.reduce((acc, val) => [...val, ...acc], [])
|
|
73
|
+
.find((x) => x.value === str)) === null || _a === void 0 ? void 0 : _a.warning;
|
|
74
|
+
}
|
|
75
|
+
exports.getWarning = getWarning;
|
|
61
76
|
function getMigrationNames() {
|
|
62
77
|
return Object.values(exports.migrations).reduce((acc, val) => [...val.map((x) => x.value), ...acc], []);
|
|
63
78
|
}
|
|
@@ -58,6 +58,7 @@ function runCodeshift(input, options, program) {
|
|
|
58
58
|
});
|
|
59
59
|
console.log("\nRunning migration:", chalk_1.default.green("input"));
|
|
60
60
|
(options === null || options === void 0 ? void 0 : options.glob) && console.log(`Using glob: ${chalk_1.default.green(options.glob)}\n`);
|
|
61
|
+
const warning = (0, migrations_1.getWarning)(input);
|
|
61
62
|
try {
|
|
62
63
|
yield jscodeshift.run(codemodPath, filepaths, {
|
|
63
64
|
babel: true,
|
|
@@ -72,6 +73,7 @@ function runCodeshift(input, options, program) {
|
|
|
72
73
|
force: options === null || options === void 0 ? void 0 : options.force,
|
|
73
74
|
print: options === null || options === void 0 ? void 0 : options.print,
|
|
74
75
|
});
|
|
76
|
+
warning && console.log(`\n${chalk_1.default.yellow(warning)}\n`);
|
|
75
77
|
}
|
|
76
78
|
catch (error) {
|
|
77
79
|
program.error(chalk_1.default.red("Error:", error.message));
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
7
|
+
const moveAndRenameImport_1 = __importDefault(require("../../../utils/moveAndRenameImport"));
|
|
8
|
+
const removeProps_1 = __importDefault(require("../../../utils/removeProps"));
|
|
9
|
+
/**
|
|
10
|
+
* @param {import('jscodeshift').FileInfo} file
|
|
11
|
+
* @param {import('jscodeshift').API} api
|
|
12
|
+
*/
|
|
13
|
+
function transformer(file, api, options, ...rest) {
|
|
14
|
+
const j = api.jscodeshift;
|
|
15
|
+
let root;
|
|
16
|
+
try {
|
|
17
|
+
root = j(file.source);
|
|
18
|
+
}
|
|
19
|
+
catch (_a) {
|
|
20
|
+
return file.source;
|
|
21
|
+
}
|
|
22
|
+
const toName = "CopyButton";
|
|
23
|
+
const localName = (0, moveAndRenameImport_1.default)(j, root, {
|
|
24
|
+
fromImport: "@navikt/ds-react-internal",
|
|
25
|
+
toImport: "@navikt/ds-react",
|
|
26
|
+
fromName: "CopyToClipboard",
|
|
27
|
+
toName,
|
|
28
|
+
});
|
|
29
|
+
if (localName === null) {
|
|
30
|
+
return root.toSource(options.printOptions);
|
|
31
|
+
}
|
|
32
|
+
/* Finds and replaces import from CopyToClipboard -> CopyButton */
|
|
33
|
+
if (root.findJSXElements(localName)) {
|
|
34
|
+
(0, removeProps_1.default)(j, root, localName, [
|
|
35
|
+
"popoverText",
|
|
36
|
+
"popoverPlacement",
|
|
37
|
+
"iconPosition",
|
|
38
|
+
"variant",
|
|
39
|
+
]);
|
|
40
|
+
const component = root.findJSXElements(localName);
|
|
41
|
+
component.forEach((node) => {
|
|
42
|
+
const children = node.node.children;
|
|
43
|
+
let flagged = false;
|
|
44
|
+
if (children.length > 0 &&
|
|
45
|
+
!node.node.openingElement.attributes.some((attr) => attr.name.name === "text")) {
|
|
46
|
+
if (children.length === 1 && children[0].type === "JSXText") {
|
|
47
|
+
node.node.openingElement.attributes.push(j.jsxAttribute(j.jsxIdentifier("text"), j.literal(children[0].value.trim())));
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
flagged = true;
|
|
51
|
+
console.log(chalk_1.default.yellow(`\n\nWarning: Detected advanced children-type!\nCodemod can't convert into "text" prop so you will need to update this manually.`));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (!flagged) {
|
|
55
|
+
node.node.children = [];
|
|
56
|
+
node.node.openingElement.selfClosing = true;
|
|
57
|
+
node.node.closingElement = null;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const compRoot = root.find(j.JSXElement, {
|
|
61
|
+
openingElement: { name: { name: localName } },
|
|
62
|
+
});
|
|
63
|
+
compRoot.forEach((x) => {
|
|
64
|
+
x.node.openingElement.name.name = "CopyButton";
|
|
65
|
+
if (x.node.children.length > 0) {
|
|
66
|
+
x.node.closingElement.name.name = "CopyButton";
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return root.toSource(options.printOptions);
|
|
71
|
+
}
|
|
72
|
+
exports.default = transformer;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function moveAndRenameImport(j, root, { fromImport, toImport, fromName, toName, }) {
|
|
4
|
+
var _a, _b;
|
|
5
|
+
const existingFromImport = root.find(j.ImportDeclaration, {
|
|
6
|
+
source: {
|
|
7
|
+
value: fromImport,
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
if (!existingFromImport.length) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
let localname = fromName;
|
|
14
|
+
const existingFromImportSpecifier = existingFromImport === null || existingFromImport === void 0 ? void 0 : existingFromImport.find(j.ImportSpecifier, (node) => {
|
|
15
|
+
if (node.imported.name === fromName) {
|
|
16
|
+
localname = node.local.name;
|
|
17
|
+
}
|
|
18
|
+
return node.imported.name === fromName;
|
|
19
|
+
});
|
|
20
|
+
if (!existingFromImport.length || !(existingFromImportSpecifier === null || existingFromImportSpecifier === void 0 ? void 0 : existingFromImportSpecifier.length)) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
if ((existingFromImportSpecifier === null || existingFromImportSpecifier === void 0 ? void 0 : existingFromImportSpecifier.length) > 0) {
|
|
24
|
+
existingFromImportSpecifier.remove();
|
|
25
|
+
}
|
|
26
|
+
if (!((_a = existingFromImport.get("specifiers").value) === null || _a === void 0 ? void 0 : _a.length) ||
|
|
27
|
+
((_b = existingFromImport.get("specifiers").value) === null || _b === void 0 ? void 0 : _b.length) === 0) {
|
|
28
|
+
existingFromImport.remove();
|
|
29
|
+
}
|
|
30
|
+
const existingImport = root.find(j.ImportDeclaration, {
|
|
31
|
+
source: {
|
|
32
|
+
value: toImport,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
const existingImportSpecifier = existingImport.find(j.ImportSpecifier, {
|
|
36
|
+
imported: {
|
|
37
|
+
name: toName,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
if (existingImportSpecifier.length <= 0) {
|
|
41
|
+
const newImportSpecifier = j.importSpecifier(j.identifier(toName));
|
|
42
|
+
if (existingImport.length > 0) {
|
|
43
|
+
existingImport.get("specifiers").push(newImportSpecifier);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const newImport = j.importDeclaration([newImportSpecifier], j.stringLiteral(toImport));
|
|
47
|
+
const lastImport = root.find(j.ImportDeclaration).at(-1);
|
|
48
|
+
if (lastImport.length > 0) {
|
|
49
|
+
lastImport.insertAfter(newImport);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
root.get().node.program.body.unshift(newImport);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return localname;
|
|
57
|
+
}
|
|
58
|
+
exports.default = moveAndRenameImport;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function removePropsFromComponent(j, root, componentName, propsToRemove) {
|
|
4
|
+
const component = root.findJSXElements(componentName);
|
|
5
|
+
component.forEach((node) => {
|
|
6
|
+
const attributes = node.node.openingElement.attributes;
|
|
7
|
+
for (let i = attributes.length - 1; i >= 0; i--) {
|
|
8
|
+
const attribute = attributes[i];
|
|
9
|
+
if (attribute.type === "JSXAttribute" &&
|
|
10
|
+
propsToRemove.includes(attribute.name.name.toString())) {
|
|
11
|
+
attributes.splice(i, 1);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
exports.default = removePropsFromComponent;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@navikt/aksel",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"description": "Aksel command line interface. Handles css-imports, codemods and more",
|
|
5
5
|
"author": "Aksel | NAV designsystem team",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"homepage": "https://aksel.nav.no/grunnleggende/kode/kommandolinje",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@navikt/ds-css": "3.1
|
|
32
|
+
"@navikt/ds-css": "3.2.1",
|
|
33
33
|
"axios": "1.3.6",
|
|
34
34
|
"chalk": "4.1.0",
|
|
35
35
|
"clipboardy": "^2.3.0",
|