UNPKG

@shopify/slate

Version:
126 lines (97 loc) 3.51 kB
#!/usr/bin/env node 'use strict'; var _fs = require('fs'); var _path = require('path'); var _chalk = require('chalk'); var _figures = require('figures'); var _figures2 = _interopRequireDefault(_figures); var _findRoot = require('find-root'); var _findRoot2 = _interopRequireDefault(_findRoot); var _updateNotifier = require('update-notifier'); var _updateNotifier2 = _interopRequireDefault(_updateNotifier); var _commander = require('@shopify/commander'); var _commander2 = _interopRequireDefault(_commander); var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Find closest package.json to be at root of theme. * * @param {string} directory - A path. */ function getThemeRoot(directory) { try { return (0, _path.normalize)((0, _findRoot2.default)(directory)); } catch (err) { return null; } } /** * Check package.json for slate-tools. * * @param {string} themeRoot - The path for the root of the theme. */ function checkForSlateTools(themeRoot) { var pkgPath = (0, _path.join)(themeRoot, 'package.json'); var pkg = require(pkgPath); return (0, _utils.hasDependency)('@shopify/slate-tools', pkg); } /** * Output information if/else slate theme directory. * * @param {boolean} isSlateTheme - Whether in slate theme or not. */ function outputSlateThemeCheck(isSlateTheme) { if (isSlateTheme) { return; } console.log(''); console.log((0, _chalk.yellow)(' ' + _figures2.default.cross + ' You are not in a slate theme directory')); console.log(' For a full list of commands, generate a new theme or switch to an existing slate theme directory'); console.log(''); } var currentDirectory = __dirname; var workingDirectory = process.cwd(); var pkg = require((0, _path.join)(currentDirectory, (0, _path.normalize)('../package.json'))); // Notify for updates every 1 week (0, _updateNotifier2.default)({ pkg: pkg, updateCheckInterval: 1000 * 60 * 60 * 24 * 7 }).notify(); // Global commands require('./commands/theme').default(_commander2.default); require('./commands/migrate').default(_commander2.default); require('./commands/version').default(_commander2.default); // Dynamically add in theme commands var themeRoot = getThemeRoot(workingDirectory); var isSlateTheme = themeRoot && checkForSlateTools(themeRoot); if (isSlateTheme) { var slateToolsCommands = (0, _path.join)(themeRoot, (0, _path.normalize)('/node_modules/@shopify/slate-tools/lib/commands')); (0, _fs.readdirSync)(slateToolsCommands).filter(function (file) { return ~file.search(/^[^\.].*\.js$/); }) // eslint-disable-line no-useless-escape .forEach(function (file) { return require((0, _path.join)(slateToolsCommands, file)).default(_commander2.default); }); } // Custom help _commander2.default.on('--helpStart', function () { outputSlateThemeCheck(isSlateTheme); }); _commander2.default.on('--helpEnd', function () { console.log(' Docs:'); console.log(''); console.log(' https://shopify.github.io/slate/'); console.log(''); }); // Unknown command _commander2.default.on('*', function () { console.log(''); console.log((0, _chalk.red)(' ' + _figures2.default.cross + ' Unknown command: ' + _commander2.default.args.join(' '))); console.log(''); _commander2.default.help(); }); _commander2.default.parse(process.argv); // output help if no commands or options passed if (!process.argv.slice(2).length) { _commander2.default.help(); }