UNPKG

4.17 kBJavaScriptView Raw
1#!/usr/bin/env node
2/**
3* The MIT License (MIT)
4* Copyright (c) 2016 Shopify Inc.
5*
6* Permission is hereby granted, free of charge, to any person obtaining a copy
7* of this software and associated documentation files (the "Software"), to deal
8* in the Software without restriction, including without limitation the rights
9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10* copies of the Software, and to permit persons to whom the Software is
11* furnished to do so, subject to the following conditions:
12*
13* The above copyright notice and this permission notice shall be included in all
14* copies or substantial portions of the Software.
15*
16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22* OR OTHER DEALINGS IN THE SOFTWARE.
23*
24* Version: 0.8.3 Commit: b267cce
25**/'use strict';
26
27var _fs = require('fs');
28
29var _path = require('path');
30
31var _chalk = require('chalk');
32
33var _findRoot = require('find-root');
34
35var _findRoot2 = _interopRequireDefault(_findRoot);
36
37var _updateNotifier = require('update-notifier');
38
39var _updateNotifier2 = _interopRequireDefault(_updateNotifier);
40
41var _commander = require('commander');
42
43var _commander2 = _interopRequireDefault(_commander);
44
45var _utils = require('./utils');
46
47function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
48
49/**
50 * Find closest package.json to be at root of theme.
51 *
52 * @param {string} directory - A path.
53 */
54function getThemeRoot(directory) {
55 try {
56 return (0, _path.normalize)((0, _findRoot2.default)(directory));
57 } catch (err) {
58 return null;
59 }
60}
61
62/**
63 * Check package.json for slate-tools.
64 *
65 * @param {string} themeRoot - The path for the root of the theme.
66 */
67function checkForSlateTools(themeRoot) {
68 var pkgPath = (0, _path.join)(themeRoot, 'package.json');
69 var pkg = require(pkgPath);
70
71 return (0, _utils.hasDependency)('@shopify/slate-tools', pkg);
72}
73
74/**
75 * Output information if/else slate theme directory.
76 *
77 * @param {boolean} isSlateTheme - Whether in slate theme or not.
78 */
79function outputSlateThemeCheck(isSlateTheme) {
80 if (isSlateTheme) {
81 console.log(' Slate theme: ' + (0, _chalk.green)('✓') + ' inside slate theme directory');
82 console.log('');
83 } else {
84 console.log(' Slate theme: ' + (0, _chalk.red)('✗') + ' switch to a slate theme directory for full list of commands');
85 console.log('');
86 }
87}
88
89var currentDirectory = __dirname;
90var workingDirectory = process.cwd();
91var pkg = require((0, _path.join)(currentDirectory, (0, _path.normalize)('../package.json')));
92
93(0, _updateNotifier2.default)({
94 pkg: pkg,
95 updateCheckInterval: 1000 * 60 * 60 * 24 * 7 }).notify();
96
97// Global commands
98require('./commands/theme').default(_commander2.default);
99require('./commands/version').default(_commander2.default);
100
101// Dynamically add in theme commands
102var themeRoot = getThemeRoot(workingDirectory);
103var isSlateTheme = themeRoot && checkForSlateTools(themeRoot);
104
105if (isSlateTheme) {
106 (function () {
107 var slateToolsCommands = (0, _path.join)(themeRoot, (0, _path.normalize)('/node_modules/@shopify/slate-tools/lib/commands'));
108
109 (0, _fs.readdirSync)(slateToolsCommands).filter(function (file) {
110 return ~file.search(/^[^\.].*\.js$/);
111 }).forEach(function (file) {
112 return require((0, _path.join)(slateToolsCommands, file)).default(_commander2.default);
113 });
114 })();
115}
116
117// Custom help
118_commander2.default.on('--help', function () {
119 outputSlateThemeCheck(isSlateTheme);
120});
121
122// Unknown command
123_commander2.default.on('*', function () {
124 console.log('');
125 console.log(' Unknown command: ' + (0, _chalk.red)(_commander2.default.args.join(' ')));
126 console.log('');
127 _commander2.default.help();
128 outputSlateThemeCheck(isSlateTheme);
129});
130
131_commander2.default.parse(process.argv);
\No newline at end of file