@shopify/slate
Version:
Slate CLI tools
217 lines (164 loc) • 8.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (program) {
var _this = this;
program.command('migrate').description('Converts an existing theme to work with Slate.').option('--yarn', 'installs theme dependencies with yarn instead of npm').action(function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var workingDirectory, answers, configYml, pkgJson, srcDir, configDir, iconsDir, stylesDir, scriptsDir, movePromiseFactory, files, whitelistFiles, movePromises, unminifyJsonPromiseFactory, configDirFiles, themeSettingsFiles, unminifyPromises, configUrl;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
unminifyJsonPromiseFactory = function unminifyJsonPromiseFactory(file) {
return utils.unminifyJson((0, _path.join)(configDir, file));
};
movePromiseFactory = function movePromiseFactory(file) {
console.log(' Migrating ' + file + ' to src/...');
return utils.move((0, _path.join)(workingDirectory, file), (0, _path.join)(srcDir, file));
};
workingDirectory = process.cwd();
_context.next = 5;
return (0, _inquirer.prompt)({
type: 'confirm',
name: 'confirmation',
message: "Warning! This will change your theme's folder structure. Are you sure you want to proceed?"
});
case 5:
answers = _context.sent;
if (answers.confirmation) {
_context.next = 8;
break;
}
return _context.abrupt('return');
case 8:
if (utils.isShopifyTheme(workingDirectory)) {
_context.next = 15;
break;
}
console.log('');
console.error((0, _chalk.yellow)(" The current directory doesn't have /layout/theme.liquid. We have to assume this isn't a Shopify theme"));
console.log('');
console.error((0, _chalk.red)(' ' + _figures2.default.cross + ' Migration failed'));
console.log('');
return _context.abrupt('return');
case 15:
configYml = (0, _path.join)(workingDirectory, 'config.yml');
pkgJson = (0, _path.join)(workingDirectory, 'package.json');
srcDir = (0, _path.join)(workingDirectory, 'src');
configDir = (0, _path.join)(workingDirectory, 'src/config');
iconsDir = (0, _path.join)(srcDir, 'icons');
stylesDir = (0, _path.join)(srcDir, 'styles');
scriptsDir = (0, _path.join)(srcDir, 'scripts');
console.log('');
console.log(' ' + (0, _chalk.green)(_figures2.default.tick) + ' Your theme is a valid Shopify theme');
console.log('');
if (!(0, _fs.existsSync)(srcDir)) {
_context.next = 32;
break;
}
console.error((0, _chalk.yellow)(' Migrate task could not create a new src directory since your theme already has one'));
console.error((0, _chalk.yellow)(' Please remove or rename your current src directory'));
console.log('');
console.error((0, _chalk.red)(' ' + _figures2.default.cross + ' Migration failed'));
console.log('');
return _context.abrupt('return');
case 32:
console.log(' ' + (0, _chalk.green)(_figures2.default.tick) + ' Migration checks completed');
console.log('');
console.log(' Starting migration...');
console.log('');
(0, _fs.mkdirSync)(srcDir);
(0, _fs.mkdirSync)(iconsDir);
(0, _fs.mkdirSync)(stylesDir);
(0, _fs.mkdirSync)(scriptsDir);
if (!(0, _fs.existsSync)(pkgJson)) {
utils.writePackageJsonSync(pkgJson);
}
files = (0, _fs.readdirSync)(workingDirectory);
whitelistFiles = files.filter(utils.isShopifyThemeWhitelistedDir);
movePromises = whitelistFiles.map(movePromiseFactory);
configDirFiles = (0, _fs.readdirSync)(configDir);
themeSettingsFiles = configDirFiles.filter(utils.isShopifyThemeSettingsFile);
unminifyPromises = themeSettingsFiles.map(unminifyJsonPromiseFactory);
_context.prev = 47;
_context.next = 50;
return Promise.all(movePromises);
case 50:
console.log('');
console.log(' ' + (0, _chalk.green)(_figures2.default.tick) + ' Migration to src/ completed');
console.log('');
_context.next = 55;
return Promise.all(unminifyPromises);
case 55:
console.log(' Installing Slate dependencies...');
console.log('');
if (!options.yarn) {
_context.next = 62;
break;
}
_context.next = 60;
return utils.startProcess('yarn', ['add', '@shopify/slate-tools@slate-v0', '--dev', '--exact'], {
cwd: workingDirectory
});
case 60:
_context.next = 64;
break;
case 62:
_context.next = 64;
return utils.startProcess('npm', ['install', '@shopify/slate-tools@slate-v0', '--save-dev', '--save-exact'], {
cwd: workingDirectory
});
case 64:
console.log('');
console.log(' ' + (0, _chalk.green)(_figures2.default.tick) + ' Slate dependencies installed');
console.log('');
if ((0, _fs.existsSync)(configYml)) {
_context.next = 74;
break;
}
configUrl = 'https://raw.githubusercontent.com/Shopify/slate/0.x/packages/slate-theme/config-sample.yml';
_context.next = 71;
return utils.downloadFromUrl(configUrl, (0, _path.join)(workingDirectory, 'config.yml'));
case 71:
console.error(' ' + (0, _chalk.green)(_figures2.default.tick) + ' Configuration file generated');
console.error((0, _chalk.yellow)(' Your theme was missing config.yml in the root directory. Please open and edit it before using Slate commands'));
console.log('');
case 74:
console.log(' ' + (0, _chalk.green)(_figures2.default.tick) + ' Migration complete!');
console.log('');
_context.next = 84;
break;
case 78:
_context.prev = 78;
_context.t0 = _context['catch'](47);
console.error((0, _chalk.red)(' ' + _context.t0));
console.log('');
console.error((0, _chalk.red)(' ' + _figures2.default.cross + ' Migration failed. Please check src/ directory'));
console.log('');
case 84:
case 'end':
return _context.stop();
}
}
}, _callee, _this, [[47, 78]]);
}));
return function () {
return _ref.apply(this, arguments);
};
}());
};
require('babel-polyfill');
var _fs = require('fs');
var _path = require('path');
var _inquirer = require('inquirer');
var _chalk = require('chalk');
var _figures = require('figures');
var _figures2 = _interopRequireDefault(_figures);
var _utils = require('../utils');
var utils = _interopRequireWildcard(_utils);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }