@atlaskit/codemod-cli 0.11.3 → 0.11.5
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.md +13 -0
- package/dist/cjs/cli.js +0 -10
- package/dist/cjs/filepath.js +1 -15
- package/dist/cjs/index.js +0 -3
- package/dist/cjs/main.js +7 -113
- package/dist/cjs/presets/css-to-design-tokens/css-to-design-tokens.js +21 -70
- package/dist/cjs/presets/css-to-design-tokens/utils/legacy-colors.js +0 -1
- package/dist/cjs/presets/css-to-design-tokens/utils/meta.js +0 -10
- package/dist/cjs/presets/index.js +1 -6
- package/dist/cjs/presets/styled-to-emotion/styled-to-emotion.js +1 -6
- package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +4 -23
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast-meta.js +2 -16
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast.js +0 -2
- package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +2 -17
- package/dist/cjs/presets/theme-to-design-tokens/utils/fuzzy-search.js +38 -96
- package/dist/cjs/presets/theme-to-design-tokens/utils/tokens.js +0 -6
- package/dist/cjs/sinceRef.js +2 -23
- package/dist/cjs/transforms.js +4 -31
- package/dist/cjs/types.js +0 -19
- package/dist/cjs/utils.js +1 -14
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/filepath.js +7 -5
- package/dist/es2019/main.js +17 -51
- package/dist/es2019/presets/css-to-design-tokens/css-to-design-tokens.js +19 -51
- package/dist/es2019/presets/css-to-design-tokens/utils/legacy-colors.js +0 -1
- package/dist/es2019/presets/css-to-design-tokens/utils/meta.js +0 -6
- package/dist/es2019/presets/index.js +1 -1
- package/dist/es2019/presets/styled-to-emotion/styled-to-emotion.js +1 -4
- package/dist/es2019/presets/theme-to-design-tokens/theme-to-design-tokens.js +4 -14
- package/dist/es2019/presets/theme-to-design-tokens/utils/ast-meta.js +2 -11
- package/dist/es2019/presets/theme-to-design-tokens/utils/color.js +2 -7
- package/dist/es2019/presets/theme-to-design-tokens/utils/fuzzy-search.js +38 -95
- package/dist/es2019/presets/theme-to-design-tokens/utils/tokens.js +0 -1
- package/dist/es2019/sinceRef.js +2 -11
- package/dist/es2019/transforms.js +3 -13
- package/dist/es2019/types.js +1 -0
- package/dist/es2019/utils.js +1 -12
- package/dist/es2019/version.json +1 -1
- package/dist/esm/cli.js +0 -2
- package/dist/esm/filepath.js +1 -9
- package/dist/esm/main.js +7 -98
- package/dist/esm/presets/css-to-design-tokens/css-to-design-tokens.js +22 -58
- package/dist/esm/presets/css-to-design-tokens/utils/legacy-colors.js +0 -1
- package/dist/esm/presets/css-to-design-tokens/utils/meta.js +0 -6
- package/dist/esm/presets/index.js +1 -1
- package/dist/esm/presets/styled-to-emotion/styled-to-emotion.js +1 -4
- package/dist/esm/presets/theme-to-design-tokens/theme-to-design-tokens.js +4 -15
- package/dist/esm/presets/theme-to-design-tokens/utils/ast-meta.js +2 -11
- package/dist/esm/presets/theme-to-design-tokens/utils/color.js +2 -7
- package/dist/esm/presets/theme-to-design-tokens/utils/fuzzy-search.js +38 -95
- package/dist/esm/presets/theme-to-design-tokens/utils/tokens.js +0 -1
- package/dist/esm/sinceRef.js +2 -16
- package/dist/esm/transforms.js +4 -14
- package/dist/esm/types.js +1 -11
- package/dist/esm/utils.js +1 -12
- package/dist/esm/version.json +1 -1
- package/package.json +3 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
|
+
|
|
2
3
|
import { isDecendantOfToken, isDecendantOfType } from './utils/ast';
|
|
3
4
|
import { cleanMeta, getMetaFromAncestors } from './utils/ast-meta';
|
|
4
5
|
import { includesHardCodedColor, isHardCodedColor, isLegacyColor, isLegacyNamedColor } from './utils/color';
|
|
@@ -6,11 +7,9 @@ import Search from './utils/fuzzy-search';
|
|
|
6
7
|
import { legacyColorMetaMap } from './utils/legacy-colors';
|
|
7
8
|
import { tokens } from './utils/tokens';
|
|
8
9
|
const search = Search(tokens, false);
|
|
9
|
-
|
|
10
10
|
function hasImportDeclaration(j, source, sourcePath) {
|
|
11
11
|
return !!source.find(j.ImportDeclaration).filter(path => path.node.source.value === sourcePath).length;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
13
|
function hasImportSpecifier(j, source, specifier, sourcePath) {
|
|
15
14
|
return !!source.find(j.ImportDeclaration).filter(path => path.node.source.value === sourcePath).find(j.ImportSpecifier, {
|
|
16
15
|
local: {
|
|
@@ -18,39 +17,33 @@ function hasImportSpecifier(j, source, specifier, sourcePath) {
|
|
|
18
17
|
}
|
|
19
18
|
}).length;
|
|
20
19
|
}
|
|
21
|
-
|
|
22
20
|
function insertTokenImport(j, source) {
|
|
23
21
|
if (hasImportDeclaration(j, source, '@atlaskit/tokens')) {
|
|
24
22
|
return;
|
|
25
23
|
}
|
|
26
|
-
|
|
27
24
|
const newImport = j.importDeclaration([j.importSpecifier(j.identifier('token'))], j.stringLiteral('@atlaskit/tokens'));
|
|
28
25
|
source.get().node.program.body.unshift(newImport);
|
|
29
26
|
}
|
|
30
|
-
|
|
31
27
|
function buildToken(j, tokenId, node) {
|
|
32
28
|
const callExpr = j.callExpression(j.identifier('token'), [j.stringLiteral(tokenId), node].filter(Boolean));
|
|
33
29
|
return callExpr;
|
|
34
30
|
}
|
|
35
|
-
|
|
36
31
|
function getTokenFromNode(j, path, baseMeta = []) {
|
|
37
32
|
const foundMeta = getMetaFromAncestors(j, path);
|
|
38
33
|
const meta = cleanMeta([...foundMeta, ...baseMeta]);
|
|
39
34
|
const results = search.get(meta.join(' '));
|
|
40
|
-
let tokenId = ['
|
|
41
|
-
|
|
35
|
+
let tokenId = ['MISSING_TOKEN'];
|
|
42
36
|
if (results) {
|
|
43
37
|
tokenId = results.map(result => result[1]);
|
|
44
38
|
}
|
|
45
|
-
|
|
46
39
|
return tokenId[0];
|
|
47
40
|
}
|
|
48
|
-
|
|
49
41
|
export default function transformer(file, api, debug = false) {
|
|
50
42
|
const j = api.jscodeshift;
|
|
51
43
|
const source = j(file.source);
|
|
52
44
|
let transformed = false;
|
|
53
|
-
source
|
|
45
|
+
source
|
|
46
|
+
// Handle colors.N100
|
|
54
47
|
.find(j.MemberExpression).filter(path => {
|
|
55
48
|
return path.value.object.type === 'Identifier' && path.value.object.name === 'colors' && path.value.property.type === 'Identifier' && isLegacyColor(path.value.property.name);
|
|
56
49
|
}).filter(path => !isDecendantOfToken(j, path)).forEach(path => {
|
|
@@ -81,7 +74,6 @@ export default function transformer(file, api, debug = false) {
|
|
|
81
74
|
});
|
|
82
75
|
source.find(j.Literal).filter(path => typeof path.value.value === 'string' && (includesHardCodedColor(path.value.value) || isHardCodedColor(path.value.value))).filter(path => !isDecendantOfToken(j, path)).forEach(path => {
|
|
83
76
|
var _path$value, _path$value$value;
|
|
84
|
-
|
|
85
77
|
debug && console.log('file:', file.path);
|
|
86
78
|
insertTokenImport(j, source);
|
|
87
79
|
const value = path === null || path === void 0 ? void 0 : (_path$value = path.value) === null || _path$value === void 0 ? void 0 : (_path$value$value = _path$value.value) === null || _path$value$value === void 0 ? void 0 : _path$value$value.toString();
|
|
@@ -91,10 +83,8 @@ export default function transformer(file, api, debug = false) {
|
|
|
91
83
|
j(path).replaceWith(isDecendantOfType(j, path, j.JSXAttribute) ? j.jsxExpressionContainer(tokenNode) : tokenNode);
|
|
92
84
|
transformed = true;
|
|
93
85
|
});
|
|
94
|
-
|
|
95
86
|
if (transformed) {
|
|
96
87
|
return source.toSource();
|
|
97
88
|
}
|
|
98
|
-
|
|
99
89
|
return file.source;
|
|
100
90
|
}
|
|
@@ -2,27 +2,23 @@ import { getUniqueWordsFromTokens } from './tokens';
|
|
|
2
2
|
export function getMetaFromAncestors(j, path, meta = []) {
|
|
3
3
|
const parent = path.parentPath;
|
|
4
4
|
const grandParent = parent && parent.parentPath;
|
|
5
|
-
|
|
6
5
|
if (parent && parent.value.type === 'ObjectProperty') {
|
|
7
6
|
let value = '';
|
|
8
|
-
|
|
9
7
|
if (parent.value.key.type === 'Literal' || parent.value.key.type === 'StringLiteral' || parent.value.key.type === 'NumericLiteral') {
|
|
10
8
|
value = parent.value.key.value.toString();
|
|
11
9
|
} else {
|
|
12
10
|
value = parent.value.key.name;
|
|
13
11
|
}
|
|
14
|
-
|
|
15
12
|
meta.push(value);
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
if (parent && grandParent && grandParent.value.type === 'TemplateLiteral') {
|
|
19
15
|
const expressionIndex = grandParent.value.expressions.findIndex(exp => exp.name === path.value.name);
|
|
20
16
|
const quasi = grandParent.value.quasis[expressionIndex];
|
|
21
17
|
const propertyName = (quasi.value.cooked || quasi.value.raw || '').replace(/\n/g, '').split(/;|{|}/).filter(el => !el.match(/\.|\@|\(|\)/)).pop().split(/:/g)[0].trim();
|
|
22
|
-
grandParent.value.quasis.slice(0, expressionIndex + 1).map(q => q.value.cooked)
|
|
18
|
+
grandParent.value.quasis.slice(0, expressionIndex + 1).map(q => q.value.cooked)
|
|
19
|
+
// We reverse so the most nested one is first which we're more likely than not interested in
|
|
23
20
|
.reverse().some(str => {
|
|
24
21
|
const result = /(hover|active|disabled|focus)/.exec(str.toLowerCase());
|
|
25
|
-
|
|
26
22
|
if (result) {
|
|
27
23
|
meta.push(result[0]);
|
|
28
24
|
return true;
|
|
@@ -30,21 +26,17 @@ export function getMetaFromAncestors(j, path, meta = []) {
|
|
|
30
26
|
});
|
|
31
27
|
meta.push(propertyName);
|
|
32
28
|
}
|
|
33
|
-
|
|
34
29
|
if (parent && parent.value.type === 'JSXAttribute') {
|
|
35
30
|
if (!['css', 'styles', 'style'].includes(parent.value.name.name)) {
|
|
36
31
|
meta.push(parent.value.name.name);
|
|
37
32
|
}
|
|
38
33
|
}
|
|
39
|
-
|
|
40
34
|
if (parent && parent.value.type === 'VariableDeclarator') {
|
|
41
35
|
meta.push(parent.value.id.name);
|
|
42
36
|
}
|
|
43
|
-
|
|
44
37
|
if (parent) {
|
|
45
38
|
return getMetaFromAncestors(j, parent, meta);
|
|
46
39
|
}
|
|
47
|
-
|
|
48
40
|
return meta;
|
|
49
41
|
}
|
|
50
42
|
export function cleanMeta(meta) {
|
|
@@ -57,7 +49,6 @@ export function cleanMeta(meta) {
|
|
|
57
49
|
if (!accum.includes(val)) {
|
|
58
50
|
accum.push(val);
|
|
59
51
|
}
|
|
60
|
-
|
|
61
52
|
return accum;
|
|
62
53
|
}, []);
|
|
63
54
|
}
|
|
@@ -4,32 +4,27 @@ export const isLegacyColor = value => legacyColors.includes(value);
|
|
|
4
4
|
export const isLegacyNamedColor = value => legacyColorMixins.includes(value);
|
|
5
5
|
export const includesHardCodedColor = raw => {
|
|
6
6
|
const value = raw.toLowerCase();
|
|
7
|
-
|
|
8
7
|
if (/#(?:[a-f0-9]{3}|[a-f0-9]{6}|[a-f0-9]{8})\b|(?:rgb|hsl)a?\([^\)]*\)/.exec(value.toLowerCase())) {
|
|
9
8
|
return true;
|
|
10
9
|
}
|
|
11
|
-
|
|
12
10
|
for (let i = 0; i < namedColors.length; i++) {
|
|
13
11
|
if (value.includes(`${namedColors[i]};`)) {
|
|
14
12
|
return true;
|
|
15
13
|
}
|
|
16
14
|
}
|
|
17
|
-
|
|
18
15
|
return false;
|
|
19
16
|
};
|
|
20
17
|
export const isHardCodedColor = value => {
|
|
21
18
|
if (namedColors.includes(value.toLowerCase())) {
|
|
22
19
|
return true;
|
|
23
20
|
}
|
|
24
|
-
|
|
25
21
|
if (value.startsWith('rgb(') || value.startsWith('rgba(') || value.startsWith('hsl(') || value.startsWith('hsla(') || value.startsWith('lch(') || value.startsWith('lab(') || value.startsWith('color(')) {
|
|
26
22
|
return true;
|
|
27
23
|
}
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
if (value.startsWith('#') && (
|
|
25
|
+
// short hex, hex, or hex with alpha
|
|
30
26
|
value.length === 4 || value.length === 7 || value.length === 9)) {
|
|
31
27
|
return true;
|
|
32
28
|
}
|
|
33
|
-
|
|
34
29
|
return false;
|
|
35
30
|
};
|
|
@@ -12,12 +12,10 @@ const FuzzySet = function (arr = [], useLevenshtein, gramSizeLower = 2, gramSize
|
|
|
12
12
|
matchDict: {},
|
|
13
13
|
items: {}
|
|
14
14
|
};
|
|
15
|
-
|
|
16
15
|
var levenshtein = function (str1, str2) {
|
|
17
16
|
var current = [];
|
|
18
17
|
var prev;
|
|
19
18
|
var value;
|
|
20
|
-
|
|
21
19
|
for (var i = 0; i <= str2.length; i++) {
|
|
22
20
|
for (var j = 0; j <= str1.length; j++) {
|
|
23
21
|
if (i && j) {
|
|
@@ -31,65 +29,52 @@ const FuzzySet = function (arr = [], useLevenshtein, gramSizeLower = 2, gramSize
|
|
|
31
29
|
} else {
|
|
32
30
|
value = i + j;
|
|
33
31
|
}
|
|
34
|
-
|
|
35
32
|
prev = current[j];
|
|
36
33
|
current[j] = value;
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
|
-
|
|
40
36
|
return current.pop();
|
|
41
|
-
};
|
|
42
|
-
|
|
37
|
+
};
|
|
43
38
|
|
|
39
|
+
// return an edit distance from 0 to 1
|
|
44
40
|
var _distance = function (str1, str2) {
|
|
45
41
|
if (str1 === null && str2 === null) {
|
|
46
42
|
throw new Error('Trying to compare two null values');
|
|
47
43
|
}
|
|
48
|
-
|
|
49
44
|
if (str1 === null || str2 === null) {
|
|
50
45
|
return 0;
|
|
51
46
|
}
|
|
52
|
-
|
|
53
47
|
str1 = String(str1);
|
|
54
48
|
str2 = String(str2);
|
|
55
49
|
var distance = levenshtein(str1, str2);
|
|
56
|
-
|
|
57
50
|
if (str1.length > str2.length) {
|
|
58
51
|
return 1 - distance / str1.length;
|
|
59
52
|
} else {
|
|
60
53
|
return 1 - distance / str2.length;
|
|
61
54
|
}
|
|
62
55
|
};
|
|
63
|
-
|
|
64
56
|
var _nonWordRe = /[^a-zA-Z0-9\u00C0-\u00FF, ]+/g;
|
|
65
|
-
|
|
66
57
|
var _iterateGrams = function (value, gramSize) {
|
|
67
58
|
gramSize = gramSize || 2;
|
|
68
59
|
var simplified = '-' + value.toLowerCase().replace(_nonWordRe, '') + '-',
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
60
|
+
lenDiff = gramSize - simplified.length,
|
|
61
|
+
results = [];
|
|
72
62
|
if (lenDiff > 0) {
|
|
73
63
|
for (var i = 0; i < lenDiff; ++i) {
|
|
74
64
|
simplified += '-';
|
|
75
65
|
}
|
|
76
66
|
}
|
|
77
|
-
|
|
78
67
|
for (var i = 0; i < simplified.length - gramSize + 1; ++i) {
|
|
79
68
|
results.push(simplified.slice(i, i + gramSize));
|
|
80
69
|
}
|
|
81
|
-
|
|
82
70
|
return results;
|
|
83
71
|
};
|
|
84
|
-
|
|
85
72
|
var _gramCounter = function (value, gramSize) {
|
|
86
73
|
// return an object where key=gram, value=number of occurrences
|
|
87
74
|
gramSize = gramSize || 2;
|
|
88
|
-
|
|
89
75
|
var result = {},
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
76
|
+
grams = _iterateGrams(value, gramSize),
|
|
77
|
+
i = 0;
|
|
93
78
|
for (i; i < grams.length; ++i) {
|
|
94
79
|
if (grams[i] in result) {
|
|
95
80
|
// @ts-expect-error
|
|
@@ -99,62 +84,51 @@ const FuzzySet = function (arr = [], useLevenshtein, gramSizeLower = 2, gramSize
|
|
|
99
84
|
result[grams[i]] = 1;
|
|
100
85
|
}
|
|
101
86
|
}
|
|
102
|
-
|
|
103
87
|
return result;
|
|
104
|
-
};
|
|
105
|
-
|
|
88
|
+
};
|
|
106
89
|
|
|
90
|
+
// the main functions
|
|
107
91
|
fuzzyset.get = function (value, defaultValue, minMatchScore) {
|
|
108
92
|
// check for value in set, returning defaultValue or null if none found
|
|
109
93
|
if (minMatchScore === undefined) {
|
|
110
94
|
minMatchScore = 0.33;
|
|
111
95
|
}
|
|
112
|
-
|
|
113
96
|
var result = this._get(value, minMatchScore);
|
|
114
|
-
|
|
115
97
|
if (!result && typeof defaultValue !== 'undefined') {
|
|
116
98
|
return defaultValue;
|
|
117
99
|
}
|
|
118
|
-
|
|
119
100
|
return result;
|
|
120
101
|
};
|
|
121
|
-
|
|
122
102
|
fuzzyset._get = function (value, minMatchScore) {
|
|
123
|
-
var results = [];
|
|
124
|
-
|
|
103
|
+
var results = [];
|
|
104
|
+
// start with high gram size and if there are no results, go to lower gram sizes
|
|
125
105
|
for (var gramSize = this.gramSizeUpper; gramSize >= this.gramSizeLower; --gramSize) {
|
|
126
106
|
results = this.__get(value, gramSize, minMatchScore);
|
|
127
|
-
|
|
128
107
|
if (results && results.length > 0) {
|
|
129
108
|
return results;
|
|
130
109
|
}
|
|
131
110
|
}
|
|
132
|
-
|
|
133
111
|
return null;
|
|
134
112
|
};
|
|
135
|
-
|
|
136
113
|
fuzzyset.__get = function (value, gramSize, minMatchScore) {
|
|
137
114
|
var normalizedValue = this._normalizeStr(value),
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
115
|
+
matches = {},
|
|
116
|
+
gramCounts = _gramCounter(normalizedValue, gramSize),
|
|
117
|
+
items = this.items[gramSize],
|
|
118
|
+
sumOfSquareGramCounts = 0,
|
|
119
|
+
gram,
|
|
120
|
+
gramCount,
|
|
121
|
+
i,
|
|
122
|
+
index,
|
|
123
|
+
otherGramCount;
|
|
148
124
|
for (gram in gramCounts) {
|
|
149
125
|
// @ts-expect-error
|
|
150
126
|
gramCount = gramCounts[gram];
|
|
151
127
|
sumOfSquareGramCounts += Math.pow(gramCount, 2);
|
|
152
|
-
|
|
153
128
|
if (gram in this.matchDict) {
|
|
154
129
|
for (i = 0; i < this.matchDict[gram].length; ++i) {
|
|
155
130
|
index = this.matchDict[gram][i][0];
|
|
156
131
|
otherGramCount = this.matchDict[gram][i][1];
|
|
157
|
-
|
|
158
132
|
if (index in matches) {
|
|
159
133
|
// @ts-expect-error
|
|
160
134
|
matches[index] += gramCount * otherGramCount;
|
|
@@ -165,31 +139,26 @@ const FuzzySet = function (arr = [], useLevenshtein, gramSizeLower = 2, gramSize
|
|
|
165
139
|
}
|
|
166
140
|
}
|
|
167
141
|
}
|
|
168
|
-
|
|
169
142
|
function isEmptyObject(obj) {
|
|
170
143
|
for (var prop in obj) {
|
|
171
144
|
if (obj.hasOwnProperty(prop)) {
|
|
172
145
|
return false;
|
|
173
146
|
}
|
|
174
147
|
}
|
|
175
|
-
|
|
176
148
|
return true;
|
|
177
149
|
}
|
|
178
|
-
|
|
179
150
|
if (isEmptyObject(matches)) {
|
|
180
151
|
return null;
|
|
181
152
|
}
|
|
182
|
-
|
|
183
153
|
var vectorNormal = Math.sqrt(sumOfSquareGramCounts),
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
154
|
+
results = [],
|
|
155
|
+
matchScore;
|
|
156
|
+
// build a results list of [score, str]
|
|
187
157
|
for (var matchIndex in matches) {
|
|
188
158
|
// @ts-expect-error
|
|
189
159
|
matchScore = matches[matchIndex];
|
|
190
160
|
results.push([matchScore / (vectorNormal * items[matchIndex][0]), items[matchIndex][1]]);
|
|
191
161
|
}
|
|
192
|
-
|
|
193
162
|
var sortDescending = function (a, b) {
|
|
194
163
|
if (a[0] < b[0]) {
|
|
195
164
|
return 1;
|
|
@@ -199,23 +168,19 @@ const FuzzySet = function (arr = [], useLevenshtein, gramSizeLower = 2, gramSize
|
|
|
199
168
|
return 0;
|
|
200
169
|
}
|
|
201
170
|
};
|
|
202
|
-
|
|
203
171
|
results.sort(sortDescending);
|
|
204
|
-
|
|
205
172
|
if (this.useLevenshtein) {
|
|
206
173
|
var newResults = [],
|
|
207
|
-
|
|
174
|
+
endIndex = Math.min(50, results.length);
|
|
175
|
+
// truncate somewhat arbitrarily to 50
|
|
208
176
|
// @ts-expect-error
|
|
209
|
-
|
|
210
177
|
for (var i = 0; i < endIndex; ++i) {
|
|
211
178
|
// @ts-expect-error
|
|
212
179
|
newResults.push([_distance(results[i][1], normalizedValue), results[i][1]]);
|
|
213
180
|
}
|
|
214
|
-
|
|
215
181
|
results = newResults;
|
|
216
182
|
results.sort(sortDescending);
|
|
217
183
|
}
|
|
218
|
-
|
|
219
184
|
newResults = [];
|
|
220
185
|
results.forEach(function (scoreWordPair) {
|
|
221
186
|
if (scoreWordPair[0] >= minMatchScore) {
|
|
@@ -225,112 +190,90 @@ const FuzzySet = function (arr = [], useLevenshtein, gramSizeLower = 2, gramSize
|
|
|
225
190
|
}.bind(this));
|
|
226
191
|
return newResults;
|
|
227
192
|
};
|
|
228
|
-
|
|
229
193
|
fuzzyset.add = function (value) {
|
|
230
194
|
var normalizedValue = this._normalizeStr(value);
|
|
231
|
-
|
|
232
195
|
if (normalizedValue in this.exactSet) {
|
|
233
196
|
return false;
|
|
234
197
|
}
|
|
235
|
-
|
|
236
198
|
var i = this.gramSizeLower;
|
|
237
|
-
|
|
238
199
|
for (i; i < this.gramSizeUpper + 1; ++i) {
|
|
239
200
|
this._add(value, i);
|
|
240
201
|
}
|
|
241
202
|
};
|
|
242
|
-
|
|
243
203
|
fuzzyset._add = function (value, gramSize) {
|
|
244
204
|
var normalizedValue = this._normalizeStr(value),
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
205
|
+
items = this.items[gramSize] || [],
|
|
206
|
+
index = items.length;
|
|
248
207
|
items.push(0);
|
|
249
|
-
|
|
250
208
|
var gramCounts = _gramCounter(normalizedValue, gramSize);
|
|
251
|
-
|
|
252
209
|
var sumOfSquareGramCounts = 0;
|
|
253
210
|
var gram;
|
|
254
211
|
var gramCount;
|
|
255
|
-
|
|
256
212
|
for (gram in gramCounts) {
|
|
257
213
|
// @ts-expect-error
|
|
258
214
|
gramCount = gramCounts[gram];
|
|
259
215
|
sumOfSquareGramCounts += Math.pow(gramCount, 2);
|
|
260
|
-
|
|
261
216
|
if (gram in this.matchDict) {
|
|
262
217
|
this.matchDict[gram].push([index, gramCount]);
|
|
263
218
|
} else {
|
|
264
219
|
this.matchDict[gram] = [[index, gramCount]];
|
|
265
220
|
}
|
|
266
221
|
}
|
|
267
|
-
|
|
268
222
|
var vectorNormal = Math.sqrt(sumOfSquareGramCounts);
|
|
269
223
|
items[index] = [vectorNormal, normalizedValue];
|
|
270
224
|
this.items[gramSize] = items;
|
|
271
225
|
this.exactSet[normalizedValue] = value;
|
|
272
226
|
};
|
|
273
|
-
|
|
274
227
|
fuzzyset._normalizeStr = function (str) {
|
|
275
228
|
if (Object.prototype.toString.call(str) !== '[object String]') {
|
|
276
229
|
throw new Error('Must use a string as argument to FuzzySet functions');
|
|
277
230
|
}
|
|
278
|
-
|
|
279
231
|
return str.toLowerCase();
|
|
280
|
-
};
|
|
281
|
-
|
|
232
|
+
};
|
|
282
233
|
|
|
234
|
+
// return length of items in set
|
|
283
235
|
fuzzyset.length = function () {
|
|
284
236
|
var count = 0,
|
|
285
|
-
|
|
286
|
-
|
|
237
|
+
prop;
|
|
287
238
|
for (prop in this.exactSet) {
|
|
288
239
|
if (this.exactSet.hasOwnProperty(prop)) {
|
|
289
240
|
count += 1;
|
|
290
241
|
}
|
|
291
242
|
}
|
|
292
|
-
|
|
293
243
|
return count;
|
|
294
|
-
};
|
|
295
|
-
|
|
244
|
+
};
|
|
296
245
|
|
|
246
|
+
// return is set is empty
|
|
297
247
|
fuzzyset.isEmpty = function () {
|
|
298
248
|
for (var prop in this.exactSet) {
|
|
299
249
|
if (this.exactSet.hasOwnProperty(prop)) {
|
|
300
250
|
return false;
|
|
301
251
|
}
|
|
302
252
|
}
|
|
303
|
-
|
|
304
253
|
return true;
|
|
305
|
-
};
|
|
306
|
-
|
|
254
|
+
};
|
|
307
255
|
|
|
256
|
+
// return list of values loaded into set
|
|
308
257
|
fuzzyset.values = function () {
|
|
309
258
|
var values = [],
|
|
310
|
-
|
|
311
|
-
|
|
259
|
+
prop;
|
|
312
260
|
for (prop in this.exactSet) {
|
|
313
261
|
if (this.exactSet.hasOwnProperty(prop)) {
|
|
314
262
|
values.push(this.exactSet[prop]);
|
|
315
263
|
}
|
|
316
264
|
}
|
|
317
|
-
|
|
318
265
|
return values;
|
|
319
|
-
};
|
|
320
|
-
|
|
266
|
+
};
|
|
321
267
|
|
|
268
|
+
// initialization
|
|
322
269
|
var i = fuzzyset.gramSizeLower;
|
|
323
|
-
|
|
324
270
|
for (i; i < fuzzyset.gramSizeUpper + 1; ++i) {
|
|
325
271
|
fuzzyset.items[i] = [];
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
|
|
272
|
+
}
|
|
273
|
+
// add all the items to the set
|
|
329
274
|
for (i = 0; i < arr.length; ++i) {
|
|
330
275
|
fuzzyset.add(arr[i]);
|
|
331
276
|
}
|
|
332
|
-
|
|
333
277
|
return fuzzyset;
|
|
334
278
|
};
|
|
335
|
-
|
|
336
279
|
export default FuzzySet;
|
package/dist/es2019/sinceRef.js
CHANGED
|
@@ -1,50 +1,42 @@
|
|
|
1
1
|
import simpleGit from 'simple-git';
|
|
2
2
|
import { ValidationError } from './types';
|
|
3
3
|
const packageRegex = /"(@(?:atlaskit|atlassian|atlassiansox)\/.*)": "(.*)"/;
|
|
4
|
-
|
|
5
4
|
const parseDiffLine = line => {
|
|
6
5
|
const type = line.startsWith('-') ? 'deleted' : line.startsWith('+') ? 'added' : null;
|
|
7
6
|
const match = line.match(packageRegex);
|
|
8
|
-
|
|
9
7
|
if (!type || !match) {
|
|
10
8
|
return null;
|
|
11
9
|
}
|
|
12
|
-
|
|
13
10
|
return {
|
|
14
11
|
type,
|
|
15
12
|
name: match[1],
|
|
16
13
|
version: match[2]
|
|
17
14
|
};
|
|
18
15
|
};
|
|
16
|
+
|
|
19
17
|
/** Returns packages that have been upgraded in package.json since ref. The version refers to their previous
|
|
20
18
|
* version
|
|
21
19
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
20
|
export const getPackagesSinceRef = async ref => {
|
|
25
21
|
const git = simpleGit();
|
|
26
22
|
let commit;
|
|
27
|
-
|
|
28
23
|
try {
|
|
29
24
|
commit = await git.revparse(['--verify', ref]);
|
|
30
25
|
} catch (e) {
|
|
31
26
|
throw new ValidationError(`Invalid git ref "${ref}"`);
|
|
32
27
|
}
|
|
33
|
-
|
|
34
28
|
const diff = await git.diff([commit, '--', 'package.json']);
|
|
35
29
|
const modifiedPackages = diff.split('\n').map(parseDiffLine).filter(pkg => Boolean(pkg));
|
|
36
30
|
const addedPackages = new Map(modifiedPackages.filter(pkg => pkg.type === 'added').map(pkg => [pkg.name, pkg]));
|
|
31
|
+
|
|
37
32
|
/* This is holds the previous version of packages that have been upgraded. Packages are treated as
|
|
38
33
|
* upgraded if they have both an addition/deletion entry in the diff and their versions differ
|
|
39
34
|
*/
|
|
40
|
-
|
|
41
35
|
const upgradedPackages = modifiedPackages.filter(pkg => {
|
|
42
36
|
const addedEntry = addedPackages.get(pkg.name);
|
|
43
|
-
|
|
44
37
|
if (pkg.type !== 'deleted' || !addedEntry) {
|
|
45
38
|
return false;
|
|
46
39
|
}
|
|
47
|
-
|
|
48
40
|
return pkg.version !== addedEntry.version;
|
|
49
41
|
}).map(({
|
|
50
42
|
name,
|
|
@@ -55,7 +47,6 @@ export const getPackagesSinceRef = async ref => {
|
|
|
55
47
|
}));
|
|
56
48
|
return upgradedPackages;
|
|
57
49
|
};
|
|
58
|
-
|
|
59
50
|
if (require.main === module) {
|
|
60
51
|
getPackagesSinceRef(process.argv[2]).then(res => console.log(res));
|
|
61
52
|
}
|
|
@@ -2,22 +2,18 @@ import path from 'path';
|
|
|
2
2
|
import glob from 'glob';
|
|
3
3
|
import semver from 'semver';
|
|
4
4
|
import presets from './presets';
|
|
5
|
-
|
|
6
5
|
const basePath = packages => {
|
|
7
6
|
const packageDirectory = packages && packages.length > 0 ? [`{${packages.map(pkg => pkg.name).join(',')},}`] : ['@{atlaskit,atlassian,atlassiansox}', '*'];
|
|
8
7
|
return path.join(process.cwd(), 'node_modules', ...packageDirectory, 'codemods');
|
|
9
8
|
};
|
|
10
|
-
|
|
11
9
|
export const hasTransform = transformPath => glob.sync(transformPath).length > 0;
|
|
12
|
-
/** Retrieves transforms for `packages` if provided, otherwise all transforms including presets */
|
|
13
10
|
|
|
11
|
+
/** Retrieves transforms for `packages` if provided, otherwise all transforms including presets */
|
|
14
12
|
export const getTransforms = packages => {
|
|
15
13
|
const transforms = [path.join(basePath(packages), '*.@(ts|tsx|js)'), path.join(basePath(packages), '*', 'index.@(ts|tsx|js)')];
|
|
16
|
-
|
|
17
14
|
if (!packages) {
|
|
18
15
|
transforms.unshift(...presets);
|
|
19
16
|
}
|
|
20
|
-
|
|
21
17
|
return transforms.map(transform => glob.sync(transform)).reduce((acc, val) => acc.concat(val), []).map(transform => parseTransformPath(transform)).filter(filterTransforms(packages)).sort();
|
|
22
18
|
};
|
|
23
19
|
export const parseTransformPath = transformPath => path.parse(transformPath);
|
|
@@ -27,35 +23,29 @@ export const getTransformPath = ({
|
|
|
27
23
|
}) => `${dir}/${base}`;
|
|
28
24
|
export const getTransformModule = transform => {
|
|
29
25
|
const pathSegments = transform.dir.split(path.sep);
|
|
30
|
-
const nodeModulesIdx = pathSegments.indexOf('node_modules');
|
|
31
|
-
|
|
26
|
+
const nodeModulesIdx = pathSegments.indexOf('node_modules');
|
|
27
|
+
// pathSegments will be of the form [node_modules, '@atlaskit', 'avatar', 'codemods']
|
|
32
28
|
return pathSegments.slice(nodeModulesIdx + 1, nodeModulesIdx + 3).join('/');
|
|
33
29
|
};
|
|
34
30
|
export const getTransformVersion = transform => {
|
|
35
31
|
let transformName = transform.base;
|
|
36
|
-
|
|
37
32
|
if (transformName.startsWith('index.')) {
|
|
38
33
|
const pathSegments = transform.dir.split(path.sep);
|
|
39
34
|
transformName = pathSegments[pathSegments.length - 1];
|
|
40
35
|
}
|
|
41
|
-
|
|
42
36
|
return transformName.split('-')[0];
|
|
43
37
|
};
|
|
44
|
-
|
|
45
38
|
const filterTransforms = packages => {
|
|
46
39
|
if (!packages || packages.length === 0) {
|
|
47
40
|
return () => true;
|
|
48
41
|
}
|
|
49
|
-
|
|
50
42
|
const packageMap = new Map(packages.map(pkg => [pkg.name, pkg.version]));
|
|
51
43
|
return transform => {
|
|
52
44
|
const transformVersion = getTransformVersion(transform);
|
|
53
45
|
const pkgVersion = packageMap.get(getTransformModule(transform));
|
|
54
|
-
|
|
55
46
|
if (pkgVersion === undefined) {
|
|
56
47
|
throw Error(`No corresponding package found for transform "${transform.dir}/${transform.base}`);
|
|
57
48
|
}
|
|
58
|
-
|
|
59
49
|
if (transformVersion === 'next' || pkgVersion === null) {
|
|
60
50
|
return true;
|
|
61
51
|
} else if (semver.valid(transformVersion)) {
|
package/dist/es2019/types.js
CHANGED