1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _regenerator = require('babel-runtime/regenerator');
|
8 |
|
9 | var _regenerator2 = _interopRequireDefault(_regenerator);
|
10 |
|
11 | var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
|
12 |
|
13 | var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
14 |
|
15 | var renamePackage = function () {
|
16 | var _ref = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee(options) {
|
17 | var pkg;
|
18 | return _regenerator2.default.wrap(function _callee$(_context) {
|
19 | while (1) {
|
20 | switch (_context.prev = _context.next) {
|
21 | case 0:
|
22 | _context.next = 2;
|
23 | return (0, _fsExtra.pathExists)('package.json');
|
24 |
|
25 | case 2:
|
26 | if (_context.sent) {
|
27 | _context.next = 4;
|
28 | break;
|
29 | }
|
30 |
|
31 | return _context.abrupt('return', console.log('No package.json file found'));
|
32 |
|
33 | case 4:
|
34 | console.log('Renaming in package.json file...');
|
35 | _context.next = 7;
|
36 | return (0, _fsExtra.readJson)('package.json');
|
37 |
|
38 | case 7:
|
39 | pkg = _context.sent;
|
40 |
|
41 | if (pkg.name) {
|
42 | options.oldName = pkg.name;
|
43 | pkg.name = options.name;
|
44 | recursiveReset(options, pkg);
|
45 | }
|
46 | _context.next = 11;
|
47 | return (0, _fsExtra.outputJson)('package.json', pkg, { spaces: 2 });
|
48 |
|
49 | case 11:
|
50 | case 'end':
|
51 | return _context.stop();
|
52 | }
|
53 | }
|
54 | }, _callee, this);
|
55 | }));
|
56 |
|
57 | return function renamePackage(_x) {
|
58 | return _ref.apply(this, arguments);
|
59 | };
|
60 | }();
|
61 |
|
62 | var renameServerless = function () {
|
63 | var _ref2 = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee2(options) {
|
64 | var config, i, line;
|
65 | return _regenerator2.default.wrap(function _callee2$(_context2) {
|
66 | while (1) {
|
67 | switch (_context2.prev = _context2.next) {
|
68 | case 0:
|
69 | _context2.next = 2;
|
70 | return (0, _fsExtra.pathExists)('serverless.yml');
|
71 |
|
72 | case 2:
|
73 | if (_context2.sent) {
|
74 | _context2.next = 4;
|
75 | break;
|
76 | }
|
77 |
|
78 | return _context2.abrupt('return', console.log('No serverless.yml file found'));
|
79 |
|
80 | case 4:
|
81 | console.log('Renaming in serverless config...');
|
82 | _context2.next = 7;
|
83 | return (0, _fsExtra.readFile)('serverless.yml');
|
84 |
|
85 | case 7:
|
86 | config = _context2.sent;
|
87 |
|
88 | config = config.toString();
|
89 | config = config.split('\n');
|
90 | i = 0;
|
91 |
|
92 | case 11:
|
93 | if (!(i < config.length)) {
|
94 | _context2.next = 19;
|
95 | break;
|
96 | }
|
97 |
|
98 | line = config[i];
|
99 |
|
100 | if (!(line.indexOf('service:') === 0)) {
|
101 | _context2.next = 16;
|
102 | break;
|
103 | }
|
104 |
|
105 | config[i] = 'service: ' + (options.name || (0, _path.basename)(process.cwd()));
|
106 | return _context2.abrupt('break', 19);
|
107 |
|
108 | case 16:
|
109 | i++;
|
110 | _context2.next = 11;
|
111 | break;
|
112 |
|
113 | case 19:
|
114 | config = config.join('\n');
|
115 | _context2.next = 22;
|
116 | return (0, _fsExtra.outputFile)('serverless.yml', config);
|
117 |
|
118 | case 22:
|
119 | case 'end':
|
120 | return _context2.stop();
|
121 | }
|
122 | }
|
123 | }, _callee2, this);
|
124 | }));
|
125 |
|
126 | return function renameServerless(_x2) {
|
127 | return _ref2.apply(this, arguments);
|
128 | };
|
129 | }();
|
130 |
|
131 | var rename = function () {
|
132 | var _ref3 = (0, _asyncToGenerator3.default)( _regenerator2.default.mark(function _callee3(options) {
|
133 | return _regenerator2.default.wrap(function _callee3$(_context3) {
|
134 | while (1) {
|
135 | switch (_context3.prev = _context3.next) {
|
136 | case 0:
|
137 | if (!options.name) {
|
138 | options.name = (0, _path.basename)(process.cwd());
|
139 | }
|
140 | _context3.next = 3;
|
141 | return Promise.all([renamePackage(options), renameServerless(options)]);
|
142 |
|
143 | case 3:
|
144 | case 'end':
|
145 | return _context3.stop();
|
146 | }
|
147 | }
|
148 | }, _callee3, this);
|
149 | }));
|
150 |
|
151 | return function rename(_x3) {
|
152 | return _ref3.apply(this, arguments);
|
153 | };
|
154 | }();
|
155 |
|
156 | var _path = require('path');
|
157 |
|
158 | var _fsExtra = require('fs-extra');
|
159 |
|
160 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
161 |
|
162 | function recursiveReset(options, obj) {
|
163 | for (var i in obj) {
|
164 | if (i === 'dependencies' || i === 'devDependencies') {
|
165 | continue;
|
166 | }
|
167 | if (typeof obj[i] === 'object') {
|
168 | obj[i] = recursiveReset(options, obj[i]);
|
169 | } else {
|
170 | obj[i] = obj[i].replace(options.oldName, options.name);
|
171 | }
|
172 | }
|
173 | return obj;
|
174 | }
|
175 |
|
176 | exports.default = rename;
|
177 |
|
\ | No newline at end of file |