@storybook/codemod 6.4.0-beta.20 → 6.4.0-beta.24

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.
Files changed (41) hide show
  1. package/package.json +4 -4
  2. package/dist/cjs/index.js +0 -142
  3. package/dist/cjs/lib/utils.js +0 -40
  4. package/dist/cjs/transforms/add-component-parameters.js +0 -64
  5. package/dist/cjs/transforms/csf-2-to-3.js +0 -169
  6. package/dist/cjs/transforms/csf-hoist-story-annotations.js +0 -93
  7. package/dist/cjs/transforms/csf-to-mdx.js +0 -196
  8. package/dist/cjs/transforms/mdx-to-csf.js +0 -153
  9. package/dist/cjs/transforms/move-builtin-addons.js +0 -57
  10. package/dist/cjs/transforms/storiesof-to-csf.js +0 -300
  11. package/dist/cjs/transforms/update-addon-info.js +0 -101
  12. package/dist/cjs/transforms/update-organisation-name.js +0 -83
  13. package/dist/cjs/transforms/upgrade-hierarchy-separators.js +0 -42
  14. package/dist/esm/index.js +0 -99
  15. package/dist/esm/lib/utils.js +0 -25
  16. package/dist/esm/transforms/add-component-parameters.js +0 -57
  17. package/dist/esm/transforms/csf-2-to-3.js +0 -152
  18. package/dist/esm/transforms/csf-hoist-story-annotations.js +0 -86
  19. package/dist/esm/transforms/csf-to-mdx.js +0 -188
  20. package/dist/esm/transforms/mdx-to-csf.js +0 -139
  21. package/dist/esm/transforms/move-builtin-addons.js +0 -50
  22. package/dist/esm/transforms/storiesof-to-csf.js +0 -287
  23. package/dist/esm/transforms/update-addon-info.js +0 -94
  24. package/dist/esm/transforms/update-organisation-name.js +0 -74
  25. package/dist/esm/transforms/upgrade-hierarchy-separators.js +0 -35
  26. package/dist/modern/index.js +0 -99
  27. package/dist/modern/lib/utils.js +0 -25
  28. package/dist/modern/transforms/add-component-parameters.js +0 -57
  29. package/dist/modern/transforms/csf-2-to-3.js +0 -152
  30. package/dist/modern/transforms/csf-hoist-story-annotations.js +0 -86
  31. package/dist/modern/transforms/csf-to-mdx.js +0 -188
  32. package/dist/modern/transforms/mdx-to-csf.js +0 -139
  33. package/dist/modern/transforms/move-builtin-addons.js +0 -50
  34. package/dist/modern/transforms/storiesof-to-csf.js +0 -287
  35. package/dist/modern/transforms/update-addon-info.js +0 -94
  36. package/dist/modern/transforms/update-organisation-name.js +0 -74
  37. package/dist/modern/transforms/upgrade-hierarchy-separators.js +0 -35
  38. package/dist/ts3.4/lib/utils.d.ts +0 -2
  39. package/dist/ts3.4/transforms/csf-2-to-3.d.ts +0 -6
  40. package/dist/ts3.9/lib/utils.d.ts +0 -2
  41. package/dist/ts3.9/transforms/csf-2-to-3.d.ts +0 -6
@@ -1,300 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = transformer;
7
-
8
- var _prettier = _interopRequireDefault(require("prettier"));
9
-
10
- var _nodeLogger = require("@storybook/node-logger");
11
-
12
- var _csf = require("@storybook/csf");
13
-
14
- var _utils = require("../lib/utils");
15
-
16
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
19
-
20
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
21
-
22
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
23
-
24
- /**
25
- * Convert a legacy story API to component story format
26
- *
27
- * For example:
28
- *
29
- * ```
30
- * input { Button } from './Button';
31
- * storiesOf('Button', module).add('story', () => <Button label="The Button" />);
32
- * ```
33
- *
34
- * Becomes:
35
- *
36
- * ```
37
- * input { Button } from './Button';
38
- * export default {
39
- * title: 'Button'
40
- * }
41
- * export const story = () => <Button label="The Button" />;
42
- *
43
- * NOTES: only support chained storiesOf() calls
44
- */
45
- function transformer(file, api, options) {
46
- var LITERAL = ['ts', 'tsx'].includes(options.parser) ? 'StringLiteral' : 'Literal';
47
- var j = api.jscodeshift;
48
- var root = j(file.source);
49
-
50
- function extractDecorators(parameters) {
51
- if (!parameters) {
52
- return {};
53
- }
54
-
55
- if (!parameters.properties) {
56
- return {
57
- storyParams: parameters
58
- };
59
- }
60
-
61
- var storyDecorators = parameters.properties.find(function (p) {
62
- return p.key.name === 'decorators';
63
- });
64
-
65
- if (!storyDecorators) {
66
- return {
67
- storyParams: parameters
68
- };
69
- }
70
-
71
- storyDecorators = storyDecorators.value;
72
-
73
- var storyParams = _objectSpread({}, parameters);
74
-
75
- storyParams.properties = storyParams.properties.filter(function (p) {
76
- return p.key.name !== 'decorators';
77
- });
78
-
79
- if (storyParams.properties.length === 0) {
80
- return {
81
- storyDecorators: storyDecorators
82
- };
83
- }
84
-
85
- return {
86
- storyParams: storyParams,
87
- storyDecorators: storyDecorators
88
- };
89
- }
90
-
91
- function convertToModuleExports(path, originalExports) {
92
- var base = j(path);
93
- var statements = [];
94
- var extraExports = []; // .addDecorator
95
-
96
- var decorators = [];
97
- base.find(j.CallExpression).filter(function (call) {
98
- return call.node.callee.property && call.node.callee.property.name === 'addDecorator';
99
- }).forEach(function (add) {
100
- var decorator = add.node.arguments[0];
101
- decorators.push(decorator);
102
- });
103
-
104
- if (decorators.length > 0) {
105
- decorators.reverse();
106
- extraExports.push(j.property('init', j.identifier('decorators'), j.arrayExpression(decorators)));
107
- } // .addParameters
108
-
109
-
110
- var parameters = [];
111
- base.find(j.CallExpression).filter(function (call) {
112
- return call.node.callee.property && call.node.callee.property.name === 'addParameters';
113
- }).forEach(function (add) {
114
- // jscodeshift gives us the find results in reverse, but these args come in
115
- // order, so we double reverse here. ugh.
116
- var params = [...add.node.arguments[0].properties];
117
- params.reverse();
118
- params.forEach(function (prop) {
119
- return parameters.push(prop);
120
- });
121
- });
122
-
123
- if (parameters.length > 0) {
124
- parameters.reverse();
125
- extraExports.push(j.property('init', j.identifier('parameters'), j.objectExpression(parameters)));
126
- }
127
-
128
- if (originalExports.length > 0) {
129
- extraExports.push(j.property('init', j.identifier('excludeStories'), j.arrayExpression(originalExports.map(function (exp) {
130
- return j.literal(exp);
131
- }))));
132
- } // storiesOf(...)
133
-
134
-
135
- base.find(j.CallExpression).filter(function (call) {
136
- return call.node.callee.name === 'storiesOf';
137
- }).filter(function (call) {
138
- return call.node.arguments.length > 0 && call.node.arguments[0].type === LITERAL;
139
- }).forEach(function (storiesOf) {
140
- var title = storiesOf.node.arguments[0].value;
141
- statements.push(j.exportDefaultDeclaration(j.objectExpression([j.property('init', j.identifier('title'), j.literal(title)), ...extraExports])));
142
- }); // .add(...)
143
-
144
- var adds = [];
145
- base.find(j.CallExpression).filter(function (add) {
146
- return add.node.callee.property && add.node.callee.property.name === 'add';
147
- }).filter(function (add) {
148
- return add.node.arguments.length >= 2 && add.node.arguments[0].type === LITERAL;
149
- }).forEach(function (add) {
150
- return adds.push(add);
151
- });
152
- adds.reverse();
153
- adds.push(path);
154
- var identifiers = new Set();
155
- root.find(j.Identifier).forEach(function ({
156
- value: value
157
- }) {
158
- return identifiers.add(value.name);
159
- });
160
- adds.forEach(function (add) {
161
- var name = add.node.arguments[0].value;
162
- var key = (0, _utils.sanitizeName)(name);
163
-
164
- while (identifiers.has(key)) {
165
- key = `_${key}`;
166
- }
167
-
168
- identifiers.add(key);
169
-
170
- if ((0, _csf.storyNameFromExport)(key) === name) {
171
- name = null;
172
- }
173
-
174
- var val = add.node.arguments[1];
175
- statements.push(j.exportDeclaration(false, j.variableDeclaration('const', [j.variableDeclarator(j.identifier(key), val)])));
176
- var storyAnnotations = [];
177
-
178
- if (name) {
179
- storyAnnotations.push(j.property('init', j.identifier('name'), j.literal(name)));
180
- }
181
-
182
- if (add.node.arguments.length > 2) {
183
- var originalStoryParams = add.node.arguments[2];
184
-
185
- var _extractDecorators = extractDecorators(originalStoryParams),
186
- storyParams = _extractDecorators.storyParams,
187
- storyDecorators = _extractDecorators.storyDecorators;
188
-
189
- if (storyParams) {
190
- storyAnnotations.push(j.property('init', j.identifier('parameters'), storyParams));
191
- }
192
-
193
- if (storyDecorators) {
194
- storyAnnotations.push(j.property('init', j.identifier('decorators'), storyDecorators));
195
- }
196
- }
197
-
198
- if (storyAnnotations.length > 0) {
199
- statements.push(j.assignmentStatement('=', j.memberExpression(j.identifier(key), j.identifier('story')), j.objectExpression(storyAnnotations)));
200
- }
201
- });
202
- var stmt = path.parent.node.type === 'VariableDeclarator' ? path.parent.parent : path.parent;
203
- statements.reverse();
204
- statements.forEach(function (s) {
205
- return stmt.insertAfter(s);
206
- });
207
- j(stmt).remove();
208
- } // Save the original storiesOf
209
-
210
-
211
- var initialStoriesOf = root.find(j.CallExpression).filter(function (call) {
212
- return call.node.callee.name === 'storiesOf';
213
- });
214
- var defaultExports = root.find(j.ExportDefaultDeclaration); // If there's already a default export
215
-
216
- if (defaultExports.size() > 0) {
217
- if (initialStoriesOf.size() > 0) {
218
- _nodeLogger.logger.warn(`Found ${initialStoriesOf.size()} 'storiesOf' calls but existing default export, SKIPPING: '${file.path}'`);
219
- }
220
-
221
- return root.toSource();
222
- } // Exclude all the original named exports
223
-
224
-
225
- var originalExports = [];
226
- root.find(j.ExportNamedDeclaration).forEach(function (exp) {
227
- var _exp$node = exp.node,
228
- declaration = _exp$node.declaration,
229
- specifiers = _exp$node.specifiers;
230
-
231
- if (declaration) {
232
- var id = declaration.id,
233
- declarations = declaration.declarations;
234
-
235
- if (declarations) {
236
- declarations.forEach(function (decl) {
237
- var _decl$id = decl.id,
238
- name = _decl$id.name,
239
- properties = _decl$id.properties;
240
-
241
- if (name) {
242
- originalExports.push(name);
243
- } else if (properties) {
244
- properties.forEach(function (prop) {
245
- return originalExports.push(prop.key.name);
246
- });
247
- }
248
- });
249
- } else if (id) {
250
- originalExports.push(id.name);
251
- }
252
- } else if (specifiers) {
253
- specifiers.forEach(function (spec) {
254
- return originalExports.push(spec.exported.name);
255
- });
256
- }
257
- }); // each top-level add expression corresponds to the last "add" of the chain.
258
- // replace it with the entire export statements
259
-
260
- root.find(j.CallExpression).filter(function (add) {
261
- return add.node.callee.property && add.node.callee.property.name === 'add';
262
- }).filter(function (add) {
263
- return add.node.arguments.length >= 2 && add.node.arguments[0].type === LITERAL;
264
- }).filter(function (add) {
265
- return ['ExpressionStatement', 'VariableDeclarator'].includes(add.parentPath.node.type);
266
- }).forEach(function (path) {
267
- return convertToModuleExports(path, originalExports);
268
- }); // remove storiesOf import
269
-
270
- root.find(j.ImportSpecifier).filter(function (spec) {
271
- return spec.node.imported.name === 'storiesOf' && spec.parent.node.source.value.startsWith('@storybook/');
272
- }).forEach(function (spec) {
273
- var toRemove = spec.parent.node.specifiers.length > 1 ? spec : spec.parent;
274
- j(toRemove).remove();
275
- });
276
- var source = root.toSource({
277
- trailingComma: true,
278
- quote: 'single',
279
- tabWidth: 2
280
- });
281
-
282
- if (initialStoriesOf.size() > 1) {
283
- _nodeLogger.logger.warn(`Found ${initialStoriesOf.size()} 'storiesOf' calls, PLEASE FIX BY HAND: '${file.path}'`);
284
-
285
- return source;
286
- }
287
-
288
- var prettierConfig = _prettier.default.resolveConfig.sync('.', {
289
- editorconfig: true
290
- }) || {
291
- printWidth: 100,
292
- tabWidth: 2,
293
- bracketSpacing: true,
294
- trailingComma: 'es5',
295
- singleQuote: true
296
- };
297
- return _prettier.default.format(source, _objectSpread(_objectSpread({}, prettierConfig), {}, {
298
- parser: (0, _utils.jscodeshiftToPrettierParser)(options.parser) || 'babel'
299
- }));
300
- }
@@ -1,101 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = transformer;
7
-
8
- /**
9
- * Takes the deprecated addon-info API, addWithInfo, and
10
- * converts to the new withInfo API.
11
- *
12
- * Example of deprecated addWithInfo API:
13
- *
14
- * storiesOf('Button')
15
- * .addWithInfo(
16
- * 'story name',
17
- * 'Story description.',
18
- * () => (
19
- * <Button label="The Button" />
20
- * )
21
- * )
22
- *
23
- * Converts to the new withInfo API:
24
- *
25
- * storiesOf('Button')
26
- * .add('story name', withInfo(
27
- * 'Story description.'
28
- * )(() => (
29
- * <Button label="The Button" />
30
- * )))
31
- */
32
- function transformer(file, api) {
33
- var j = api.jscodeshift;
34
- var root = j(file.source);
35
- /**
36
- * Returns a list of parameters for the withInfo function. The contents
37
- * of this list is either the second argument from the original
38
- * addWithInfo function, if no additional options were used, or a
39
- * combined object of all the options from the original function.
40
- * @param {list} args - original addWithInfo function parameters
41
- * @return {list} the modified list of parameters for the new function
42
- */
43
-
44
- var getOptions = function (args) {
45
- if (args[3] === undefined) {
46
- if (args[2] === undefined) {
47
- // when the optional description string is not supplied for addWithInfo, use story name
48
- return [args[0]];
49
- }
50
-
51
- return [args[1]];
52
- }
53
-
54
- return [j.objectExpression([j.property('init', j.identifier('text'), args[1]), ...args[3].properties])];
55
- };
56
- /**
57
- * Constructs the new withInfo function from the parameters of the
58
- * original addWithInfo function.
59
- * @param {CallExpression} addWithInfoExpression - original function
60
- * @returns {CallExpression} the new withInfo function
61
- */
62
-
63
-
64
- var withInfo = function (addWithInfoExpression) {
65
- var node = addWithInfoExpression.node;
66
- var args = node.arguments; // if optional description string is not supplied, the story component becomes second arg
67
-
68
- var storyComponent = args[2] ? args[2] : args[1];
69
- node.callee.property.name = 'add';
70
- node.arguments = [args[0], j.callExpression(j.callExpression(j.identifier('withInfo'), getOptions(args)), [storyComponent])];
71
- return node;
72
- };
73
- /**
74
- * Checks for - import { withInfo } from "@storybook/addon-info";
75
- * Adds the import if necessary.
76
- */
77
-
78
-
79
- var checkWithInfoImport = function () {
80
- var importExists = root.find(j.ImportDeclaration).filter(function (imp) {
81
- return imp.node.source.value === '@storybook/addon-info';
82
- }).size();
83
- if (importExists) return;
84
- root.find(j.ImportDeclaration).at(-1).insertAfter(j.importDeclaration([j.importSpecifier(j.identifier('withInfo'))], j.literal('@storybook/addon-info')));
85
- };
86
-
87
- var addWithInfoExpressions = root.find(j.CallExpression, {
88
- callee: {
89
- property: {
90
- name: 'addWithInfo'
91
- }
92
- }
93
- });
94
-
95
- if (addWithInfoExpressions.size()) {
96
- checkWithInfoImport();
97
- addWithInfoExpressions.replaceWith(withInfo);
98
- }
99
-
100
- return root.toSource();
101
- }
@@ -1,83 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = transformer;
7
- exports.packageNames = void 0;
8
- var packageNames = {
9
- '@kadira/react-storybook-decorator-centered': '@storybook/addon-centered',
10
- '@kadira/storybook-addons': '@storybook/addons',
11
- '@kadira/storybook-addon-actions': '@storybook/addon-actions',
12
- '@kadira/storybook-addon-comments': '@storybook/addon-comments',
13
- '@kadira/storybook-addon-graphql': '@storybook/addon-graphql',
14
- '@kadira/storybook-addon-info': '@storybook/addon-info',
15
- '@kadira/storybook-addon-knobs': '@storybook/addon-knobs',
16
- '@kadira/storybook-addon-links': '@storybook/addon-links',
17
- '@kadira/storybook-addon-notes': '@storybook/addon-notes',
18
- '@kadira/storybook-addon-options': '@storybook/addon-options',
19
- '@kadira/storybook-channels': '@storybook/channels',
20
- '@kadira/storybook-channel-postmsg': '@storybook/channel-postmessage',
21
- '@kadira/storybook-channel-websocket': '@storybook/channel-websocket',
22
- '@kadira/storybook-ui': '@storybook/ui',
23
- '@kadira/react-native-storybook': '@storybook/react-native',
24
- '@kadira/react-storybook': '@storybook/react',
25
- '@kadira/getstorybook': '@storybook/cli',
26
- '@kadira/storybook': '@storybook/react',
27
- storyshots: '@storybook/addon-storyshots',
28
- getstorybook: '@storybook/cli'
29
- };
30
- exports.packageNames = packageNames;
31
-
32
- function transformer(file, api) {
33
- var j = api.jscodeshift;
34
- var packageNamesKeys = Object.keys(packageNames);
35
- /**
36
- * Checks whether the node value matches a Storybook package
37
- * @param {string} the import declaration node
38
- * @returns {string} whether the node value matches a Storybook package
39
- */
40
-
41
- var getMatch = function (oldpart) {
42
- return packageNamesKeys.find(function (newpart) {
43
- return oldpart.match(newpart);
44
- });
45
- };
46
- /**
47
- * Returns the name of the Storybook packages with the organisation name,
48
- * replacing the old `@kadira/` prefix.
49
- * @param {string} oldPackageName the name of the old package
50
- * @return {string} the new package name
51
- * @example
52
- * // returns '@storybook/storybook'
53
- * getNewPackageName('@kadira/storybook')
54
- */
55
-
56
-
57
- var getNewPackageName = function (oldPackageName) {
58
- var match = getMatch(oldPackageName);
59
-
60
- if (match) {
61
- var replacement = packageNames[match];
62
- return oldPackageName.replace(match, replacement);
63
- }
64
-
65
- return oldPackageName;
66
- };
67
- /**
68
- * updatePackageName - updates the source name of the Storybook packages
69
- * @param {ImportDeclaration} declaration the import declaration
70
- * @returns {ImportDeclaration.Node} the import declaration node
71
- */
72
-
73
-
74
- var updatePackageName = function (declaration) {
75
- // eslint-disable-next-line no-param-reassign
76
- declaration.node.source.value = getNewPackageName(declaration.node.source.value);
77
- return declaration.node;
78
- };
79
-
80
- return j(file.source).find(j.ImportDeclaration).replaceWith(updatePackageName).toSource({
81
- quote: 'single'
82
- });
83
- }
@@ -1,42 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = transformer;
7
-
8
- function upgradeSeparator(path) {
9
- return path.replace(/[|.]/g, '/');
10
- }
11
-
12
- function transformer(file, api, options) {
13
- var j = api.jscodeshift;
14
- var root = j(file.source); // storiesOf(...)
15
-
16
- root.find(j.CallExpression).filter(function (call) {
17
- return call.node.callee.name === 'storiesOf';
18
- }).filter(function (call) {
19
- return call.node.arguments.length > 0 && ['Literal', 'StringLiteral'].includes(call.node.arguments[0].type);
20
- }).forEach(function (call) {
21
- var arg0 = call.node.arguments[0];
22
- arg0.value = upgradeSeparator(arg0.value);
23
- }); // export default { title: ... }
24
-
25
- root.find(j.ExportDefaultDeclaration).filter(function (def) {
26
- return def.node.declaration.properties.map(function (p) {
27
- return p.key.name;
28
- }).includes('title');
29
- }).forEach(function (def) {
30
- if (def.node.declaration && def.node.declaration.properties) {
31
- def.node.declaration.properties.forEach(function (p) {
32
- if (p.key.name === 'title') {
33
- // eslint-disable-next-line no-param-reassign
34
- p.value.value = upgradeSeparator(p.value.value);
35
- }
36
- });
37
- }
38
- });
39
- return root.toSource({
40
- quote: 'single'
41
- });
42
- }
package/dist/esm/index.js DELETED
@@ -1,99 +0,0 @@
1
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
-
3
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
-
5
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
-
7
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
8
-
9
- function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
10
-
11
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
12
-
13
- import "core-js/modules/es.promise.js";
14
- import "core-js/modules/es.symbol.description.js";
15
-
16
- /* eslint import/prefer-default-export: "off" */
17
- import fs from 'fs';
18
- import path from 'path';
19
- import { promisify } from 'util';
20
- import globby from 'globby';
21
- import { sync as spawnSync } from 'cross-spawn';
22
- import { jscodeshiftToPrettierParser } from './lib/utils';
23
- export { default as updateOrganisationName, packageNames } from './transforms/update-organisation-name';
24
- export { default as updateAddonInfo } from './transforms/update-addon-info';
25
- var TRANSFORM_DIR = `${__dirname}/transforms`;
26
- export function listCodemods() {
27
- return fs.readdirSync(TRANSFORM_DIR).filter(function (fname) {
28
- return fname.endsWith('.js');
29
- }).map(function (fname) {
30
- return fname.slice(0, -3);
31
- });
32
- }
33
- var renameAsync = promisify(fs.rename);
34
-
35
- async function renameFile(file, from, to, {
36
- logger: logger
37
- }) {
38
- var newFile = file.replace(from, to);
39
- logger.log(`Rename: ${file} ${newFile}`);
40
- return renameAsync(file, newFile);
41
- }
42
-
43
- export async function runCodemod(codemod, {
44
- glob: glob,
45
- logger: logger,
46
- dryRun: dryRun,
47
- rename: rename,
48
- parser: parser
49
- }) {
50
- var codemods = listCodemods();
51
-
52
- if (!codemods.includes(codemod)) {
53
- throw new Error(`Unknown codemod ${codemod}. Run --list for options.`);
54
- }
55
-
56
- var renameParts = null;
57
-
58
- if (rename) {
59
- renameParts = rename.split(':');
60
-
61
- if (renameParts.length !== 2) {
62
- throw new Error(`Codemod rename: expected format "from:to", got "${rename}"`);
63
- }
64
- } // jscodeshift/prettier know how to handle .ts/.tsx extensions,
65
- // so if the user uses one of those globs, we can auto-infer
66
-
67
-
68
- var inferredParser = parser;
69
-
70
- if (!parser) {
71
- var extension = path.extname(glob).slice(1);
72
- var knownParser = jscodeshiftToPrettierParser(extension);
73
- if (knownParser !== 'babel') inferredParser = extension;
74
- }
75
-
76
- var files = await globby([glob, '!**/node_modules', '!**/dist']);
77
- logger.log(`=> Applying ${codemod}: ${files.length} files`);
78
-
79
- if (!dryRun) {
80
- var parserArgs = inferredParser ? ['--parser', inferredParser] : [];
81
- spawnSync('npx', ['jscodeshift', '-t', `${TRANSFORM_DIR}/${codemod}.js`, ...parserArgs, ...files], {
82
- stdio: 'inherit'
83
- });
84
- }
85
-
86
- if (renameParts) {
87
- var _renameParts = renameParts,
88
- _renameParts2 = _slicedToArray(_renameParts, 2),
89
- from = _renameParts2[0],
90
- to = _renameParts2[1];
91
-
92
- logger.log(`=> Renaming ${rename}: ${files.length} files`);
93
- await Promise.all(files.map(function (file) {
94
- return renameFile(file, new RegExp(`${from}$`), to, {
95
- logger: logger
96
- });
97
- }));
98
- }
99
- }
@@ -1,25 +0,0 @@
1
- import camelCase from 'lodash/camelCase';
2
- import upperFirst from 'lodash/upperFirst';
3
- export var sanitizeName = function (name) {
4
- var key = upperFirst(camelCase(name)); // prepend _ if name starts with a digit
5
-
6
- if (/^\d/.test(key)) {
7
- key = `_${key}`;
8
- } // prepend _ if name starts with a digit
9
-
10
-
11
- if (/^\d/.test(key)) {
12
- key = `_${key}`;
13
- }
14
-
15
- return key;
16
- };
17
- export function jscodeshiftToPrettierParser(parser) {
18
- var parserMap = {
19
- babylon: 'babel',
20
- flow: 'flow',
21
- ts: 'typescript',
22
- tsx: 'typescript'
23
- };
24
- return parserMap[parser] || 'babel';
25
- }